Skip to content

Commit b2bc31b

Browse files
Kelvin LauKelvin Lau
Kelvin Lau
authored and
Kelvin Lau
committed
Changed tabs from 4 spaces to 2 spaces. Updated README file to reflect the updated syntax.
1 parent 3fe357f commit b2bc31b

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

Fixed Size Array/FixedSizeArray.playground/Contents.swift

+38-38
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@
66
Performance is always O(1).
77
*/
88
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 removeAt(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
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 removeAt(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
4745
}
46+
count = 0
47+
}
4848
}
4949

5050
var array = FixedSizeArray(maxSize: 5, defaultValue: 0)

Fixed Size Array/FixedSizeArray.playground/timeline.xctimeline

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1213&amp;EndingColumnNumber=6&amp;EndingLineNumber=52&amp;StartingColumnNumber=1&amp;StartingLineNumber=52&amp;Timestamp=499691083.961245"
6+
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1093&amp;EndingColumnNumber=6&amp;EndingLineNumber=52&amp;StartingColumnNumber=1&amp;StartingLineNumber=52&amp;Timestamp=499691409.537369"
77
selectedRepresentationIndex = "0"
88
shouldTrackSuperviewWidth = "NO">
99
</LoggerValueHistoryTimelineItem>
1010
<LoggerValueHistoryTimelineItem
11-
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1263&amp;EndingColumnNumber=6&amp;EndingLineNumber=54&amp;StartingColumnNumber=1&amp;StartingLineNumber=54&amp;Timestamp=499691083.961359"
11+
documentLocation = "#CharacterRangeLen=5&amp;CharacterRangeLoc=1143&amp;EndingColumnNumber=6&amp;EndingLineNumber=54&amp;StartingColumnNumber=1&amp;StartingLineNumber=54&amp;Timestamp=499691409.537492"
1212
selectedRepresentationIndex = "0"
1313
shouldTrackSuperviewWidth = "NO">
1414
</LoggerValueHistoryTimelineItem>

Fixed Size Array/README.markdown

+42-42
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ Fixed-size arrays are a good solution when:
5555
1. You know beforehand the maximum number of elements you'll need. In a game this could be the number of sprites that can be active at a time. It's not unreasonable to put a limit on this. (For games it's a good idea to allocate all the objects you need in advance anyway.)
5656
2. It is not necessary to have a sorted version of the array, i.e. the order of the elements does not matter.
5757

58-
If the array does not need to be sorted, then an `insertAtIndex()` operation is not needed. You can simply append any new elements to the end, until the array is full.
58+
If the array does not need to be sorted, then an `insertAt(index)` operation is not needed. You can simply append any new elements to the end, until the array is full.
5959

6060
The code for adding an element becomes:
6161

6262
```swift
63-
func append(newElement) {
63+
func append(_ newElement: T) {
6464
if count < maxSize {
6565
array[count] = newElement
6666
count += 1
@@ -75,7 +75,7 @@ Determining the number of elements in the array is just a matter of reading the
7575
The code for removing an element is equally simple:
7676

7777
```swift
78-
func removeAtIndex(index) {
78+
func removeAt(index: Int) {
7979
count -= 1
8080
array[index] = array[count]
8181
}
@@ -95,45 +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](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
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 removeAt(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
136134
}
135+
count = 0
136+
}
137137
}
138138
```
139139

@@ -143,6 +143,6 @@ When creating the array, you specify the maximum size and a default value:
143143
var a = FixedSizeArray(maxSize: 10, defaultValue: 0)
144144
```
145145

146-
Note that `removeAtIndex()` overwrites the last element with this `defaultValue` to clean up the "junk" object that gets left behind. Normally it wouldn't matter to leave that duplicate object in the array, but if it's a class or a struct it may have strong references to other objects and it's good boyscout practice to zero those out.
146+
Note that `removeAt(index: Int)` overwrites the last element with this `defaultValue` to clean up the "junk" object that gets left behind. Normally it wouldn't matter to leave that duplicate object in the array, but if it's a class or a struct it may have strong references to other objects and it's good boyscout practice to zero those out.
147147

148148
*Written for Swift Algorithm Club by Matthijs Hollemans*

0 commit comments

Comments
 (0)