@@ -160,143 +160,41 @@ extension BinaryDistinctString {
160160 }
161161}
162162
163- public struct BinaryDistinctDictionary < V> : Collection , ExpressibleByDictionaryLiteral , Sendable where V: Any , V: Sendable , V: Hashable {
164- public typealias Key = BinaryDistinctString
165-
166- public var storage : [ Key : V ] = [ : ]
167-
168- // MARK: - Initializers
169- public init ( _ dictionary: [ Key : V ] = [ : ] ) {
170- self . storage = dictionary
171- }
172-
173- /// Initializes from `[String: Value]`
174- public init ( _ dictionary: [ String : V ] ) {
175- self . storage = Dictionary ( uniqueKeysWithValues: dictionary. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
176- }
177-
178- /// Initializes from `[NSString: Value]`
179- public init ( _ dictionary: [ NSString : V ] ) {
180- self . storage = Dictionary ( uniqueKeysWithValues: dictionary. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
181- }
182-
183- public init ( dictionaryLiteral elements: ( Key , V ) ... ) {
184- self . storage = Dictionary ( uniqueKeysWithValues: elements)
185- }
186-
187- // MARK: - Dictionary Operations
188- public subscript( key: Key ) -> V ? {
189- get { return storage [ key] }
190- set { storage [ key] = newValue }
191- }
192-
193- public var keys : [ Key ] {
194- return Array ( storage. keys)
195- }
196-
197- public var values : [ V ] {
198- return Array ( storage. values)
199- }
200-
201- // MARK: - Collection Conformance
202- public typealias Index = Dictionary < Key , V > . Index
203- public typealias Element = ( key: Key , value: V )
204-
205- public var startIndex : Index { storage. startIndex }
206- public var endIndex : Index { storage. endIndex }
207-
208- public func index( after i: Index ) -> Index {
209- return storage. index ( after: i)
210- }
211-
212- public subscript( position: Index ) -> Element {
213- return storage [ position]
214- }
215-
216- /// Returns a new dictionary with keys mapped to the requested type.
217- public func mapKeys< K: StringConvertible > ( _ type: K . Type ) -> [ K : V ] {
218- return Dictionary (
219- uniqueKeysWithValues: storage. map {
220- ( K . self == String . self ? $0. key. string as! K : $0. key. nsString as! K , $0. value)
221- }
222- )
223- }
224-
225- mutating public func removeValue( forKey key: Key ) -> V ? {
226- return self . storage. removeValue ( forKey: key)
227- }
228-
229- // MARK: - Merging Methods
230-
163+ extension Dictionary where Key == BinaryDistinctString {
231164 /// Merges another `BinaryDistinctDictionary` into this one
232- public mutating func merge( _ other: BinaryDistinctDictionary < Value > , strategy: ( V , V ) -> V = { _, new in new } ) {
233- self . storage. merge ( other. storage, uniquingKeysWith: strategy)
234- }
235-
236- /// Merges a `[String: Value]` dictionary into this one
237- public mutating func merge( _ other: [ BinaryDistinctString : V ] , strategy: ( V , V ) -> V = { _, new in new } ) {
238- let converted = Dictionary ( uniqueKeysWithValues: other. map { ( $0. key, $0. value) } )
239- self . storage. merge ( converted, uniquingKeysWith: strategy)
165+ public mutating func merge( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
166+ self . merge ( other, uniquingKeysWith: strategy)
240167 }
241168
242169 /// Merges a `[String: Value]` dictionary into this one
243- public mutating func merge( _ other: [ String : V ] , strategy: ( V , V ) -> V = { _, new in new } ) {
170+ public mutating func merge( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
244171 let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
245- self . storage . merge ( converted, uniquingKeysWith: strategy)
172+ self . merge ( converted, uniquingKeysWith: strategy)
246173 }
247174
248175 /// Merges a `[NSString: Value]` dictionary into this one
249- public mutating func merge( _ other: [ NSString : V ] , strategy: ( V , V ) -> V = { _, new in new } ) {
176+ public mutating func merge( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) {
250177 let converted = Dictionary ( uniqueKeysWithValues: other. map { ( BinaryDistinctString ( $0. key) , $0. value) } )
251- self . storage. merge ( converted, uniquingKeysWith: strategy)
252- }
253-
254- /// Returns a new dictionary by merging `other` while keeping the current dictionary unchanged.
255- public func merging( _ other: BinaryDistinctDictionary < V > , strategy: ( V , V ) -> V = { _, new in new } ) -> BinaryDistinctDictionary {
256- var newDict = self
257- newDict. merge ( other, strategy: strategy)
258- return newDict
178+ self . merge ( converted, uniquingKeysWith: strategy)
259179 }
260180
261- public func merging( _ other: [ String : V ] , strategy: ( V , V ) -> V = { _, new in new } ) -> BinaryDistinctDictionary {
181+ public func merging( _ other: [ String : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
262182 var newDict = self
263183 newDict. merge ( other, strategy: strategy)
264184 return newDict
265185 }
266186
267- public func merging( _ other: [ BinaryDistinctString : V ] , strategy: ( V , V ) -> V = { _, new in new } ) -> BinaryDistinctDictionary {
187+ public func merging( _ other: [ BinaryDistinctString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
268188 var newDict = self
269189 newDict. merge ( other, strategy: strategy)
270190 return newDict
271191 }
272192
273- public func merging( _ other: [ NSString : V ] , strategy: ( V , V ) -> V = { _, new in new } ) -> BinaryDistinctDictionary {
193+ public func merging( _ other: [ NSString : Value ] , strategy: ( Value , Value ) -> Value = { _, new in new } ) -> Self {
274194 var newDict = self
275195 newDict. merge ( other, strategy: strategy)
276196 return newDict
277197 }
278-
279- public func invert( ) -> [ V : BinaryDistinctString ] {
280- var inverted : [ V : BinaryDistinctString ] = [ : ]
281- for (k, v) in self . storage {
282- inverted [ v] = k
283- }
284- return inverted
285- }
286- }
287-
288- extension BinaryDistinctDictionary : Hashable {
289- public func hash( into hasher: inout Hasher ) {
290- // Combine the count to distinguish between dictionaries of different sizes.
291- hasher. combine ( self . storage. count)
292- // Sort keys for a deterministic order.
293- for key in self . storage. keys. sorted ( ) {
294- hasher. combine ( key)
295- if let value = self . storage [ key] {
296- hasher. combine ( value)
297- }
298- }
299- }
300198}
301199
302200public protocol StringConvertible : ExpressibleByStringLiteral { }
0 commit comments