From 287c677482b6d4b40491de03ab6dac7d4a23d382 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 23 Sep 2022 10:09:49 -0700 Subject: [PATCH] [PersistentCollections] Fix indexing oversight when spawning a new child in a non-unique node Oops, this did not account for reversed item order, so it was spawning the new child with the wrong item. --- .../Node/_Node+Subtree Insertions.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift b/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift index 079dbfbc..0d9b9c05 100644 --- a/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift +++ b/Sources/PersistentCollections/Node/_Node+Subtree Insertions.swift @@ -226,6 +226,7 @@ extension _Node { ) { dstChildren, dstItems in let srcChildren = src.children let srcItems = src.reverseItems + let i = srcItems.count &- 1 &- slot.value // Initialize children. dstChildren.prefix(childSlot.value) @@ -236,14 +237,13 @@ extension _Node { let r = _Node.build( level: level.descend(), - item1: srcItems[slot.value], existingHash, + item1: srcItems[i], existingHash, item2: inserter, newHash) dstChildren.initializeElement(at: childSlot.value, to: r.top) // Initialize items. - dstItems.prefix(slot.value) - .initializeAll(fromContentsOf: srcItems.prefix(slot.value)) - let rest2 = dstItems.count &- slot.value + dstItems.prefix(i).initializeAll(fromContentsOf: srcItems.prefix(i)) + let rest2 = dstItems.count &- i dstItems.suffix(rest2) .initializeAll(fromContentsOf: srcItems.suffix(rest2)) @@ -254,7 +254,7 @@ extension _Node { } @inlinable @inline(never) - internal mutating func moveNodeAndMakeNewCollision( + internal mutating func moveNodeAndSpawnChild( level: _Level, replacing slot: _Slot, _ bucket: _Bucket, @@ -272,6 +272,7 @@ extension _Node { ) { dstChildren, dstItems in let srcChildren = src.children let srcItems = src.reverseItems + let i = srcItems.count &- 1 &- slot.value // Initialize children. dstChildren.prefix(childSlot.value) @@ -282,14 +283,13 @@ extension _Node { let r = _Node.build( level: level.descend(), - item1: srcItems.moveElement(from: slot.value), existingHash, + item1: srcItems.moveElement(from: i), existingHash, item2: inserter, newHash) dstChildren.initializeElement(at: childSlot.value, to: r.top) // Initialize items. - dstItems.prefix(slot.value) - .moveInitializeAll(fromContentsOf: srcItems.prefix(slot.value)) - let rest2 = dstItems.count &- slot.value + dstItems.prefix(i).moveInitializeAll(fromContentsOf: srcItems.prefix(i)) + let rest2 = dstItems.count &- i dstItems.suffix(rest2) .moveInitializeAll(fromContentsOf: srcItems.suffix(rest2))