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
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;
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);
61protected:
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(); }
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); }
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()); }
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
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);
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
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
285template <class T> void purge_ptr_vector(BCP_vec<T*>& pvec)
286{
287 purge_ptr_vector(pvec, pvec.begin(), pvec.end());
288}
289
290
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
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)
Definition: BCP_vector.hpp:248
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.
Definition: BCP_vector.hpp:266
bool operator<(BCP_vec< T > &x, BCP_vec< T > &y)
Definition: BCP_vector.hpp:254
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.
Definition: BCP_vector.hpp:316
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.
Definition: BCP_vector.hpp:297
Abstract base class that defines members common to all types of variables.
Definition: BCP_var.hpp:28
The class BCP_vec serves the same purpose as the vector class in the standard template library.
Definition: BCP_vector.hpp:24
T & reference
Definition: BCP_vector.hpp:37
const_iterator end() const
Return a const iterator to the end of the object.
Definition: BCP_vector.hpp:106
const T * const_iterator
Definition: BCP_vector.hpp:35
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.
Definition: BCP_vector.hpp:121
void pop_back()
Delete the last entry.
const T & const_reference
Definition: BCP_vector.hpp:39
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.
Definition: BCP_vector.hpp:126
iterator end_of_storage
Iterator pointing to right after the last memory location usable by the vector without reallocation.
Definition: BCP_vector.hpp:71
void append(const BCP_vec< T > &x)
Append the entries in x to the end of the vector.
Definition: BCP_vector.hpp:169
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.
Definition: BCP_vector.hpp:104
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
T value_type
Definition: BCP_vector.hpp:31
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.
Definition: BCP_vector.hpp:124
const_iterator entry(const int i) const
Return a const iterator to the i-th entry.
Definition: BCP_vector.hpp:111
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.
Definition: BCP_vector.hpp:93
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.
Definition: BCP_vector.hpp:114
iterator entry(const int i)
Return an iterator to the i-th entry.
Definition: BCP_vector.hpp:109
size_t size_type
Definition: BCP_vector.hpp:29
reference back()
Return a reference to the last entry.
Definition: BCP_vector.hpp:133
size_t capacity() const
Return the capacity of the object (space allocated for this many entries).
Definition: BCP_vector.hpp:119
iterator start
Iterator pointing to the beginning of the memory array where the vector is stored.
Definition: BCP_vector.hpp:66
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.
Definition: BCP_vector.hpp:68
iterator begin()
Return an iterator to the beginning of the object.
Definition: BCP_vector.hpp:99
reference front()
Return a reference to the first entry.
Definition: BCP_vector.hpp:129
const_reference back() const
Return a const reference to the last entry.
Definition: BCP_vector.hpp:135
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.
Definition: BCP_vector.hpp:173
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.
Definition: BCP_vector.hpp:101
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
Definition: BCP_vector.hpp:33
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.
Definition: BCP_vector.hpp:131