forked from MartinNowak/dranges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.d
56 lines (45 loc) · 968 Bytes
/
stack.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Written in the D programming language
/**
A simple stack module.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
module dranges.stack;
///
struct Stack(T) {
T[] data;
size_t index;
///
this(int initialLength = 1000) {
data.length = initialLength;
index = 0;
}
///
void push(T value) {
if (index == data.length) {
data.length = data.length*2;
}
data[index] = value;
index++;
}
///
T top() {
return data[index-1];
}
///
T pop() {
T pop = data[index-1];
index--;
return pop;
}
///
bool empty() {
return (index == 0);
}
///
size_t length() {
return index;
}
}