Skip to content

Commit df6e54a

Browse files
committed
Added 'Code' segment
1 parent 09b74c4 commit df6e54a

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

Rootish Array Stack/README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ A resizable array holds references to blocks (arrays of fixed size). A block's c
1010

1111
Here you can see how insert/remove operations would behave (very similar to how a Swift array handles such operations).
1212

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:
1516
```
1617
sum from 1...n = n * (n + 1) / 2
1718
```
@@ -51,10 +52,10 @@ But we only want to calculate the amount of `x`s, not the amount of `o`s. Since
5152
```
5253
area of only x = n * (n + 1) / 2
5354
```
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.
5556

5657
## 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!
5859
Determining the inner block `index` turns out to be easy. If `index` is in some `block` then:
5960
```
6061
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
8182
![Rootish Array Stack Intro](images/RootishArrayStackExample2.png)
8283

8384
# The Code
85+
Lets start with instance variables and struct declaration:
86+
```swift
87+
import Darwin
88+
89+
public struct RootishArrayStack<T> {
90+
fileprivate var blocks = [Array<T?>]()
91+
fileprivate var internalCount = 0
92+
93+
public init() { }
8494

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:
86107
```swift
87108
var capacity: Int {
88109
return blocks.count * (blocks.count + 1) / 2
89110
}
90111
```
91-
Since Swift arrays check `count` in `O(1)` time, this capacity lookup is also `O(1)`.
92112

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+
fileprivate static func toBlock(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

Comments
 (0)