00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _DDS_SEQ_HH
00025 #define _DDS_SEQ_HH
00026
00027 # include <stdio.h>
00042 namespace DDS
00043 {
00054 template <class T> class sequence {
00055 public:
00057 sequence();
00061 sequence( const sequence &x);
00065 ~sequence();
00069 sequence& operator= (const sequence& x) {
00070 this->resize(x.core_seq._length);
00071 for (unsigned int i = 0; i < this->core_seq._length; i++)
00072 this->core_seq._buffer[i] = x.core_seq._buffer[i];
00073 return *this;
00074 };
00075
00076
00078 uint32_t size() const;
00082 uint32_t capacity() const;
00091 void reserve(uint32_t n);
00100 void resize(uint32_t n);
00102 bool empty() const;
00104 void shrink_to_fit();
00105
00106
00108 T& operator[] (uint32_t n);
00110 const T& operator[] (uint32_t n) const;
00112 T& at (uint32_t n);
00114 const T& at (uint32_t n) const;
00116 T& front ();
00118 const T& front () const;
00120 T& back ();
00122 const T& back () const;
00123
00124
00128 void push_back(const T& v);
00130 void pop_back();
00132 void swap(sequence& x);
00134 void clear();
00135
00136 private:
00137
00138 typedef struct CoreSeqType
00139 {
00140 T * _buffer;
00141 unsigned int _maximum;
00142 unsigned int _length;
00143 unsigned int _size;
00144 unsigned char _own;
00145 unsigned int _item_size;
00146 } CoreSeqType_t;
00147
00148 CoreSeqType_t core_seq;
00149 };
00150
00151 template<class T>
00152 sequence<T>::sequence() {
00153 this->core_seq._buffer = NULL;
00154 this->core_seq._maximum = 0;
00155 this->core_seq._length = 0;
00156 this->core_seq._size = 0;
00157 this->core_seq._own = 0;
00158 this->core_seq._item_size = sizeof(T);
00159 }
00160
00161
00162 template<class T>
00163 sequence<T>::sequence(const sequence&x) {
00164 this->core_seq._buffer = NULL;
00165 this->core_seq._maximum = 0;
00166 this->core_seq._length = 0;
00167 this->core_seq._size = 0;
00168 this->core_seq._own = 0;
00169 this->core_seq._item_size = sizeof(T);
00170 (*this) = x;
00171 }
00172
00173 template<class T>
00174 sequence<T>::~sequence() {
00175 clear();
00176 }
00177
00178
00179 template<class T>
00180 uint32_t
00181 sequence<T>::capacity() const {
00182 return this->core_seq._size;
00183 }
00184
00185 template<class T>
00186 uint32_t sequence<T>::size() const {
00187 return this->core_seq._length;
00188 }
00189
00190 template<class T>
00191 bool
00192 sequence<T>::empty() const {
00193 return (this->core_seq._length == 0)?true:false;
00194 }
00195
00196
00197
00198
00199 template<class T>
00200 void
00201 sequence<T>::reserve(uint32_t n) {
00202 T * tmpbuf = NULL;
00203 if (n > 0)
00204 {
00205 T * newbuf = new T[n];
00206 if (newbuf)
00207 {
00208 this->core_seq._size = n;
00209 if (this->core_seq._length > n)
00210 this->core_seq._length = n;
00211
00212 if (this->core_seq._buffer)
00213 for (uint32_t i = 0;i < this->core_seq._length; i++)
00214 newbuf[i] = this->core_seq._buffer[i];
00215 tmpbuf = this->core_seq._buffer;
00216 this->core_seq._buffer = newbuf;
00217 }
00218 }
00219 else
00220 {
00221 this->core_seq._size = 0;
00222 this->core_seq._length = 0;
00223 tmpbuf = this->core_seq._buffer;
00224 this->core_seq._buffer = NULL;
00225 }
00226 if ( tmpbuf )
00227 delete[] tmpbuf;
00228 }
00229
00230 template<class T>
00231 void sequence<T>::resize(uint32_t n) {
00232 reserve(n);
00233 if (this->core_seq._size == n)
00234 this->core_seq._length = n;
00235 }
00236
00237 template<class T>
00238 void
00239 sequence<T>::shrink_to_fit() {
00240 resize(&this->core_seq, this->core_seq._length);
00241 }
00242
00243
00244 template<class T>
00245 T&
00246 sequence<T>::operator[] (uint32_t n) {
00247 return this->core_seq._buffer[n];
00248 }
00249
00250 template<class T>
00251 const T&
00252 sequence<T>::operator[] (uint32_t n) const {
00253 return this->core_seq._buffer[n];
00254 }
00255
00256 template<class T> T& sequence<T>::at (uint32_t n) { return (*this)[n]; }
00257 template<class T> const T& sequence<T>::at (uint32_t n) const { return (*this)[n]; }
00258 template<class T> T& sequence<T>::front () { return (*this)[0]; }
00259 template<class T> const T& sequence<T>::front () const { return (*this)[0]; }
00260
00261 template<class T> T& sequence<T>::back () { return (*this)[this->core_seq._length-1]; }
00262 template<class T> const T& sequence<T>::back () const { return (*this)[this->core_seq._length-1]; }
00263
00264
00265 template<class T> void sequence<T>::push_back(const T& v) {
00266 if (this->capacity() <= this->size())
00267 reserve(this->size() + 2);
00268
00269 if (this->size() < this->capacity())
00270 {
00271 this->core_seq._buffer[this->size()] = v;
00272 this->core_seq._length ++;
00273 }
00274 }
00275
00276 template<class T> void sequence<T>::pop_back() {
00277 if (this->core_seq._length > 0) this->core_seq._length--;
00278 }
00279
00280 template<class T> void sequence<T>::swap(sequence& x) {
00281 CoreSeqType tmp;
00282 tmp = this->core_seq;
00283 this->core_seq = x.core_seq;
00284 x.core_seq = tmp;
00285 }
00286
00287 template<class T> void sequence<T>::clear() {
00288 delete[] this->core_seq._buffer;
00289 this->core_seq._buffer = NULL;
00290 this->core_seq._length = 0;
00291 this->core_seq._size = 0;
00292 }
00293
00294 # define DECLARE_CPP_UNBOUNDED_SEQ( type, name ) typedef DDS::sequence<type> name
00295
00296 }
00297 #endif
00298
00299