2929*/
3030
3131public struct RedBlackTree < Key: Comparable , Value> : Probable , Collection , BidirectionalCollection , CustomStringConvertible {
32+ public typealias Element = ( key: Key , value: Value ? )
33+ public typealias ProbableElement = Key
34+
3235 /// Returns the position immediately after the given index.
3336 ///
3437 /// - Parameter i: A valid index of the collection. `i` must be less than
@@ -42,7 +45,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
4245 return i - 1
4346 }
4447
45- public typealias Iterator = AnyIterator < ( key : Key , value : Value ? ) >
48+ public typealias Iterator = AnyIterator < Element >
4649
4750 /**
4851 Total number of elements within the RedBlackTree
@@ -99,6 +102,46 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
99102 public var endIndex : Int {
100103 return count
101104 }
105+
106+ /**
107+ :name: first
108+ :description: Get the first node value in the tree, this is
109+ the first node based on the order of keys where
110+ k1 <= k2 <= K3 ... <= Kn
111+ - returns: Element?
112+ */
113+ public var first : Element ? {
114+ guard 0 < count else {
115+ return nil
116+ }
117+
118+ return self [ 0 ]
119+ }
120+
121+ /**
122+ :name: last
123+ :description: Get the last node value in the tree, this is
124+ the last node based on the order of keys where
125+ k1 <= k2 <= K3 ... <= Kn
126+ - returns: Element?
127+ */
128+ public var last : Element ? {
129+ guard 0 < count else {
130+ return nil
131+ }
132+
133+ return self [ count - 1 ]
134+ }
135+
136+ /// Retrieves an Array of the key values in order.
137+ public var keys : [ Key ] {
138+ return map { $0. key }
139+ }
140+
141+ /// Retrieves an Array of the values that are sorted based.
142+ public var values : [ Value ] {
143+ return flatMap { $0. value }
144+ }
102145
103146 /**
104147 :name: init
@@ -123,6 +166,10 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
123166 root = sentinel
124167 }
125168
169+ public func _customIndexOfEquatableElement( _ element: Key ) -> Int ? ? {
170+ return nil
171+ }
172+
126173 //
127174 // :name: generate
128175 // :description: Conforms to the SequenceType Protocol. Returns
@@ -147,9 +194,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
147194 */
148195 public func count( of keys: [ Key ] ) -> Int {
149196 var c = 0
150- for key in keys {
151- internalCount ( key, node: root, count: & c)
197+
198+ for k in keys {
199+ internalCount ( k, node: root, count: & c)
152200 }
201+
153202 return c
154203 }
155204
@@ -176,11 +225,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
176225 }
177226
178227 var c = 0
179- for (k, v) in self {
228+
229+ for (k, v) in self {
180230 if block ( k, v) {
181231 c += 1
182232 }
183233 }
234+
184235 return Double ( c) / Double( count)
185236 }
186237
@@ -249,6 +300,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
249300 mutating public func removeValue( for keys: [ Key ] ) {
250301 for x in keys {
251302 var z = internalRemoveValueForKey ( x)
303+
252304 while sentinel !== z {
253305 z = internalRemoveValueForKey ( x)
254306 }
@@ -296,6 +348,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
296348 return internalFindNodeForKey ( key) . value
297349 }
298350
351+ /**
352+ Returns the Key value at a given position.
353+ - Parameter position: An Int.
354+ - Returns: A Key.
355+ */
356+ public subscript( position: Int ) -> Key {
357+ return self [ position] . key
358+ }
359+
299360 /**
300361 :name: operator [0...count - 1]
301362 :description: Allows array like access of the index.
@@ -664,13 +725,16 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
664725 */
665726 private func internalSelect( _ x: RedBlackNode < Key , Value > , order: Int ) -> RedBlackNode < Key , Value > {
666727 validateOrder ( order)
728+
667729 let r = x. left. order + 1
668- if order == r {
730+
731+ if order == r {
669732 return x
670733 } else if order < r {
671734 return internalSelect ( x. left, order: order)
672735 }
673- return internalSelect ( x. right, order: order - r)
736+
737+ return internalSelect ( x. right, order: order - r)
674738 }
675739
676740 /**
@@ -682,6 +746,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
682746 if key == node. key {
683747 count += 1
684748 }
749+
685750 internalCount ( key, node: node. left, count: & count)
686751 internalCount ( key, node: node. right, count: & count)
687752 }
@@ -696,6 +761,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
696761 if key == node. key {
697762 node. value = value
698763 }
764+
699765 internalUpdateValue ( value, for: key, node: node. left)
700766 internalUpdateValue ( value, for: key, node: node. right)
701767 }
@@ -709,12 +775,14 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
709775 private func internalOrder( _ node: RedBlackNode < Key , Value > ) -> Int {
710776 var x = node
711777 var r : Int = x. left. order + 1
712- while root !== x {
778+
779+ while root !== x {
713780 if x. parent. right === x {
714781 r += x. parent. left. order + 1
715782 }
716783 x = x. parent
717784 }
785+
718786 return r
719787 }
720788
@@ -727,19 +795,26 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
727795 }
728796
729797 public static func == ( lhs: RedBlackTree , rhs: RedBlackTree ) -> Bool {
730- return lhs. count == rhs. count && lhs. elementsEqual ( rhs) {
731- $0. 0 . key == $0. 1 . key
732- }
798+ return lhs. count == rhs. count && lhs. elementsEqual ( rhs, by: { a, b -> Bool in
799+ return a. key == b. key
800+ } )
801+ }
802+
803+ public static func != ( lhs: RedBlackTree , rhs: RedBlackTree ) -> Bool {
804+ return !( lhs == rhs)
733805 }
734806
735807 public static func + ( lhs: RedBlackTree , rhs: RedBlackTree ) -> RedBlackTree < Key , Value > {
736808 var t = RedBlackTree ( )
809+
737810 for (k, v) in lhs {
738811 t. insert ( value: v, for: k)
739812 }
813+
740814 for (k, v) in rhs {
741815 t. insert ( value: v, for: k)
742816 }
817+
743818 return t
744819 }
745820
@@ -751,9 +826,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
751826
752827 public static func - ( lhs: RedBlackTree , rhs: RedBlackTree ) -> RedBlackTree {
753828 var t = rhs
829+
754830 for (k, _) in rhs {
755831 t. removeValue ( for: k)
756832 }
833+
757834 return t
758835 }
759836
0 commit comments