Skip to content

Commit

Permalink
development: Algorithm ready for Swift 3 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Dahan committed Nov 18, 2016
1 parent ebc8a6d commit 4091d3f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
12 changes: 6 additions & 6 deletions Sources/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

public class Queue<Element>: CustomStringConvertible, Sequence {
public struct Queue<Element>: CustomStringConvertible, Sequence {
public typealias Iterator = AnyIterator<Element>

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public class Queue<Element>: CustomStringConvertible, Sequence {
:name: enqueue
:description: Insert a new element at the back of the Queue.
*/
public func enqueue(_ element: Element) {
mutating public func enqueue(_ element: Element) {
list.insert(atBack: element)
}

Expand All @@ -107,21 +107,21 @@ public class Queue<Element>: CustomStringConvertible, Sequence {
of the Queue.
- returns: Element?
*/
public func dequeue() -> Element? {
mutating public func dequeue() -> Element? {
return list.removeAtFront()
}

/**
:name: removeAll
:description: Remove all elements from the Queue.
*/
public func removeAll() {
mutating public func removeAll() {
list.removeAll()
}
}

public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Element> {
let q = Queue<Element>()
var q = Queue<Element>()
for x in lhs {
q.enqueue(x)
}
Expand All @@ -131,7 +131,7 @@ public func +<Element>(lhs: Queue<Element>, rhs: Queue<Element>) -> Queue<Elemen
return q
}

public func +=<Element>(lhs: Queue<Element>, rhs: Queue<Element>) {
public func +=<Element>(lhs: inout Queue<Element>, rhs: Queue<Element>) {
for x in rhs {
lhs.enqueue(x)
}
Expand Down
62 changes: 33 additions & 29 deletions Sources/RedBlackTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
the given key value will be updated.
*/
mutating public func update(value: Value?, for key: Key) {
internalUpdateValue(value, forKey: key, node: root)
internalUpdateValue(value, for: key, node: root)
}

/**
Expand All @@ -348,11 +348,11 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
*/
public subscript(index: Int) -> (key: Key, value: Value?) {
get {
let x = internalSelect(root, order: index + 1)
let x = internalSelect(root, order: index + 1)
return (x.key, x.value)
}
set(element) {
internalUpdateValue(element.value, forKey: element.key, node: root)
internalUpdateValue(element.value, for: element.key, node: root)
}
}

Expand All @@ -368,7 +368,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
return internalFindNodeForKey(key).value
}
set(value) {
if sentinel == internalFindNodeForKey(key) {
if sentinel === internalFindNodeForKey(key) {
_ = internalInsert(key, value: value)
} else {
update(value: value, for: key)
Expand All @@ -383,7 +383,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
*/
public func index(of key: Key) -> Int {
let x = internalFindNodeForKey(key)
return sentinel == x ? -1 : internalOrder(x) - 1
return sentinel === x ? -1 : internalOrder(x) - 1
}

/**
Expand All @@ -407,7 +407,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom

let z = RedBlackNode<Key, Value>(parent: y, sentinel: sentinel, key: key, value: value)

if y == sentinel {
if y === sentinel {
root = z
} else if key < y.key as Key {
y.left = z
Expand All @@ -428,7 +428,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
mutating private func insertCleanUp(_ node: RedBlackNode<Key, Value>) {
var z = node
while z.parent.isRed {
if z.parent == z.parent.parent.left {
if z.parent === z.parent.parent.left {
let y = z.parent.parent.right!
// violation 1, parent child relationship re to isRed
if y.isRed {
Expand All @@ -438,7 +438,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
z = z.parent.parent
} else {
// case 2, parent is isRed, uncle is black
if z == z.parent.right {
if z === z.parent.right {
z = z.parent
leftRotate(z)
}
Expand All @@ -458,7 +458,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
z = z.parent.parent
} else {
// case 2, parent is isRed, uncle is black
if z == z.parent.left {
if z === z.parent.left {
z = z.parent
rightRotate(z)
}
Expand All @@ -481,7 +481,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
@discardableResult
mutating private func internalRemoveValueForKey(_ key: Key) -> RedBlackNode<Key, Value> {
let z = internalFindNodeForKey(key)
if z == sentinel {
if z === sentinel {
return sentinel
}

Expand All @@ -498,17 +498,17 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
var y = z
var isRed: Bool = y.isRed

if z.left == sentinel {
if z.left === sentinel {
x = z.right
transplant(z, v: z.right)
} else if z.right == sentinel {
} else if z.right === sentinel {
x = z.left
transplant(z, v: z.left)
} else {
y = minimum(z.right)
isRed = y.isRed
x = y.right
if y.parent == z {
if y.parent === z {
x.parent = y
} else {
transplant(y, v: y.right)
Expand Down Expand Up @@ -542,7 +542,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
mutating private func removeCleanUp(_ node: RedBlackNode<Key, Value>) {
var x = node
while x !== root && !x.isRed {
if x == x.parent.left {
if x === x.parent.left {
var y = x.parent.right!
if y.isRed {
y.isRed = false
Expand Down Expand Up @@ -615,9 +615,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
:description: Swaps two subTrees in the tree.
*/
mutating private func transplant(_ u: RedBlackNode<Key, Value>, v: RedBlackNode<Key, Value>) {
if u.parent == sentinel {
if u.parent === sentinel {
root = v
} else if u == u.parent.left {
} else if u === u.parent.left {
u.parent.left = v
} else {
u.parent.right = v
Expand All @@ -640,9 +640,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom

y.parent = x.parent

if sentinel == x.parent {
if sentinel === x.parent {
root = y
} else if x == x.parent.left {
} else if x === x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
Expand All @@ -669,9 +669,9 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom

x.parent = y.parent

if sentinel == y.parent {
if sentinel === y.parent {
root = x
} else if y == y.parent.right {
} else if y === y.parent.right {
y.parent.right = x
} else {
y.parent.left = x
Expand Down Expand Up @@ -733,13 +733,13 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
:name: internalUpdateValue
:description: Traverses the Tree and updates all the values that match the key.
*/
private func internalUpdateValue(_ value: Value?, forKey: Key, node: RedBlackNode<Key, Value>) {
private func internalUpdateValue(_ value: Value?, for key: Key, node: RedBlackNode<Key, Value>) {
if node !== sentinel {
if forKey == node.key {
if key == node.key {
node.value = value
}
internalUpdateValue(value, forKey: forKey, node: node.left)
internalUpdateValue(value, forKey: forKey, node: node.right)
internalUpdateValue(value, for: key, node: node.left)
internalUpdateValue(value, for: key, node: node.right)
}
}

Expand All @@ -752,7 +752,7 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
var x = node
var r: Int = x.left.order + 1
while root !== x {
if x.parent.right == x {
if x.parent.right === x {
r += x.parent.left.order + 1
}
x = x.parent
Expand All @@ -765,14 +765,15 @@ public struct RedBlackTree<Key: Comparable, Value>: Probable, Collection, Custom
:description: Validates the order statistic being within range of 1...n.
*/
private func validateOrder(_ order: Int) {
assert(order >= startIndex || order < endIndex, "[Algorithm Error: Order out of bounds.]")
assert(order > startIndex || order <= endIndex, "[Algorithm Error: Order out of bounds.]")
}
}

public func ==<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> Bool {
if lhs.count != rhs.count {
guard lhs.count == rhs.count else {
return false
}

for i in 0..<lhs.count {
if lhs[i].key != rhs[i].key {
return false
Expand All @@ -786,8 +787,11 @@ public func !=<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedB
}

public func +<Key : Comparable, Value>(lhs: RedBlackTree<Key, Value>, rhs: RedBlackTree<Key, Value>) -> RedBlackTree<Key, Value> {
var t = lhs
for (k, v) in rhs {
var t = RedBlackTree<Key, Value>()
for (k, v) in lhs {
t.insert(value: v, for: k)
}
for (k, v) in rhs {
t.insert(value: v, for: k)
}
return t
Expand Down
5 changes: 4 additions & 1 deletion Sources/SortedMultiDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ public func !=<Key : Comparable, Value>(lhs: SortedMultiDictionary<Key, Value>,
}

public func +<Key : Comparable, Value>(lhs: SortedMultiDictionary<Key, Value>, rhs: SortedMultiDictionary<Key, Value>) -> SortedMultiDictionary<Key, Value> {
var t = lhs
var t = SortedMultiDictionary<Key, Value>()
for (k, v) in lhs {
t.insert(value: v, for: k)
}
for (k, v) in rhs {
t.insert(value: v, for: k)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

public class Stack<Element>: CustomStringConvertible, Sequence {
public struct Stack<Element>: CustomStringConvertible, Sequence {
public typealias Iterator = AnyIterator<Element>

/// Underlying data structure.
Expand Down Expand Up @@ -72,7 +72,7 @@ public class Stack<Element>: CustomStringConvertible, Sequence {
Insert a new element at the top of the Stack.
- Parameter _ element: An Element type.
*/
public func push(_ element: Element) {
mutating public func push(_ element: Element) {
list.insert(atFront: element)
}

Expand All @@ -81,18 +81,18 @@ public class Stack<Element>: CustomStringConvertible, Sequence {
the Stack.
- Returns: Element?
*/
public func pop() -> Element? {
mutating public func pop() -> Element? {
return list.removeAtFront()
}

/// Remove all elements from the Stack.
public func removeAll() {
mutating public func removeAll() {
list.removeAll()
}
}

public func +<Element>(lhs: Stack<Element>, rhs: Stack<Element>) -> Stack<Element> {
let s = Stack<Element>()
var s = Stack<Element>()
for x in lhs {
s.push(x)
}
Expand All @@ -102,7 +102,7 @@ public func +<Element>(lhs: Stack<Element>, rhs: Stack<Element>) -> Stack<Elemen
return s
}

public func +=<Element>(lhs: Stack<Element>, rhs: Stack<Element>) {
public func +=<Element>(lhs: inout Stack<Element>, rhs: Stack<Element>) {
for x in rhs {
lhs.push(x)
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/QueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class QueueTests: XCTestCase {
}

func testInt() {
let q: Queue<Int> = Queue<Int>()
var q = Queue<Int>()

q.enqueue(1)
q.enqueue(2)
Expand Down Expand Up @@ -78,17 +78,17 @@ class QueueTests: XCTestCase {
}

func testConcat() {
let q1: Queue<Int> = Queue<Int>()
var q1 = Queue<Int>()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)

let q2: Queue<Int> = Queue<Int>()
var q2 = Queue<Int>()
q2.enqueue(4)
q2.enqueue(5)
q2.enqueue(6)

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

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

q3.removeAll()
let q4: Queue<Int> = q1 + q2 + q3
var q4 = q1 + q2 + q3
for x in q4 {
XCTAssert(x == q4.dequeue(), "Concat incorrect.")
}
Expand Down
Loading

0 comments on commit 4091d3f

Please sign in to comment.