summaryrefslogtreecommitdiff
path: root/src/stable/stack.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/stack.h')
-rw-r--r--src/stable/stack.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/stable/stack.h b/src/stable/stack.h
new file mode 100644
index 0000000..0d1ed3c
--- /dev/null
+++ b/src/stable/stack.h
@@ -0,0 +1,85 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_STACK_H
9#define BU_STACK_H
10
11#include <memory>
12#include "bu/config.h"
13
14namespace Bu
15{
16 template<typename value, typename valuealloc=std::allocator<value> >
17 class Stack
18 {
19 private:
20 typedef struct Chunk
21 {
22 value *pValue;
23 Chunk *pPrev;
24 } Chunk;
25 public:
26 Stack() :
27 pTop( NULL )
28 {
29 }
30
31 virtual ~Stack()
32 {
33 }
34
35 void push( const value &v )
36 {
37 Chunk *pChnk = new Chunk;
38 pChnk->pValue = va.allocate( 1 );
39 va.construct( pChnk->pValue, v );
40 pChnk->pPrev = pTop;
41 pTop = pChnk;
42 }
43
44 value &peek()
45 {
46 return *pTop->pValue;
47 }
48
49 value &top()
50 {
51 return *pTop->pValue;
52 }
53
54 value pop()
55 {
56 value ret( *pTop->pValue );
57
58 Chunk *pChnk = pTop;
59 pTop = pTop->pPrev;
60
61 va.destroy( pChnk->pValue );
62 va.deallocate( pChnk->pValue, 1 );
63 delete pChnk;
64
65 return ret;
66 }
67
68 void clear()
69 {
70 while( !isEmpty() )
71 pop();
72 }
73
74 bool isEmpty()
75 {
76 return pTop == NULL;
77 }
78
79 private:
80 Chunk *pTop;
81 valuealloc va;
82 };
83}
84
85#endif