29
29
*/
30
30
31
31
public struct RedBlackTree < Key: Comparable , Value> : Probable , Collection , BidirectionalCollection , CustomStringConvertible {
32
+ public typealias Element = ( key: Key , value: Value ? )
33
+ public typealias ProbableElement = Key
34
+
32
35
/// Returns the position immediately after the given index.
33
36
///
34
37
/// - 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
42
45
return i - 1
43
46
}
44
47
45
- public typealias Iterator = AnyIterator < ( key : Key , value : Value ? ) >
48
+ public typealias Iterator = AnyIterator < Element >
46
49
47
50
/**
48
51
Total number of elements within the RedBlackTree
@@ -99,6 +102,46 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
99
102
public var endIndex : Int {
100
103
return count
101
104
}
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
+ }
102
145
103
146
/**
104
147
:name: init
@@ -123,6 +166,10 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
123
166
root = sentinel
124
167
}
125
168
169
+ public func _customIndexOfEquatableElement( _ element: Key ) -> Int ? ? {
170
+ return nil
171
+ }
172
+
126
173
//
127
174
// :name: generate
128
175
// :description: Conforms to the SequenceType Protocol. Returns
@@ -147,9 +194,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
147
194
*/
148
195
public func count( of keys: [ Key ] ) -> Int {
149
196
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)
152
200
}
201
+
153
202
return c
154
203
}
155
204
@@ -176,11 +225,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
176
225
}
177
226
178
227
var c = 0
179
- for (k, v) in self {
228
+
229
+ for (k, v) in self {
180
230
if block ( k, v) {
181
231
c += 1
182
232
}
183
233
}
234
+
184
235
return Double ( c) / Double( count)
185
236
}
186
237
@@ -249,6 +300,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
249
300
mutating public func removeValue( for keys: [ Key ] ) {
250
301
for x in keys {
251
302
var z = internalRemoveValueForKey ( x)
303
+
252
304
while sentinel !== z {
253
305
z = internalRemoveValueForKey ( x)
254
306
}
@@ -296,6 +348,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
296
348
return internalFindNodeForKey ( key) . value
297
349
}
298
350
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
+
299
360
/**
300
361
:name: operator [0...count - 1]
301
362
:description: Allows array like access of the index.
@@ -664,13 +725,16 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
664
725
*/
665
726
private func internalSelect( _ x: RedBlackNode < Key , Value > , order: Int ) -> RedBlackNode < Key , Value > {
666
727
validateOrder ( order)
728
+
667
729
let r = x. left. order + 1
668
- if order == r {
730
+
731
+ if order == r {
669
732
return x
670
733
} else if order < r {
671
734
return internalSelect ( x. left, order: order)
672
735
}
673
- return internalSelect ( x. right, order: order - r)
736
+
737
+ return internalSelect ( x. right, order: order - r)
674
738
}
675
739
676
740
/**
@@ -682,6 +746,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
682
746
if key == node. key {
683
747
count += 1
684
748
}
749
+
685
750
internalCount ( key, node: node. left, count: & count)
686
751
internalCount ( key, node: node. right, count: & count)
687
752
}
@@ -696,6 +761,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
696
761
if key == node. key {
697
762
node. value = value
698
763
}
764
+
699
765
internalUpdateValue ( value, for: key, node: node. left)
700
766
internalUpdateValue ( value, for: key, node: node. right)
701
767
}
@@ -709,12 +775,14 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
709
775
private func internalOrder( _ node: RedBlackNode < Key , Value > ) -> Int {
710
776
var x = node
711
777
var r : Int = x. left. order + 1
712
- while root !== x {
778
+
779
+ while root !== x {
713
780
if x. parent. right === x {
714
781
r += x. parent. left. order + 1
715
782
}
716
783
x = x. parent
717
784
}
785
+
718
786
return r
719
787
}
720
788
@@ -727,19 +795,26 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
727
795
}
728
796
729
797
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)
733
805
}
734
806
735
807
public static func + ( lhs: RedBlackTree , rhs: RedBlackTree ) -> RedBlackTree < Key , Value > {
736
808
var t = RedBlackTree ( )
809
+
737
810
for (k, v) in lhs {
738
811
t. insert ( value: v, for: k)
739
812
}
813
+
740
814
for (k, v) in rhs {
741
815
t. insert ( value: v, for: k)
742
816
}
817
+
743
818
return t
744
819
}
745
820
@@ -751,9 +826,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Bidire
751
826
752
827
public static func - ( lhs: RedBlackTree , rhs: RedBlackTree ) -> RedBlackTree {
753
828
var t = rhs
829
+
754
830
for (k, _) in rhs {
755
831
t. removeValue ( for: k)
756
832
}
833
+
757
834
return t
758
835
}
759
836
0 commit comments