Skip to content

Commit 9afe748

Browse files
authored
[Basics] Switch IdentifiableSet to use OrderedDictionary (swiftlang#7634)
- Explanation: Turns `IdentifiableSet` into an ordered collection which should cut on flakiness in tests and other spots that expect certain ordering of elements. - Scope: All uses of `IdentifiableSet` i.e. package graph. - Main Branch PR: swiftlang#7630 - Risk: Low - Reviewed By: @MaxDesiatov - Testing: No tests since the change is somewhat NFC. (cherry picked from commit b0afde9)
1 parent b48dd91 commit 9afe748

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Sources/Basics/Collections/IdentifiableSet.swift

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import struct OrderedCollections.OrderedDictionary
14+
1315
/// Replacement for `Set` elements that can't be `Hashable`, but can be `Identifiable`.
1416
public struct IdentifiableSet<Element: Identifiable>: Collection {
1517
public init() {
@@ -20,7 +22,7 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
2022
self.storage = .init(pickLastWhenDuplicateFound: sequence)
2123
}
2224

23-
fileprivate typealias Storage = [Element.ID: Element]
25+
fileprivate typealias Storage = OrderedDictionary<Element.ID, Element>
2426

2527
public struct Index: Comparable {
2628
public static func < (lhs: IdentifiableSet<Element>.Index, rhs: IdentifiableSet<Element>.Index) -> Bool {
@@ -33,23 +35,23 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
3335
private var storage: Storage
3436

3537
public var startIndex: Index {
36-
Index(storageIndex: storage.startIndex)
38+
Index(storageIndex: self.storage.elements.startIndex)
3739
}
3840

3941
public var endIndex: Index {
40-
Index(storageIndex: storage.endIndex)
42+
Index(storageIndex: self.storage.elements.endIndex)
4143
}
4244

4345
public subscript(position: Index) -> Element {
44-
self.storage[position.storageIndex].value
46+
self.storage.elements[position.storageIndex].value
4547
}
4648

4749
public subscript(id: Element.ID) -> Element? {
4850
self.storage[id]
4951
}
5052

5153
public func index(after i: Index) -> Index {
52-
Index(storageIndex: self.storage.index(after: i.storageIndex))
54+
Index(storageIndex: self.storage.elements.index(after: i.storageIndex))
5355
}
5456

5557
public func union(_ otherSequence: some Sequence<Element>) -> Self {
@@ -88,14 +90,14 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
8890
}
8991
}
9092

91-
extension Dictionary where Value: Identifiable, Key == Value.ID {
93+
extension OrderedDictionary where Value: Identifiable, Key == Value.ID {
9294
fileprivate init(pickLastWhenDuplicateFound sequence: some Sequence<Value>) {
9395
self.init(sequence.map { ($0.id, $0) }, uniquingKeysWith: { $1 })
9496
}
9597
}
9698

9799
extension IdentifiableSet: Equatable {
98-
public static func ==(_ lhs: Self, _ rhs: Self) -> Bool {
100+
public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
99101
lhs.storage.keys == rhs.storage.keys
100102
}
101103
}

0 commit comments

Comments
 (0)