Skip to content

Commit 30dd3a0

Browse files
author
Divyendu Singh
committed
move the text cases to swift 3 syntax
1 parent 2245c95 commit 30dd3a0

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

Bucket Sort/BucketSort.swift

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@
2121
//
2222

2323
import Foundation
24+
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
25+
// Consider refactoring the code to use the non-optional operators.
26+
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
27+
switch (lhs, rhs) {
28+
case let (l?, r?):
29+
return l < r
30+
case (nil, _?):
31+
return true
32+
default:
33+
return false
34+
}
35+
}
36+
37+
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
38+
// Consider refactoring the code to use the non-optional operators.
39+
fileprivate func >= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
40+
switch (lhs, rhs) {
41+
case let (l?, r?):
42+
return l >= r
43+
default:
44+
return !(lhs < rhs)
45+
}
46+
}
47+
2448

2549
//////////////////////////////////////
2650
// MARK: Main algorithm
@@ -39,7 +63,7 @@ import Foundation
3963
- Returns: A new array with sorted elements
4064
*/
4165

42-
public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sorter: Sorter, buckets: [Bucket<T>]) -> [T] {
66+
public func bucketSort<T: Sortable>(_ elements: [T], distributor: Distributor, sorter: Sorter, buckets: [Bucket<T>]) -> [T] {
4367
precondition(allPositiveNumbers(elements))
4468
precondition(enoughSpaceInBuckets(buckets, elements: elements))
4569

@@ -57,12 +81,12 @@ public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sor
5781
return results
5882
}
5983

60-
private func allPositiveNumbers<T: Sortable>(array: [T]) -> Bool {
84+
private func allPositiveNumbers<T: Sortable>(_ array: [T]) -> Bool {
6185
return array.filter { $0.toInt() >= 0 }.count > 0
6286
}
6387

64-
private func enoughSpaceInBuckets<T: Sortable>(buckets: [Bucket<T>], elements: [T]) -> Bool {
65-
let maximumValue = elements.maxElement()?.toInt()
88+
private func enoughSpaceInBuckets<T: Sortable>(_ buckets: [Bucket<T>], elements: [T]) -> Bool {
89+
let maximumValue = elements.max()?.toInt()
6690
let totalCapacity = buckets.count * (buckets.first?.capacity)!
6791

6892
return totalCapacity >= maximumValue
@@ -74,7 +98,7 @@ private func enoughSpaceInBuckets<T: Sortable>(buckets: [Bucket<T>], elements: [
7498

7599

76100
public protocol Distributor {
77-
func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>])
101+
func distribute<T: Sortable>(_ element: T, buckets: inout [Bucket<T>])
78102
}
79103

80104
/*
@@ -96,7 +120,7 @@ public struct RangeDistributor: Distributor {
96120

97121
public init() {}
98122

99-
public func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>]) {
123+
public func distribute<T: Sortable>(_ element: T, buckets: inout [Bucket<T>]) {
100124
let value = element.toInt()
101125
let bucketCapacity = buckets.first!.capacity
102126

@@ -121,14 +145,14 @@ public protocol Sortable: IntConvertible, Comparable {
121145
//////////////////////////////////////
122146

123147
public protocol Sorter {
124-
func sort<T: Sortable>(items: [T]) -> [T]
148+
func sort<T: Sortable>(_ items: [T]) -> [T]
125149
}
126150

127151
public struct InsertionSorter: Sorter {
128152

129153
public init() {}
130154

131-
public func sort<T: Sortable>(items: [T]) -> [T] {
155+
public func sort<T: Sortable>(_ items: [T]) -> [T] {
132156
var results = items
133157
for i in 0 ..< results.count {
134158
var j = i
@@ -158,13 +182,13 @@ public struct Bucket<T:Sortable> {
158182
elements = [T]()
159183
}
160184

161-
public mutating func add(item: T) {
185+
public mutating func add(_ item: T) {
162186
if elements.count < capacity {
163187
elements.append(item)
164188
}
165189
}
166190

167-
public func sort(algorithm: Sorter) -> [T] {
191+
public func sort(_ algorithm: Sorter) -> [T] {
168192
return algorithm.sort(elements)
169193
}
170194
}

Bucket Sort/Tests/Tests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestTests: XCTestCase {
2525

2626
largeArray = [Int]()
2727
for _ in 0..<total {
28-
largeArray!.append( random() % maximum )
28+
largeArray!.append( Int(arc4random_uniform( UInt32( maximum ) ) ) )
2929
}
3030

3131
sparsedArray = [Int]()
@@ -57,9 +57,9 @@ class TestTests: XCTestCase {
5757

5858
// MARK: Private functions
5959

60-
private func performBucketSort(elements: [Int], totalBuckets: Int) -> [Int] {
60+
fileprivate func performBucketSort(_ elements: [Int], totalBuckets: Int) -> [Int] {
6161

62-
let value = (elements.maxElement()?.toInt())! + 1
62+
let value = (elements.max()?.toInt())! + 1
6363
let capacityRequired = Int( ceil( Double(value) / Double(totalBuckets) ) )
6464

6565
var buckets = [Bucket<Int>]()
@@ -71,7 +71,7 @@ class TestTests: XCTestCase {
7171
return results
7272
}
7373

74-
func isSorted(array: [Int]) -> Bool {
74+
func isSorted(_ array: [Int]) -> Bool {
7575

7676
var index = 0
7777
var sorted = true

Bucket Sort/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
TargetAttributes = {
8989
7B2BBC7F1C779D720067B71D = {
9090
CreatedOnToolsVersion = 7.2;
91+
LastSwiftMigration = 0810;
9192
};
9293
};
9394
};
@@ -222,6 +223,7 @@
222223
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
223224
PRODUCT_NAME = "$(TARGET_NAME)";
224225
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
226+
SWIFT_VERSION = 3.0;
225227
};
226228
name = Debug;
227229
};
@@ -234,6 +236,7 @@
234236
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
235237
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
236238
PRODUCT_NAME = "$(TARGET_NAME)";
239+
SWIFT_VERSION = 3.0;
237240
};
238241
name = Release;
239242
};

0 commit comments

Comments
 (0)