@@ -11,40 +11,48 @@ public struct Bimap <Key: Hashable, Value: Hashable>: Hashable {
11
11
12
12
// MARK: - Instance Properties
13
13
14
- private var valueByKey : [ Key : Value ]
15
- private var keyByValue : [ Value : Key ]
14
+ @usableFromInline
15
+ var valueByKey : [ Key : Value ]
16
+
17
+ @usableFromInline
18
+ var keyByValue : [ Value : Key ]
16
19
}
17
20
18
21
extension Bimap {
19
22
20
23
// MARK: - Initializers
21
24
22
25
/// Create an empty `Bimap`.
26
+ @inlinable
23
27
public init ( ) {
24
28
self . valueByKey = [ : ]
25
29
self . keyByValue = [ : ]
26
30
}
27
31
28
32
/// Create an empty `Bimap` reserving the amount of memory needed to store the given
29
33
/// `minimumCapacity` of key-value pairs.
34
+ @inlinable
30
35
public init ( minimumCapacity: Int ) {
31
36
valueByKey = [ Key: Value] ( minimumCapacity: minimumCapacity)
32
37
keyByValue = [ Value: Key] ( minimumCapacity: minimumCapacity)
33
38
}
34
39
35
40
/// Create a `Bimap` from a dictionary.
41
+ @inlinable
36
42
public init ( _ elements: Dictionary < Key , Value > ) {
37
43
self . init ( minimumCapacity: elements. count)
38
44
for (k, value) in elements { self [ key: k] = value }
39
45
}
40
46
41
47
/// Create a `Bimap` from the given `sequence` of key-value pairs.
48
+ @inlinable
42
49
public init < S: Sequence > ( _ sequence: S ) where S. Element == ( Key , Value ) {
43
50
self . init ( )
44
51
for (k, value) in sequence { self [ key: k] = value }
45
52
}
46
53
47
54
/// Create a `Bimap` from the given `collection` of key-value pairs.
55
+ @inlinable
48
56
public init < C: Collection > ( _ collection: C ) where C. Element == ( Key , Value ) {
49
57
self . init ( minimumCapacity: collection. count)
50
58
for (k, value) in collection { self [ key: k] = value }
@@ -56,21 +64,25 @@ extension Bimap {
56
64
// MARK: - Computed Properties
57
65
58
66
/// - Returns: The amount of key-value pairs contained herein.
67
+ @inlinable
59
68
public var count : Int {
60
69
return valueByKey. count
61
70
}
62
71
63
72
/// - Returns: `true` if there are no key-value pairs contained herein. Otherwise, `false`.
73
+ @inlinable
64
74
public var isEmpty : Bool {
65
75
return valueByKey. isEmpty
66
76
}
67
77
68
78
/// - Returns: A collection of `Key` values.
79
+ @inlinable
69
80
public var keys : AnyCollection < Key > {
70
81
return AnyCollection ( valueByKey. keys)
71
82
}
72
83
73
84
/// - Returns: A collection of `Value` values.
85
+ @inlinable
74
86
public var values : AnyCollection < Value > {
75
87
return AnyCollection ( keyByValue. keys)
76
88
}
@@ -81,6 +93,7 @@ extension Bimap {
81
93
// MARK: - Subscripts
82
94
83
95
/// Get and set the `Key` for the given `value`.
96
+ @inlinable
84
97
public subscript( value value: Value ) -> Key ? {
85
98
get { return keyByValue [ value] }
86
99
set ( newKey) {
@@ -92,6 +105,7 @@ extension Bimap {
92
105
}
93
106
94
107
/// Get and set the `Value` for the given `key`.
108
+ @inlinable
95
109
public subscript( key key: Key ) -> Value ? {
96
110
get { return valueByKey [ key] }
97
111
set {
@@ -110,6 +124,7 @@ extension Bimap {
110
124
/// Updates the current value to the given `value` for the given `key`.
111
125
///
112
126
/// - Returns: The previous value for the given `key`, if it existed. Otherwise, `nil`.
127
+ @inlinable
113
128
@discardableResult
114
129
public mutating func updateValue( _ value: Value , forKey key: Key ) -> Value ? {
115
130
let previous = self [ key: key]
@@ -120,6 +135,7 @@ extension Bimap {
120
135
/// Updates the current key to the given `key` for the given `value`.
121
136
///
122
137
/// - Returns: The previous key for the given `value`, if it existed. Otherwise, `nil`.
138
+ @inlinable
123
139
@discardableResult
124
140
public mutating func updateKey( _ key: Key , forValue value: Value ) -> Key ? {
125
141
let previous = self [ value: value]
@@ -130,6 +146,7 @@ extension Bimap {
130
146
/// Removes the value for the given `key`.
131
147
///
132
148
/// - Returns: The previous value for the given `key`, if it existed. Otherwise, `nil`.
149
+ @inlinable
133
150
@discardableResult
134
151
public mutating func removeValue( forKey key: Key ) -> Value ? {
135
152
let previous = self [ key: key]
@@ -140,6 +157,7 @@ extension Bimap {
140
157
/// Removes the key for the given `value`.
141
158
///
142
159
/// - Returns: The previous key for the given `value`, if it existed. Otherwise, `nil`.
160
+ @inlinable
143
161
@discardableResult
144
162
public mutating func removeKey( forValue value: Value ) -> Key ? {
145
163
let previous = self [ value: value]
@@ -148,22 +166,31 @@ extension Bimap {
148
166
}
149
167
150
168
/// Removes all key-value pairs, without releasing memory.
169
+ @inlinable
151
170
public mutating func removeAll( keepCapacity capacity: Bool = true ) {
152
171
keyByValue. removeAll ( keepingCapacity: capacity)
153
172
valueByKey. removeAll ( keepingCapacity: capacity)
154
173
}
174
+
175
+ /// - Returns: A `Bimap` with the keys and values filtered by the given `isIncluded`.
176
+ @inlinable
177
+ public func filter( _ isIncluded: ( ( key: Key , value: Value ) ) throws -> Bool ) rethrows -> Bimap {
178
+ return try Bimap ( base. lazy. filter ( isIncluded) )
179
+ }
155
180
}
156
181
157
182
extension Bimap : DictionaryProtocol {
158
183
159
184
/// Gets and sets the value for the `key`.
185
+ @inlinable
160
186
public subscript( key: Key ) -> Value ? {
161
187
get { return self [ key: key] }
162
188
set { self [ key: key] = newValue }
163
189
}
164
190
165
191
/// Reserves the amount of memory needed to store the given `minimumCapacity` of key-value
166
192
/// pairs.
193
+ @inlinable
167
194
public mutating func reserveCapacity( _ minimumCapacity: Int ) {
168
195
valueByKey. reserveCapacity ( minimumCapacity)
169
196
keyByValue. reserveCapacity ( minimumCapacity)
@@ -175,6 +202,7 @@ extension Bimap: CollectionWrapping {
175
202
// MARK: - CollectionWrapping
176
203
177
204
/// - Returns: The `[Key: Value]` base for `Collection` operations.
205
+ @inlinable
178
206
public var base : [ Key : Value ] {
179
207
return valueByKey
180
208
}
@@ -184,6 +212,7 @@ extension Bimap: ExpressibleByDictionaryLiteral {
184
212
185
213
// MARK: ExpressibleByDictionaryLiteral Protocol Conformance
186
214
/// Constructs a bimap using a dictionary literal.
215
+ @inlinable
187
216
public init ( dictionaryLiteral elements: ( Key , Value ) ... ) {
188
217
self . init ( elements)
189
218
}
0 commit comments