aboutsummaryrefslogtreecommitdiff
path: root/src/stack.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/stack.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/stack.h b/src/stack.h
new file mode 100644
index 0000000..955332d
--- /dev/null
+++ b/src/stack.h
@@ -0,0 +1,71 @@
1#ifndef BU_STACK_H
2#define BU_STACK_H
3
4#include <memory>
5
6namespace Bu
7{
8 template<typename value, typename valuealloc=std::allocator<value> >
9 class Stack
10 {
11 private:
12 typedef struct Chunk
13 {
14 value *pValue;
15 Chunk *pPrev;
16 } Chunk;
17 public:
18 Stack() :
19 pTop( NULL )
20 {
21 }
22
23 virtual ~Stack()
24 {
25 }
26
27 void push( const value &v )
28 {
29 Chunk *pChnk = new Chunk;
30 pChnk->pValue = va.allocate( 1 );
31 va.construct( pChnk->pValue, v );
32 pChnk->pPrev = pTop;
33 pTop = pChnk;
34 }
35
36 value &peek()
37 {
38 return *pTop->pValue;
39 }
40
41 value &top()
42 {
43 return *pTop->pValue;
44 }
45
46 value pop()
47 {
48 value ret( *pTop->pValue );
49
50 Chunk *pChnk = pTop;
51 pTop = pTop->pPrev;
52
53 va.destroy( pChnk->pValue );
54 va.deallocate( pChnk->pValue, 1 );
55 delete pChnk;
56
57 return ret;
58 }
59
60 bool isEmpty()
61 {
62 return pTop == NULL;
63 }
64
65 private:
66 Chunk *pTop;
67 valuealloc va;
68 };
69}
70
71#endif