Skip to content

Commit 4a211c3

Browse files
authored
Merge pull request #232 from JaapWijnen/update-ordered-array
Update Ordered Array to Swift 3
2 parents dca1e2c + 3011410 commit 4a211c3

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

Ordered Array/OrderedArray.playground/Contents.swift

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//: Playground - noun: a place where people can play
22

33
public struct OrderedArray<T: Comparable> {
4-
private var array = [T]()
4+
fileprivate var array = [T]()
55

66
public init(array: [T]) {
7-
self.array = array.sort()
7+
self.array = array.sorted()
88
}
99

1010
public var isEmpty: Bool {
@@ -20,16 +20,16 @@ public struct OrderedArray<T: Comparable> {
2020
}
2121

2222
public mutating func removeAtIndex(index: Int) -> T {
23-
return array.removeAtIndex(index)
23+
return array.remove(at: index)
2424
}
2525

2626
public mutating func removeAll() {
2727
array.removeAll()
2828
}
2929

30-
public mutating func insert(newElement: T) -> Int {
30+
public mutating func insert(_ newElement: T) -> Int {
3131
let i = findInsertionPoint(newElement)
32-
array.insert(newElement, atIndex: i)
32+
array.insert(newElement, at: i)
3333
return i
3434
}
3535

@@ -46,19 +46,21 @@ public struct OrderedArray<T: Comparable> {
4646
*/
4747

4848
// Fast version that uses a binary search.
49-
private func findInsertionPoint(newElement: T) -> Int {
50-
var range = 0..<array.count
51-
while range.startIndex < range.endIndex {
52-
let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
53-
if array[midIndex] == newElement {
54-
return midIndex
55-
} else if array[midIndex] < newElement {
56-
range.startIndex = midIndex + 1
57-
} else {
58-
range.endIndex = midIndex
59-
}
49+
private func findInsertionPoint(_ newElement: T) -> Int {
50+
var startIndex = 0
51+
var endIndex = array.count
52+
53+
while startIndex < endIndex {
54+
let midIndex = startIndex + (endIndex - startIndex) / 2
55+
if array[midIndex] == newElement {
56+
return midIndex
57+
} else if array[midIndex] < newElement {
58+
startIndex = midIndex + 1
59+
} else {
60+
endIndex = midIndex
61+
}
6062
}
61-
return range.startIndex
63+
return startIndex
6264
}
6365
}
6466

Ordered Array/README.markdown

+27-25
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ The implementation is quite basic. It's simply a wrapper around Swift's built-in
88

99
```swift
1010
public struct OrderedArray<T: Comparable> {
11-
private var array = [T]()
12-
11+
fileprivate var array = [T]()
12+
1313
public init(array: [T]) {
14-
self.array = array.sort()
14+
self.array = array.sorted()
1515
}
1616

1717
public var isEmpty: Bool {
1818
return array.isEmpty
1919
}
20-
20+
2121
public var count: Int {
2222
return array.count
2323
}
24-
24+
2525
public subscript(index: Int) -> T {
2626
return array[index]
2727
}
28-
28+
2929
public mutating func removeAtIndex(index: Int) -> T {
30-
return array.removeAtIndex(index)
30+
return array.remove(at: index)
3131
}
32-
32+
3333
public mutating func removeAll() {
3434
array.removeAll()
3535
}
@@ -47,13 +47,13 @@ As you can see, all these methods simply call the corresponding method on the in
4747
What remains is the `insert()` function. Here is an initial stab at it:
4848

4949
```swift
50-
public mutating func insert(newElement: T) -> Int {
50+
public mutating func insert(_ newElement: T) -> Int {
5151
let i = findInsertionPoint(newElement)
52-
array.insert(newElement, atIndex: i)
52+
array.insert(newElement, at: i)
5353
return i
5454
}
5555

56-
private func findInsertionPoint(newElement: T) -> Int {
56+
private func findInsertionPoint(_ newElement: T) -> Int {
5757
for i in 0..<array.count {
5858
if newElement <= array[i] {
5959
return i
@@ -63,7 +63,7 @@ What remains is the `insert()` function. Here is an initial stab at it:
6363
}
6464
```
6565

66-
The helper function `findInsertionPoint()` simply iterates through the entire array, looking for the right place to insert the new element.
66+
The helper function `findInsertionPoint()` simply iterates through the entire array, looking for the right place to insert the new element.
6767

6868
> **Note:** Quite conveniently, `array.insert(... atIndex: array.count)` adds the new object to the end of the array, so if no suitable insertion point was found we can simply return `array.count` as the index.
6969
@@ -81,26 +81,28 @@ a.insert(10) // inserted at index 8
8181
a // [-2, -1, 1, 3, 4, 5, 7, 9, 10]
8282
```
8383

84-
The array's contents will always be sorted from low to high, now matter what.
84+
The array's contents will always be sorted from low to high, now matter what.
8585

8686
Unfortunately, the current `findInsertionPoint()` function is a bit slow. In the worst case, it needs to scan through the entire array. We can speed this up by using a [binary search](../Binary Search) to find the insertion point.
8787

8888
Here is the new version:
8989

9090
```swift
91-
private func findInsertionPoint(newElement: T) -> Int {
92-
var range = 0..<array.count
93-
while range.startIndex < range.endIndex {
94-
let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
95-
if array[midIndex] == newElement {
96-
return midIndex
97-
} else if array[midIndex] < newElement {
98-
range.startIndex = midIndex + 1
99-
} else {
100-
range.endIndex = midIndex
101-
}
91+
private func findInsertionPoint(_ newElement: T) -> Int {
92+
var startIndex = 0
93+
var endIndex = array.count
94+
95+
while startIndex < endIndex {
96+
let midIndex = startIndex + (endIndex - startIndex) / 2
97+
if array[midIndex] == newElement {
98+
return midIndex
99+
} else if array[midIndex] < newElement {
100+
startIndex = midIndex + 1
101+
} else {
102+
endIndex = midIndex
103+
}
102104
}
103-
return range.startIndex
105+
return startIndex
104106
}
105107
```
106108

0 commit comments

Comments
 (0)