@@ -325,7 +325,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
325325 the given key value will be updated.
326326 */
327327 mutating public func update( value: Value ? , for key: Key ) {
328- internalUpdateValue ( value, forKey : key, node: root)
328+ internalUpdateValue ( value, for : key, node: root)
329329 }
330330
331331 /**
@@ -348,11 +348,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
348348 */
349349 public subscript( index: Int ) -> ( key: Key , value: Value ? ) {
350350 get {
351- let x = internalSelect ( root, order: index + 1 )
351+ let x = internalSelect ( root, order: index + 1 )
352352 return ( x. key, x. value)
353353 }
354354 set ( element) {
355- internalUpdateValue ( element. value, forKey : element. key, node: root)
355+ internalUpdateValue ( element. value, for : element. key, node: root)
356356 }
357357 }
358358
@@ -368,7 +368,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
368368 return internalFindNodeForKey ( key) . value
369369 }
370370 set ( value) {
371- if sentinel == internalFindNodeForKey ( key) {
371+ if sentinel === internalFindNodeForKey ( key) {
372372 _ = internalInsert ( key, value: value)
373373 } else {
374374 update ( value: value, for: key)
@@ -383,7 +383,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
383383 */
384384 public func index( of key: Key ) -> Int {
385385 let x = internalFindNodeForKey ( key)
386- return sentinel == x ? - 1 : internalOrder ( x) - 1
386+ return sentinel === x ? - 1 : internalOrder ( x) - 1
387387 }
388388
389389 /**
@@ -407,7 +407,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
407407
408408 let z = RedBlackNode < Key , Value > ( parent: y, sentinel: sentinel, key: key, value: value)
409409
410- if y == sentinel {
410+ if y === sentinel {
411411 root = z
412412 } else if key < y. key as Key {
413413 y. left = z
@@ -428,7 +428,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
428428 mutating private func insertCleanUp( _ node: RedBlackNode < Key , Value > ) {
429429 var z = node
430430 while z. parent. isRed {
431- if z. parent == z. parent. parent. left {
431+ if z. parent === z. parent. parent. left {
432432 let y = z. parent. parent. right!
433433 // violation 1, parent child relationship re to isRed
434434 if y. isRed {
@@ -438,7 +438,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
438438 z = z. parent. parent
439439 } else {
440440 // case 2, parent is isRed, uncle is black
441- if z == z. parent. right {
441+ if z === z. parent. right {
442442 z = z. parent
443443 leftRotate ( z)
444444 }
@@ -458,7 +458,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
458458 z = z. parent. parent
459459 } else {
460460 // case 2, parent is isRed, uncle is black
461- if z == z. parent. left {
461+ if z === z. parent. left {
462462 z = z. parent
463463 rightRotate ( z)
464464 }
@@ -481,7 +481,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
481481 @discardableResult
482482 mutating private func internalRemoveValueForKey( _ key: Key ) -> RedBlackNode < Key , Value > {
483483 let z = internalFindNodeForKey ( key)
484- if z == sentinel {
484+ if z === sentinel {
485485 return sentinel
486486 }
487487
@@ -498,17 +498,17 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
498498 var y = z
499499 var isRed : Bool = y. isRed
500500
501- if z. left == sentinel {
501+ if z. left === sentinel {
502502 x = z. right
503503 transplant ( z, v: z. right)
504- } else if z. right == sentinel {
504+ } else if z. right === sentinel {
505505 x = z. left
506506 transplant ( z, v: z. left)
507507 } else {
508508 y = minimum ( z. right)
509509 isRed = y. isRed
510510 x = y. right
511- if y. parent == z {
511+ if y. parent === z {
512512 x. parent = y
513513 } else {
514514 transplant ( y, v: y. right)
@@ -542,7 +542,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
542542 mutating private func removeCleanUp( _ node: RedBlackNode < Key , Value > ) {
543543 var x = node
544544 while x !== root && !x. isRed {
545- if x == x. parent. left {
545+ if x === x. parent. left {
546546 var y = x. parent. right!
547547 if y. isRed {
548548 y. isRed = false
@@ -615,9 +615,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
615615 :description: Swaps two subTrees in the tree.
616616 */
617617 mutating private func transplant( _ u: RedBlackNode < Key , Value > , v: RedBlackNode < Key , Value > ) {
618- if u. parent == sentinel {
618+ if u. parent === sentinel {
619619 root = v
620- } else if u == u. parent. left {
620+ } else if u === u. parent. left {
621621 u. parent. left = v
622622 } else {
623623 u. parent. right = v
@@ -640,9 +640,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
640640
641641 y. parent = x. parent
642642
643- if sentinel == x. parent {
643+ if sentinel === x. parent {
644644 root = y
645- } else if x == x. parent. left {
645+ } else if x === x. parent. left {
646646 x. parent. left = y
647647 } else {
648648 x. parent. right = y
@@ -669,9 +669,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
669669
670670 x. parent = y. parent
671671
672- if sentinel == y. parent {
672+ if sentinel === y. parent {
673673 root = x
674- } else if y == y. parent. right {
674+ } else if y === y. parent. right {
675675 y. parent. right = x
676676 } else {
677677 y. parent. left = x
@@ -733,13 +733,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
733733 :name: internalUpdateValue
734734 :description: Traverses the Tree and updates all the values that match the key.
735735 */
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 > ) {
737737 if node !== sentinel {
738- if forKey == node. key {
738+ if key == node. key {
739739 node. value = value
740740 }
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)
743743 }
744744 }
745745
@@ -752,7 +752,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
752752 var x = node
753753 var r : Int = x. left. order + 1
754754 while root !== x {
755- if x. parent. right == x {
755+ if x. parent. right === x {
756756 r += x. parent. left. order + 1
757757 }
758758 x = x. parent
@@ -765,14 +765,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
765765 :description: Validates the order statistic being within range of 1...n.
766766 */
767767 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.] " )
769769 }
770770}
771771
772772public 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 {
774774 return false
775775 }
776+
776777 for i in 0 ..< lhs. count {
777778 if lhs [ i] . key != rhs [ i] . key {
778779 return false
@@ -786,8 +787,11 @@ public func !=<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedB
786787}
787788
788789public 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 {
791795 t. insert ( value: v, for: k)
792796 }
793797 return t
0 commit comments