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_hpp_
00029 #define _terimber_stack_hpp_
00030
00031 #include "base/stack.h"
00032 #include "base/common.hpp"
00033
00034 BEGIN_TERIMBER_NAMESPACE
00035 #pragma pack(4)
00036
00037
00038
00039
00040
00041 template< class T >
00042 base_stack< T >::base_stack()
00043 : _head()
00044 {
00045 clear();
00046 }
00047
00048
00049
00050
00051 template< class T >
00052 base_stack< T >::~base_stack()
00053 {
00054 }
00055
00056
00057
00058
00059 template< class T >
00060 inline
00061 bool
00062 base_stack< T >::empty() const
00063 {
00064 return _head._next == head();
00065 }
00066
00067
00068
00069
00070 template< class T >
00071 inline
00072 const T&
00073 base_stack< T >::top() const
00074 {
00075 return _head._next->_value;
00076 }
00077
00078
00079
00080
00081 template< class T >
00082 inline
00083 T&
00084 base_stack< T >::top()
00085 {
00086 return _head._next->_value;
00087 }
00088
00089 template< class T >
00090 inline
00091 TYPENAME base_stack< T >::const_iterator
00092 base_stack< T >::begin() const
00093 {
00094 return const_iterator(_head._next);
00095 }
00096
00097 template< class T >
00098 inline
00099 TYPENAME base_stack< T >::iterator
00100 base_stack< T >::begin()
00101 {
00102 return iterator(_head._next);
00103 }
00104
00105 template< class T >
00106 inline
00107 TYPENAME base_stack< T >::const_iterator
00108 base_stack< T >::end() const
00109 {
00110 return const_iterator(head());
00111 }
00112
00113 template< class T >
00114 inline
00115 TYPENAME base_stack< T >::iterator
00116 base_stack< T >::end()
00117 {
00118 return iterator(head());
00119 }
00120
00121 template < class T >
00122 inline
00123 void
00124 base_stack< T >::clear()
00125 {
00126 _head._next = head();
00127 }
00128
00129 template < class T >
00130 inline
00131 TYPENAME base_stack< T >::_node*
00132 base_stack< T >::head()
00133 {
00134 return static_cast< TYPENAME base_stack< T >::_node* >(&_head);
00135 }
00136
00137 template < class T >
00138 inline
00139 TYPENAME base_stack< T >::_node*
00140 base_stack< T >::head() const
00141 {
00142 return const_cast< TYPENAME base_stack< T >::_node* >(static_cast< const TYPENAME base_stack< T >::_node* >(&_head));
00143 }
00145
00146
00147
00148 template< class T, class A >
00149 _stack< T, A >::_stack()
00150 : base_stack< T >()
00151 {
00152 }
00153
00154
00155
00156
00157 template< class T, class A >
00158 _stack< T, A >::~_stack()
00159 {
00160 base_stack< T >::clear();
00161 }
00162
00163
00164
00165
00166
00167 template< class T, class A >
00168 _stack< T, A >::_stack(const _stack< T, A >& x) :
00169 base_stack< T >(x)
00170 {
00171 *this = x;
00172 }
00173
00174 template< class T, class A >
00175 inline
00176 _stack< T, A >&
00177 _stack< T, A >::operator=(const _stack< T, A >& x)
00178 {
00179 if (this != &x)
00180 {
00181 base_stack< T >::clear();
00182 if (!x.empty())
00183 this->_head._next = x._head._next;
00184 }
00185 return *this;
00186 }
00187
00188
00189 template < class T, class A >
00190 inline
00191 size_t
00192 _stack< T, A >::size()
00193 {
00194 size_t count = 0;
00195 for (TYPENAME _stack< T, A >::const_iterator I = this->begin(); I != this->end(); ++I, ++count);
00196 return count;
00197 }
00198
00199
00200
00201
00202
00203 template< class T, class A >
00204 inline
00205 TYPENAME
00206 _stack< T, A >::iterator
00207 _stack< T, A >::push(A& allocator_, const T& x)
00208 {
00209
00210 TYPENAME _stack< T, A >::_node* n = _buynode(allocator_, x);
00211 if (!n)
00212 return this->end();
00213 n->_next = this->_head._next;
00214 this->_head._next = n;
00215 return TYPENAME _stack< T, A >::iterator(n);
00216 }
00217
00218
00219
00220
00221
00222 template< class T, class A >
00223 inline
00224 bool
00225 _stack< T, A >::pop()
00226 {
00227 if (base_stack< T >::empty())
00228 return false;
00229
00230 this->_head._next = this->_head._next->_next;
00231 return true;
00232 }
00233
00234 template< class T, class A >
00235 inline
00236 bool
00237 _stack< T, A >::pop(node_allocator< TYPENAME base_stack< T >::_node >& allocator_)
00238 {
00239 if (this->empty())
00240 return false;
00241
00242 TYPENAME _stack< T, A >::_node* f = this->_head._next;
00243 this->_head._next = this->_head._next->_next;
00244 _freenode(allocator_, f);
00245
00246 return true;
00247 }
00248
00249 template< class T, class A >
00250 inline
00251 TYPENAME _stack< T, A >::_node*
00252 _stack< T, A >::_buynode(byte_allocator& allocator_, const T& x)
00253 {
00254 void* ptr = allocator_.allocate(sizeof(TYPENAME _stack< T, A >::_node));
00255 if (!ptr)
00256 return 0;
00257
00258 return new (ptr) TYPENAME _stack< T, A >::_node(x);
00259 }
00260
00261 template< class T, class A >
00262 inline
00263 TYPENAME _stack< T, A >::_node*
00264 _stack< T, A >::_buynode(node_allocator< TYPENAME base_stack< T >::_node >& allocator_, const T& x)
00265 {
00266 void* ptr = allocator_.allocate();
00267 if (!ptr)
00268 return 0;
00269 return new (ptr) TYPENAME _stack< T, A >::_node(x);
00270 }
00271
00272 template< class T, class A >
00273 inline
00274 void
00275 _stack< T, A >::_freenode(node_allocator< TYPENAME base_stack< T >::_node >& allocator_, TYPENAME _stack< T, A >::_node* p)
00276 {
00277 allocator_.deallocate(p);
00278 }
00279
00280
00282
00283
00284
00285 template< class T >
00286 stack< T >::stack(size_t size)
00287 : base_stack< T >(), _length(0), _allocator(size)
00288 {
00289 }
00290
00291
00292
00293
00294 template< class T >
00295 stack< T >::~stack()
00296 {
00297 clear();
00298 }
00299
00300
00301
00302
00303 template< class T >
00304 stack< T >::stack(const stack< T >& x)
00305 : base_stack< T >(x), _length(0), _allocator(x._allocator.capacity())
00306 {
00307 *this = x;
00308 }
00309
00310
00311
00312
00313 template< class T >
00314 inline
00315 stack< T >&
00316 stack< T >::operator=(const stack< T >& x)
00317 {
00318 if (this != &x)
00319 {
00320 clear();
00321
00322 stack< const T* > tmp(x._allocator.capacity());
00323 for (TYPENAME stack< T >::const_iterator iter = x.begin(); iter != x.end(); ++iter) tmp.push(&*iter);
00324 for (TYPENAME stack< const T* >::const_iterator iter_ = tmp.begin(); iter_ != tmp.end(); ++iter_) push(**iter_);
00325 }
00326
00327 return *this;
00328 }
00329
00330
00331
00332
00333 template< class T >
00334 inline
00335 size_t
00336 stack< T >::size() const
00337 {
00338 return _length;
00339 }
00340
00341
00342
00343
00344 template< class T >
00345 inline
00346 TYPENAME
00347 stack< T >::iterator
00348 stack< T >::push(const T& x)
00349 {
00350
00351 TYPENAME stack< T >::_node* n = _buynode(x);
00352 if (!n)
00353 return this->end();
00354 n->_next = this->_head._next;
00355 this->_head._next = n;
00356
00357 ++_length;
00358
00359 return TYPENAME stack< T >::iterator(n);
00360 }
00361
00362
00363
00364
00365 template< class T >
00366 inline
00367 bool
00368 stack< T >::pop()
00369 {
00370 if (!_length)
00371 return false;
00372
00373 TYPENAME stack< T >::_node* f = this->_head._next;
00374 this->_head._next = this->_head._next->_next;
00375
00376 --_length;
00377 _freenode(f);
00378 return true;
00379 }
00380
00381
00382
00383 template< class T >
00384 inline
00385 void
00386 stack< T >::clear()
00387 {
00388 for (TYPENAME stack< T >::iterator I = this->begin(); I != this->end();)
00389 {
00390 TYPENAME stack< T >::_node* f = I.node();
00391 ++I;
00392 _freenode(f);
00393 }
00394
00395 base_stack< T >::clear();
00396 _allocator.clear_all();
00397 _length = 0;
00398 }
00399
00400 template< class T >
00401 inline
00402 TYPENAME stack< T >::_node*
00403 stack< T >::_buynode(const T& x)
00404 {
00405 void* ptr = _allocator.allocate();
00406 if (!ptr)
00407 return 0;
00408 return new (ptr) TYPENAME stack< T >::_node(x);
00409 }
00410
00411 template< class T >
00412 inline
00413 void
00414 stack< T >::_freenode(TYPENAME stack< T >::_node* p)
00415 {
00416 p->_value.~T();
00417 _allocator.deallocate(p);
00418 }
00420
00421
00422
00423 inline
00424 unique_key_generator::unique_key_generator(size_t capacity) : _last(0), _rep(capacity)
00425 {
00426 }
00427
00428
00429
00430
00431 inline
00432 unique_key_generator::~unique_key_generator()
00433 {
00434 clear();
00435 }
00436
00437
00438
00439
00440 inline
00441 void
00442 unique_key_generator::clear()
00443 {
00444 _rep.clear();
00445 _last = 0;
00446 }
00447
00448
00449
00450
00451 inline
00452 size_t
00453 unique_key_generator::generate()
00454 {
00455 if (_rep.empty())
00456 return ++_last;
00457 else
00458 {
00459 size_t retVal = _rep.top();
00460 _rep.pop();
00461 return retVal;
00462 }
00463 }
00464
00465
00466
00467
00468 inline
00469 void
00470 unique_key_generator::save(size_t key)
00471 {
00472 _rep.push(key);
00473 }
00474
00475 #pragma pack()
00476 END_TERIMBER_NAMESPACE
00477
00478 #endif // _terimber_stack_hpp_
00479