Skip to content

Commit

Permalink
fix(core): don't trust supplied ordering when merging nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Aug 10, 2024
1 parent eafa7a4 commit 409c9f1
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,23 @@ class AncestryTree<T : MappingTreeView, E : ElementMappingView>(
* Respects existing ordering in case of conflicts - only latest entries are kept, older are discarded.
*
* @param nodes the nodes to merge
* @param tree the parent tree of the merged node, defaults to the tree of the first specified node
* @return the merged node
*/
fun <T : MappingTreeView, E : ElementMappingView> nodeOf(vararg nodes: AncestryTree.Node<T, E>): AncestryTree.Node<T, E> {
fun <T : MappingTreeView, E : ElementMappingView> nodeOf(
vararg nodes: AncestryTree.Node<T, E>,
tree: AncestryTree<T, E>? = null,
): AncestryTree.Node<T, E> {
require(nodes.isNotEmpty()) {
"No nodes provided"
}

if (nodes.size == 1) return nodes.first()

// create merged node
val lastNode = nodes.last()
return AncestryTree.Node(
lastNode.tree,
tree ?: nodes.first().tree,
nodes.flatMap { it.entries }
.associate { it.key to it.value },
last = lastNode.last
.associateTo(sortedMapOf()) { it.key to it.value }
)
}

0 comments on commit 409c9f1

Please sign in to comment.