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
00023 template <class T> class BCP_vec {
00024 public:
00027
00028 typedef size_t size_type;
00030 typedef T value_type;
00032 typedef T* iterator;
00034 typedef const T* const_iterator;
00036 typedef T& reference;
00038 typedef const T& const_reference;
00041 private:
00042 inline void destroy(iterator pos);
00043 inline void destroy_range(iterator first, iterator last);
00044 inline void construct(iterator pos);
00045 inline void construct(iterator pos, const_reference x);
00046
00047 protected:
00051 inline iterator allocate(size_t len);
00054 inline void deallocate();
00057 void insert_aux(iterator position, const_reference x);
00060 protected:
00065 iterator start;
00067 iterator finish;
00070 iterator end_of_storage;
00073 public:
00077 BCP_vec();
00079 BCP_vec(const BCP_vec<T>& x);
00083 BCP_vec(const size_t n, const_reference value = T());
00086 BCP_vec(const_iterator first, const_iterator last);
00089 BCP_vec(const T* x, const size_t num);
00092 virtual ~BCP_vec() { deallocate(); }
00098 iterator begin() { return start; }
00100 const_iterator begin() const { return start; }
00101
00103 iterator end() { return finish; }
00105 const_iterator end() const { return finish; }
00106
00108 iterator entry(const int i) { return start + i; }
00110 const_iterator entry(const int i) const { return start + i; }
00111
00113 size_t index(const_iterator pos) const { return size_t(pos - start); }
00115 size_t size() const { return finish - start; }
00118 size_t capacity() const { return end_of_storage - start;}
00120 bool empty() const { return start == finish; }
00121
00123 reference operator[](const size_t i) { return *(start + i); }
00125 const_reference operator[](const size_t i) const { return *(start + i); }
00126
00128 reference front() { return *start; }
00130 const_reference front() const { return *start; }
00132 reference back() { return *(finish - 1); }
00134 const_reference back() const { return *(finish - 1); }
00140 void reserve(const size_t n);
00142 inline void swap(BCP_vec<T>& x);
00143
00146 BCP_vec<T>& operator=(const BCP_vec<T>& x);
00147
00152 void assign(const void* x, const size_t num);
00155 void insert(iterator position, const void* first, const size_t num);
00156
00159 void insert(iterator position, const_iterator first, const_iterator last);
00162 void insert(iterator position, const size_t n, const_reference x);
00165 iterator insert(iterator position, const_reference x);
00166
00168 void append(const BCP_vec<T>& x) {
00169 insert(end(), x.begin(), x.end()); }
00172 void append(const_iterator first, const_iterator last) {
00173 insert(end(), first, last); }
00174
00177 inline void push_back(const_reference x);
00180 inline void unchecked_push_back(const_reference x);
00182 inline void pop_back();
00183
00185 inline void clear();
00186
00190 inline void update(const BCP_vec<int>& positions,
00191 const BCP_vec<T>& values);
00193 inline void unchecked_update(const BCP_vec<int>& positions,
00194 const BCP_vec<T>& values);
00195
00198
00199
00203 inline void keep(iterator pos);
00205 inline void keep(iterator first, iterator last);
00209 inline void keep_by_index(const BCP_vec<int>& positions);
00211 inline void unchecked_keep_by_index(const BCP_vec<int>& positions);
00216 inline void keep_by_index(const int * firstpos, const int * lastpos);
00218 void unchecked_keep_by_index(const int * firstpos, const int * lastpos);
00221
00222
00226 inline void erase(iterator pos);
00228 inline void erase(iterator first, iterator last);
00232 inline void erase_by_index(const BCP_vec<int>& positions);
00234 inline void unchecked_erase_by_index(const BCP_vec<int>& positions);
00238 inline void erase_by_index(const int * firstpos, const int * lastpos);
00240 void unchecked_erase_by_index(const int * firstpos, const int * lastpos);
00242 };
00243
00244
00245
00246 template <class T>
00247 bool operator==(const BCP_vec<T>& x, const BCP_vec<T>& y)
00248 {
00249 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
00250 }
00251
00252 template <class T>
00253 bool operator< (BCP_vec<T>& x, BCP_vec<T>& y)
00254 {
00255 return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00256 }
00257
00258
00259
00265 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec,
00266 typename BCP_vec<T*>::iterator first,
00267 typename BCP_vec<T*>::iterator last)
00268 {
00269 typename BCP_vec<T*>::iterator origfirst = first;
00270 while (first != last) {
00271 delete *first;
00272 *first = 0;
00273 ++first;
00274 }
00275 pvec.erase(origfirst, last);
00276 }
00277
00278
00284 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec)
00285 {
00286 purge_ptr_vector(pvec, pvec.begin(), pvec.end());
00287 }
00288
00289
00295 template <class T>
00296 void purge_ptr_vector_by_index(BCP_vec<T*>& pvec,
00297 typename BCP_vec<int>::const_iterator first,
00298 typename BCP_vec<int>::const_iterator last)
00299 {
00300 BCP_vec<int>::const_iterator origfirst = first;
00301 while (first != last) {
00302 delete pvec[*first];
00303 pvec[*first] = 0;
00304 ++first;
00305 }
00306 pvec.erase_by_index(origfirst, last);
00307 }
00308
00309
00314 template <class T>
00315 void keep_ptr_vector_by_index(BCP_vec<T*>& pvec,
00316 typename BCP_vec<int>::const_iterator first,
00317 typename BCP_vec<int>::const_iterator last)
00318 {
00319 BCP_vec<int>::const_iterator origfirst = first;
00320 const int pvec_size = pvec.size();
00321 int i;
00322
00323 for (i = 0; i < pvec_size && first != last; ++i) {
00324 if (i != *first) {
00325 delete pvec[i];
00326 pvec[i] = 0;
00327 } else {
00328 ++first;
00329 }
00330 }
00331
00332 for ( ; i < pvec_size; ++i) {
00333 delete pvec[i];
00334 pvec[i] = 0;
00335 }
00336 pvec.keep_by_index(origfirst, last);
00337 }
00338
00339
00340
00341
00342 #include <algorithm>
00343
00344 #include "BCP_vector_sanity.hpp"
00345 #include "BCP_error.hpp"
00346
00347 #include "BCP_vector_bool.hpp"
00348 #include "BCP_vector_char.hpp"
00349 #include "BCP_vector_short.hpp"
00350 #include "BCP_vector_int.hpp"
00351 #include "BCP_vector_double.hpp"
00352
00353 #include "BCP_vector_general.hpp"
00354
00355 #endif