10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import struct OrderedCollections. OrderedDictionary
14
+
13
15
/// Replacement for `Set` elements that can't be `Hashable`, but can be `Identifiable`.
14
16
public struct IdentifiableSet < Element: Identifiable > : Collection {
15
17
public init ( ) {
@@ -20,7 +22,7 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
20
22
self . storage = . init( pickLastWhenDuplicateFound: sequence)
21
23
}
22
24
23
- fileprivate typealias Storage = [ Element . ID : Element ]
25
+ fileprivate typealias Storage = OrderedDictionary < Element . ID , Element >
24
26
25
27
public struct Index : Comparable {
26
28
public static func < ( lhs: IdentifiableSet < Element > . Index , rhs: IdentifiableSet < Element > . Index ) -> Bool {
@@ -33,15 +35,15 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
33
35
private var storage : Storage
34
36
35
37
public var startIndex : Index {
36
- Index ( storageIndex: storage. startIndex)
38
+ Index ( storageIndex: self . storage. elements . startIndex)
37
39
}
38
40
39
41
public var endIndex : Index {
40
- Index ( storageIndex: storage. endIndex)
42
+ Index ( storageIndex: self . storage. elements . endIndex)
41
43
}
42
44
43
45
public subscript( position: Index ) -> Element {
44
- self . storage [ position. storageIndex] . value
46
+ self . storage. elements [ position. storageIndex] . value
45
47
}
46
48
47
49
public subscript( id: Element . ID ) -> Element ? {
@@ -54,7 +56,7 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
54
56
}
55
57
56
58
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) )
58
60
}
59
61
60
62
public mutating func insert( _ element: Element ) {
@@ -97,14 +99,14 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
97
99
}
98
100
}
99
101
100
- extension Dictionary where Value: Identifiable , Key == Value . ID {
102
+ extension OrderedDictionary where Value: Identifiable , Key == Value . ID {
101
103
fileprivate init ( pickLastWhenDuplicateFound sequence: some Sequence < Value > ) {
102
104
self . init ( sequence. map { ( $0. id, $0) } , uniquingKeysWith: { $1 } )
103
105
}
104
106
}
105
107
106
108
extension IdentifiableSet : Equatable {
107
- public static func == ( _ lhs: Self , _ rhs: Self ) -> Bool {
109
+ public static func == ( _ lhs: Self , _ rhs: Self ) -> Bool {
108
110
lhs. storage. keys == rhs. storage. keys
109
111
}
110
112
}
0 commit comments