1#ifndef UTIL_G_P_ARRAY_H
2#define UTIL_G_P_ARRAY_H
11#include <util/containers/PArray.h>
27 template <
typename Data>
112 template <
typename Data>
122 template <
typename Data>
127 if (other.
ptrs_ == 0) {
129 assert(other.
size_ == 0);
141 for (
int i = 0; i <
size_; ++i) {
157 template <
typename Data>
161 if (
this == &other)
return *
this;
164 for (
int i = 0; i < other.
size_; ++i) {
173 template <
typename Data>
186 template <
typename Data>
190 UTIL_THROW(
"Cannot reserve with capacity <=0");
193 assert(capacity_ == 0);
195 Memory::allocate<Data*>(ptrs_, capacity);
196 capacity_ = capacity;
198 }
else if (capacity > capacity_) {
199 assert(capacity_ > 0);
200 assert(capacity_ >= size_);
203 Memory::allocate<Data*>(newPtr, capacity);
205 for (
int i = 0; i < size_; ++i) {
206 newPtr[i] = ptrs_[i];
209 Memory::deallocate<Data*>(ptrs_, capacity_);
211 capacity_ = capacity;
218 template <
typename Data>
223 assert(capacity_ > 0);
224 Memory::deallocate<Data*>(ptrs_, capacity_);
232 template <
typename Data>
235 if (!isAllocated()) {
236 assert(capacity_ == 0);
237 Memory::allocate<Data*>(ptrs_, 64);
240 }
else if (size_ == capacity_) {
241 assert(capacity_ > 0);
242 assert(capacity_ >= size_);
245 Memory::allocate<Data*>(newPtr, 2*capacity_);
247 for (
int i = 0; i < size_; ++i) {
248 newPtr[i] = ptrs_[i];
251 Memory::deallocate<Data*>(ptrs_, capacity_);
253 capacity_ = 2*capacity_;
257 ptrs_[size_] = &data;
259 assert(size_ <= capacity_);
265 template <
typename Data>
272 template <
class Data>
274 {
return (
bool)ptrs_; }
An automatically growable PArray.
void reserve(int capacity)
Reserve memory for specified number of elements.
void clear()
Reset to empty state.
bool isAllocated() const
Is this GPArray allocated?
void append(Data &data)
Append an element to the end of the sequence.
GPArray< Data > & operator=(GPArray< Data > const &other)
Assignment, element by element.
virtual ~GPArray()
Destructor.
void deallocate()
Deallocate (delete) underlying array of pointers.
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
An array that only holds pointers to its elements.
int capacity_
Allocated size of ptrs_ array.
int capacity() const
Return allocated size.
int size_
Logical size (number of elements with initialized data).
Data ** ptrs_
PArray of of pointers to Data objects.
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.