1 //Written in the D programming language 2 /* 3 * Simple stack. 4 * 5 * Copyright (C) 2014 Jaypha 6 * 7 * Distributed under the Boost Software License, Version 1.0. 8 * (See http://www.boost.org/LICENSE_1_0.txt) 9 * 10 * Authors: Jason den Dulk 11 */ 12 13 module jaypha.container.stack; 14 15 import std.array; 16 17 struct Stack(E) 18 { 19 private: 20 21 E[] q; 22 23 public: 24 25 void put(E e) 26 { 27 q ~= e; 28 } 29 30 @property bool empty() { return q.empty; } 31 @property ref E front() { return q[$-1]; } 32 void popFront() { q = q[0..$-1]; } 33 34 void clear() { q.length = 0; } 35 } 36 37 unittest 38 { 39 Stack!long stack; 40 41 assert(stack.empty); 42 43 stack.put(5); 44 45 assert(!stack.empty); 46 assert(stack.front == 5); 47 48 stack.put(12); 49 50 assert(!stack.empty); 51 assert(stack.front == 12); 52 53 stack.popFront(); 54 55 assert(!stack.empty); 56 assert(stack.front == 5); 57 58 stack.put(3); 59 60 assert(!stack.empty); 61 assert(stack.front == 3); 62 63 stack.popFront(); 64 65 assert(!stack.empty); 66 assert(stack.front == 5); 67 68 stack.popFront(); 69 70 assert(stack.empty); 71 72 stack.put(3); 73 stack.put(12); 74 75 assert(!stack.empty); 76 assert(stack.front == 12); 77 78 stack.clear(); 79 assert(stack.empty); 80 }