Skip to content

Commit 09325c0

Browse files
committed
* refactoring sorted dictionary
1 parent e18789a commit 09325c0

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Sources/SortedDictionary.swift

+12-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

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 {
3232
public typealias Element = Key
3333

3434
/// Returns the position immediately after the given index.
@@ -37,7 +37,11 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
3737
/// `endIndex`.
3838
/// - Returns: The index value immediately after `i`.
3939
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
4145
}
4246

4347
public typealias Iterator = AnyIterator<(key: Key, value: Value?)>
@@ -62,11 +66,6 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
6266
return tree.description
6367
}
6468

65-
/// Get the last (key, value) pair.
66-
public var last: (key: Key, value: Value?)? {
67-
return tree.last
68-
}
69-
7069
/// Conforms to the Collection Protocol.
7170
public var startIndex: Int {
7271
return 0
@@ -89,7 +88,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
8988

9089
/// Initializer.
9190
public init() {
92-
tree = RedBlackTree<Key, Value>(uniqueKeys: true)
91+
tree = RedBlackTree(uniqueKeys: true)
9392
}
9493

9594
/**
@@ -341,26 +340,25 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
341340
traverse(for: key, node: node.left, dictionary: &dictionary)
342341
traverse(for: key, node: node.right, dictionary: &dictionary)
343342
}
344-
343+
345344
static public func ==(lhs: SortedDictionary, rhs: SortedDictionary) -> Bool {
346345
return lhs.tree == rhs.tree
347346
}
348-
347+
349348
static public func +(lhs: SortedDictionary, rhs: SortedDictionary) -> SortedDictionary<Key, Value> {
350349
return SortedDictionary(tree : lhs.tree + rhs.tree)
351350
}
352-
351+
353352
static public func +=(lhs: inout SortedDictionary, rhs: SortedDictionary) {
354353
lhs.tree += rhs.tree
355354
}
356-
355+
357356
static public func -(lhs: SortedDictionary, rhs: SortedDictionary) -> SortedDictionary<Key, Value> {
358357
return SortedDictionary(tree : lhs.tree - rhs.tree)
359358
}
360-
359+
361360
static public func -=(lhs: inout SortedDictionary, rhs: SortedDictionary) {
362361
lhs.tree -= rhs.tree
363362
}
364363
}
365364

366-

0 commit comments

Comments
 (0)