Skip to content

Commit 0d557ec

Browse files
authored
[Basics] Switch IdentifiableSet to use OrderedDictionary (swiftlang#7630)
### Motivation: Turns `IdentifiableSet` into an ordered collection which should cut on flakiness in tests and other spots that expect certain ordering of elements. ### Modifications: Changes `storage` of `IdentifiableSet` from `Dictionary` to `OrderedDictionary`. ### Result: Less flakiness throughout.
1 parent b73aaa4 commit 0d557ec

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,15 +35,15 @@ 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? {
@@ -54,7 +56,7 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
5456
}
5557

5658
public func index(after i: Index) -> Index {
57-
Index(storageIndex: self.storage.index(after: i.storageIndex))
59+
Index(storageIndex: self.storage.elements.index(after: i.storageIndex))
5860
}
5961

6062
public mutating func insert(_ element: Element) {
@@ -97,14 +99,14 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
9799
}
98100
}
99101

100-
extension Dictionary where Value: Identifiable, Key == Value.ID {
102+
extension OrderedDictionary where Value: Identifiable, Key == Value.ID {
101103
fileprivate init(pickLastWhenDuplicateFound sequence: some Sequence<Value>) {
102104
self.init(sequence.map { ($0.id, $0) }, uniquingKeysWith: { $1 })
103105
}
104106
}
105107

106108
extension IdentifiableSet: Equatable {
107-
public static func ==(_ lhs: Self, _ rhs: Self) -> Bool {
109+
public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
108110
lhs.storage.keys == rhs.storage.keys
109111
}
110112
}

0 commit comments

Comments
 (0)