Skip to content

Commit f512375

Browse files
authored
Add customized Bimap.filter, sprinkle in performance enhancements (#169)
1 parent 1e7bb09 commit f512375

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Sources/DataStructures/Bimap.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,48 @@ public struct Bimap <Key: Hashable, Value: Hashable>: Hashable {
1111

1212
// MARK: - Instance Properties
1313

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]
1619
}
1720

1821
extension Bimap {
1922

2023
// MARK: - Initializers
2124

2225
/// Create an empty `Bimap`.
26+
@inlinable
2327
public init() {
2428
self.valueByKey = [:]
2529
self.keyByValue = [:]
2630
}
2731

2832
/// Create an empty `Bimap` reserving the amount of memory needed to store the given
2933
/// `minimumCapacity` of key-value pairs.
34+
@inlinable
3035
public init(minimumCapacity: Int) {
3136
valueByKey = [Key: Value](minimumCapacity: minimumCapacity)
3237
keyByValue = [Value: Key](minimumCapacity: minimumCapacity)
3338
}
3439

3540
/// Create a `Bimap` from a dictionary.
41+
@inlinable
3642
public init(_ elements: Dictionary<Key, Value>) {
3743
self.init(minimumCapacity: elements.count)
3844
for (k, value) in elements { self[key: k] = value }
3945
}
4046

4147
/// Create a `Bimap` from the given `sequence` of key-value pairs.
48+
@inlinable
4249
public init <S: Sequence> (_ sequence: S) where S.Element == (Key, Value) {
4350
self.init()
4451
for (k, value) in sequence { self[key: k] = value }
4552
}
4653

4754
/// Create a `Bimap` from the given `collection` of key-value pairs.
55+
@inlinable
4856
public init <C: Collection> (_ collection: C) where C.Element == (Key, Value) {
4957
self.init(minimumCapacity: collection.count)
5058
for (k, value) in collection { self[key: k] = value }
@@ -56,21 +64,25 @@ extension Bimap {
5664
// MARK: - Computed Properties
5765

5866
/// - Returns: The amount of key-value pairs contained herein.
67+
@inlinable
5968
public var count: Int {
6069
return valueByKey.count
6170
}
6271

6372
/// - Returns: `true` if there are no key-value pairs contained herein. Otherwise, `false`.
73+
@inlinable
6474
public var isEmpty: Bool {
6575
return valueByKey.isEmpty
6676
}
6777

6878
/// - Returns: A collection of `Key` values.
79+
@inlinable
6980
public var keys: AnyCollection<Key> {
7081
return AnyCollection(valueByKey.keys)
7182
}
7283

7384
/// - Returns: A collection of `Value` values.
85+
@inlinable
7486
public var values: AnyCollection<Value> {
7587
return AnyCollection(keyByValue.keys)
7688
}
@@ -81,6 +93,7 @@ extension Bimap {
8193
// MARK: - Subscripts
8294

8395
/// Get and set the `Key` for the given `value`.
96+
@inlinable
8497
public subscript(value value: Value) -> Key? {
8598
get { return keyByValue[value] }
8699
set(newKey) {
@@ -92,6 +105,7 @@ extension Bimap {
92105
}
93106

94107
/// Get and set the `Value` for the given `key`.
108+
@inlinable
95109
public subscript(key key: Key) -> Value? {
96110
get { return valueByKey[key] }
97111
set {
@@ -110,6 +124,7 @@ extension Bimap {
110124
/// Updates the current value to the given `value` for the given `key`.
111125
///
112126
/// - Returns: The previous value for the given `key`, if it existed. Otherwise, `nil`.
127+
@inlinable
113128
@discardableResult
114129
public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? {
115130
let previous = self[key: key]
@@ -120,6 +135,7 @@ extension Bimap {
120135
/// Updates the current key to the given `key` for the given `value`.
121136
///
122137
/// - Returns: The previous key for the given `value`, if it existed. Otherwise, `nil`.
138+
@inlinable
123139
@discardableResult
124140
public mutating func updateKey(_ key: Key, forValue value: Value) -> Key? {
125141
let previous = self[value: value]
@@ -130,6 +146,7 @@ extension Bimap {
130146
/// Removes the value for the given `key`.
131147
///
132148
/// - Returns: The previous value for the given `key`, if it existed. Otherwise, `nil`.
149+
@inlinable
133150
@discardableResult
134151
public mutating func removeValue(forKey key: Key) -> Value? {
135152
let previous = self[key: key]
@@ -140,6 +157,7 @@ extension Bimap {
140157
/// Removes the key for the given `value`.
141158
///
142159
/// - Returns: The previous key for the given `value`, if it existed. Otherwise, `nil`.
160+
@inlinable
143161
@discardableResult
144162
public mutating func removeKey(forValue value: Value) -> Key? {
145163
let previous = self[value: value]
@@ -148,22 +166,31 @@ extension Bimap {
148166
}
149167

150168
/// Removes all key-value pairs, without releasing memory.
169+
@inlinable
151170
public mutating func removeAll(keepCapacity capacity: Bool = true) {
152171
keyByValue.removeAll(keepingCapacity: capacity)
153172
valueByKey.removeAll(keepingCapacity: capacity)
154173
}
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+
}
155180
}
156181

157182
extension Bimap: DictionaryProtocol {
158183

159184
/// Gets and sets the value for the `key`.
185+
@inlinable
160186
public subscript(key: Key) -> Value? {
161187
get { return self[key: key] }
162188
set { self[key: key] = newValue }
163189
}
164190

165191
/// Reserves the amount of memory needed to store the given `minimumCapacity` of key-value
166192
/// pairs.
193+
@inlinable
167194
public mutating func reserveCapacity(_ minimumCapacity: Int) {
168195
valueByKey.reserveCapacity(minimumCapacity)
169196
keyByValue.reserveCapacity(minimumCapacity)
@@ -175,6 +202,7 @@ extension Bimap: CollectionWrapping {
175202
// MARK: - CollectionWrapping
176203

177204
/// - Returns: The `[Key: Value]` base for `Collection` operations.
205+
@inlinable
178206
public var base: [Key: Value] {
179207
return valueByKey
180208
}
@@ -184,6 +212,7 @@ extension Bimap: ExpressibleByDictionaryLiteral {
184212

185213
// MARK: ExpressibleByDictionaryLiteral Protocol Conformance
186214
/// Constructs a bimap using a dictionary literal.
215+
@inlinable
187216
public init(dictionaryLiteral elements: (Key, Value)...) {
188217
self.init(elements)
189218
}

0 commit comments

Comments
 (0)