1 module epsilon.soag.listacks; 2 3 import runtime; 4 5 const emptyStack = -1; 6 const firstStackElem = 0; 7 alias DataType = long; 8 alias StackList = DataType[]; 9 10 struct Stack 11 { 12 int Top; 13 StackList Elem; 14 } 15 16 void Expand(ref Stack S) nothrow pure @safe 17 { 18 StackList List1; 19 20 if (S.Elem.length < DIV(int.max, 2)) 21 { 22 List1 = new DataType[2 * S.Elem.length + 1]; 23 } 24 else 25 { 26 assert(0); 27 } 28 for (size_t i = firstStackElem; i <= S.Top; ++i) 29 { 30 List1[i] = S.Elem[i]; 31 } 32 S.Elem = List1; 33 } 34 35 void New(ref Stack S, int Len) nothrow pure @safe 36 { 37 S = Stack(); 38 S.Elem = new DataType[Len]; 39 S.Top = emptyStack; 40 } 41 42 void Reset(ref Stack S) @nogc nothrow pure @safe 43 { 44 S.Top = emptyStack; 45 } 46 47 void Push(ref Stack S, DataType Val) nothrow pure @safe 48 { 49 if (S.Top + 2 >= S.Elem.length) 50 Expand(S); 51 52 ++S.Top; 53 S.Elem[S.Top] = Val; 54 } 55 56 void Pop(ref Stack S) @nogc nothrow pure @safe 57 { 58 --S.Top; 59 } 60 61 DataType Top(ref Stack S) @nogc nothrow pure @safe 62 { 63 return S.Elem[S.Top]; 64 } 65 66 DataType TopPop(ref Stack S) @nogc nothrow pure @safe 67 { 68 DataType R; 69 R = S.Elem[S.Top]; 70 --S.Top; 71 return R; 72 } 73 74 bool IsEmpty(Stack S) @nogc nothrow pure @safe 75 { 76 return S.Top <= emptyStack; 77 }