Skip to content

Commit 411cbd2

Browse files
author
mattthousand
committed
Convert to swift 3.0 syntax.
1 parent ecd560f commit 411cbd2

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//: Playground - noun: a place where people can play
2+
3+
extension Heap {
4+
public mutating func sort() -> [T] {
5+
for i in (elements.count - 1).stride(through: 1, by: -1) {
6+
swap(&elements[0], &elements[i])
7+
shiftDown(index: 0, heapSize: i)
8+
}
9+
return elements
10+
}
11+
}
12+
13+
/*
14+
Sorts an array using a heap.
15+
Heapsort can be performed in-place, but it is not a stable sort.
16+
*/
17+
public func heapsort<T>(a: [T], _ sort: (T, T) -> Bool) -> [T] {
18+
let reverseOrder = { i1, i2 in sort(i2, i1) }
19+
var h = Heap(array: a, sort: reverseOrder)
20+
return h.sort()
21+
}

Heap Sort/HeapSort.swift

Lines changed: 3 additions & 3 deletions
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/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 9 deletions
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 {

0 commit comments

Comments
 (0)