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