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_number_hpp_
00029 #define _terimber_number_hpp_
00030
00031 #include "base/number.h"
00032
00033 BEGIN_TERIMBER_NAMESPACE
00034 #pragma pack(4)
00035
00036 template < class T >
00037 room_array< T >::room_array(size_t size, byte_allocator* allocator_) :
00038 _allocator(allocator_), _buffer(0), _size(size)
00039 {
00040 assert(_size);
00041 _buffer = _allocator ? (T*)_allocator->allocate(S * _size) : new T[_size];
00042 }
00043
00044 template < class T >
00045 room_array< T >::room_array(const room_array< T >& x) :
00046 _allocator(x._allocator), _buffer(0), _size(0)
00047 {
00048 *this = x;
00049 }
00050
00051 template < class T >
00052 inline
00053 room_array< T >&
00054 room_array< T >::operator>>(size_t shift)
00055 {
00056 assert(shift <= _size);
00057 memmove(_buffer, _buffer + shift * S, (_size - shift) * S);
00058 memset(_buffer + (_size - shift) * S, 0, shift * S);
00059 return *this;
00060 }
00061
00062 template < class T >
00063 inline
00064 room_array< T >&
00065 room_array< T >::operator<<(size_t shift)
00066 {
00067 assert(shift <= _size);
00068 memmove(_buffer + shift * S, _buffer, (_size - shift) * S);
00069 memset(_buffer, 0, shift * S);
00070 return *this;
00071 }
00072
00073 template < class T >
00074 inline
00075 void
00076 room_array< T >::swap(room_array< T >& x)
00077 {
00078 byte_allocator* allocator_ = _allocator;
00079 T* buffer_ = _buffer;
00080 size_t size_ = _size;
00081
00082 _allocator = x._allocator;
00083 _buffer = x._buffer;
00084 _size = x._size;
00085
00086 x._allocator = allocator_;
00087 x._buffer = buffer_;
00088 x._size = size_;
00089 }
00090
00091 template < class T >
00092 inline
00093 size_t
00094 room_array< T >::size() const
00095 {
00096 return _size;
00097 }
00098
00099 template < class T >
00100 inline
00101 byte_allocator*
00102 room_array< T >::get_allocator()
00103 {
00104 return _allocator;
00105 }
00106
00107 template < class T >
00108 room_array< T >&
00109 room_array< T >::operator=(const room_array< T >& x)
00110 {
00111 if (this != &x)
00112 {
00113
00114 if (_size < x._size)
00115 resize(x._size);
00116
00117 memcpy(_buffer, x._buffer, x._size * S);
00118 memset(_buffer + x._size * S, 0, (_size - x._size) * S);
00119 }
00120
00121 return *this;
00122 }
00123
00124 template < class T >
00125 room_array< T >::~room_array()
00126 {
00127 clear();
00128 }
00129
00130 template < class T >
00131 room_array< T >&
00132 room_array< T >::resize(size_t size, bool clean)
00133 {
00134 assert(size);
00135 if (_size < size)
00136 {
00137 T* new_buffer = _allocator ? (T*)_allocator->allocate(S * size) : new T[size];
00138 if (_size)
00139 memcpy(new_buffer, _buffer, _size * S);
00140
00141 memset(new_buffer + _size, 0, (size - _size) * S);
00142
00143 clear();
00144 _buffer = new_buffer;
00145 }
00146
00147 _size = size;
00148 if (clean)
00149 memset(_buffer, 0, _size * S);
00150
00151 return *this;
00152 }
00153
00154 template < class T >
00155 room_array< T >&
00156 room_array< T >::reserve(size_t size)
00157 {
00158 return _size < size ? resize(size) : *this;
00159 }
00160
00161 template < class T >
00162 room_array< T >&
00163 room_array< T >::fill(const T& x, size_t len)
00164 {
00165 assert(len <= _size);
00166 memset(_buffer, x, len * S);
00167 return *this;
00168 }
00169
00170 template < class T >
00171 room_array< T >&
00172 room_array< T >::copy(const T* x, size_t len)
00173 {
00174 assert(len <= _size);
00175 memcpy(_buffer, x, len * S);
00176 return *this;
00177 }
00178
00179 template < class T >
00180 void
00181 room_array< T >::back(T* x, size_t len) const
00182 {
00183 memcpy(x, _buffer, __min(len, _size) * S);
00184 }
00185
00186 template < class T >
00187 room_array< T >&
00188 room_array< T >::clear()
00189 {
00190 if (_buffer)
00191 {
00192 _allocator ? _allocator->deallocate(_buffer) : delete [] _buffer;
00193 _buffer = 0;
00194 }
00195
00196 _size = 0;
00197 return *this;
00198 }
00199
00200 #pragma pack()
00201 END_TERIMBER_NAMESPACE
00202
00203 #endif // _terimber_number_hpp_
00204