00001
00002
00003 #ifndef _BCP_VECTOR_GENERAL_H
00004 #define _BCP_VECTOR_GENERAL_H
00005
00006
00007
00008
00009 #include <memory>
00010 #include <cstddef>
00011 #include <cstring>
00012
00024 template <class T> class BCP_vec {
00025 public:
00028
00029 typedef size_t size_type;
00031 typedef T value_type;
00033 typedef T* iterator;
00035 typedef const T* const_iterator;
00037 typedef T& reference;
00039 typedef const T& const_reference;
00042 private:
00043 inline void destroy(iterator pos);
00044 inline void destroy_range(iterator first, iterator last);
00045 inline void construct(iterator pos);
00046 inline void construct(iterator pos, const_reference x);
00047
00048 protected:
00052 inline iterator allocate(size_t len);
00055 inline void deallocate();
00058 void insert_aux(iterator position, const_reference x);
00061 protected:
00066 iterator start;
00068 iterator finish;
00071 iterator end_of_storage;
00074 public:
00078 BCP_vec();
00080 BCP_vec(const BCP_vec<T>& x);
00084 BCP_vec(const size_t n, const_reference value = T());
00087 BCP_vec(const_iterator first, const_iterator last);
00090 BCP_vec(const T* x, const size_t num);
00093 virtual ~BCP_vec() { deallocate(); }
00099 iterator begin() { return start; }
00101 const_iterator begin() const { return start; }
00102
00104 iterator end() { return finish; }
00106 const_iterator end() const { return finish; }
00107
00109 iterator entry(const int i) { return start + i; }
00111 const_iterator entry(const int i) const { return start + i; }
00112
00114 size_t index(const_iterator pos) const { return size_t(pos - start); }
00116 size_t size() const { return finish - start; }
00119 size_t capacity() const { return end_of_storage - start;}
00121 bool empty() const { return start == finish; }
00122
00124 reference operator[](const size_t i) { return *(start + i); }
00126 const_reference operator[](const size_t i) const { return *(start + i); }
00127
00129 reference front() { return *start; }
00131 const_reference front() const { return *start; }
00133 reference back() { return *(finish - 1); }
00135 const_reference back() const { return *(finish - 1); }
00141 void reserve(const size_t n);
00143 inline void swap(BCP_vec<T>& x);
00144
00147 BCP_vec<T>& operator=(const BCP_vec<T>& x);
00148
00153 void assign(const void* x, const size_t num);
00156 void insert(iterator position, const void* first, const size_t num);
00157
00160 void insert(iterator position, const_iterator first, const_iterator last);
00163 void insert(iterator position, const size_t n, const_reference x);
00166 iterator insert(iterator position, const_reference x);
00167
00169 void append(const BCP_vec<T>& x) {
00170 insert(end(), x.begin(), x.end()); }
00173 void append(const_iterator first, const_iterator last) {
00174 insert(end(), first, last); }
00175
00178 inline void push_back(const_reference x);
00181 inline void unchecked_push_back(const_reference x);
00183 inline void pop_back();
00184
00186 inline void clear();
00187
00191 inline void update(const BCP_vec<int>& positions,
00192 const BCP_vec<T>& values);
00194 inline void unchecked_update(const BCP_vec<int>& positions,
00195 const BCP_vec<T>& values);
00196
00199
00200
00204 inline void keep(iterator pos);
00206 inline void keep(iterator first, iterator last);
00210 inline void keep_by_index(const BCP_vec<int>& positions);
00212 inline void unchecked_keep_by_index(const BCP_vec<int>& positions);
00217 inline void keep_by_index(const int * firstpos, const int * lastpos);
00219 void unchecked_keep_by_index(const int * firstpos, const int * lastpos);
00222
00223
00227 inline void erase(iterator pos);
00229 inline void erase(iterator first, iterator last);
00233 inline void erase_by_index(const BCP_vec<int>& positions);
00235 inline void unchecked_erase_by_index(const BCP_vec<int>& positions);
00239 inline void erase_by_index(const int * firstpos, const int * lastpos);
00241 void unchecked_erase_by_index(const int * firstpos, const int * lastpos);
00243 };
00244
00245
00246
00247 template <class T>
00248 bool operator==(const BCP_vec<T>& x, const BCP_vec<T>& y)
00249 {
00250 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
00251 }
00252
00253 template <class T>
00254 bool operator< (BCP_vec<T>& x, BCP_vec<T>& y)
00255 {
00256 return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00257 }
00258
00259
00260
00266 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec,
00267 typename BCP_vec<T*>::iterator first,
00268 typename BCP_vec<T*>::iterator last)
00269 {
00270 typename BCP_vec<T*>::iterator origfirst = first;
00271 while (first != last) {
00272 delete *first;
00273 *first = 0;
00274 ++first;
00275 }
00276 pvec.erase(origfirst, last);
00277 }
00278
00279
00285 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec)
00286 {
00287 purge_ptr_vector(pvec, pvec.begin(), pvec.end());
00288 }
00289
00290
00296 template <class T>
00297 void purge_ptr_vector_by_index(BCP_vec<T*>& pvec,
00298 typename BCP_vec<int>::const_iterator first,
00299 typename BCP_vec<int>::const_iterator last)
00300 {
00301 BCP_vec<int>::const_iterator origfirst = first;
00302 while (first != last) {
00303 delete pvec[*first];
00304 pvec[*first] = 0;
00305 ++first;
00306 }
00307 pvec.erase_by_index(origfirst, last);
00308 }
00309
00310
00315 template <class T>
00316 void keep_ptr_vector_by_index(BCP_vec<T*>& pvec,
00317 typename BCP_vec<int>::const_iterator first,
00318 typename BCP_vec<int>::const_iterator last)
00319 {
00320 BCP_vec<int>::const_iterator origfirst = first;
00321 const int pvec_size = pvec.size();
00322 int i;
00323
00324 for (i = 0; i < pvec_size && first != last; ++i) {
00325 if (i != *first) {
00326 delete pvec[i];
00327 pvec[i] = 0;
00328 } else {
00329 ++first;
00330 }
00331 }
00332
00333 for ( ; i < pvec_size; ++i) {
00334 delete pvec[i];
00335 pvec[i] = 0;
00336 }
00337 pvec.keep_by_index(origfirst, last);
00338 }
00339
00340
00341
00342
00343 #include <algorithm>
00344
00345 #include "BCP_vector_sanity.hpp"
00346 #include "BCP_error.hpp"
00347
00348 #include "BCP_vector_bool.hpp"
00349 #include "BCP_vector_char.hpp"
00350 #include "BCP_vector_short.hpp"
00351 #include "BCP_vector_int.hpp"
00352 #include "BCP_vector_double.hpp"
00353
00354 #include "BCP_vector_general.hpp"
00355
00356 #endif