Skip to content

Commit 4091d3f

Browse files
author
Daniel Dahan
committed
development: Algorithm ready for Swift 3 release
1 parent ebc8a6d commit 4091d3f

7 files changed

+69
-61
lines changed

Sources/Queue.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
public class Queue<Element>: CustomStringConvertible, Sequence {
31+
public struct Queue<Element>: CustomStringConvertible, Sequence {
3232
public typealias Iterator = AnyIterator<Element>
3333

3434
/**
@@ -97,7 +97,7 @@ public class Queue<Element>: CustomStringConvertible, Sequence {
9797
:name: enqueue
9898
:description: Insert a new element at the back of the Queue.
9999
*/
100-
public func enqueue(_ element: Element) {
100+
mutating public func enqueue(_ element: Element) {
101101
list.insert(atBack: element)
102102
}
103103

@@ -107,21 +107,21 @@ public class Queue<Element>: CustomStringConvertible, Sequence {
107107
of the Queue.
108108
- returns: Element?
109109
*/
110-
public func dequeue() -> Element? {
110+
mutating public func dequeue() -> Element? {
111111
return list.removeAtFront()
112112
}
113113

114114
/**
115115
:name: removeAll
116116
:description: Remove all elements from the Queue.
117117
*/
118-
public func removeAll() {
118+
mutating public func removeAll() {
119119
list.removeAll()
120120
}
121121
}
122122

123123
public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Element> {
124-
let q = Queue<Element>()
124+
var q = Queue<Element>()
125125
for x in lhs {
126126
q.enqueue(x)
127127
}
@@ -131,7 +131,7 @@ public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Elemen
131131
return q
132132
}
133133

134-
public func +=<Element>(lhs: Queue<Element>, rhs: Queue<Element>) {
134+
public func +=<Element>(lhs: inout Queue<Element>, rhs: Queue<Element>) {
135135
for x in rhs {
136136
lhs.enqueue(x)
137137
}

Sources/RedBlackTree.swift

+33-29
Original file line numberDiff line numberDiff line change
@@ -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

772772
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 {
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

788789
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 {
791795
t.insert(value: v, for: k)
792796
}
793797
return t

Sources/SortedMultiDictionary.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ public func !=<Key : Comparable, Value>(lhs: SortedMultiDictionary<Key, Value>,
380380
}
381381

382382
public func +<Key : Comparable, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> SortedMultiDictionary<Key, Value> {
383-
var t = lhs
383+
var t = SortedMultiDictionary<Key, Value>()
384+
for (k, v) in lhs {
385+
t.insert(value: v, for: k)
386+
}
384387
for (k, v) in rhs {
385388
t.insert(value: v, for: k)
386389
}

Sources/Stack.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

31-
public class Stack<Element>: CustomStringConvertible, Sequence {
31+
public struct Stack<Element>: CustomStringConvertible, Sequence {
3232
public typealias Iterator = AnyIterator<Element>
3333

3434
/// Underlying data structure.
@@ -72,7 +72,7 @@ public class Stack<Element>: CustomStringConvertible, Sequence {
7272
Insert a new element at the top of the Stack.
7373
- Parameter _ element: An Element type.
7474
*/
75-
public func push(_ element: Element) {
75+
mutating public func push(_ element: Element) {
7676
list.insert(atFront: element)
7777
}
7878

@@ -81,18 +81,18 @@ public class Stack<Element>: CustomStringConvertible, Sequence {
8181
the Stack.
8282
- Returns: Element?
8383
*/
84-
public func pop() -> Element? {
84+
mutating public func pop() -> Element? {
8585
return list.removeAtFront()
8686
}
8787

8888
/// Remove all elements from the Stack.
89-
public func removeAll() {
89+
mutating public func removeAll() {
9090
list.removeAll()
9191
}
9292
}
9393

9494
public func +<Element>(lhs: Stack<Element>, rhs: Stack<Element>) -> Stack<Element> {
95-
let s = Stack<Element>()
95+
var s = Stack<Element>()
9696
for x in lhs {
9797
s.push(x)
9898
}
@@ -102,7 +102,7 @@ public func +<Element>(lhs: Stack<Element>, rhs: Stack<Element>) -> Stack<Elemen
102102
return s
103103
}
104104

105-
public func +=<Element>(lhs: Stack<Element>, rhs: Stack<Element>) {
105+
public func +=<Element>(lhs: inout Stack<Element>, rhs: Stack<Element>) {
106106
for x in rhs {
107107
lhs.push(x)
108108
}

Tests/QueueTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class QueueTests: XCTestCase {
4242
}
4343

4444
func testInt() {
45-
let q: Queue<Int> = Queue<Int>()
45+
var q = Queue<Int>()
4646

4747
q.enqueue(1)
4848
q.enqueue(2)
@@ -78,17 +78,17 @@ class QueueTests: XCTestCase {
7878
}
7979

8080
func testConcat() {
81-
let q1: Queue<Int> = Queue<Int>()
81+
var q1 = Queue<Int>()
8282
q1.enqueue(1)
8383
q1.enqueue(2)
8484
q1.enqueue(3)
8585

86-
let q2: Queue<Int> = Queue<Int>()
86+
var q2 = Queue<Int>()
8787
q2.enqueue(4)
8888
q2.enqueue(5)
8989
q2.enqueue(6)
9090

91-
let q3: Queue<Int> = q1 + q2
91+
var q3 = q1 + q2
9292

9393
for x in q1 {
9494
XCTAssert(x == q3.dequeue(), "Concat incorrect.")
@@ -99,7 +99,7 @@ class QueueTests: XCTestCase {
9999
}
100100

101101
q3.removeAll()
102-
let q4: Queue<Int> = q1 + q2 + q3
102+
var q4 = q1 + q2 + q3
103103
for x in q4 {
104104
XCTAssert(x == q4.dequeue(), "Concat incorrect.")
105105
}

0 commit comments

Comments
 (0)