Bcp 1.4.4
Loading...
Searching...
No Matches
BCP_vector.hpp
Go to the documentation of this file.
1// Copyright (C) 2000, International Business Machines
2// Corporation and others. All Rights Reserved.
3#ifndef _BCP_VECTOR_GENERAL_H
4#define _BCP_VECTOR_GENERAL_H
5
6// This file is fully docified.
7
8
9#include <memory>
10#include <cstddef>
11#include <cstring>
12
23
24template <class T> class BCP_vec {
25public:
29 typedef size_t size_type;
31 typedef T value_type;
33 typedef T* iterator;
35 typedef const T* const_iterator;
37 typedef T& reference;
39 typedef const T& const_reference;
41
42private:
43 inline void destroy(iterator pos);
44 inline void destroy_range(iterator first, iterator last);
45 inline void construct(iterator pos);
46 inline void construct(iterator pos, const_reference x);
47
48protected:
52 inline iterator allocate(size_t len);
55 inline void deallocate();
58 void insert_aux(iterator position, const_reference x);
60
61protected:
73
74public:
78 BCP_vec();
80 BCP_vec(const BCP_vec<T>& x);
84 BCP_vec(const size_t n, const_reference value = T());
90 BCP_vec(const T* x, const size_t num);
93 virtual ~BCP_vec() { deallocate(); }
95
99 iterator begin() { return start; }
101 const_iterator begin() const { return start; }
102
104 iterator end() { return finish; }
106 const_iterator end() const { return finish; }
107
109 iterator entry(const int i) { return start + i; }
111 const_iterator entry(const int i) const { return start + i; }
112
114 size_t index(const_iterator pos) const { return size_t(pos - start); }
116 size_t size() const { return finish - start; }
119 size_t capacity() const { return end_of_storage - start;}
121 bool empty() const { return start == finish; }
122
124 reference operator[](const size_t i) { return *(start + i); }
126 const_reference operator[](const size_t i) const { return *(start + i); }
127
129 reference front() { return *start; }
131 const_reference front() const { return *start; }
133 reference back() { return *(finish - 1); }
135 const_reference back() const { return *(finish - 1); }
137
141 void reserve(const size_t n);
143 inline void swap(BCP_vec<T>& x);
144
148
153 void assign(const void* x, const size_t num);
156 void insert(iterator position, const void* first, const size_t num);
157
160 void insert(iterator position, const_iterator first, const_iterator last);
163 void insert(iterator position, const size_t n, const_reference x);
167
169 void append(const BCP_vec<T>& x) {
170 insert(end(), x.begin(), x.end()); }
171
174 insert(end(), first, last); }
175
178 inline void push_back(const_reference x);
183 inline void pop_back();
184
186 inline void clear();
187
191 inline void update(const BCP_vec<int>& positions,
192 const BCP_vec<T>& values);
194 inline void unchecked_update(const BCP_vec<int>& positions,
195 const BCP_vec<T>& values);
196
198
199 //--------------------------------------------------------------------------
200
204 inline void keep(iterator pos);
206 inline void keep(iterator first, iterator last);
210 inline void keep_by_index(const BCP_vec<int>& positions);
212 inline void unchecked_keep_by_index(const BCP_vec<int>& positions);
217 inline void keep_by_index(const int * firstpos, const int * lastpos);
219 void unchecked_keep_by_index(const int * firstpos, const int * lastpos);
221
222 //-------------------------------------------------------------------------
223
227 inline void erase(iterator pos);
229 inline void erase(iterator first, iterator last);
233 inline void erase_by_index(const BCP_vec<int>& positions);
235 inline void unchecked_erase_by_index(const BCP_vec<int>& positions);
239 inline void erase_by_index(const int * firstpos, const int * lastpos);
241 void unchecked_erase_by_index(const int * firstpos, const int * lastpos);
243};
244
245//##############################################################################
246
247template <class T>
248bool operator==(const BCP_vec<T>& x, const BCP_vec<T>& y)
249{
250 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
251}
252
253template <class T>
255{
256 return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
257}
258
259//#############################################################################
260
265
266template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec,
267 typename BCP_vec<T*>::iterator first,
268 typename BCP_vec<T*>::iterator last)
269{
270 typename BCP_vec<T*>::iterator origfirst = first;
271 while (first != last) {
272 delete *first;
273 *first = 0;
274 ++first;
275 }
276 pvec.erase(origfirst, last);
277}
278
279
284
285template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec)
286{
287 purge_ptr_vector(pvec, pvec.begin(), pvec.end());
288}
289
290
295
296template <class T>
298 typename BCP_vec<int>::const_iterator first,
299 typename BCP_vec<int>::const_iterator last)
300{
301 BCP_vec<int>::const_iterator origfirst = first;
302 while (first != last) {
303 delete pvec[*first];
304 pvec[*first] = 0;
305 ++first;
306 }
307 pvec.erase_by_index(origfirst, last);
308}
309
310
314
315template <class T>
317 typename BCP_vec<int>::const_iterator first,
318 typename BCP_vec<int>::const_iterator last)
319{
320 BCP_vec<int>::const_iterator origfirst = first;
321 const int pvec_size = pvec.size();
322 int i;
323
324 for (i = 0; i < pvec_size && first != last; ++i) {
325 if (i != *first) {
326 delete pvec[i];
327 pvec[i] = 0;
328 } else {
329 ++first;
330 }
331 }
332
333 for ( ; i < pvec_size; ++i) {
334 delete pvec[i];
335 pvec[i] = 0;
336 }
337 pvec.keep_by_index(origfirst, last);
338}
339
340/* Now include the implementation of the methods so the compiler could
341 instantiate any requested vector class */
342
343#include <algorithm>
344
345#include "BCP_vector_sanity.hpp"
346#include "BCP_error.hpp"
347
348#include "BCP_vector_bool.hpp"
349#include "BCP_vector_char.hpp"
350#include "BCP_vector_short.hpp"
351#include "BCP_vector_int.hpp"
352#include "BCP_vector_double.hpp"
353
354#include "BCP_vector_general.hpp"
355
356#endif
bool operator==(const BCP_vec< T > &x, const BCP_vec< T > &y)
void purge_ptr_vector(BCP_vec< T * > &pvec, typename BCP_vec< T * >::iterator first, typename BCP_vec< T * >::iterator last)
This function purges the entries [first,last) from the vector of pointers pvec.
bool operator<(BCP_vec< T > &x, BCP_vec< T > &y)
void keep_ptr_vector_by_index(BCP_vec< T * > &pvec, typename BCP_vec< int >::const_iterator first, typename BCP_vec< int >::const_iterator last)
This function keeps only the entries indexed by [first,last) from the vector of pointers pvec.
void purge_ptr_vector_by_index(BCP_vec< T * > &pvec, typename BCP_vec< int >::const_iterator first, typename BCP_vec< int >::const_iterator last)
This function purges the entries indexed by [first,last) from the vector of pointers pvec.
The class BCP_vec serves the same purpose as the vector class in the standard template library.
T & reference
const_iterator end() const
Return a const iterator to the end of the object.
const T * const_iterator
void deallocate()
Destroy the entries in the vector and free the memory allocated for the vector.
void clear()
Delete every entry.
void unchecked_keep_by_index(const BCP_vec< int > &positions)
Same as the previous method but without the sanity checks.
bool empty() const
Test if there are any entries in the object.
void pop_back()
Delete the last entry.
const T & const_reference
void push_back(const_reference x)
Append x to the end of the vector.
const_reference operator[](const size_t i) const
Return a const reference to the i-th entry.
iterator end_of_storage
Iterator pointing to right after the last memory location usable by the vector without reallocation.
void append(const BCP_vec< T > &x)
Append the entries in x to the end of the vector.
void keep_by_index(const BCP_vec< int > &positions)
Keep the entries indexed by indices.
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
void erase_by_index(const BCP_vec< int > &positions)
Erase the entries indexed by indices.
iterator end()
Return an iterator to the end of the object.
size_t size() const
Return the current number of entries.
void keep(iterator pos)
Keep only the entry pointed to by pos.
reference operator[](const size_t i)
Return a reference to the i-th entry.
const_iterator entry(const int i) const
Return a const iterator to the i-th entry.
BCP_vec< T > & operator=(const BCP_vec< T > &x)
Copy the contents of x into the object and return a reference the the object itself.
void keep_by_index(const int *firstpos, const int *lastpos)
Keep the entries indexed by the values in [firstpos,lastpos).
virtual ~BCP_vec()
The destructor deallocates the memory allocated for the BCP_vec.
void erase(iterator pos)
Erase the entry pointed to by pos.
size_t index(const_iterator pos) const
Return the index of the entry pointed to by pos.
iterator entry(const int i)
Return an iterator to the i-th entry.
size_t size_type
reference back()
Return a reference to the last entry.
size_t capacity() const
Return the capacity of the object (space allocated for this many entries).
iterator start
Iterator pointing to the beginning of the memory array where the vector is stored.
void unchecked_keep_by_index(const int *firstpos, const int *lastpos)
Same as the previous method but without the sanity checks.
iterator finish
Iterator pointing to right after the last entry in the vector.
iterator begin()
Return an iterator to the beginning of the object.
reference front()
Return a reference to the first entry.
const_reference back() const
Return a const reference to the last entry.
void update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Update those entries listed in positions to the given values.
void unchecked_update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Same as the previous method but without sanity checks.
BCP_vec()
The default constructor initializes the data members as 0 pointers.
void reserve(const size_t n)
Reallocate the object to make space for n entries.
void append(const_iterator first, const_iterator last)
Append the entries [first,last) to the end of the vector.
void swap(BCP_vec< T > &x)
Exchange the contents of the object with that of x.
iterator allocate(size_t len)
allocate raw, uninitialized memory for len entries.
void unchecked_erase_by_index(const BCP_vec< int > &positions)
Same as the previous method but without the sanity check.
void erase_by_index(const int *firstpos, const int *lastpos)
Like the other erase_by_index method (including sanity checks), just the indices of the entries to be...
const_iterator begin() const
Return a const iterator to the beginning of the object.
void insert(iterator position, const void *first, const size_t num)
Insert num entries starting from memory location first into the vector from position pos.
void unchecked_erase_by_index(const int *firstpos, const int *lastpos)
Same as the previous method but without the sanity checks.
T * iterator
void assign(const void *x, const size_t num)
Copy num entries of type T starting at the memory location x into the object.
void insert_aux(iterator position, const_reference x)
insert x into the given position in the vector.
const_reference front() const
Return a const reference to the first entry.