-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SortedSet
returns different results for equal indexes.
#138
Comments
Yep, this is definitely a bug! (Beware, |
Yup, I saw the Readme notice but I needed a |
After spending some time I figured out:
func test_indexOf() {
var set = SortedSet<Int>()
for i in 0..<50 {
set.insert(i)
}
for (item, index) in zip(set, set.indices) {
let i = set.index(of: item)!
if i != index {
print("Some issue with index(of:)")
break
}
}
} Output: Some issue with index(of:)
func test_indexForKey() {
var sortedDict: SortedDictionary<Int, String> = [:]
for i in 0..<50 {
sortedDict[i] = "\(i)"
}
for (index, item) in zip(sortedDict.indices, sortedDict) {
let i = sortedDict.index(forKey: item.key)!
if i != index {
print("Some issue with index(forKey:)")
break
}
}
} Output: Some issue with index(forKey:)
func test_findAnyIndex() {
var tree = _BTree<Int, String>()
for (key, value) in (0..<50).map({ ($0, "\($0)") }) {
tree.updateAnyValue(value, forKey: key)
}
for (index, item) in zip(tree.indices, tree) {
let i = tree.findAnyIndex(forKey: item.key)
if i != index {
print("Problems with findAnyIndex")
break
}
}
} Output: Problems with findAnyIndex
extension _BTree {
@inlinable
internal func findAnyIndex(forKey key: Key) -> Index? {
var (lo, hi) = (startIndex, endIndex)
while lo < hi {
let mid = index(lo, offsetBy: distance(from: lo, to: hi)/2)
if self[mid].key < key {
lo = index(after: mid)
} else {
hi = mid
}
}
return lo < endIndex && self[lo].key == key ? lo : nil
}
} and all the bugs vanished. 0 --> 0
Current `index` equals `startIndex`.
set[index] = 0. set[startIndex] = 0
1 --> 1
2 --> 2
3 --> 3
4 --> 4
5 --> 5
6 --> 6
7 --> 7
8 --> 8
9 --> 9
10 --> 10
11 --> 11
12 --> 12
13 --> 13
14 --> 14
15 --> 15
16 --> 16
17 --> 17
18 --> 18
19 --> 19
20 --> 20
21 --> 21
22 --> 22
23 --> 23
24 --> 24
25 --> 25
26 --> 26
27 --> 27
28 --> 28
29 --> 29
30 --> 30
31 --> 31
32 --> 32
33 --> 33
34 --> 34
35 --> 35
36 --> 36
37 --> 37
38 --> 38
39 --> 39
40 --> 40
41 --> 41
42 --> 42
43 --> 43
44 --> 44
45 --> 45
46 --> 46
47 --> 47
48 --> 48
49 --> 49 And also the test cases I made are all working just fine now. Hope this helps!🤝 |
I encountered the issue when trying to get an absolute position of the element and got incorrect results.
I started a conversation on forum because I wasn't sure if it's a bug.
Information
Package version: Branch feature/SortedCollections
Platform version: macOS 12.2 Beta (21D5025f)
Swift version:
Checklist
main
branch of this package.Steps to Reproduce
Sample code:
Output:
Expected behavior
When indexes are equal, getting a value for those indexes should return the same value.
Actual behavior
Even though indexes are equal, getting a value for them returns different results.
The text was updated successfully, but these errors were encountered: