@@ -416,59 +416,66 @@ internal fun List<ColumnWithPath<*>>.allColumnsExceptAndUnpack(
416416 * Empty groups will be removed if [removeEmptyGroups]` == true`
417417 */
418418internal fun List <ColumnWithPath <* >>.allColumnsExceptKeepingStructure (
419- columns : Iterable <ColumnWithPath <* >>,
419+ columns : Set <ColumnWithPath <* >>,
420420 removeEmptyGroups : Boolean = true,
421421): List <ColumnWithPath <* >> {
422422 if (isEmpty()) return emptyList()
423- val fullTree = collectTree()
424- for (columnToExcept in columns) {
425- // grab the node representing the column from the tree
426- val nodeToExcept = fullTree.getOrPut(columnToExcept.path).asNullable()
427- if (nodeToExcept != null ) {
428- // remove the children from the node (if it's a column group) and remove its data (the column itself)
429- nodeToExcept.allChildren().forEach { it.data = null }
430- nodeToExcept.data = null
431-
432- // we need to update the data of the parent node(s) to reflect the removal of the column
433- if (nodeToExcept.parent != null ) {
434- // we grab the data of the parent node, which should be a column group
435- // treat it as a DF to remove the column to except from it and
436- // convert it back to a column group
437- val current = nodeToExcept.parent.data as ColumnGroup <* >? ? : continue
438- val adjustedCurrent = current
439- .remove(nodeToExcept.name)
440- .asColumnGroup(current.name)
441- .addPath(current.path())
442-
443- // remove the group if it's empty and removeEmptyGroups is true
444- // else, simply update the parent's data with the adjusted column group
445- nodeToExcept.parent.data =
446- if (adjustedCurrent.cols().isEmpty() && removeEmptyGroups) {
447- null
448- } else {
449- adjustedCurrent
423+ return flatMap {
424+ val fullTree = listOf (it).collectTree()
425+ for (columnToExcept in columns.sortedByDescending { it.path.size }) {
426+ // grab the node representing the column from the tree
427+ val nodeToExcept = fullTree.getOrPut(columnToExcept.path).asNullable()
428+ if (nodeToExcept != null ) {
429+ // remove the children from the node (if it's a column group) and remove its data (the column itself)
430+ nodeToExcept.allChildren().forEach { it.data = null }
431+ nodeToExcept.data = null
432+
433+ // we need to update the data of the parent node(s) to reflect the removal of the column
434+ if (nodeToExcept.parent != null ) {
435+ // we grab the data of the parent node, which should be a column group
436+ // treat it as a DF to remove the column to except from it and
437+ // convert it back to a column group
438+ val current = nodeToExcept.parent.data as ColumnGroup <* >? ? : continue
439+ val adjustedCurrent = current
440+ .remove(nodeToExcept.name)
441+ .asColumnGroup(current.name)
442+ .addPath(current.path())
443+
444+ // remove the group if it's empty and removeEmptyGroups is true
445+ // else, simply update the parent's data with the adjusted column group
446+ nodeToExcept.parent.data =
447+ if (adjustedCurrent.cols().isEmpty() && removeEmptyGroups) {
448+ null
449+ } else {
450+ adjustedCurrent
451+ }
452+
453+ // now we update the parent's parents recursively with new column group instances
454+ var parent = nodeToExcept.parent.parent
455+
456+ @Suppress(" UNNECESSARY_NOT_NULL_ASSERTION" )
457+ var currentNode = nodeToExcept.parent!!
458+ while (parent != null ) {
459+ val parentData = parent.data as ColumnGroup <* >? ? : break
460+ val currentData = currentNode.data
461+ val modifiedParentData =
462+ if (currentData == null ) {
463+ parentData.remove(currentNode.name)
464+ } else {
465+ parentData.replace(currentNode.name).with { currentData }
466+ }
467+ parent.data = modifiedParentData
468+ .asColumnGroup(parentData.name)
469+ .addPath(parentData.path())
470+ currentNode = parent
471+ parent = parent.parent
450472 }
451-
452- // now we update the parent's parents recursively with new column group instances
453- var parent = nodeToExcept.parent.parent
454-
455- @Suppress(" UNNECESSARY_NOT_NULL_ASSERTION" )
456- var currentNode = nodeToExcept.parent!!
457- while (parent != null ) {
458- val parentData = parent.data as ColumnGroup <* >? ? : break
459- parent.data = parentData
460- .replace(currentNode.name).with { currentNode.data!! }
461- .asColumnGroup(parentData.name)
462- .addPath(parentData.path())
463-
464- currentNode = parent
465- parent = parent.parent
466473 }
467474 }
468475 }
476+ val subtrees = fullTree.topmostChildren { it.data != null }
477+ subtrees.map { it.data!! .addPath(it.pathFromRoot()) }
469478 }
470- val subtrees = fullTree.topmostChildren { it.data != null }
471- return subtrees.map { it.data!! .addPath(it.pathFromRoot()) }
472479}
473480
474481/* *
0 commit comments