11#include <util/containers/ArrayIterator.h>
12#include <util/containers/ConstArrayIterator.h>
13#include <util/misc/Memory.h>
32 template <
typename Data>
96 template <
class Archive>
97 void serialize(Archive& ar,
const unsigned int version);
187 template <
typename Data>
199 template <
typename Data>
205 assert(other.size_ <= other.capacity_);
207 assert(other.capacity_ > 0);
210 capacity_ = other.capacity_;
212 for (
int i = 0; i < other.size_; ++i) {
213 data_[i] = other.data_[i];
222 template <
typename Data>
235 template <
typename Data>
239 if (
this == &other)
return *
this;
242 for (
int i = 0; i < other.size_; ++i) {
251 template <
typename Data>
255 UTIL_THROW(
"Cannot reserve with capacity <=0");
258 assert(capacity_ == 0);
264 assert(capacity_ > 0);
265 assert(capacity_ >= size_);
266 Data* newPtr =
nullptr;
269 for (
int i = 0; i < size_; ++i) {
270 newPtr[i] = data_[i];
282 template <
typename Data>
295 template <
typename Data>
302 template <
typename Data>
305 assert(size_ <= capacity_);
306 if (size_ == capacity_) {
307 if (capacity_ == 0) {
313 assert(capacity_ > 0);
314 Data* newPtr =
nullptr;
317 for (
int i = 0; i < size_; ++i) {
318 newPtr[i] = data_[i];
323 capacity_ = 2*capacity_;
329 assert(size_ <= capacity_);
335 template <
typename Data>
350 Data* newPtr =
nullptr;
353 assert(capacity_ > 0);
354 for (
int i = 0; i < size_; ++i) {
355 newPtr[i] = data_[i];
363 for (
int i = size_; i < n; ++i) {
364 new(data_ + i) Data();
373 template <
typename Data>
374 template <
class Archive>
379 if (Archive::is_saving()) {
385 if (Archive::is_loading()) {
390 for (
int i = 0; i < size_; ++i) {
398 template <
typename Data>
403 iterator.
setEnd(data_ + size_);
409 template <
typename Data>
414 iterator.
setEnd(data_ + size_);
420 template <
typename Data>
431 template <
typename Data>
442 template <
typename Data>
444 {
return capacity_; }
449 template <
typename Data>
456 template <
typename Data>
458 {
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.
virtual ~GArray()
Destructor.
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.
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 append(Data const &data)
Append an element to the end of the sequence.
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.