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: Rootish Array Stack/README.md
+67-2Lines changed: 67 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,9 @@ public struct RootishArrayStack<T> {
96
96
return internalCount
97
97
}
98
98
99
+
...
100
+
101
+
}
99
102
100
103
```
101
104
The elements are of generic type `T`, so data of any kind can be stored in the list. `blocks` will be a resizable array to hold fixed sized arrays that take type `T?`.
@@ -110,7 +113,7 @@ var capacity: Int {
110
113
}
111
114
```
112
115
113
-
Next lets build what we need to get/set elements:
116
+
Next lets look at we would `get` and `set` elements:
114
117
```swift
115
118
fileprivatefunctoBlock(index: Int) ->Int {
116
119
let block =Int(ceil((-3.0+sqrt(9.0+8.0*Double(index))) /2))
@@ -130,4 +133,66 @@ public subscript(index: Int) -> T {
130
133
}
131
134
}
132
135
```
133
-
`toBlock` is really just wrapping the `block` equation derived earlier. `superscript` lets us have `get/set` access to the structure with the familiar `[index]` syntax.
136
+
`toBlock(index:)` is really just wrapping the `block` equation derived earlier to return the block that an index maps to. `superscript` lets us have `get` and `set` access to the structure with the familiar `[index:]` syntax. For both `get` and `set` in superscript we use the same logic:
137
+
1. determine the block that the index points to
138
+
2. determine the inner block index
139
+
3. `get`/`set` the value
140
+
141
+
Next lets look at how we would `growIfNeeded()` and `shrinkIfNeeded()` the structure.
142
+
```swift
143
+
fileprivatemutatingfuncgrowIfNeeded() {
144
+
if capacity - blocks.count< count +1 {
145
+
let newArray = [T?](repeating: nil, count: blocks.count+1)
If our data set grows or shrinks in size, we want our data structure to accommodate the change.
161
+
Just like a Swift array when a capacity threshold is met we will `grow` or `shrink` the size of our structure. For the Rootish Array Stack we want to `grow` if the second last block is full on an `insert` operation, and `shrink` if the two last blocks are empty.
To `insert(element:, atIndex:)` we move all elements after the `index` to the right by 1. After space has been made for the element we set the value using the `subscript` convenience. `append(element:)` is just a convenience method to add to the end. To `remove(atIndex:)` we move all the elements after the `index` to the left by 1. After the removed value is covered by it's proceeding value, we set the last value in the structure to `nil`. `makeNil(atIndex:)` uses the same logic as our `subscript` method but is used to set the root optional at a particular index to `nil` (because setting it's wrapped value to `nil` is something only the user of the data structure should do).
198
+
> Setting a optionals value to `nil` is different than setting it's wrapped value to `nil`. An optionals wrapped value is an embedded type within the optional reference. This means that a `nil` wrapped value is actually `.some(.none)` wheres setting the root reference to `nil` is `.none`. To better understand Swift optionals I recommend checking out @SebastianBoldt's article [Swift! Optionals?](https://medium.com/ios-os-x-development/swift-optionals-78dafaa53f3#.rvjobhuzs).
0 commit comments