Skip to content

Commit

Permalink
removed c-style looping and operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Dahan committed Apr 1, 2016
1 parent d22803d commit 11f4c1f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Algorithm.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Algorithm'
s.version = '1.0.6'
s.version = '1.0.7'
s.license = 'BSD'
s.summary = 'A toolset for writing algorithms in Swift.'
s.homepage = 'http://cosmicmind.io'
Expand Down
4 changes: 2 additions & 2 deletions Sources/Algorithm+Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension Array : ProbableType {
for v in elements {
for x in self {
if v == x as! Element {
++c
c += 1
}
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ extension Array : ProbableType {
var c: Int = 0
for x in self {
if block(element: x) {
++c
c += 1
}
}
return Double(c) / Double(count)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Algorithm+Set.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension Set : ProbableType {
for v in elements {
for x in self {
if v == x as! Element {
++c
c += 1
}
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ extension Set : ProbableType {
var c: Int = 0
for x in self {
if block(element: x) {
++c
c += 1
}
}
return Double(c) / Double(count)
Expand Down
23 changes: 14 additions & 9 deletions Sources/DoublyLinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
var x: DoublyLinkedListNode<Element>? = head
while nil !== x {
output += "\(x)"
if ++c != count {
c += 1
if c != count {
output += ", "
}
x = x!.next
Expand Down Expand Up @@ -187,7 +188,7 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
//
public func generate() -> DoublyLinkedList.Generator {
cursorToFront()
return anyGenerator {
return AnyGenerator {
if !self.isCursorAtBack {
let element: Element? = self.cursor
self.next
Expand Down Expand Up @@ -222,7 +223,8 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
head!.previous = z
}
head = z
if 1 == ++count {
count += 1
if 1 == count {
current = head
} else if head === current {
current = head!.next
Expand All @@ -240,7 +242,8 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
return nil
}
let element: Element? = head!.element
if 0 == --count {
count -= 1
if 0 == count {
reset()
} else {
head = head!.next
Expand All @@ -263,7 +266,8 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
tail!.next = z
}
tail = z
if 1 == ++count {
count += 1
if 1 == count {
current = tail
} else if tail === current {
current = tail!.previous
Expand All @@ -281,7 +285,8 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
return nil
}
let element: Element? = tail!.element
if 0 == --count {
count -= 1
if 0 == count {
reset()
} else {
tail = tail!.previous
Expand Down Expand Up @@ -316,7 +321,7 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
let z: DoublyLinkedListNode<Element> = DoublyLinkedListNode<Element>(next: current, previous: current!.previous, element: element)
current!.previous?.next = z
current!.previous = z
++count
count += 1
}
}

Expand All @@ -331,7 +336,7 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
let z: DoublyLinkedListNode<Element> = DoublyLinkedListNode<Element>(next: current!.next, previous: current, element: element)
current!.next?.previous = z
current!.next = z
++count
count += 1
}
}

Expand All @@ -356,7 +361,7 @@ public class DoublyLinkedList<Element> : CustomStringConvertible, SequenceType {
} else {
current = current!.next
}
--count
count -= 1
return element
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.6</string>
<string>1.0.7</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
34 changes: 20 additions & 14 deletions Sources/RedBlackTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
//
public func generate() -> RedBlackTree.Generator {
var index = startIndex
return anyGenerator {
return AnyGenerator {
if index < self.endIndex {
return self[index++]
let i: Int = index
index += 1
return self[i]
}
return nil
}
Expand Down Expand Up @@ -219,7 +221,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
var c: Int = 0
for (k, v) in self {
if block(key: k, value: v) {
++c
c += 1
}
}
return Double(c) / Double(count)
Expand Down Expand Up @@ -398,7 +400,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp

while x !== sentinel {
y = x
++y.order
y.order += 1
x = key < x.key ? x.left : x.right
}

Expand All @@ -413,7 +415,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
}

insertCleanUp(z)
++count
count += 1
return z
}

Expand All @@ -422,7 +424,8 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
:description: The clean up procedure needed to maintain the RedBlackTree balance.
- returns: RedBlackNode<Key, Value>
*/
private func insertCleanUp(var z: RedBlackNode<Key, Value>) {
private func insertCleanUp(node: RedBlackNode<Key, Value>) {
var z: RedBlackNode<Key, Value> = node
while z.parent.isRed {
if z.parent === z.parent.parent.left {
let y: RedBlackNode<Key, Value> = z.parent.parent.right
Expand Down Expand Up @@ -483,10 +486,10 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
if z !== root {
var t: RedBlackNode<Key, Value> = z.parent
while t !== root {
--t.order
t.order -= 1
t = t.parent
}
--root.order
root.order -= 1
}


Expand All @@ -512,7 +515,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
y.right.parent = y
var t: RedBlackNode<Key, Value> = x.parent
while t !== y {
--t.order
t.order -= 1
t = t.parent
}
y.order = y.left.order + 1
Expand All @@ -526,7 +529,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
if !isRed {
removeCleanUp(x)
}
--count
count -= 1
return z
}

Expand All @@ -535,7 +538,8 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
:description: After a successful removal of a node, the RedBlackTree
is rebalanced by this method.
*/
private func removeCleanUp(var x: RedBlackNode<Key, Value>) {
private func removeCleanUp(node: RedBlackNode<Key, Value>) {
var x: RedBlackNode<Key, Value> = node
while x !== root && !x.isRed {
if x === x.parent.left {
var y: RedBlackNode<Key, Value> = x.parent.right
Expand Down Expand Up @@ -595,7 +599,8 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
:description: Finds the minimum keyed node.
- returns: RedBlackNode<Key, Value>
*/
private func minimum(var x: RedBlackNode<Key, Value>) -> RedBlackNode<Key, Value> {
private func minimum(node: RedBlackNode<Key, Value>) -> RedBlackNode<Key, Value> {
var x: RedBlackNode<Key, Value> = node
var y: RedBlackNode<Key, Value> = sentinel
while x !== sentinel {
y = x
Expand Down Expand Up @@ -716,7 +721,7 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
private func internalCount(key: Key, node: RedBlackNode<Key, Value>, inout count: Int) {
if sentinel !== node {
if key == node.key {
++count
count += 1
}
internalCount(key, node: node.left, count: &count)
internalCount(key, node: node.right, count: &count)
Expand All @@ -742,7 +747,8 @@ public class RedBlackTree<Key : Comparable, Value> : ProbableType, CollectionTyp
:description: Traverses the Tree for the internal order statistic of a key.
- returns: Int
*/
private func internalOrder(var x: RedBlackNode<Key, Value>) -> Int {
private func internalOrder(node: RedBlackNode<Key, Value>) -> Int {
var x: RedBlackNode<Key, Value> = node
var r: Int = x.left.order + 1
while root !== x {
if x.parent.right === x {
Expand Down
6 changes: 4 additions & 2 deletions Sources/SortedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ public class SortedDictionary<Key : Comparable, Value where Key : Hashable> : Pr
//
public func generate() -> SortedDictionary.Generator {
var index = startIndex
return anyGenerator {
return AnyGenerator {
if index < self.endIndex {
return self[index++]
let i: Int = index
index += 1
return self[i]
}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/SortedMultiDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ public class SortedMultiDictionary<Key : Comparable, Value where Key : Hashable>
//
public func generate() -> SortedMultiDictionary.Generator {
var index = startIndex
return anyGenerator {
return AnyGenerator {
if index < self.endIndex {
return self[index++]
let i: Int = index
index += 1
return self[i]
}
return nil
}
Expand Down
Loading

0 comments on commit 11f4c1f

Please sign in to comment.