@@ -325,7 +325,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
325
325
the given key value will be updated.
326
326
*/
327
327
mutating public func update( value: Value ? , for key: Key ) {
328
- internalUpdateValue ( value, forKey : key, node: root)
328
+ internalUpdateValue ( value, for : key, node: root)
329
329
}
330
330
331
331
/**
@@ -348,11 +348,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
348
348
*/
349
349
public subscript( index: Int ) -> ( key: Key , value: Value ? ) {
350
350
get {
351
- let x = internalSelect ( root, order: index + 1 )
351
+ let x = internalSelect ( root, order: index + 1 )
352
352
return ( x. key, x. value)
353
353
}
354
354
set ( element) {
355
- internalUpdateValue ( element. value, forKey : element. key, node: root)
355
+ internalUpdateValue ( element. value, for : element. key, node: root)
356
356
}
357
357
}
358
358
@@ -368,7 +368,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
368
368
return internalFindNodeForKey ( key) . value
369
369
}
370
370
set ( value) {
371
- if sentinel == internalFindNodeForKey ( key) {
371
+ if sentinel === internalFindNodeForKey ( key) {
372
372
_ = internalInsert ( key, value: value)
373
373
} else {
374
374
update ( value: value, for: key)
@@ -383,7 +383,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
383
383
*/
384
384
public func index( of key: Key ) -> Int {
385
385
let x = internalFindNodeForKey ( key)
386
- return sentinel == x ? - 1 : internalOrder ( x) - 1
386
+ return sentinel === x ? - 1 : internalOrder ( x) - 1
387
387
}
388
388
389
389
/**
@@ -407,7 +407,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
407
407
408
408
let z = RedBlackNode < Key , Value > ( parent: y, sentinel: sentinel, key: key, value: value)
409
409
410
- if y == sentinel {
410
+ if y === sentinel {
411
411
root = z
412
412
} else if key < y. key as Key {
413
413
y. left = z
@@ -428,7 +428,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
428
428
mutating private func insertCleanUp( _ node: RedBlackNode < Key , Value > ) {
429
429
var z = node
430
430
while z. parent. isRed {
431
- if z. parent == z. parent. parent. left {
431
+ if z. parent === z. parent. parent. left {
432
432
let y = z. parent. parent. right!
433
433
// violation 1, parent child relationship re to isRed
434
434
if y. isRed {
@@ -438,7 +438,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
438
438
z = z. parent. parent
439
439
} else {
440
440
// case 2, parent is isRed, uncle is black
441
- if z == z. parent. right {
441
+ if z === z. parent. right {
442
442
z = z. parent
443
443
leftRotate ( z)
444
444
}
@@ -458,7 +458,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
458
458
z = z. parent. parent
459
459
} else {
460
460
// case 2, parent is isRed, uncle is black
461
- if z == z. parent. left {
461
+ if z === z. parent. left {
462
462
z = z. parent
463
463
rightRotate ( z)
464
464
}
@@ -481,7 +481,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
481
481
@discardableResult
482
482
mutating private func internalRemoveValueForKey( _ key: Key ) -> RedBlackNode < Key , Value > {
483
483
let z = internalFindNodeForKey ( key)
484
- if z == sentinel {
484
+ if z === sentinel {
485
485
return sentinel
486
486
}
487
487
@@ -498,17 +498,17 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
498
498
var y = z
499
499
var isRed : Bool = y. isRed
500
500
501
- if z. left == sentinel {
501
+ if z. left === sentinel {
502
502
x = z. right
503
503
transplant ( z, v: z. right)
504
- } else if z. right == sentinel {
504
+ } else if z. right === sentinel {
505
505
x = z. left
506
506
transplant ( z, v: z. left)
507
507
} else {
508
508
y = minimum ( z. right)
509
509
isRed = y. isRed
510
510
x = y. right
511
- if y. parent == z {
511
+ if y. parent === z {
512
512
x. parent = y
513
513
} else {
514
514
transplant ( y, v: y. right)
@@ -542,7 +542,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
542
542
mutating private func removeCleanUp( _ node: RedBlackNode < Key , Value > ) {
543
543
var x = node
544
544
while x !== root && !x. isRed {
545
- if x == x. parent. left {
545
+ if x === x. parent. left {
546
546
var y = x. parent. right!
547
547
if y. isRed {
548
548
y. isRed = false
@@ -615,9 +615,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
615
615
:description: Swaps two subTrees in the tree.
616
616
*/
617
617
mutating private func transplant( _ u: RedBlackNode < Key , Value > , v: RedBlackNode < Key , Value > ) {
618
- if u. parent == sentinel {
618
+ if u. parent === sentinel {
619
619
root = v
620
- } else if u == u. parent. left {
620
+ } else if u === u. parent. left {
621
621
u. parent. left = v
622
622
} else {
623
623
u. parent. right = v
@@ -640,9 +640,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
640
640
641
641
y. parent = x. parent
642
642
643
- if sentinel == x. parent {
643
+ if sentinel === x. parent {
644
644
root = y
645
- } else if x == x. parent. left {
645
+ } else if x === x. parent. left {
646
646
x. parent. left = y
647
647
} else {
648
648
x. parent. right = y
@@ -669,9 +669,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
669
669
670
670
x. parent = y. parent
671
671
672
- if sentinel == y. parent {
672
+ if sentinel === y. parent {
673
673
root = x
674
- } else if y == y. parent. right {
674
+ } else if y === y. parent. right {
675
675
y. parent. right = x
676
676
} else {
677
677
y. parent. left = x
@@ -733,13 +733,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
733
733
:name: internalUpdateValue
734
734
:description: Traverses the Tree and updates all the values that match the key.
735
735
*/
736
- private func internalUpdateValue( _ value: Value ? , forKey : Key , node: RedBlackNode < Key , Value > ) {
736
+ private func internalUpdateValue( _ value: Value ? , for key : Key , node: RedBlackNode < Key , Value > ) {
737
737
if node !== sentinel {
738
- if forKey == node. key {
738
+ if key == node. key {
739
739
node. value = value
740
740
}
741
- internalUpdateValue ( value, forKey : forKey , node: node. left)
742
- internalUpdateValue ( value, forKey : forKey , node: node. right)
741
+ internalUpdateValue ( value, for : key , node: node. left)
742
+ internalUpdateValue ( value, for : key , node: node. right)
743
743
}
744
744
}
745
745
@@ -752,7 +752,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
752
752
var x = node
753
753
var r : Int = x. left. order + 1
754
754
while root !== x {
755
- if x. parent. right == x {
755
+ if x. parent. right === x {
756
756
r += x. parent. left. order + 1
757
757
}
758
758
x = x. parent
@@ -765,14 +765,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
765
765
:description: Validates the order statistic being within range of 1...n.
766
766
*/
767
767
private func validateOrder( _ order: Int ) {
768
- assert ( order >= startIndex || order < endIndex, " [Algorithm Error: Order out of bounds.] " )
768
+ assert ( order > startIndex || order <= endIndex, " [Algorithm Error: Order out of bounds.] " )
769
769
}
770
770
}
771
771
772
772
public func == < Key : Comparable , Value> ( lhs: RedBlackTree < Key , Value > , rhs: RedBlackTree < Key , Value > ) -> Bool {
773
- if lhs. count != rhs. count {
773
+ guard lhs. count == rhs. count else {
774
774
return false
775
775
}
776
+
776
777
for i in 0 ..< lhs. count {
777
778
if lhs [ i] . key != rhs [ i] . key {
778
779
return false
@@ -786,8 +787,11 @@ public func !=<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedB
786
787
}
787
788
788
789
public func + < Key : Comparable , Value> ( lhs: RedBlackTree < Key , Value > , rhs: RedBlackTree < Key , Value > ) -> RedBlackTree < Key , Value > {
789
- var t = lhs
790
- for (k, v) in rhs {
790
+ var t = RedBlackTree < Key , Value > ( )
791
+ for (k, v) in lhs {
792
+ t. insert ( value: v, for: k)
793
+ }
794
+ for (k, v) in rhs {
791
795
t. insert ( value: v, for: k)
792
796
}
793
797
return t
0 commit comments