Skip to content

Commit 94967d2

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents ab2949e + f895ba1 commit 94967d2

File tree

92 files changed

+1677
-1538
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1677
-1538
lines changed

Binary Tree/BinaryTree.playground/Contents.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ tree.count // 12
5353

5454

5555
extension BinaryTree {
56-
public func traverseInOrder(@noescape process: T -> Void) {
56+
public func traverseInOrder(process: (T) -> Void) {
5757
if case let .Node(left, value, right) = self {
58-
left.traverseInOrder(process)
58+
left.traverseInOrder(process: process)
5959
process(value)
60-
right.traverseInOrder(process)
60+
right.traverseInOrder(process: process)
6161
}
6262
}
6363

64-
public func traversePreOrder(@noescape process: T -> Void) {
64+
public func traversePreOrder(process: (T) -> Void) {
6565
if case let .Node(left, value, right) = self {
6666
process(value)
67-
left.traversePreOrder(process)
68-
right.traversePreOrder(process)
67+
left.traversePreOrder(process: process)
68+
right.traversePreOrder(process: process)
6969
}
7070
}
7171

72-
public func traversePostOrder(@noescape process: T -> Void) {
72+
public func traversePostOrder(process: (T) -> Void) {
7373
if case let .Node(left, value, right) = self {
74-
left.traversePostOrder(process)
75-
right.traversePostOrder(process)
74+
left.traversePostOrder(process: process)
75+
right.traversePostOrder(process: process)
7676
process(value)
7777
}
7878
}

Binary Tree/README.markdown

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,26 @@ Something you often need to do with trees is traverse them, i.e. look at all the
111111
Here is how you'd implement that:
112112

113113
```swift
114-
public func traverseInOrder(@noescape process: T -> Void) {
114+
public func traverseInOrder(process: (T) -> Void) {
115115
if case let .Node(left, value, right) = self {
116-
left.traverseInOrder(process)
116+
left.traverseInOrder(process: process)
117117
process(value)
118-
right.traverseInOrder(process)
118+
right.traverseInOrder(process: process)
119119
}
120120
}
121121

122-
public func traversePreOrder(@noescape process: T -> Void) {
122+
public func traversePreOrder(process: (T) -> Void) {
123123
if case let .Node(left, value, right) = self {
124124
process(value)
125-
left.traversePreOrder(process)
126-
right.traversePreOrder(process)
125+
left.traversePreOrder(process: process)
126+
right.traversePreOrder(process: process)
127127
}
128128
}
129129

130-
public func traversePostOrder(@noescape process: T -> Void) {
130+
public func traversePostOrder(process: (T) -> Void) {
131131
if case let .Node(left, value, right) = self {
132-
left.traversePostOrder(process)
133-
right.traversePostOrder(process)
132+
left.traversePostOrder(process: process)
133+
right.traversePostOrder(process: process)
134134
process(value)
135135
}
136136
}

Bounded Priority Queue/BoundedPriorityQueue.playground/Sources/BoundedPriorityQueue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BoundedPriorityQueue<T: Comparable> {
1212
private typealias Node = LinkedListNode<T>
1313

1414
private(set) public var count = 0
15-
private var head: Node?
15+
fileprivate var head: Node?
1616
private var tail: Node?
1717
private var maxElements: Int
1818

@@ -28,7 +28,7 @@ public class BoundedPriorityQueue<T: Comparable> {
2828
return head?.value
2929
}
3030

31-
public func enqueue(value: T) {
31+
public func enqueue(_ value: T) {
3232
if let node = insert(value, after: findInsertionPoint(value)) {
3333
// If the newly inserted node is the last one in the list, then update
3434
// the tail pointer.
@@ -44,7 +44,7 @@ public class BoundedPriorityQueue<T: Comparable> {
4444
}
4545
}
4646

47-
private func insert(value: T, after: Node?) -> Node? {
47+
private func insert(_ value: T, after: Node?) -> Node? {
4848
if let previous = after {
4949

5050
// If the queue is full and we have to insert at the end of the list,
@@ -78,11 +78,11 @@ public class BoundedPriorityQueue<T: Comparable> {
7878

7979
/* Find the node after which to insert the new value. If this returns nil,
8080
the new value should be inserted at the head of the list. */
81-
private func findInsertionPoint(value: T) -> Node? {
81+
private func findInsertionPoint(_ value: T) -> Node? {
8282
var node = head
8383
var prev: Node? = nil
8484

85-
while let current = node where value < current.value {
85+
while let current = node, value < current.value {
8686
prev = node
8787
node = current.next
8888
}

Bounded Priority Queue/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BoundedPriorityQueue<T: Comparable> {
3535
private typealias Node = LinkedListNode<T>
3636

3737
private(set) public var count = 0
38-
private var head: Node?
38+
fileprivate var head: Node?
3939
private var tail: Node?
4040
private var maxElements: Int
4141

@@ -55,7 +55,7 @@ public class BoundedPriorityQueue<T: Comparable> {
5555
The `BoundedPriorityQueue` class contains a doubly linked list of `LinkedListNode` objects. Nothing special here yet. The fun stuff happens in the `enqueue()` method:
5656

5757
```swift
58-
public func enqueue(value: T) {
58+
public func enqueue(_ value: T) {
5959
if let node = insert(value, after: findInsertionPoint(value)) {
6060
// If the newly inserted node is the last one in the list, then update
6161
// the tail pointer.
@@ -71,7 +71,7 @@ public func enqueue(value: T) {
7171
}
7272
}
7373

74-
private func insert(value: T, after: Node?) -> Node? {
74+
private func insert(_ value: T, after: Node?) -> Node? {
7575
if let previous = after {
7676

7777
// If the queue is full and we have to insert at the end of the list,
@@ -105,7 +105,7 @@ private func insert(value: T, after: Node?) -> Node? {
105105

106106
/* Find the node after which to insert the new value. If this returns nil,
107107
the new value should be inserted at the head of the list. */
108-
private func findInsertionPoint(value: T) -> Node? {
108+
private func findInsertionPoint(_ value: T) -> Node? {
109109
var node = head
110110
var prev: Node? = nil
111111

Breadth-First Search/BreadthFirstSearch.playground/Pages/Simple example.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var queue = Queue<Node>()
33
queue.enqueue(source)
44

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public class Edge: Equatable {
22
public var neighbor: Node
33

4-
public init(neighbor: Node) {
4+
public init(_ neighbor: Node) {
55
self.neighbor = neighbor
66
}
77
}
88

9-
public func == (lhs: Edge, rhs: Edge) -> Bool {
9+
public func == (_ lhs: Edge, rhs: Edge) -> Bool {
1010
return lhs.neighbor == rhs.neighbor
1111
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Graph.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ public class Graph: CustomStringConvertible, Equatable {
55
self.nodes = []
66
}
77

8-
public func addNode(label: String) -> Node {
9-
let node = Node(label: label)
8+
public func addNode(_ label: String) -> Node {
9+
let node = Node(label)
1010
nodes.append(node)
1111
return node
1212
}
1313

14-
public func addEdge(source: Node, neighbor: Node) {
15-
let edge = Edge(neighbor: neighbor)
14+
public func addEdge(_ source: Node, neighbor: Node) {
15+
let edge = Edge(neighbor)
1616
source.neighbors.append(edge)
1717
}
1818

@@ -27,7 +27,7 @@ public class Graph: CustomStringConvertible, Equatable {
2727
return description
2828
}
2929

30-
public func findNodeWithLabel(label: String) -> Node {
30+
public func findNodeWithLabel(_ label: String) -> Node {
3131
return nodes.filter { $0.label == label }.first!
3232
}
3333

@@ -50,6 +50,6 @@ public class Graph: CustomStringConvertible, Equatable {
5050
}
5151
}
5252

53-
public func == (lhs: Graph, rhs: Graph) -> Bool {
53+
public func == (_ lhs: Graph, rhs: Graph) -> Bool {
5454
return lhs.nodes == rhs.nodes
5555
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Node.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Node: CustomStringConvertible, Equatable {
55
public var distance: Int?
66
public var visited: Bool
77

8-
public init(label: String) {
8+
public init(_ label: String) {
99
self.label = label
1010
neighbors = []
1111
visited = false
@@ -22,11 +22,11 @@ public class Node: CustomStringConvertible, Equatable {
2222
return distance != nil
2323
}
2424

25-
public func remove(edge: Edge) {
26-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
25+
public func remove(_ edge: Edge) {
26+
neighbors.remove(at: neighbors.index { $0 === edge }!)
2727
}
2828
}
2929

30-
public func == (lhs: Node, rhs: Node) -> Bool {
30+
public func == (_ lhs: Node, rhs: Node) -> Bool {
3131
return lhs.label == rhs.label && lhs.neighbors == rhs.neighbors
3232
}

Breadth-First Search/BreadthFirstSearch.playground/Sources/Queue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct Queue<T> {
1313
return array.count
1414
}
1515

16-
public mutating func enqueue(element: T) {
16+
public mutating func enqueue(_ element: T) {
1717
array.append(element)
1818
}
1919

Breadth-First Search/BreadthFirstSearch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearch(graph: Graph, source: Node) -> [String] {
1+
func breadthFirstSearch(_ graph: Graph, source: Node) -> [String] {
22
var queue = Queue<Node>()
33
queue.enqueue(source)
44

0 commit comments

Comments
 (0)