00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_stack_h_
00029 #define _terimber_stack_h_
00030
00031 #include "base/memory.h"
00032
00033 BEGIN_TERIMBER_NAMESPACE
00034 #pragma pack(4)
00035
00038 template < class T >
00039 class base_stack
00040 {
00041 public:
00043 class _node;
00046 class _node_
00047 {
00048 public:
00050 _node_() :
00051 _next(0)
00052 {
00053 }
00054
00055 _node* _next;
00056 };
00057
00060 class _node : public _node_
00061 {
00062 public:
00064 _node( const T& x
00065 ) :
00066 _node_(),
00067 _value(x)
00068 {
00069 }
00070
00071 T _value;
00072 };
00073 protected:
00075 base_stack< T >();
00077 ~base_stack< T >();
00079 base_stack< T >(const base_stack< T >& x);
00080 public:
00082 inline
00083 bool
00084 empty() const;
00087 inline
00088 const T&
00089 top() const;
00092 inline T&
00093 top();
00094
00098 class iterator;
00101 class const_iterator
00102 {
00103 public:
00105 typedef std::forward_iterator_tag iterator_category;
00107 typedef size_t size_type;
00109 typedef T* pointer;
00111 typedef const T* const_pointer;
00113 typedef T& reference;
00115 typedef const T& const_reference;
00117 typedef T value_type;
00119 typedef size_t difference_type;
00120
00122 inline
00123 const_iterator(_node* p
00124 ) :
00125 _ptr(p)
00126 {
00127 }
00129 inline
00130 const_iterator(const iterator& x) :
00131 _ptr(x._ptr)
00132 {
00133 }
00135 inline
00136 const T&
00137 operator*() const
00138 {
00139 return _ptr->_value;
00140 }
00142 inline
00143 const T*
00144 operator->() const
00145 {
00146 return &_ptr->_value;
00147 }
00149 inline
00150 const_iterator
00151 operator++()
00152 {
00153 _ptr = _ptr->_next;
00154 return *this;
00155 }
00157 inline
00158 const_iterator
00159 operator++(int)
00160 {
00161 const_iterator tmp = _ptr;
00162 _ptr = _ptr->_next;
00163 return tmp;
00164 }
00166 inline
00167 bool
00168 operator==(const const_iterator& x) const
00169 {
00170 return _ptr == x._ptr;
00171 }
00173 inline
00174 bool
00175 operator!=(const const_iterator& x) const
00176 {
00177 return _ptr != x._ptr;
00178 }
00180 inline
00181 _node*
00182 next() const
00183 {
00184 return _ptr->_next;
00185 }
00187 inline
00188 _node*
00189 node() const
00190 {
00191 return _ptr;
00192 }
00193 protected:
00194 _node* _ptr;
00195 };
00198 class iterator : public const_iterator
00199 {
00200 public:
00202 inline
00203 iterator( _node* p
00204 ) :
00205 const_iterator(p)
00206 {
00207 }
00209 inline
00210 T&
00211 operator*() const
00212 {
00213 return this->_ptr->_value;
00214 }
00216 inline
00217 T*
00218 operator->() const
00219 {
00220 return &this->_ptr->_value;
00221 }
00223 inline
00224 iterator
00225 operator++()
00226 {
00227 this->_ptr = this->_ptr->_next;
00228 return *this;
00229 }
00231 inline
00232 iterator
00233 operator++(int)
00234 {
00235 iterator tmp = this->_ptr;
00236 this->_ptr = this->_ptr->_next;
00237 return tmp;
00238 }
00240 inline
00241 bool
00242 operator==(const iterator& x) const
00243 {
00244 return this->_ptr == x._ptr;
00245 }
00247 inline
00248 bool
00249 operator!=(const iterator& x) const
00250 {
00251 return this->_ptr != x._ptr;
00252 }
00253 };
00254
00256 inline
00257 TYPENAME
00258 base_stack< T >::const_iterator
00259 begin() const;
00261 inline
00262 TYPENAME
00263 base_stack< T >::iterator
00264 begin();
00266 inline
00267 TYPENAME
00268 base_stack< T >::const_iterator
00269 end() const;
00271 inline
00272 TYPENAME
00273 base_stack< T >::iterator
00274 end();
00276 inline
00277 void
00278 clear();
00279
00280 protected:
00282 inline
00283 TYPENAME
00284 base_stack< T >::_node*
00285 head();
00287 inline
00288 TYPENAME base_stack< T >::_node*
00289 head() const;
00290
00291 TYPENAME base_stack< T >::_node_ _head;
00292 };
00293
00294
00299 template < class T, class A = byte_allocator >
00300 class _stack : public base_stack< T >
00301 {
00302 public:
00304 _stack< T, A >();
00306 ~_stack< T, A >();
00310 _stack< T, A >(const _stack< T, A >& x);
00314 inline
00315 _stack< T, A >&
00316 operator=(const _stack< T, A >& x);
00318 inline
00319 size_t
00320 size();
00322 inline
00323 TYPENAME
00324 _stack< T, A >::iterator
00325 push( A& allocator_,
00326 const T& x
00327 );
00330 inline
00331 bool
00332 pop();
00335 inline
00336 bool
00337 pop( node_allocator< TYPENAME base_stack< T >::_node >& allocator_
00338 );
00339
00340 protected:
00342 inline
00343 TYPENAME _stack< T, A >::_node*
00344 _buynode( byte_allocator& allocator_,
00345 const T& x
00346 );
00348 inline
00349 TYPENAME
00350 _stack< T, A >::_node*
00351 _buynode( node_allocator< TYPENAME base_stack< T >::_node >& allocator_,
00352 const T& x
00353 );
00355 inline
00356 void
00357 _freenode( node_allocator< TYPENAME base_stack< T >::_node >& allocator_,
00358 TYPENAME _stack< T, A >::_node* p
00359 );
00360 };
00361
00366 template < class T >
00367 class stack : public base_stack< T >
00368 {
00369 public:
00371 stack< T >( size_t size = os_def_size
00372 );
00374 ~stack< T >();
00376 stack< T >(const stack< T >& x);
00378 inline
00379 stack< T >& operator=(const stack< T >& x);
00381 inline
00382 size_t
00383 size() const;
00385 inline
00386 TYPENAME
00387 stack< T >::iterator
00388 push( const T& x
00389 );
00391 inline
00392 bool
00393 pop();
00396 inline
00397 void
00398 clear();
00399
00400 protected:
00402 inline
00403 TYPENAME
00404 stack< T >::_node*
00405 _buynode( const T& x
00406 );
00408 inline
00409 void
00410 _freenode( TYPENAME stack< T >::_node* p
00411 );
00412
00413 protected:
00414 size_t _length;
00415 node_allocator< TYPENAME base_stack< T >::_node > _allocator;
00416 };
00417
00420
00421 class unique_key_generator
00422 {
00424 unique_key_generator(const unique_key_generator& x);
00426 unique_key_generator& operator=(const unique_key_generator& x);
00427
00428 public:
00430 inline
00431 unique_key_generator(size_t capacity = os_def_size
00432 );
00434 inline
00435 ~unique_key_generator();
00437 inline
00438 void
00439 clear();
00441 inline
00442 size_t
00443 generate();
00445 inline
00446 void
00447 save( size_t key
00448 );
00449 private:
00450 size_t _last;
00451 stack< size_t > _rep;
00452 };
00453
00454 #pragma pack()
00455 END_TERIMBER_NAMESPACE
00456
00457 #endif // _terimber_stack_h_
00458