Skip to content

Commit e18789a

Browse files
committed
* refactor sorteddictionary
1 parent bb70da5 commit e18789a

File tree

1 file changed

+30
-53
lines changed

1 file changed

+30
-53
lines changed

Sources/SortedDictionary.swift

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,11 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
6262
return tree.description
6363
}
6464

65-
/// Get the first (key, value) pair.
66-
public var first: (key: Key, value: Value?)? {
67-
return tree.first
68-
}
69-
7065
/// Get the last (key, value) pair.
7166
public var last: (key: Key, value: Value?)? {
7267
return tree.last
7368
}
7469

75-
/// A boolean of whether the SortedDictionary is empty.
76-
public var isEmpty: Bool {
77-
return 0 == count
78-
}
79-
8070
/// Conforms to the Collection Protocol.
8171
public var startIndex: Int {
8272
return 0
@@ -118,8 +108,13 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
118108
self.init()
119109
insert(elements)
120110
}
111+
112+
fileprivate init(tree : RedBlackTree<Key, Value>) {
113+
self.init()
114+
self.tree = tree
115+
}
121116

122-
public func makeIterator() -> SortedDictionary.Iterator {
117+
public func makeIterator() -> Iterator {
123118
var i = indices.makeIterator()
124119
return AnyIterator { i.next().map { self[$0] } }
125120
}
@@ -309,7 +304,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
309304
/**
310305
Searches for given keys in the SortedDictionary.
311306
- Parameter for keys: A list of Key types.
312-
- Returns: A SortedDictionary<Key, Value>.
307+
- Returns: A SortedDictionary.
313308
*/
314309
public func search(for keys: Key...) -> SortedDictionary<Key, Value> {
315310
return search(for: keys)
@@ -318,7 +313,7 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
318313
/**
319314
Searches for given keys in the SortedDictionary.
320315
- Parameter for keys: An Array of Key types.
321-
- Returns: A SortedDictionary<Key, Value>.
316+
- Returns: A SortedDictionary.
322317
*/
323318
public func search(for keys: [Key]) -> SortedDictionary<Key, Value> {
324319
var d = SortedDictionary<Key, Value>()
@@ -332,9 +327,9 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
332327
Traverses the SortedDictionary, looking for a key matches.
333328
- Parameter for key: A Key type.
334329
- 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.
336331
*/
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) {
338333
guard tree.sentinel !== node else {
339334
return
340335
}
@@ -346,44 +341,26 @@ public struct SortedDictionary<Key: Comparable, Value>: Probable, Collection, Eq
346341
traverse(for: key, node: node.left, dictionary: &dictionary)
347342
traverse(for: key, node: node.right, dictionary: &dictionary)
348343
}
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+
}
349364
}
350365

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-
}
386366

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

Comments
 (0)