00001
00002
00003 #ifndef _BCP_VECTOR_GENERAL_H
00004 #define _BCP_VECTOR_GENERAL_H
00005
00006
00007
00008
00009 #include <memory>
00010
00022 template <class T> class BCP_vec {
00023 public:
00026
00027 typedef size_t size_type;
00029 typedef T value_type;
00031 typedef T* iterator;
00033 typedef const T* const_iterator;
00035 typedef T& reference;
00037 typedef const T& const_reference;
00040 private:
00041 inline void destroy(iterator pos);
00042 inline void destroy_range(iterator first, iterator last);
00043 inline void construct(iterator pos);
00044 inline void construct(iterator pos, const_reference x);
00045
00046 protected:
00050 inline iterator allocate(size_t len);
00053 inline void deallocate();
00056 void insert_aux(iterator position, const_reference x);
00059 protected:
00064 iterator start;
00066 iterator finish;
00069 iterator end_of_storage;
00072 public:
00076 BCP_vec();
00078 BCP_vec(const BCP_vec<T>& x);
00082 BCP_vec(const size_t n, const_reference value = T());
00085 BCP_vec(const_iterator first, const_iterator last);
00088 BCP_vec(const T* x, const size_t num);
00091 virtual ~BCP_vec() { deallocate(); }
00097 iterator begin() { return start; }
00099 const_iterator begin() const { return start; }
00100
00102 iterator end() { return finish; }
00104 const_iterator end() const { return finish; }
00105
00107 iterator entry(const int i) { return start + i; }
00109 const_iterator entry(const int i) const { return start + i; }
00110
00112 size_t index(const_iterator pos) const { return size_t(pos - start); }
00114 size_t size() const { return finish - start; }
00117 size_t capacity() const { return end_of_storage - start;}
00119 bool empty() const { return start == finish; }
00120
00122 reference operator[](const size_t i) { return *(start + i); }
00124 const_reference operator[](const size_t i) const { return *(start + i); }
00125
00127 reference front() { return *start; }
00129 const_reference front() const { return *start; }
00131 reference back() { return *(finish - 1); }
00133 const_reference back() const { return *(finish - 1); }
00139 void reserve(const size_t n);
00141 inline void swap(BCP_vec<T>& x);
00142
00145 BCP_vec<T>& operator=(const BCP_vec<T>& x);
00146
00151 void assign(const void* x, const size_t num);
00154 void insert(iterator position, const void* first, const size_t num);
00155
00158 void insert(iterator position, const_iterator first, const_iterator last);
00161 void insert(iterator position, const size_t n, const_reference x);
00164 iterator insert(iterator position, const_reference x);
00165
00167 void append(const BCP_vec<T>& x) {
00168 insert(end(), x.begin(), x.end()); }
00171 void append(const_iterator first, const_iterator last) {
00172 insert(end(), first, last); }
00173
00176 inline void push_back(const_reference x);
00179 inline void unchecked_push_back(const_reference x);
00181 inline void pop_back();
00182
00184 inline void clear();
00185
00189 inline void update(const BCP_vec<int>& positions,
00190 const BCP_vec<T>& values);
00192 inline void unchecked_update(const BCP_vec<int>& positions,
00193 const BCP_vec<T>& values);
00194
00197
00198
00202 inline void keep(iterator pos);
00204 inline void keep(iterator first, iterator last);
00208 inline void keep_by_index(const BCP_vec<int>& positions);
00210 inline void unchecked_keep_by_index(const BCP_vec<int>& positions);
00215 inline void keep_by_index(const int * firstpos, const int * lastpos);
00217 void unchecked_keep_by_index(const int * firstpos, const int * lastpos);
00220
00221
00225 inline void erase(iterator pos);
00227 inline void erase(iterator first, iterator last);
00231 inline void erase_by_index(const BCP_vec<int>& positions);
00233 inline void unchecked_erase_by_index(const BCP_vec<int>& positions);
00237 inline void erase_by_index(const int * firstpos, const int * lastpos);
00239 void unchecked_erase_by_index(const int * firstpos, const int * lastpos);
00241 };
00242
00243
00244
00245 template <class T>
00246 bool operator==(const BCP_vec<T>& x, const BCP_vec<T>& y)
00247 {
00248 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
00249 }
00250
00251 template <class T>
00252 bool operator< (BCP_vec<T>& x, BCP_vec<T>& y)
00253 {
00254 return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00255 }
00256
00257
00258
00264 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec,
00265 typename BCP_vec<T*>::iterator first,
00266 typename BCP_vec<T*>::iterator last)
00267 {
00268 typename BCP_vec<T*>::iterator origfirst = first;
00269 while (first != last) {
00270 delete *first;
00271 *first = 0;
00272 ++first;
00273 }
00274 pvec.erase(origfirst, last);
00275 }
00276
00277
00283 template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec)
00284 {
00285 purge_ptr_vector(pvec, pvec.begin(), pvec.end());
00286 }
00287
00288
00294 template <class T>
00295 void purge_ptr_vector_by_index(BCP_vec<T*>& pvec,
00296 typename BCP_vec<int>::const_iterator first,
00297 typename BCP_vec<int>::const_iterator last)
00298 {
00299 BCP_vec<int>::const_iterator origfirst = first;
00300 while (first != last) {
00301 delete pvec[*first];
00302 pvec[*first] = 0;
00303 ++first;
00304 }
00305 pvec.erase_by_index(origfirst, last);
00306 }
00307
00308
00313 template <class T>
00314 void keep_ptr_vector_by_index(BCP_vec<T*>& pvec,
00315 typename BCP_vec<int>::const_iterator first,
00316 typename BCP_vec<int>::const_iterator last)
00317 {
00318 BCP_vec<int>::const_iterator origfirst = first;
00319 const int pvec_size = pvec.size();
00320 int i;
00321
00322 for (i = 0; i < pvec_size && first != last; ++i) {
00323 if (i != *first) {
00324 delete pvec[i];
00325 pvec[i] = 0;
00326 } else {
00327 ++first;
00328 }
00329 }
00330
00331 for ( ; i < pvec_size; ++i) {
00332 delete pvec[i];
00333 pvec[i] = 0;
00334 }
00335 pvec.keep_by_index(origfirst, last);
00336 }
00337
00338
00339
00340
00341 #include <algorithm>
00342
00343 #include "BCP_vector_sanity.hpp"
00344 #include "BCP_error.hpp"
00345
00346 #include "BCP_vector_bool.hpp"
00347 #include "BCP_vector_char.hpp"
00348 #include "BCP_vector_short.hpp"
00349 #include "BCP_vector_int.hpp"
00350 #include "BCP_vector_double.hpp"
00351
00352 #include "BCP_vector_general.hpp"
00353
00354 #endif