PSCF v1.1
DPArray.h
1#ifndef UTIL_D_P_ARRAY_H
2#define UTIL_D_P_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/PArray.h>
12#include <util/misc/Memory.h>
13#include <util/global.h>
14
15namespace Util
16{
17
23 template <typename Data>
24 class DPArray : public PArray<Data>
25 {
26
27 public:
28
32 DPArray();
33
41 DPArray(DPArray<Data> const & other);
42
49 virtual ~DPArray();
50
61
70 void allocate(int capacity);
71
77 void append(Data& data);
78
82 void clear();
83
87 bool isAllocated() const;
88
89 protected:
90
91 using PArray<Data>::ptrs_;
92 using PArray<Data>::capacity_;
93 using PArray<Data>::size_;
94
95 };
96
97 /*
98 * Default constructor.
99 */
100 template <typename Data>
102 : PArray<Data>()
103 {}
104
105 /*
106 * Copy constructor, copy pointers.
107 *
108 * Allocates a new Data* array and copies all pointer values.
109 */
110 template <typename Data>
112 : PArray<Data>()
113 {
114 if (other.size_ > other.capacity_) {
115 UTIL_THROW("Inconsistent size and capacity");
116 }
117 if (other.isAllocated()) {
118 // Allocate array of Data* pointers
119 Memory::allocate<Data*>(ptrs_, other.capacity_);
120 capacity_ = other.capacity_;
121 // Copy pointers
122 for (int i = 0; i < other.size_; ++i) {
123 ptrs_[i] = other.ptrs_[i];
124 }
125 size_ = other.size_;
126 // Nullify unused elements of ptrs_ array
127 if (capacity_ > size_) {
128 for (int i = size_; i < capacity_; ++i) {
129 ptrs_[i] = 0;
130 }
131 }
132 }
133 }
134
135 /*
136 * Assignment, element by element.
137 */
138 template <typename Data>
140 {
141
142 // Check for self assignment
143 if (this == &other) return *this;
144
145 // Preconditions
146 if (ptrs_ == 0) {
147 UTIL_THROW("LHS DPArray in assignment is not allocated");
148 }
149 if (other.ptrs_ == 0) {
150 UTIL_THROW("RHS DPArray in assignment is not allocated");
151 }
152 if (capacity_ < other.size_) {
153 UTIL_THROW("LHS DPArray is too small");
154 }
155
156 // Copy pointers
157 int i;
158 for (i = 0; i < other.size_; ++i) {
159 ptrs_[i] = other[i];
160 }
161 size_ = other.size_;
162
163 // Nullify any unused elements
164 if (capacity_ > size_) {
165 for (i = size_; i < capacity_; ++i) {
166 ptrs_[i] = 0;
167 }
168 }
169
170 return *this;
171 }
172
173 /*
174 * Destructor.
175 */
176 template <typename Data>
178 {
179 size_ = 0;
180 if (ptrs_) {
181 assert(capacity_ > 0);
182 Memory::deallocate<Data*>(ptrs_, capacity_);
183 capacity_ = 0;
184 }
185 }
186
187 /*
188 * Allocate the underlying array of Data* pointers.
189 */
190 template <typename Data>
191 void DPArray<Data>::allocate(int capacity)
192 {
193 // Preconditions
194 if (!(ptrs_ == 0)) {
195 UTIL_THROW("Cannot re-allocate a DPArray");
196 }
197 if (capacity <= 0) {
198 UTIL_THROW("Cannot allocate a DPArray with capacity <=0");
199 }
200 Memory::allocate<Data*>(ptrs_, capacity);
201 capacity_ = capacity;
202 }
203
204 /*
205 * Append an element to the end of the PArray.
206 */
207 template <typename Data>
208 inline void DPArray<Data>::append(Data& data)
209 {
210 if (!isAllocated()) {
211 UTIL_THROW("Error: Attempt to append to unallocated DPArray");
212 }
213 if (size_ == capacity_) {
214 UTIL_THROW("Error: Attempt to append data to a full DPArray");
215 }
216 ptrs_[size_] = &data;
217 ++size_;
218 }
219
220 /*
221 * Reset to empty state.
222 */
223 template <typename Data>
225 {
226 assert(isAllocated());
227 size_ = 0;
228 }
229
230 /*
231 * Is this DPArray allocated?
232 */
233 template <typename Data>
234 inline
236 { return (bool)ptrs_; }
237
238
239
240}
241#endif
A dynamic array that only holds pointers to its elements.
Definition: DPArray.h:25
bool isAllocated() const
Is this DPArray allocated?
Definition: DPArray.h:235
virtual ~DPArray()
Destructor.
Definition: DPArray.h:177
DPArray()
Constructor.
Definition: DPArray.h:101
void allocate(int capacity)
Allocate an array of pointers to Data.
Definition: DPArray.h:191
void clear()
Reset to empty state.
Definition: DPArray.h:224
DPArray< Data > & operator=(DPArray< Data > const &other)
Assignment, element by element.
Definition: DPArray.h:139
void append(Data &data)
Append an element to the end of the sequence.
Definition: DPArray.h:208
An array that only holds pointers to its elements.
Definition: PArray.h:37
int capacity_
Allocated size of ptrs_ array.
Definition: PArray.h:93
int capacity() const
Return allocated size.
Definition: PArray.h:133
int size_
Logical size (number of elements with initialized data).
Definition: PArray.h:96
Data ** ptrs_
PArray of of pointers to Data objects.
Definition: PArray.h:90
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1