28
28
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
*/
30
30
31
- public struct SortedDictionary < Key: Comparable , Value> : Probable , Collection , Equatable , CustomStringConvertible where Key : Hashable {
31
+ public struct SortedDictionary < Key: Comparable & Hashable , Value> : Probable , Collection , BidirectionalCollection , Equatable , CustomStringConvertible {
32
32
public typealias Element = Key
33
33
34
34
/// Returns the position immediately after the given index.
@@ -37,7 +37,11 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
37
37
/// `endIndex`.
38
38
/// - Returns: The index value immediately after `i`.
39
39
public func index( after i: Int ) -> Int {
40
- return i < endIndex ? i + 1 : 0
40
+ return i + 1
41
+ }
42
+
43
+ public func index( before i: Int ) -> Int {
44
+ return i - 1
41
45
}
42
46
43
47
public typealias Iterator = AnyIterator < ( key: Key , value: Value ? ) >
@@ -62,11 +66,6 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
62
66
return tree. description
63
67
}
64
68
65
- /// Get the last (key, value) pair.
66
- public var last : ( key: Key , value: Value ? ) ? {
67
- return tree. last
68
- }
69
-
70
69
/// Conforms to the Collection Protocol.
71
70
public var startIndex : Int {
72
71
return 0
@@ -89,7 +88,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
89
88
90
89
/// Initializer.
91
90
public init ( ) {
92
- tree = RedBlackTree < Key , Value > ( uniqueKeys: true )
91
+ tree = RedBlackTree ( uniqueKeys: true )
93
92
}
94
93
95
94
/**
@@ -341,26 +340,25 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
341
340
traverse ( for: key, node: node. left, dictionary: & dictionary)
342
341
traverse ( for: key, node: node. right, dictionary: & dictionary)
343
342
}
344
-
343
+
345
344
static public func == ( lhs: SortedDictionary , rhs: SortedDictionary ) -> Bool {
346
345
return lhs. tree == rhs. tree
347
346
}
348
-
347
+
349
348
static public func + ( lhs: SortedDictionary , rhs: SortedDictionary ) -> SortedDictionary < Key , Value > {
350
349
return SortedDictionary ( tree : lhs. tree + rhs. tree)
351
350
}
352
-
351
+
353
352
static public func += ( lhs: inout SortedDictionary , rhs: SortedDictionary ) {
354
353
lhs. tree += rhs. tree
355
354
}
356
-
355
+
357
356
static public func - ( lhs: SortedDictionary , rhs: SortedDictionary ) -> SortedDictionary < Key , Value > {
358
357
return SortedDictionary ( tree : lhs. tree - rhs. tree)
359
358
}
360
-
359
+
361
360
static public func -= ( lhs: inout SortedDictionary , rhs: SortedDictionary ) {
362
361
lhs. tree -= rhs. tree
363
362
}
364
363
}
365
364
366
-
0 commit comments