Skip to content

Commit 06d3ef3

Browse files
committed
[Swift 4] Update Bucket Search
1 parent 5f64dc0 commit 06d3ef3

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

Bucket Sort/BucketSort.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,34 @@
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+
}
2447

2548
//////////////////////////////////////
2649
// MARK: Main algorithm
2750
//////////////////////////////////////
2851

29-
3052
/**
3153
Performs bucket sort algorithm on the given input elements.
3254
[Bucket Sort Algorithm Reference](https://en.wikipedia.org/wiki/Bucket_sort)
@@ -39,7 +61,7 @@ import Foundation
3961
- Returns: A new array with sorted elements
4062
*/
4163

42-
public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sorter: Sorter, buckets: [Bucket<T>]) -> [T] {
64+
public func bucketSort<T>(_ elements: [T], distributor: Distributor, sorter: Sorter, buckets: [Bucket<T>]) -> [T] {
4365
precondition(allPositiveNumbers(elements))
4466
precondition(enoughSpaceInBuckets(buckets, elements: elements))
4567

@@ -57,12 +79,12 @@ public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sor
5779
return results
5880
}
5981

60-
private func allPositiveNumbers<T: Sortable>(array: [T]) -> Bool {
82+
private func allPositiveNumbers<T: Sortable>(_ array: [T]) -> Bool {
6183
return array.filter { $0.toInt() >= 0 }.count > 0
6284
}
6385

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

6890
return totalCapacity >= maximumValue
@@ -72,9 +94,8 @@ private func enoughSpaceInBuckets<T: Sortable>(buckets: [Bucket<T>], elements: [
7294
// MARK: Distributor
7395
//////////////////////////////////////
7496

75-
7697
public protocol Distributor {
77-
func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>])
98+
func distribute<T>(_ element: T, buckets: inout [Bucket<T>])
7899
}
79100

80101
/*
@@ -96,7 +117,7 @@ public struct RangeDistributor: Distributor {
96117

97118
public init() {}
98119

99-
public func distribute<T: Sortable>(element: T, inout buckets: [Bucket<T>]) {
120+
public func distribute<T>(_ element: T, buckets: inout [Bucket<T>]) {
100121
let value = element.toInt()
101122
let bucketCapacity = buckets.first!.capacity
102123

@@ -121,14 +142,14 @@ public protocol Sortable: IntConvertible, Comparable {
121142
//////////////////////////////////////
122143

123144
public protocol Sorter {
124-
func sort<T: Sortable>(items: [T]) -> [T]
145+
func sort<T: Sortable>(_ items: [T]) -> [T]
125146
}
126147

127148
public struct InsertionSorter: Sorter {
128149

129150
public init() {}
130151

131-
public func sort<T: Sortable>(items: [T]) -> [T] {
152+
public func sort<T: Sortable>(_ items: [T]) -> [T] {
132153
var results = items
133154
for i in 0 ..< results.count {
134155
var j = i
@@ -149,7 +170,7 @@ public struct InsertionSorter: Sorter {
149170
// MARK: Bucket
150171
//////////////////////////////////////
151172

152-
public struct Bucket<T:Sortable> {
173+
public struct Bucket<T: Sortable> {
153174
var elements: [T]
154175
let capacity: Int
155176

@@ -158,13 +179,13 @@ public struct Bucket<T:Sortable> {
158179
elements = [T]()
159180
}
160181

161-
public mutating func add(item: T) {
182+
public mutating func add(_ item: T) {
162183
if elements.count < capacity {
163184
elements.append(item)
164185
}
165186
}
166187

167-
public func sort(algorithm: Sorter) -> [T] {
188+
public func sort(_ algorithm: Sorter) -> [T] {
168189
return algorithm.sort(elements)
169190
}
170191
}

Bucket Sort/Tests/Tests.swift

Lines changed: 4 additions & 5 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
@@ -86,7 +86,6 @@ class TestTests: XCTestCase {
8686
}
8787
}
8888

89-
9089
//////////////////////////////////////
9190
// MARK: Extensions
9291
//////////////////////////////////////

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@
8383
isa = PBXProject;
8484
attributes = {
8585
LastSwiftUpdateCheck = 0730;
86-
LastUpgradeCheck = 0720;
86+
LastUpgradeCheck = 0900;
8787
ORGANIZATIONNAME = "Swift Algorithm Club";
8888
TargetAttributes = {
8989
7B2BBC7F1C779D720067B71D = {
9090
CreatedOnToolsVersion = 7.2;
91+
LastSwiftMigration = 0900;
9192
};
9293
};
9394
};
@@ -140,13 +141,21 @@
140141
CLANG_CXX_LIBRARY = "libc++";
141142
CLANG_ENABLE_MODULES = YES;
142143
CLANG_ENABLE_OBJC_ARC = YES;
144+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
143145
CLANG_WARN_BOOL_CONVERSION = YES;
146+
CLANG_WARN_COMMA = YES;
144147
CLANG_WARN_CONSTANT_CONVERSION = YES;
145148
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
146149
CLANG_WARN_EMPTY_BODY = YES;
147150
CLANG_WARN_ENUM_CONVERSION = YES;
151+
CLANG_WARN_INFINITE_RECURSION = YES;
148152
CLANG_WARN_INT_CONVERSION = YES;
153+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
154+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
149155
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
156+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
157+
CLANG_WARN_STRICT_PROTOTYPES = YES;
158+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
150159
CLANG_WARN_UNREACHABLE_CODE = YES;
151160
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
152161
CODE_SIGN_IDENTITY = "-";
@@ -184,13 +193,21 @@
184193
CLANG_CXX_LIBRARY = "libc++";
185194
CLANG_ENABLE_MODULES = YES;
186195
CLANG_ENABLE_OBJC_ARC = YES;
196+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
187197
CLANG_WARN_BOOL_CONVERSION = YES;
198+
CLANG_WARN_COMMA = YES;
188199
CLANG_WARN_CONSTANT_CONVERSION = YES;
189200
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
190201
CLANG_WARN_EMPTY_BODY = YES;
191202
CLANG_WARN_ENUM_CONVERSION = YES;
203+
CLANG_WARN_INFINITE_RECURSION = YES;
192204
CLANG_WARN_INT_CONVERSION = YES;
205+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
206+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
193207
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
208+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
209+
CLANG_WARN_STRICT_PROTOTYPES = YES;
210+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
194211
CLANG_WARN_UNREACHABLE_CODE = YES;
195212
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
196213
CODE_SIGN_IDENTITY = "-";
@@ -209,6 +226,7 @@
209226
MACOSX_DEPLOYMENT_TARGET = 10.11;
210227
MTL_ENABLE_DEBUG_INFO = NO;
211228
SDKROOT = macosx;
229+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
212230
};
213231
name = Release;
214232
};
@@ -222,6 +240,8 @@
222240
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
223241
PRODUCT_NAME = "$(TARGET_NAME)";
224242
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
243+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
244+
SWIFT_VERSION = 4.0;
225245
};
226246
name = Debug;
227247
};
@@ -234,6 +254,8 @@
234254
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
235255
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
236256
PRODUCT_NAME = "$(TARGET_NAME)";
257+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
258+
SWIFT_VERSION = 4.0;
237259
};
238260
name = Release;
239261
};

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

Lines changed: 3 additions & 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 = "0900"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -26,6 +26,7 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
language = ""
2930
shouldUseLaunchSchemeArgsEnv = "YES">
3031
<Testables>
3132
<TestableReference
@@ -46,6 +47,7 @@
4647
buildConfiguration = "Debug"
4748
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4849
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
50+
language = ""
4951
launchStyle = "0"
5052
useCustomWorkingDirectory = "NO"
5153
ignoresPersistentStateOnLaunch = "NO"

0 commit comments

Comments
 (0)