@@ -62,21 +62,11 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
62
62
return tree. description
63
63
}
64
64
65
- /// Get the first (key, value) pair.
66
- public var first : ( key: Key , value: Value ? ) ? {
67
- return tree. first
68
- }
69
-
70
65
/// Get the last (key, value) pair.
71
66
public var last : ( key: Key , value: Value ? ) ? {
72
67
return tree. last
73
68
}
74
69
75
- /// A boolean of whether the SortedDictionary is empty.
76
- public var isEmpty : Bool {
77
- return 0 == count
78
- }
79
-
80
70
/// Conforms to the Collection Protocol.
81
71
public var startIndex : Int {
82
72
return 0
@@ -118,8 +108,13 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
118
108
self . init ( )
119
109
insert ( elements)
120
110
}
111
+
112
+ fileprivate init ( tree : RedBlackTree < Key , Value > ) {
113
+ self . init ( )
114
+ self . tree = tree
115
+ }
121
116
122
- public func makeIterator( ) -> SortedDictionary . Iterator {
117
+ public func makeIterator( ) -> Iterator {
123
118
var i = indices. makeIterator ( )
124
119
return AnyIterator { i. next ( ) . map { self [ $0] } }
125
120
}
@@ -309,7 +304,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
309
304
/**
310
305
Searches for given keys in the SortedDictionary.
311
306
- Parameter for keys: A list of Key types.
312
- - Returns: A SortedDictionary<Key, Value> .
307
+ - Returns: A SortedDictionary.
313
308
*/
314
309
public func search( for keys: Key ... ) -> SortedDictionary < Key , Value > {
315
310
return search ( for: keys)
@@ -318,7 +313,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
318
313
/**
319
314
Searches for given keys in the SortedDictionary.
320
315
- Parameter for keys: An Array of Key types.
321
- - Returns: A SortedDictionary<Key, Value> .
316
+ - Returns: A SortedDictionary.
322
317
*/
323
318
public func search( for keys: [ Key ] ) -> SortedDictionary < Key , Value > {
324
319
var d = SortedDictionary < Key , Value > ( )
@@ -332,9 +327,9 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
332
327
Traverses the SortedDictionary, looking for a key matches.
333
328
- Parameter for key: A Key type.
334
329
- Parameter node: A RedBlackNode<Key, Value>.
335
- - Parameter dictionary: A SortedDictionary<Key, Value> to map the results too.
330
+ - Parameter dictionary: A SortedDictionary to map the results too.
336
331
*/
337
- internal func traverse( for key: Key , node: RedBlackNode < Key , Value > , dictionary: inout SortedDictionary < Key , Value > ) {
332
+ internal func traverse( for key: Key , node: RedBlackNode < Key , Value > , dictionary: inout SortedDictionary ) {
338
333
guard tree. sentinel !== node else {
339
334
return
340
335
}
@@ -346,44 +341,26 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
346
341
traverse ( for: key, node: node. left, dictionary: & dictionary)
347
342
traverse ( for: key, node: node. right, dictionary: & dictionary)
348
343
}
344
+
345
+ static public func == ( lhs: SortedDictionary , rhs: SortedDictionary ) -> Bool {
346
+ return lhs. tree == rhs. tree
347
+ }
348
+
349
+ static public func + ( lhs: SortedDictionary , rhs: SortedDictionary ) -> SortedDictionary < Key , Value > {
350
+ return SortedDictionary ( tree : lhs. tree + rhs. tree)
351
+ }
352
+
353
+ static public func += ( lhs: inout SortedDictionary , rhs: SortedDictionary ) {
354
+ lhs. tree += rhs. tree
355
+ }
356
+
357
+ static public func - ( lhs: SortedDictionary , rhs: SortedDictionary ) -> SortedDictionary < Key , Value > {
358
+ return SortedDictionary ( tree : lhs. tree - rhs. tree)
359
+ }
360
+
361
+ static public func -= ( lhs: inout SortedDictionary , rhs: SortedDictionary ) {
362
+ lhs. tree -= rhs. tree
363
+ }
349
364
}
350
365
351
- public func == < Key : Comparable , Value> ( lhs: SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) -> Bool {
352
- if lhs. count != rhs. count {
353
- return false
354
- }
355
- for i in 0 ..< lhs. count {
356
- if lhs [ i] . key != rhs [ i] . key {
357
- return false
358
- }
359
- }
360
- return true
361
- }
362
-
363
- public func != < Key : Comparable , Value> ( lhs: SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) -> Bool {
364
- return !( lhs == rhs)
365
- }
366
-
367
- public func + < Key : Comparable , Value> ( lhs: SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) -> SortedDictionary < Key , Value > {
368
- var t = lhs
369
- for (k, v) in rhs {
370
- t. insert ( value: v, for: k)
371
- }
372
- return t
373
- }
374
-
375
- public func += < Key : Comparable , Value> ( lhs: inout SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) {
376
- for (k, v) in rhs {
377
- lhs. insert ( value: v, for: k)
378
- }
379
- }
380
-
381
- public func - < Key : Comparable , Value> ( lhs: SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) -> SortedDictionary < Key , Value > {
382
- var t = lhs
383
- t. removeValue ( for: rhs. keys)
384
- return t
385
- }
386
366
387
- public func -= < Key : Comparable , Value> ( lhs: inout SortedDictionary < Key , Value > , rhs: SortedDictionary < Key , Value > ) {
388
- lhs. removeValue ( for: rhs. keys)
389
- }
0 commit comments