13 while (first != last) {
20 ::new(static_cast<void*>(pos)) T();
25 ::new(static_cast<void*>(pos)) T(x);
32 return static_cast<iterator>(::operator
new(len *
sizeof(T)));
38 destroy_range(start, finish);
39 ::operator
delete(start);
45 if (finish != end_of_storage) {
47 std::copy_backward(position, finish - 1, finish);
51 const size_t len = (2*size() + 0x100);
53 iterator tmp_finish = std::uninitialized_copy(start, position, tmp);
54 construct(tmp_finish++, x);
55 tmp_finish = std::uninitialized_copy(position, finish, tmp_finish);
59 end_of_storage = tmp + len;
66 start(0), finish(0), end_of_storage(0) {}
69 start(0), finish(0), end_of_storage(0)
76 start(0), finish(0), end_of_storage(0)
81 std::uninitialized_fill_n(
start, n, value);
87 start(0), finish(0), end_of_storage(0)
89 const size_t len = last - first;
98 start(0), finish(0), end_of_storage(0)
102 const T*
const lastx = x + num;
114 if (capacity() < n) {
116 iterator tmp_finish = std::uninitialized_copy(start, finish, tmp);
120 end_of_storage = start + n;
126 std::swap(start, x.
start);
127 std::swap(finish, x.
finish);
134 const size_t x_size = x.
size();
135 if (x_size > capacity()) {
137 start = allocate(x_size);
138 end_of_storage = start + x_size;
139 finish = std::uninitialized_copy(x.
begin(), x.
end(), start);
141 if (x_size < size()) {
143 finish = std::copy(x.
begin(), x.
end(), start);
144 destroy_range(finish, oldfinish);
147 finish = std::uninitialized_copy(x.
entry(size()), x.
end(), finish);
159 if (num > capacity()){
161 start = allocate(num);
162 end_of_storage = start + num;
164 destroy_range(start, finish);
168 const char* charx =
static_cast<const char*
>(x);
169 for (
int i = num; i > 0; --i) {
170 memcpy(&entry, charx,
sizeof(T));
171 construct(finish++, entry);
177 const void* first,
const size_t n)
181 const char* charx =
static_cast<const char*
>(first);
182 if ((
size_t) (end_of_storage - finish) >= n) {
183 const size_t to_move = finish - position;
185 std::uninitialized_copy(position, finish, position + n);
188 for ( ; i > to_move; --i) {
189 memcpy(&entry, charx,
sizeof(T));
190 construct(position++, entry);
193 for ( ; i > 0; --i) {
194 memcpy(&entry, charx,
sizeof(T));
199 std::uninitialized_copy(finish - n, finish, finish);
200 std::copy_backward(position, finish - n, finish);
202 for (
int i = n; i > 0; --i) {
203 memcpy(&entry, charx,
sizeof(T));
209 const size_t new_size = (2*size() + n);
211 iterator tmp_finish = std::uninitialized_copy(start, position, tmp);
212 for (
int i = n; i > 0; --i) {
213 memcpy(&entry, charx,
sizeof(T));
214 construct(tmp_finish++, entry);
217 tmp_finish = std::uninitialized_copy(position, finish, tmp_finish);
221 end_of_storage = tmp + new_size;
229 if (first == last)
return;
230 const size_t n = last - first;
231 if ((
size_t) (end_of_storage - finish) >= n) {
232 const size_t to_move = finish - position;
234 std::uninitialized_copy(position, finish, position + n);
235 std::copy(first, first + to_move, position);
236 std::uninitialized_copy(first + to_move, last, finish);
238 std::uninitialized_copy(finish - n, finish, finish);
239 std::copy_backward(position, finish - n, finish);
240 std::copy(first, last, position);
244 const size_t new_size = (2*size() + n);
246 iterator tmp_finish = std::uninitialized_copy(start, position, tmp);
247 tmp_finish = std::uninitialized_copy(first, last, tmp_finish);
248 tmp_finish = std::uninitialized_copy(position, finish, tmp_finish);
252 end_of_storage = tmp + new_size;
260 if ((
size_t) (end_of_storage - finish) >= n) {
261 const size_t to_move = finish - position;
263 std::uninitialized_copy(position, finish, position + n);
264 std::fill_n(position, to_move, x);
265 std::uninitialized_fill_n(finish, n - to_move, x);
267 std::uninitialized_copy(finish - n, finish, finish);
268 std::copy_backward(position, finish - n, finish);
269 std::fill_n(position, n, x);
273 const size_t new_size = (2*size() + n);
275 iterator tmp_finish = std::uninitialized_copy(start, position, tmp);
276 std::uninitialized_fill_n(tmp_finish, n, x);
278 tmp_finish = std::uninitialized_copy(position, finish, tmp_finish);
282 end_of_storage = tmp + new_size;
289 const size_t n = position - start;
290 if (finish != end_of_storage && position == finish) {
291 construct(finish++, x);
293 insert_aux(position, x);
302 if (finish != end_of_storage) {
303 construct(finish++, x);
305 insert_aux(finish, x);
310 construct(finish++, x);
320 if (start) erase(start, finish);
323 template <
class T>
void
327 if (positions.
size() == 0)
332 while (pos != lastpos)
333 operator[](*pos++) = *val++;
339 if (positions.
size() != values.
size())
340 throw BCP_fatal_error(
"BCP_vec::update() called with unequal sizes.\n");
342 unchecked_update(positions, values);
350 finish = std::copy(pos, pos + 1, start);
351 destroy_range(finish, oldfinish);
357 finish = std::copy(first, last, start);
358 destroy_range(finish, oldfinish);
361 template <
class T>
inline void
365 unchecked_keep_by_index(positions.
begin(), positions.
end());
368 template <
class T>
inline void
371 unchecked_keep_by_index(positions.
begin(), positions.
end());
374 template <
class T>
inline void
379 unchecked_keep_by_index(firstpos, lastpos);
382 template <
class T>
void
386 if (firstpos == lastpos) {
389 iterator target = start;
390 while ( firstpos != lastpos )
391 *target++ = operator[](*firstpos++);
392 destroy_range(target, finish);
401 if (position + 1 != finish)
402 std::copy(position + 1, finish, position);
409 finish = std::copy(last, finish, first);
410 destroy_range(finish, oldfinish);
413 template <
class T>
inline void
417 unchecked_erase_by_index(positions.
begin(), positions.
end());
420 template <
class T>
inline void
423 unchecked_erase_by_index(positions.
begin(), positions.
end());
426 template <
class T>
inline void
431 unchecked_erase_by_index(firstpos, lastpos);
434 template <
class T>
void
438 if (firstpos == lastpos)
442 iterator target = entry(*firstpos);
443 while (firstpos != lastpos){
444 source = *firstpos + 1;
446 if (*firstpos > source)
447 target = std::copy( entry(source), entry(*firstpos), target );
449 iterator oldfinish = finish;
450 finish = std::copy( entry(*firstpos + 1), end(), target );
451 destroy_range(finish, oldfinish);
void deallocate()
Destroy the entries in the vector and free the memory allocated for the vector.
void insert_aux(iterator position, const_reference x)
insert x into the given position in the vector.
void clear()
Delete every entry.
iterator finish
Iterator pointing to right after the last entry in the vector.
const T & const_reference
iterator begin()
Return an iterator to the beginning of the object.
void unchecked_update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Same as the previous method but without sanity checks.
void reserve(const size_t n)
Reallocate the object to make space for n entries.
void push_back(const_reference x)
Append x to the end of the vector.
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 construct(iterator pos)
void keep_by_index(const BCP_vec< int > &positions)
Keep the entries indexed by indices.
void erase(iterator pos)
Erase the entry pointed to by pos.
iterator end_of_storage
Iterator pointing to right after the last memory location usable by the vector without reallocation...
void pop_back()
Delete the last entry.
iterator start
Iterator pointing to the beginning of the memory array where the vector is stored.
void erase_by_index(const BCP_vec< int > &positions)
Erase the entries indexed by indices.
void update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Update those entries listed in positions to the given values.
void destroy_range(iterator first, iterator last)
BCP_vec()
The default constructor initializes the data members as 0 pointers.
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 BCP_vec_sanity_check(BCP_vec< int >::const_iterator firstpos, BCP_vec< int >::const_iterator lastpos, const int maxsize)
A helper function to test whether a set positions is sane for a vector.
Currently there isn't any error handling in BCP.
size_t size() const
Return the current number of entries.
iterator end()
Return an iterator to the end of the object.
void unchecked_keep_by_index(const BCP_vec< int > &positions)
Same as the previous method but without the sanity checks.
The class BCP_vec serves the same purpose as the vector class in the standard template library...
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
iterator allocate(size_t len)
allocate raw, uninitialized memory for len entries.
iterator entry(const int i)
Return an iterator to the i-th entry.
void keep(iterator pos)
Keep only the entry pointed to by pos.
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 swap(BCP_vec< T > &x)
Exchange the contents of the object with that of x.
void unchecked_erase_by_index(const BCP_vec< int > &positions)
Same as the previous method but without the sanity check.
void destroy(iterator pos)