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
+34-7Lines changed: 34 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,9 @@ A resizable array holds references to blocks (arrays of fixed size). A block's c
10
10
11
11
Here you can see how insert/remove operations would behave (very similar to how a Swift array handles such operations).
12
12
13
-
## The Math
14
-
The data structure is based on Gauss's summation technique:
13
+
## Gauss's Summation Trick
14
+
<!-- TODO: Gaussian flavour text -->
15
+
This data structure is based on Gauss's summation technique:
15
16
```
16
17
sum from 1...n = n * (n + 1) / 2
17
18
```
@@ -51,10 +52,10 @@ But we only want to calculate the amount of `x`s, not the amount of `o`s. Since
51
52
```
52
53
area of only x = n * (n + 1) / 2
53
54
```
54
-
And voila! We have an interesting new way to arrange our data!
55
+
And voila! A super fast way to take a sum of all the blocks! This equation is useful for deriving fast `block` and `inner block index` equations.
55
56
56
57
## Get/Set with Speed
57
-
Next we want to find an efficient way to access a random index. For example which block does `rootishArrayStack[12]` point to? To answer this we will need MORE MATH!
58
+
Next we want to find an efficient and accurate way to access an element at a random index. For example which block does `rootishArrayStack[12]` point to? To answer this we will need more math!
58
59
Determining the inner block `index` turns out to be easy. If `index` is in some `block` then:
59
60
```
60
61
inner block index = index - block * (block + 1) / 2
@@ -81,13 +82,39 @@ Now we can figure out that `rootishArrayStack[12]` would point to the block at i
Lets start with instance variables and struct declaration:
86
+
```swift
87
+
importDarwin
88
+
89
+
publicstructRootishArrayStack<T> {
90
+
fileprivatevar blocks = [Array<T?>]()
91
+
fileprivatevar internalCount =0
92
+
93
+
publicinit() { }
84
94
85
-
To get the `capacity` of the structure we can use the equation we figured out above:
95
+
var count: Int {
96
+
return internalCount
97
+
}
98
+
99
+
100
+
```
101
+
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?`.
102
+
> The reason for the fixed size arrays taking type `T?` is so that references to elements aren't retained after they've been removed. Eg:if you remove the last element, the last index must be set to `nil` to prevent the last element being held in memory at an inaccessible index.
103
+
104
+
`internalCount` is an internal mutable counter that keeps track of the number of elements. `count` is a read only variables that gives the `internalCount` value. `Darwin` is imported here to provide simple math functions such as `ceil()` and `sqrt()`.
105
+
106
+
The `capacity` of the structure is simply the Gaussian summation trick:
86
107
```swift
87
108
var capacity:Int {
88
109
return blocks.count* (blocks.count+1) /2
89
110
}
90
111
```
91
-
Since Swift arrays check `count` in `O(1)` time, this capacity lookup is also `O(1)`.
92
112
93
-
To solve the problem of which block holds the block holds the element we are looking for we need
113
+
To determine which block an index map to:
114
+
```swift
115
+
fileprivatestaticfunctoBlock(index: Int) ->Int {
116
+
let block =Int(ceil((-3.0+sqrt(9.0+8.0*Double(index))) /2))
117
+
return block
118
+
}
119
+
```
120
+
This comes straight from the equations derived earlier. As mentioned `sqrt()`, and `ceil()` were imported from `Darwin`.
0 commit comments