Skip to content

Commit ee53a0d

Browse files
authored
Merge pull request kodecocodes#349 from taiheng/feature/swift3-kmeans
Migrate K-Means to Swift 3 Syntax
2 parents ebaaac2 + 0e81996 commit ee53a0d

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ script:
2525
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
2626
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
2727
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
28-
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
28+
- 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
3131
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests

K-Means/KMeans.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class KMeans<Label: Hashable> {
1111
self.numCenters = labels.count
1212
}
1313

14-
private func indexOfNearestCenter(x: Vector, centers: [Vector]) -> Int {
14+
private func indexOfNearestCenter(_ x: Vector, centers: [Vector]) -> Int {
1515
var nearestDist = DBL_MAX
1616
var minIndex = 0
1717

18-
for (idx, center) in centers.enumerate() {
18+
for (idx, center) in centers.enumerated() {
1919
let dist = x.distanceTo(center)
2020
if dist < nearestDist {
2121
minIndex = idx
@@ -25,16 +25,16 @@ class KMeans<Label: Hashable> {
2525
return minIndex
2626
}
2727

28-
func trainCenters(points: [Vector], convergeDistance: Double) {
29-
let zeroVector = Vector([Double](count: points[0].length, repeatedValue: 0))
28+
func trainCenters(_ points: [Vector], convergeDistance: Double) {
29+
let zeroVector = Vector([Double](repeating: 0, count: points[0].length))
3030

3131
// Randomly take k objects from the input data to make the initial centroids.
3232
var centers = reservoirSample(points, k: numCenters)
3333

3434
var centerMoveDist = 0.0
3535
repeat {
3636
// This array keeps track of which data points belong to which centroids.
37-
var classification: [[Vector]] = .init(count: numCenters, repeatedValue: [])
37+
var classification: [[Vector]] = .init(repeating: [], count: numCenters)
3838

3939
// For each data point, find the centroid that it is closest to.
4040
for p in points {
@@ -45,7 +45,7 @@ class KMeans<Label: Hashable> {
4545
// Take the average of all the data points that belong to each centroid.
4646
// This moves the centroid to a new position.
4747
let newCenters = classification.map { assignedPoints in
48-
assignedPoints.reduce(zeroVector, combine: +) / Double(assignedPoints.count)
48+
assignedPoints.reduce(zeroVector, +) / Double(assignedPoints.count)
4949
}
5050

5151
// Find out how far each centroid moved since the last iteration. If it's
@@ -61,22 +61,22 @@ class KMeans<Label: Hashable> {
6161
centroids = centers
6262
}
6363

64-
func fit(point: Vector) -> Label {
64+
func fit(_ point: Vector) -> Label {
6565
assert(!centroids.isEmpty, "Exception: KMeans tried to fit on a non trained model.")
6666

6767
let centroidIndex = indexOfNearestCenter(point, centers: centroids)
6868
return labels[centroidIndex]
6969
}
7070

71-
func fit(points: [Vector]) -> [Label] {
71+
func fit(_ points: [Vector]) -> [Label] {
7272
assert(!centroids.isEmpty, "Exception: KMeans tried to fit on a non trained model.")
7373

7474
return points.map(fit)
7575
}
7676
}
7777

7878
// Pick k random elements from samples
79-
func reservoirSample<T>(samples: [T], k: Int) -> [T] {
79+
func reservoirSample<T>(_ samples: [T], k: Int) -> [T] {
8080
var result = [T]()
8181

8282
// Fill the result array with first k elements

K-Means/Tests/KMeansTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import XCTest
1111

1212
class KMeansTests: XCTestCase {
13-
func genPoints(numPoints: Int, numDimensions: Int) -> [Vector] {
13+
func genPoints(_ numPoints: Int, numDimensions: Int) -> [Vector] {
1414
var points = [Vector]()
1515
for _ in 0..<numPoints {
1616
var data = [Double]()

K-Means/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@
8585
isa = PBXProject;
8686
attributes = {
8787
LastSwiftUpdateCheck = 0720;
88-
LastUpgradeCheck = 0730;
88+
LastUpgradeCheck = 0820;
8989
TargetAttributes = {
9090
B80894DF1C852D100018730E = {
9191
CreatedOnToolsVersion = 7.2.1;
92+
LastSwiftMigration = 0820;
9293
};
9394
};
9495
};
@@ -136,14 +137,49 @@
136137
B80894D91C852CDC0018730E /* Debug */ = {
137138
isa = XCBuildConfiguration;
138139
buildSettings = {
140+
CLANG_WARN_BOOL_CONVERSION = YES;
141+
CLANG_WARN_CONSTANT_CONVERSION = YES;
142+
CLANG_WARN_EMPTY_BODY = YES;
143+
CLANG_WARN_ENUM_CONVERSION = YES;
144+
CLANG_WARN_INFINITE_RECURSION = YES;
145+
CLANG_WARN_INT_CONVERSION = YES;
146+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
147+
CLANG_WARN_UNREACHABLE_CODE = YES;
148+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
149+
ENABLE_STRICT_OBJC_MSGSEND = YES;
139150
ENABLE_TESTABILITY = YES;
151+
GCC_NO_COMMON_BLOCKS = YES;
152+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
153+
GCC_WARN_ABOUT_RETURN_TYPE = YES;
154+
GCC_WARN_UNDECLARED_SELECTOR = YES;
155+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
156+
GCC_WARN_UNUSED_FUNCTION = YES;
157+
GCC_WARN_UNUSED_VARIABLE = YES;
140158
ONLY_ACTIVE_ARCH = YES;
141159
};
142160
name = Debug;
143161
};
144162
B80894DA1C852CDC0018730E /* Release */ = {
145163
isa = XCBuildConfiguration;
146164
buildSettings = {
165+
CLANG_WARN_BOOL_CONVERSION = YES;
166+
CLANG_WARN_CONSTANT_CONVERSION = YES;
167+
CLANG_WARN_EMPTY_BODY = YES;
168+
CLANG_WARN_ENUM_CONVERSION = YES;
169+
CLANG_WARN_INFINITE_RECURSION = YES;
170+
CLANG_WARN_INT_CONVERSION = YES;
171+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
172+
CLANG_WARN_UNREACHABLE_CODE = YES;
173+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
174+
ENABLE_STRICT_OBJC_MSGSEND = YES;
175+
GCC_NO_COMMON_BLOCKS = YES;
176+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
177+
GCC_WARN_ABOUT_RETURN_TYPE = YES;
178+
GCC_WARN_UNDECLARED_SELECTOR = YES;
179+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
180+
GCC_WARN_UNUSED_FUNCTION = YES;
181+
GCC_WARN_UNUSED_VARIABLE = YES;
182+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
147183
};
148184
name = Release;
149185
};
@@ -193,6 +229,7 @@
193229
PRODUCT_NAME = "$(TARGET_NAME)";
194230
SDKROOT = macosx;
195231
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
232+
SWIFT_VERSION = 3.0;
196233
};
197234
name = Debug;
198235
};
@@ -234,6 +271,7 @@
234271
PRODUCT_BUNDLE_IDENTIFIER = com.alvahouse322.Tests;
235272
PRODUCT_NAME = "$(TARGET_NAME)";
236273
SDKROOT = macosx;
274+
SWIFT_VERSION = 3.0;
237275
};
238276
name = Release;
239277
};

K-Means/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 = "0730"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

K-Means/Tests/Vector.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Vector: CustomStringConvertible, Equatable {
1313
return "Vector (\(data)"
1414
}
1515

16-
func distanceTo(other: Vector) -> Double {
16+
func distanceTo(_ other: Vector) -> Double {
1717
var result = 0.0
1818
for idx in 0..<length {
1919
result += pow(data[idx] - other.data[idx], 2.0)
@@ -39,7 +39,7 @@ func + (left: Vector, right: Vector) -> Vector {
3939
return Vector(results)
4040
}
4141

42-
func += (inout left: Vector, right: Vector) {
42+
func += (left: inout Vector, right: Vector) {
4343
left = left + right
4444
}
4545

@@ -51,18 +51,18 @@ func - (left: Vector, right: Vector) -> Vector {
5151
return Vector(results)
5252
}
5353

54-
func -= (inout left: Vector, right: Vector) {
54+
func -= (left: inout Vector, right: Vector) {
5555
left = left - right
5656
}
5757

5858
func / (left: Vector, right: Double) -> Vector {
59-
var results = [Double](count: left.length, repeatedValue: 0)
60-
for (idx, value) in left.data.enumerate() {
59+
var results = [Double](repeating: 0, count: left.length)
60+
for (idx, value) in left.data.enumerated() {
6161
results[idx] = value / right
6262
}
6363
return Vector(results)
6464
}
6565

66-
func /= (inout left: Vector, right: Double) {
66+
func /= (left: inout Vector, right: Double) {
6767
left = left / right
6868
}

0 commit comments

Comments
 (0)