Skip to content

Commit b115390

Browse files
authored
Merge pull request #506 from viere1234/main
[SortedCollections] Fix incorrect offset calculation in BTree.findAnyIndex
2 parents e7ffe90 + 4ea19dd commit b115390

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Sources/SortedCollections/BTree/_BTree.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ extension _BTree {
344344
offset += keySlot
345345

346346
if keySlot < handle.elementCount && handle[keyAt: keySlot] == key {
347+
if !handle.isLeaf {
348+
for i in 0...keySlot {
349+
offset += handle[childAt: i].storage.header.subtreeCount
350+
}
351+
}
352+
347353
return Index(
348354
node: .passUnretained(storage),
349355
slot: keySlot,

Tests/SortedCollectionsTests/SortedSet/SortedSet Tests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ class SortedSetTests: CollectionTestCase {
8080
}
8181
}
8282

83+
func test_indexOf() {
84+
withEvery("count", in: 0 ..< 40) { count in
85+
let set = SortedSet(0 ..< count)
86+
withEvery("item", in: 0 ..< count) { item in
87+
expectNotNil(set.index(of: item)) { index in
88+
expectEqual(set[index], item)
89+
let offset = set.distance(from: set.startIndex, to: index)
90+
expectEqual(offset, item)
91+
}
92+
}
93+
expectNil(set.index(of: count))
94+
}
95+
}
96+
8397
func test_removeAtIndex() {
8498
withEvery("count", in: 0 ..< 40) { count in
8599
withEvery("index", in: 0..<count) { index in

0 commit comments

Comments
 (0)