I'd expect that SortedSet.distance(from:to:) would return the same result as Array.distance(from:to).
I've noticed however that "distance" means something different in SortedSet. I don't fully understand the underlying implementation but I guess it represents something meaningful in a B-Tree structure.
var set = SortedSet<Int>()
for i in 0..<50 {
set.insert(i)
}
set.forEach {
let d = set.distance(from: set.startIndex, to: set.index(of: $0)!)
print(d)
}
Output:
...
34 --> 34
35 --> 3
36 --> 36
...
However, since SortedSet is all about sorting, I'd want to get an "absolute" index (Int) of the particular element.
Is that possible with SortedSet?
Can you clarify your output here? set.distance(from:to:) returns an Int, which you then print, but your proposed output has arrows and other fun stuff in it. I assume the second column is the Index? Can you provide the full output?
@lukasa Oh gosh, sorry, I pasted output after modifying a code a little.
Here's a code:
var set = SortedSet<Int>()
for i in 0..<50 {
set.insert(i)
}
set.forEach {
let i = set.index(of: $0)!
let v = set[i]
let d = set.distance(from: set.startIndex, to: i)
print("\(v) --> \(d)")
if i == set.startIndex {
print("Current `index` equals `startIndex`.")
print("set[index] = \(set[i]). set[startIndex] = \(set[set.startIndex])")
}
}
The issue is easiest to notice when index equals to startIndex but getting values for those indexes returns different results.
It definitely looks like a bug. I'll report on Github.