1 /* 2 Copyright (c) 2011-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Stack (based on Array) 31 * 32 * Copyright: Timur Gafarov 2011-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov, Andrey Penechko, Roman Chistokhodov 35 */ 36 module dlib.container.stack; 37 38 import dlib.container.array; 39 40 /** 41 * Stack implementation based on Array. 42 */ 43 struct Stack(T) 44 { 45 private Array!T array; 46 47 public: 48 /** 49 * Push element to stack. 50 */ 51 void push(T v) 52 { 53 array.insertBack(v); 54 } 55 56 /** 57 * Pop top element out. 58 * Returns: Removed element. 59 * Throws: Exception on underflow. 60 */ 61 T pop() 62 { 63 if (empty) 64 throw new Exception("Stack!(T): underflow"); 65 66 T res = array[$-1]; 67 array.removeBack(1); 68 return res; 69 } 70 71 /** 72 * Non-throwing version of pop. 73 * Returns: true on success, false on failure. 74 * Element is stored in value. 75 */ 76 bool pop(ref T value) 77 { 78 if (empty) 79 return false; 80 81 value = array[$-1]; 82 array.removeBack(1); 83 return true; 84 } 85 86 /** 87 * Top stack element. 88 * Note: Stack must be non-empty. 89 */ 90 T top() 91 { 92 return array[$-1]; 93 } 94 95 /** 96 * Pointer to top stack element. 97 * Note: Stack must be non-empty. 98 */ 99 T* topPtr() 100 { 101 return &array.data[$-1]; 102 } 103 104 /** 105 * Check if stack has no elements. 106 */ 107 @property bool empty() nothrow 108 { 109 return array.length == 0; 110 } 111 112 /** 113 * Free memory allocated by Stack. 114 */ 115 void free() 116 { 117 array.free(); 118 } 119 } 120 121 /// 122 unittest 123 { 124 import std.exception: assertThrown; 125 126 Stack!int s; 127 assertThrown(s.pop()); 128 s.push(100); 129 s.push(3); 130 s.push(76); 131 assert(s.top() == 76); 132 int v; 133 s.pop(v); 134 assert(v == 76); 135 assert(s.pop() == 3); 136 assert(s.pop() == 100); 137 assert(s.empty); 138 s.free(); 139 }