|
1 | 1 | # Fixed-Size Arrays
|
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | When you define an array like so,
|
6 | 6 |
|
7 | 7 | int myArray[10];
|
8 |
| - |
| 8 | + |
9 | 9 | the compiler allocates one contiguous block of memory that can hold 40 bytes (assuming an `int` is 4 bytes):
|
10 | 10 |
|
11 | 11 | 
|
@@ -42,7 +42,7 @@ The expensive operations are inserting and deleting. When you insert an element
|
42 | 42 |
|
43 | 43 | 
|
44 | 44 |
|
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. |
46 | 46 |
|
47 | 47 | Deleting requires a copy the other way around:
|
48 | 48 |
|
@@ -95,38 +95,45 @@ Here is an implementation in Swift:
|
95 | 95 |
|
96 | 96 | ```swift
|
97 | 97 | 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 | + } |
130 | 137 | }
|
131 | 138 | ```
|
132 | 139 |
|
|
0 commit comments