Skip to content

Commit 0055795

Browse files
author
Peter Bødskov
committed
Merge remote-tracking branch 'upstream/master'
2 parents 914441e + 11566eb commit 0055795

File tree

15 files changed

+53
-48
lines changed

15 files changed

+53
-48
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ script:
2323
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
2424
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
2525
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
26-
# - xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
26+
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
2727
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
2828
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
2929
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
3030
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
31-
# - xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
31+
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3232
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
3333
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
3434
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests

Heap Sort/HeapSort.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extension Heap {
22
public mutating func sort() -> [T] {
3-
for i in (elements.count - 1).stride(through: 1, by: -1) {
3+
for i in stride(from: (elements.count - 1), through: 1, by: -1) {
44
swap(&elements[0], &elements[i])
5-
shiftDown(index: 0, heapSize: i)
5+
shiftDown(0, heapSize: i)
66
}
77
return elements
88
}
@@ -12,7 +12,7 @@ extension Heap {
1212
Sorts an array using a heap.
1313
Heapsort can be performed in-place, but it is not a stable sort.
1414
*/
15-
public func heapsort<T>(a: [T], _ sort: (T, T) -> Bool) -> [T] {
15+
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
1616
let reverseOrder = { i1, i2 in sort(i2, i1) }
1717
var h = Heap(array: a, sort: reverseOrder)
1818
return h.sort()

Heap Sort/README.markdown

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ Here's how you can implement heap sort in Swift:
4848

4949
```swift
5050
extension Heap {
51-
public mutating func sort() -> [T] {
52-
for i in (elements.count - 1).stride(through: 1, by: -1) {
53-
swap(&elements[0], &elements[i])
54-
shiftDown(index: 0, heapSize: i)
55-
}
51+
public mutating func sort() -> [T] {
52+
for i in stride(from: (elements.count - 1), through: 1, by: -1) {
53+
swap(&elements[0], &elements[i])
54+
shiftDown(0, heapSize: i)
55+
}
5656
return elements
57-
}
57+
}
5858
}
5959
```
6060

@@ -70,10 +70,10 @@ Because we need a max-heap to sort from low-to-high, you need to give `Heap` the
7070
We can write a handy helper function for that:
7171

7272
```swift
73-
public func heapsort<T>(a: [T], _ sort: (T, T) -> Bool) -> [T] {
74-
let reverseOrder = { i1, i2 in sort(i2, i1) }
75-
var h = Heap(array: a, sort: reverseOrder)
76-
return h.sort()
73+
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
74+
let reverseOrder = { i1, i2 in sort(i2, i1) }
75+
var h = Heap(array: a, sort: reverseOrder)
76+
return h.sort()
7777
}
7878
```
7979

Heap Sort/Tests/Tests.xcodeproj/project.pbxproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@
8686
isa = PBXProject;
8787
attributes = {
8888
LastSwiftUpdateCheck = 0720;
89-
LastUpgradeCheck = 0720;
89+
LastUpgradeCheck = 0820;
9090
ORGANIZATIONNAME = "Swift Algorithm Club";
9191
TargetAttributes = {
9292
7B2BBC7F1C779D720067B71D = {
9393
CreatedOnToolsVersion = 7.2;
94+
LastSwiftMigration = 0820;
9495
};
9596
};
9697
};
@@ -149,8 +150,10 @@
149150
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
150151
CLANG_WARN_EMPTY_BODY = YES;
151152
CLANG_WARN_ENUM_CONVERSION = YES;
153+
CLANG_WARN_INFINITE_RECURSION = YES;
152154
CLANG_WARN_INT_CONVERSION = YES;
153155
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
156+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
154157
CLANG_WARN_UNREACHABLE_CODE = YES;
155158
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
156159
CODE_SIGN_IDENTITY = "-";
@@ -193,8 +196,10 @@
193196
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
194197
CLANG_WARN_EMPTY_BODY = YES;
195198
CLANG_WARN_ENUM_CONVERSION = YES;
199+
CLANG_WARN_INFINITE_RECURSION = YES;
196200
CLANG_WARN_INT_CONVERSION = YES;
197201
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
202+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
198203
CLANG_WARN_UNREACHABLE_CODE = YES;
199204
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
200205
CODE_SIGN_IDENTITY = "-";
@@ -213,6 +218,7 @@
213218
MACOSX_DEPLOYMENT_TARGET = 10.11;
214219
MTL_ENABLE_DEBUG_INFO = NO;
215220
SDKROOT = macosx;
221+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
216222
};
217223
name = Release;
218224
};
@@ -224,6 +230,7 @@
224230
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
225231
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
226232
PRODUCT_NAME = "$(TARGET_NAME)";
233+
SWIFT_VERSION = 3.0;
227234
};
228235
name = Debug;
229236
};
@@ -235,6 +242,7 @@
235242
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
236243
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
237244
PRODUCT_NAME = "$(TARGET_NAME)";
245+
SWIFT_VERSION = 3.0;
238246
};
239247
name = Release;
240248
};

Heap Sort/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Heap/Heap.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct Heap<T> {
4646
fileprivate mutating func buildHeap(fromArray array: [T]) {
4747
elements = array
4848
for i in stride(from: (elements.count/2 - 1), through: 0, by: -1) {
49-
shiftDown(index: i, heapSize: elements.count)
49+
shiftDown(i, heapSize: elements.count)
5050
}
5151
}
5252

@@ -98,7 +98,7 @@ public struct Heap<T> {
9898
*/
9999
public mutating func insert(_ value: T) {
100100
elements.append(value)
101-
shiftUp(index: elements.count - 1)
101+
shiftUp(elements.count - 1)
102102
}
103103

104104
public mutating func insert<S: Sequence>(_ sequence: S) where S.Iterator.Element == T {
@@ -116,7 +116,7 @@ public struct Heap<T> {
116116

117117
assert(isOrderedBefore(value, elements[i]))
118118
elements[i] = value
119-
shiftUp(index: i)
119+
shiftUp(i)
120120
}
121121

122122
/**
@@ -142,14 +142,14 @@ public struct Heap<T> {
142142
* Removes an arbitrary node from the heap. Performance: O(log n). You need
143143
* to know the node's index, which may actually take O(n) steps to find.
144144
*/
145-
public mutating func removeAt(index: Int) -> T? {
145+
public mutating func removeAt(_ index: Int) -> T? {
146146
guard index < elements.count else { return nil }
147147

148148
let size = elements.count - 1
149149
if index != size {
150150
swap(&elements[index], &elements[size])
151-
shiftDown(index: index, heapSize: size)
152-
shiftUp(index: index)
151+
shiftDown(index, heapSize: size)
152+
shiftUp(index)
153153
}
154154
return elements.removeLast()
155155
}
@@ -158,7 +158,7 @@ public struct Heap<T> {
158158
* Takes a child node and looks at its parents; if a parent is not larger
159159
* (max-heap) or not smaller (min-heap) than the child, we exchange them.
160160
*/
161-
mutating func shiftUp(index: Int) {
161+
mutating func shiftUp(_ index: Int) {
162162
var childIndex = index
163163
let child = elements[childIndex]
164164
var parentIndex = self.parentIndex(ofIndex: childIndex)
@@ -173,14 +173,14 @@ public struct Heap<T> {
173173
}
174174

175175
mutating func shiftDown() {
176-
shiftDown(index: 0, heapSize: elements.count)
176+
shiftDown(0, heapSize: elements.count)
177177
}
178178

179179
/**
180180
* Looks at a parent node and makes sure it is still larger (max-heap) or
181181
* smaller (min-heap) than its childeren.
182182
*/
183-
mutating func shiftDown(index: Int, heapSize: Int) {
183+
mutating func shiftDown(_ index: Int, heapSize: Int) {
184184
var parentIndex = index
185185

186186
while true {

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.playground/Pages/Minimum spanning tree example.xcplaygroundpage/Contents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearchMinimumSpanningTree(graph: Graph, source: Node) -> Graph {
1+
func breadthFirstSearchMinimumSpanningTree(_ graph: Graph, source: Node) -> Graph {
22
let minimumSpanningTree = graph.duplicate()
33

44
var queue = Queue<Node>()

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.playground/Pages/Minimum spanning tree example.xcplaygroundpage/timeline.xctimeline

-6
This file was deleted.

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.playground/Sources/Graph.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ public class Graph: CustomStringConvertible, Equatable {
55
self.nodes = []
66
}
77

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

14-
public func addEdge(source: Node, neighbor: Node) {
14+
public func addEdge(_ source: Node, neighbor: Node) {
1515
let edge = Edge(neighbor: neighbor)
1616
source.neighbors.append(edge)
1717
}
@@ -27,15 +27,15 @@ 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

3434
public func duplicate() -> Graph {
3535
let duplicated = Graph()
3636

3737
for node in nodes {
38-
duplicated.addNode(node.label)
38+
_ = duplicated.addNode(node.label)
3939
}
4040

4141
for node in nodes {

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.playground/Sources/Node.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ 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

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.playground/Sources/Queue.swift

+1-1
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

Minimum Spanning Tree (Unweighted)/MinimumSpanningTree.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func breadthFirstSearchMinimumSpanningTree(graph: Graph, source: Node) -> Graph {
1+
func breadthFirstSearchMinimumSpanningTree(_ graph: Graph, source: Node) -> Graph {
22
let minimumSpanningTree = graph.duplicate()
33

44
var queue = Queue<Node>()

Minimum Spanning Tree (Unweighted)/Tests/Graph.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class Node: CustomStringConvertible, Equatable {
3838
return distance != nil
3939
}
4040

41-
public func remove(edge: Edge) {
42-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
41+
public func remove(_ edge: Edge) {
42+
neighbors.remove(at: neighbors.index { $0 === edge }!)
4343
}
4444
}
4545

@@ -56,13 +56,13 @@ public class Graph: CustomStringConvertible, Equatable {
5656
self.nodes = []
5757
}
5858

59-
public func addNode(label: String) -> Node {
59+
public func addNode(_ label: String) -> Node {
6060
let node = Node(label: label)
6161
nodes.append(node)
6262
return node
6363
}
6464

65-
public func addEdge(source: Node, neighbor: Node) {
65+
public func addEdge(_ source: Node, neighbor: Node) {
6666
let edge = Edge(neighbor: neighbor)
6767
source.neighbors.append(edge)
6868
}
@@ -78,15 +78,15 @@ public class Graph: CustomStringConvertible, Equatable {
7878
return description
7979
}
8080

81-
public func findNodeWithLabel(label: String) -> Node {
81+
public func findNodeWithLabel(_ label: String) -> Node {
8282
return nodes.filter { $0.label == label }.first!
8383
}
8484

8585
public func duplicate() -> Graph {
8686
let duplicated = Graph()
8787

8888
for node in nodes {
89-
duplicated.addNode(node.label)
89+
_ = duplicated.addNode(node.label)
9090
}
9191

9292
for node in nodes {

Minimum Spanning Tree (Unweighted)/Tests/Queue.swift

+1-1
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

Minimum Spanning Tree (Unweighted)/Tests/Tests.xcodeproj/project.pbxproj

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
TargetAttributes = {
9595
7B2BBC7F1C779D720067B71D = {
9696
CreatedOnToolsVersion = 7.2;
97+
LastSwiftMigration = 0820;
9798
};
9899
};
99100
};
@@ -230,6 +231,7 @@
230231
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
231232
PRODUCT_NAME = "$(TARGET_NAME)";
232233
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
234+
SWIFT_VERSION = 3.0;
233235
};
234236
name = Debug;
235237
};
@@ -242,6 +244,7 @@
242244
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
243245
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
244246
PRODUCT_NAME = "$(TARGET_NAME)";
247+
SWIFT_VERSION = 3.0;
245248
};
246249
name = Release;
247250
};

0 commit comments

Comments
 (0)