11#include <util/containers/ArrayIterator.h>
12#include <util/containers/ConstArrayIterator.h>
13#include <util/misc/Memory.h>
32 template <
typename Data>
99 template <
class Archive>
100 void serialize(Archive& ar,
const unsigned int version);
190 template <
typename Data>
202 template <
typename Data>
208 assert(other.size_ <= other.capacity_);
210 assert(other.capacity_ > 0);
212 Memory::allocate<Data>(data_, other.capacity_);
213 capacity_ = other.capacity_;
215 for (
int i = 0; i < other.size_; ++i) {
216 data_[i] = other.data_[i];
225 template <
typename Data>
230 Memory::deallocate<Data>(data_, capacity_);
238 template <
typename Data>
242 if (
this == &other)
return *
this;
245 for (
int i = 0; i < other.size_; ++i) {
254 template <
typename Data>
258 UTIL_THROW(
"Cannot reserve with capacity <=0");
260 if (!isAllocated()) {
261 assert(capacity_ == 0);
263 Memory::allocate<Data>(data_, capacity);
264 capacity_ = capacity;
266 }
else if (capacity > capacity_) {
267 assert(capacity_ > 0);
268 assert(capacity_ >= size_);
270 Memory::allocate<Data>(newPtr, capacity);
272 for (
int i = 0; i < size_; ++i) {
273 newPtr[i] = data_[i];
276 Memory::deallocate<Data>(data_, capacity_);
278 capacity_ = capacity;
285 template <
typename Data>
290 Memory::deallocate<Data>(data_, capacity_);
298 template <
typename Data>
305 template <
typename Data>
308 assert(size_ <= capacity_);
309 if (size_ == capacity_) {
310 if (capacity_ == 0) {
312 Memory::allocate<Data>(data_, 64);
316 assert(capacity_ > 0);
318 Memory::allocate<Data>(newPtr, 2*capacity_);
320 for (
int i = 0; i < size_; ++i) {
321 newPtr[i] = data_[i];
324 Memory::deallocate<Data>(data_, capacity_);
326 capacity_ = 2*capacity_;
332 assert(size_ <= capacity_);
338 template <
typename Data>
344 assert(capacity_ >= size_);
356 Memory::allocate<Data>(newPtr, m);
358 assert(capacity_ > 0);
359 for (
int i = 0; i < size_; ++i) {
360 newPtr[i] = data_[i];
362 Memory::deallocate<Data>(data_, capacity_);
368 for (
int i = size_; i < n; ++i) {
369 new(data_ + i) Data();
378 template <
class Data>
379 template <
class Archive>
384 if (Archive::is_saving()) {
385 capacity = capacity_;
390 if (Archive::is_loading()) {
395 for (
int i = 0; i < size_; ++i) {
403 template <
class Data>
408 iterator.
setEnd(data_ + size_);
414 template <
class Data>
419 iterator.
setEnd(data_ + size_);
425 template <
class Data>
436 template <
class Data>
447 template <
class Data>
449 {
return capacity_; }
454 template <
class Data>
461 template <
class Data>
463 {
return (
bool)data_; }
Forward iterator for an Array or a C array.
void setEnd(Data *ptr)
Set the value of the end pointer.
void setCurrent(Data *ptr)
Set the current pointer value.
Forward const iterator for an Array or a C array.
void setEnd(Data *ptr)
Set the value of the end pointer.
void setCurrent(Data *ptr)
Set the current pointer value.
An automatically growable array, analogous to a std::vector.
virtual ~GArray()
Destructor.
GArray(GArray< Data > const &other)
Copy constructor, copy pointers.
void serialize(Archive &ar, const unsigned int version)
Serialize a GArray to/from an Archive.
void resize(int n)
Resizes array so that it contains n elements.
Data & operator[](int i)
Mimic C array subscripting.
int size() const
Return logical size of this array (i.e., current number of elements).
bool isAllocated() const
Is this array allocated?
void deallocate()
Deallocate (delete) underlying array of pointers.
int capacity() const
Return physical capacity of array.
void reserve(int capacity)
Reserve memory for specified number of elements.
void begin(ArrayIterator< Data > &iterator)
Set an ArrayIterator to the beginning of this Array.
GArray< Data > & operator=(GArray< Data > const &other)
Assignment, element by element.
void clear()
Reset to empty state.
void begin(ConstArrayIterator< Data > &iterator) const
Set a ConstArrayIterator to the beginning of this Array.
void append(Data const &data)
Append an element to the end of the sequence.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.