Skip to content

SortedSet returns different results for equal indexes. #138

Open
@Moriquendi

Description

@Moriquendi

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.

let index = set.index(of: X)
print(index == set.startIndex) // true
print(set[index] == set[set.startIndex]) // false

Information

Package version: Branch feature/SortedCollections
Platform version: macOS 12.2 Beta (21D5025f)
Swift version:

swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
Target: arm64-apple-macosx12.0

Checklist

  • If possible, I've reproduced the issue using the main branch of this package.
  • I've searched for existing GitHub issues.

Steps to Reproduce

Sample 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])")
   }
}

Output:

0 --> 0
Current index equals startIndex.
set[index] = 0. set[startIndex] = 0
1 --> 1
2 --> 0
Current index equals startIndex.
set[index] = 2. set[startIndex] = 0
3 --> 3
4 --> 4
5 --> 1
6 --> 6
7 --> 7
8 --> 0
...

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions