@@ -29,10 +29,10 @@ public struct StackList<Element> : Sequence, CustomReflectable {
29
29
private let context : BridgedPassContext
30
30
private var firstSlab = BridgedSlab ( data: nil )
31
31
private var lastSlab = BridgedSlab ( data: nil )
32
- private var endIndex : Int = slabCapacity
32
+ private var endIndex : Int = 0
33
33
34
34
private static var slabCapacity : Int {
35
- BridgedSlabCapacity / MemoryLayout< Element> . size
35
+ BridgedSlabCapacity / MemoryLayout< Element> . stride
36
36
}
37
37
38
38
private static func bind( _ slab: BridgedSlab ) -> UnsafeMutablePointer < Element > {
@@ -84,16 +84,24 @@ public struct StackList<Element> : Sequence, CustomReflectable {
84
84
public mutating func push( _ element: Element ) {
85
85
if endIndex >= StackList . slabCapacity {
86
86
lastSlab = PassContext_allocSlab ( context, lastSlab)
87
- if firstSlab. data == nil {
88
- firstSlab = lastSlab
89
- }
90
87
endIndex = 0
88
+ } else if firstSlab. data == nil {
89
+ assert ( endIndex == 0 )
90
+ firstSlab = PassContext_allocSlab ( context, lastSlab)
91
+ lastSlab = firstSlab
91
92
}
92
93
( StackList . bind ( lastSlab) + endIndex) . initialize ( to: element)
93
94
endIndex += 1
94
95
}
95
-
96
- public var isEmpty : Bool { return firstSlab. data == nil }
96
+
97
+ public mutating
98
+ func append< S: Sequence > ( contentsOf other: S ) where S. Element == Element {
99
+ for elem in other {
100
+ push ( elem)
101
+ }
102
+ }
103
+
104
+ public var isEmpty : Bool { return endIndex == 0 }
97
105
98
106
public mutating func pop( ) -> Element ? {
99
107
if isEmpty {
@@ -108,26 +116,18 @@ public struct StackList<Element> : Sequence, CustomReflectable {
108
116
_ = PassContext_freeSlab ( context, lastSlab)
109
117
firstSlab. data = nil
110
118
lastSlab. data = nil
119
+ endIndex = 0
111
120
} else {
112
121
lastSlab = PassContext_freeSlab ( context, lastSlab)
122
+ endIndex = StackList . slabCapacity
113
123
}
114
- endIndex = StackList . slabCapacity
115
124
}
116
125
117
126
return elem
118
127
}
119
128
120
129
public mutating func removeAll( ) {
121
- if isEmpty {
122
- return
123
- }
124
- while lastSlab. data != firstSlab. data {
125
- lastSlab = PassContext_freeSlab ( context, lastSlab)
126
- }
127
- _ = PassContext_freeSlab ( context, lastSlab)
128
- firstSlab. data = nil
129
- lastSlab. data = nil
130
- endIndex = StackList . slabCapacity
130
+ while pop ( ) != nil { }
131
131
}
132
132
133
133
public var customMirror : Mirror {
0 commit comments