Skip to content

Commit 2b61c06

Browse files
authored
Merge pull request kodecocodes#271 from JaapWijnen/migrate-fixedsizearray-swift3
migrate fixedsizearray to swift3
2 parents 5f64dc0 + b15b383 commit 2b61c06

File tree

6 files changed

+125
-81
lines changed

6 files changed

+125
-81
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//: Playground - noun: a place where people can play
2+
3+
/*
4+
An unordered array with a maximum size.
5+
6+
Performance is always O(1).
7+
*/
8+
struct FixedSizeArray<T> {
9+
private var maxSize: Int
10+
private var defaultValue: T
11+
private var array: [T]
12+
private (set) var count = 0
13+
14+
init(maxSize: Int, defaultValue: T) {
15+
self.maxSize = maxSize
16+
self.defaultValue = defaultValue
17+
self.array = [T](repeating: defaultValue, count: maxSize)
18+
}
19+
20+
subscript(index: Int) -> T {
21+
assert(index >= 0)
22+
assert(index < count)
23+
return array[index]
24+
}
25+
26+
mutating func append(newElement: T) {
27+
assert(count < maxSize)
28+
array[count] = newElement
29+
count += 1
30+
}
31+
32+
mutating func removeAtIndex(index: Int) -> T {
33+
assert(index >= 0)
34+
assert(index < count)
35+
count -= 1
36+
let result = array[index]
37+
array[index] = array[count]
38+
array[count] = defaultValue
39+
return result
40+
}
41+
42+
mutating func removeAll() {
43+
for i in 0..<count {
44+
array[i] = defaultValue
45+
}
46+
count = 0
47+
}
48+
}
49+
50+
var array = FixedSizeArray(maxSize: 5, defaultValue: 0)
51+
array.append(newElement: 4)
52+
array.append(newElement: 2)
53+
array[1]
54+
array.removeAtIndex(index: 0)
55+
array.removeAll()
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Fixed Size Array/FixedSizeArray.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1228&amp;EndingColumnNumber=6&amp;EndingLineNumber=52&amp;StartingColumnNumber=1&amp;StartingLineNumber=52&amp;Timestamp=499271741.074357"
7+
selectedRepresentationIndex = "0"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
<LoggerValueHistoryTimelineItem
11+
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1295&amp;EndingColumnNumber=6&amp;EndingLineNumber=54&amp;StartingColumnNumber=1&amp;StartingLineNumber=54&amp;Timestamp=499271760.916283"
12+
selectedRepresentationIndex = "0"
13+
shouldTrackSuperviewWidth = "NO">
14+
</LoggerValueHistoryTimelineItem>
15+
</TimelineItems>
16+
</Timeline>

Fixed Size Array/FixedSizeArray.swift

-46
This file was deleted.

Fixed Size Array/README.markdown

+42-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Fixed-Size Arrays
22

3-
Early programming languages didn't have very fancy arrays. You'd create the array with a specific size and from that moment on it would never grow or shrink. Even the standard arrays in C and Objective-C are still of this type.
3+
Early programming languages didn't have very fancy arrays. You'd create the array with a specific size and from that moment on it would never grow or shrink. Even the standard arrays in C and Objective-C are still of this type.
44

55
When you define an array like so,
66

77
int myArray[10];
8-
8+
99
the compiler allocates one contiguous block of memory that can hold 40 bytes (assuming an `int` is 4 bytes):
1010

1111
![An array with room for 10 elements](Images/array.png)
@@ -42,7 +42,7 @@ The expensive operations are inserting and deleting. When you insert an element
4242

4343
![Insert requires a memory copy](Images/insert.png)
4444

45-
If your code was using any indexes into the array beyond the insertion point, these indexes are now referring to the wrong objects.
45+
If your code was using any indexes into the array beyond the insertion point, these indexes are now referring to the wrong objects.
4646

4747
Deleting requires a copy the other way around:
4848

@@ -95,38 +95,45 @@ Here is an implementation in Swift:
9595

9696
```swift
9797
struct FixedSizeArray<T> {
98-
private var maxSize: Int
99-
private var defaultValue: T
100-
private var array: [T]
101-
private (set) var count = 0
102-
103-
init(maxSize: Int, defaultValue: T) {
104-
self.maxSize = maxSize
105-
self.defaultValue = defaultValue
106-
self.array = [T](count: maxSize, repeatedValue: defaultValue)
107-
}
108-
109-
subscript(index: Int) -> T {
110-
assert(index >= 0)
111-
assert(index < count)
112-
return array[index]
113-
}
114-
115-
mutating func append(newElement: T) {
116-
assert(count < maxSize)
117-
array[count] = newElement
118-
count += 1
119-
}
120-
121-
mutating func removeAtIndex(index: Int) -> T {
122-
assert(index >= 0)
123-
assert(index < count)
124-
count -= 1
125-
let result = array[index]
126-
array[index] = array[count]
127-
array[count] = defaultValue
128-
return result
129-
}
98+
private var maxSize: Int
99+
private var defaultValue: T
100+
private var array: [T]
101+
private (set) var count = 0
102+
103+
init(maxSize: Int, defaultValue: T) {
104+
self.maxSize = maxSize
105+
self.defaultValue = defaultValue
106+
self.array = [T](repeating: defaultValue, count: maxSize)
107+
}
108+
109+
subscript(index: Int) -> T {
110+
assert(index >= 0)
111+
assert(index < count)
112+
return array[index]
113+
}
114+
115+
mutating func append(newElement: T) {
116+
assert(count < maxSize)
117+
array[count] = newElement
118+
count += 1
119+
}
120+
121+
mutating func removeAtIndex(index: Int) -> T {
122+
assert(index >= 0)
123+
assert(index < count)
124+
count -= 1
125+
let result = array[index]
126+
array[index] = array[count]
127+
array[count] = defaultValue
128+
return result
129+
}
130+
131+
mutating func removeAll() {
132+
for i in 0..<count {
133+
array[i] = defaultValue
134+
}
135+
count = 0
136+
}
130137
}
131138
```
132139

0 commit comments

Comments
 (0)