21
21
//
22
22
23
23
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
+
24
48
25
49
//////////////////////////////////////
26
50
// MARK: Main algorithm
@@ -39,7 +63,7 @@ import Foundation
39
63
- Returns: A new array with sorted elements
40
64
*/
41
65
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 ] {
43
67
precondition ( allPositiveNumbers ( elements) )
44
68
precondition ( enoughSpaceInBuckets ( buckets, elements: elements) )
45
69
@@ -57,12 +81,12 @@ public func bucketSort<T: Sortable>(elements: [T], distributor: Distributor, sor
57
81
return results
58
82
}
59
83
60
- private func allPositiveNumbers< T: Sortable > ( array: [ T ] ) -> Bool {
84
+ private func allPositiveNumbers< T: Sortable > ( _ array: [ T ] ) -> Bool {
61
85
return array. filter { $0. toInt ( ) >= 0 } . count > 0
62
86
}
63
87
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 ( )
66
90
let totalCapacity = buckets. count * ( buckets. first? . capacity) !
67
91
68
92
return totalCapacity >= maximumValue
@@ -74,7 +98,7 @@ private func enoughSpaceInBuckets<T: Sortable>(buckets: [Bucket<T>], elements: [
74
98
75
99
76
100
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 > ] )
78
102
}
79
103
80
104
/*
@@ -96,7 +120,7 @@ public struct RangeDistributor: Distributor {
96
120
97
121
public init ( ) { }
98
122
99
- public func distribute< T: Sortable > ( element: T , inout buckets: [ Bucket < T > ] ) {
123
+ public func distribute< T: Sortable > ( _ element: T , buckets: inout [ Bucket < T > ] ) {
100
124
let value = element. toInt ( )
101
125
let bucketCapacity = buckets. first!. capacity
102
126
@@ -121,14 +145,14 @@ public protocol Sortable: IntConvertible, Comparable {
121
145
//////////////////////////////////////
122
146
123
147
public protocol Sorter {
124
- func sort< T: Sortable > ( items: [ T ] ) -> [ T ]
148
+ func sort< T: Sortable > ( _ items: [ T ] ) -> [ T ]
125
149
}
126
150
127
151
public struct InsertionSorter : Sorter {
128
152
129
153
public init ( ) { }
130
154
131
- public func sort< T: Sortable > ( items: [ T ] ) -> [ T ] {
155
+ public func sort< T: Sortable > ( _ items: [ T ] ) -> [ T ] {
132
156
var results = items
133
157
for i in 0 ..< results. count {
134
158
var j = i
@@ -158,13 +182,13 @@ public struct Bucket<T:Sortable> {
158
182
elements = [ T] ( )
159
183
}
160
184
161
- public mutating func add( item: T ) {
185
+ public mutating func add( _ item: T ) {
162
186
if elements. count < capacity {
163
187
elements. append ( item)
164
188
}
165
189
}
166
190
167
- public func sort( algorithm: Sorter ) -> [ T ] {
191
+ public func sort( _ algorithm: Sorter ) -> [ T ] {
168
192
return algorithm. sort ( elements)
169
193
}
170
194
}
0 commit comments