11//
2- // main.swift
32// RootishArrayStack
43//
5- // Created by Benjamin Emdon on 2016-11-07.
4+ // Created by @BenEmdon on 2016-11-07.
65//
76
87import Darwin
98
109public struct RootishArrayStack < T> {
10+
11+ // MARK: - Instance variables
12+
1113 fileprivate var blocks = [ Array < T ? > ] ( )
1214 fileprivate var internalCount = 0
1315
16+ // MARK: - Init
17+
1418 public init ( ) { }
1519
20+ // MARK: - Calculated variables
21+
1622 var count : Int {
1723 return internalCount
1824 }
@@ -21,6 +27,8 @@ public struct RootishArrayStack<T> {
2127 return blocks. count * ( blocks. count + 1 ) / 2
2228 }
2329
30+ // MARK: - Equations
31+
2432 fileprivate func block( fromIndex index: Int ) -> Int {
2533 let block = Int ( ceil ( ( - 3.0 + sqrt( 9.0 + 8.0 * Double( index) ) ) / 2 ) )
2634 return block
@@ -30,6 +38,8 @@ public struct RootishArrayStack<T> {
3038 return index - block * ( block + 1 ) / 2
3139 }
3240
41+ // MARK: - Behavior
42+
3343 fileprivate mutating func growIfNeeded( ) {
3444 if capacity - blocks. count < count + 1 {
3545 let newArray = [ T? ] ( repeating: nil , count: blocks. count + 1 )
@@ -89,20 +99,10 @@ public struct RootishArrayStack<T> {
8999 shrinkIfNeeded ( )
90100 return element
91101 }
92-
93- public var memoryDescription : String {
94- var description = " { \n "
95- for block in blocks {
96- description += " \t [ "
97- for rawElement in block {
98- description += " \( rawElement) , "
99- }
100- description += " ] \n "
101- }
102- return description + " } "
103- }
104102}
105103
104+ // MARK: - Struct to string
105+
106106extension RootishArrayStack : CustomStringConvertible {
107107 public var description : String {
108108 var s = " [ "
@@ -114,4 +114,16 @@ extension RootishArrayStack: CustomStringConvertible {
114114 }
115115 return s + " ] "
116116 }
117+
118+ public var memoryDescription : String {
119+ var description = " { \n "
120+ for block in blocks {
121+ description += " \t [ "
122+ for rawElement in block {
123+ description += " \( rawElement) , "
124+ }
125+ description += " ] \n "
126+ }
127+ return description + " } "
128+ }
117129}
0 commit comments