00001 #ifndef __SIMPLE_STACK__ 00002 #define __SIMPLE_STACK__ 00003 #include "system_specific.h" 00004 #include <assert.h> 00005 00015 template <class x> class simple_stack 00016 { 00017 x *buff; 00018 int buf_len; 00019 int pos; 00020 int expansion; 00021 void resize(int new_size) 00022 { 00023 if (new_size < pos) 00024 return; 00025 x *temp = new x[new_size]; 00026 for (int i=0;i<pos;i++)temp[i]=buff[i]; 00027 00028 delete [] buff; 00029 buff = temp; 00030 buf_len = new_size; 00031 assert(buf_len >= pos); 00032 } 00033 public: 00034 simple_stack(unsigned int start_size = 10, unsigned int expansion_size = 10) 00035 : buf_len(start_size>3?start_size:3),pos(0),expansion(expansion_size) 00036 { 00037 buff = new x[buf_len]; 00038 if (expansion < 1) 00039 expansion = 1; 00040 } 00041 00042 simple_stack(const simple_stack &y) : buf_len(y.buf_len), 00043 pos(y.pos),expansion(y.expansion) 00044 { 00045 buff = new x[y.buf_len]; 00046 for (int i=0;i<pos;i++) 00047 buff[i] = y.buff[i]; 00048 assert(buf_len >= pos); 00049 } 00050 00051 virtual ~simple_stack() 00052 { 00053 if (buff != 0) 00054 delete [] buff; 00055 } 00056 00057 simple_stack &operator =(const simple_stack &y) 00058 { 00059 if (buf_len < y.pos) 00060 { 00061 if (buff != 0) 00062 delete [] buff; 00063 buf_len = y.buf_len; 00064 buff = new x[buf_len]; 00065 } 00066 expansion = y.expansion; 00067 pos = y.pos; 00068 for (int i=0;i<pos;i++) 00069 buff[i] = y.buff[i]; 00070 assert(buf_len >= pos); 00071 return *this; 00072 } 00073 00074 // trim the buffer back to size actually in use 00075 void freeze() 00076 { 00077 resize(pos); 00078 assert(buf_len >= pos); 00079 } 00080 00081 x *get() const 00082 { 00083 return buff; 00084 } 00085 00086 x *get_copy() const 00087 { 00088 x *res=new x[pos]; 00089 for (int i=0;i<pos;i++) 00090 res[i]=buff[i]; 00091 00092 return res; 00093 } 00094 00095 void reset() {pos=0;} 00096 void push(x ch) 00097 { 00098 if (pos >= buf_len) 00099 { 00100 expand(expansion); 00101 } 00102 buff[pos++] = ch; 00103 assert(buf_len >= pos); 00104 } 00105 00106 x pop() 00107 { 00108 return buff[--pos]; 00109 } 00110 00111 x& top() 00112 { 00113 return buff[pos - 1]; 00114 } 00115 00116 00117 int len() const { return pos;} 00118 00119 x &operator[](int i) const {return buff[i];} 00120 00121 void set_len(int newlen) 00122 { 00123 if ((newlen >= 0) && (newlen <= pos)) 00124 pos = newlen; 00125 assert(buf_len >= pos); 00126 } 00127 00128 void cut(int len) 00129 { 00130 if (len < pos) 00131 pos = len + 1; 00132 assert(buf_len >= pos); 00133 } 00134 void remove(int p) 00135 { 00136 pos --; 00137 for (int i = p;i < pos;i++) 00138 buff[i] = buff[i + 1]; 00139 } 00140 void expand(int by) 00141 { 00142 resize(buf_len + by); 00143 } 00144 00145 void set_expansion(int i) 00146 { 00147 if (i > 0) 00148 expansion = i; 00149 } 00150 00151 int get_buf_len() {return buf_len;} 00152 }; 00153 00154 inline void push_string(simple_stack<char> &x,const char *s) 00155 { 00156 while (*s != 0) 00157 { 00158 x.push(*s); 00159 s++; 00160 } 00161 } 00162 00163 #endif