You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Fixed Size Array/README.markdown
+42-42
Original file line number
Diff line number
Diff line change
@@ -55,12 +55,12 @@ Fixed-size arrays are a good solution when:
55
55
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.)
56
56
2. It is not necessary to have a sorted version of the array, i.e. the order of the elements does not matter.
57
57
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.
59
59
60
60
The code for adding an element becomes:
61
61
62
62
```swift
63
-
funcappend(newElement) {
63
+
funcappend(_newElement: T) {
64
64
if count < maxSize {
65
65
array[count] = newElement
66
66
count +=1
@@ -75,7 +75,7 @@ Determining the number of elements in the array is just a matter of reading the
75
75
The code for removing an element is equally simple:
76
76
77
77
```swift
78
-
funcremoveAtIndex(index) {
78
+
funcremoveAt(index: Int) {
79
79
count -=1
80
80
array[index] = array[count]
81
81
}
@@ -95,45 +95,45 @@ Here is an implementation in Swift:
@@ -143,6 +143,6 @@ When creating the array, you specify the maximum size and a default value:
143
143
var a =FixedSizeArray(maxSize: 10, defaultValue: 0)
144
144
```
145
145
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.
147
147
148
148
*Written for Swift Algorithm Club by Matthijs Hollemans*
0 commit comments