PSCF v1.5.0
FPArray.h
1#ifndef UTIL_FP_ARRAY_H
2#define UTIL_FP_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/PArrayIterator.h>
12#include <util/containers/ConstPArrayIterator.h>
13#include <util/global.h>
14
15namespace Util
16{
17
40 template <typename Data, int Capacity>
41 class FPArray
42 {
43
44 public:
45
49 FPArray();
50
58 FPArray(FPArray<Data, Capacity> const & other);
59
63 ~FPArray();
64
71
77 void append(Data &data);
78
82 void clear();
83
87 int capacity() const;
88
92 int size() const;
93
99 void begin(PArrayIterator<Data> &iterator);
100
106 void begin(ConstPArrayIterator<Data> &iterator) const;
107
114 Data& operator[] (int i);
115
122 Data const & operator[] (int i) const;
123
124 protected:
125
127 Data* ptrs_[Capacity];
128
130 int size_;
131
132 };
133
134 // Method definitions
135
136 /*
137 * Constructor.
138 */
139 template <typename Data, int Capacity> inline
141 #ifdef UTIL_CXX11
142 ptrs_{},
143 #endif
144 size_(0)
145 {}
146
147 /*
148 * Copy constructor, copy all pointers.
149 */
150 template<typename Data, int Capacity>
152 #ifdef UTIL_CXX11
153 ptrs_{},
154 #endif
155 size_(0)
156 {
157 // Copy pointer values
158 int i;
159 for (i = 0; i < other.size_; ++i) {
160 ptrs_[i] = other.ptrs_[i];
161 }
162 size_ = other.size_;
163
164 // Nullify any unused elements
165 if (Capacity > size_) {
166 for (i = size_; i < Capacity; ++i) {
167 ptrs_[i] = 0;
168 }
169 }
170 }
171
172 /*
173 * Assignment, copy all pointers.
174 */
175 template <typename Data, int Capacity>
178 {
179 // Check for self assignment
180 if (this == &other) return *this;
181
182 // Precondition
183 if (Capacity < other.size_) {
184 UTIL_THROW("LHS FPArray is too small");
185 }
186
187 // Copy pointers
188 for (int i = 0; i < other.size_; ++i) {
189 ptrs_[i] = other[i];
190 }
191 size_ = other.size_;
192
193 // Nullify any unused elements
194 if (Capacity > size_) {
195 for (int i = size_; i < Capacity; ++i) {
196 ptrs_[i] = 0;
197 }
198 }
199
200 return *this;
201 }
202
203 /*
204 * Destructor.
205 */
206 template <typename Data, int Capacity>
209
210 /*
211 * Return physical capacity of array.
212 */
213 template <typename Data, int Capacity> inline
215 { return Capacity; }
216
217 /*
218 * Return logical size of this array.
219 */
220 template <typename Data, int Capacity> inline
222 { return size_; }
223
224 /*
225 * Set a PArrayIterator to the beginning of this Array.
226 */
227 template <typename Data, int Capacity> inline
229 {
230 iterator.setCurrent(const_cast<Data**>(ptrs_));
231 iterator.setEnd(const_cast<Data**>(ptrs_ + size_));
232 }
233
234 /*
235 * Set a PArrayIterator to the beginning of this Array.
236 */
237 template <typename Data, int Capacity> inline
239 const
240 {
241 iterator.setCurrent(const_cast<Data**>(ptrs_));
242 iterator.setEnd(const_cast<Data**>(ptrs_ + size_));
243 }
244
245 /*
246 * Subscript operator (returns dereferenced value).
247 */
248 template <typename Data, int Capacity>
250 {
251 assert(i < size_);
252 assert(i >= 0);
253 return *ptrs_[i];
254 }
255
256 /*
257 * Subscript operator (returns dereferenced value).
258 */
259 template <typename Data, int Capacity> inline
260 Data const & FPArray<Data, Capacity>::operator[] (int i) const
261 {
262 assert(i < size_);
263 assert(i >= 0 );
264 return *ptrs_[i];
265 }
266
267 /*
268 * Append data to the end of the array.
269 */
270 template <typename Data, int Capacity>
271 inline void FPArray<Data, Capacity>::append(Data &data)
272 {
273 if (size_ == Capacity) {
274 UTIL_THROW("Attempt to add to full FPArray");
275 }
276 ptrs_[size_] = &data;
277 ++size_;
278 }
279
280 /*
281 * Set logical size to zero.
282 */
283 template <typename Data, int Capacity>
285 {
286 size_ = 0;
287 for (int i=0; i < Capacity; ++i) {
288 ptrs_[i] = 0;
289 }
290 }
291
292}
293#endif
Forward iterator for a PArray.
void setEnd(Data **ptr)
Set the value of the end pointer.
void setCurrent(Data **ptr)
Set the current pointer value.
Fixed size, stack allocated pointer array.
Definition FPArray.h:42
Data & operator[](int i)
Get an element by reference (mimic C-array subscripting).
Definition FPArray.h:249
Data * ptrs_[Capacity]
Array of pointers to Data objects.
Definition FPArray.h:127
void clear()
Set logical size to zero and nullify all elements.
Definition FPArray.h:284
~FPArray()
Destructor.
Definition FPArray.h:207
FPArray()
Default constructor.
Definition FPArray.h:140
int size_
Logical size of array (number of elements used).
Definition FPArray.h:130
void append(Data &data)
Append an element to the end of the array.
Definition FPArray.h:271
void begin(PArrayIterator< Data > &iterator)
Set an iterator to begin this container.
Definition FPArray.h:228
int capacity() const
Return physical capacity of array.
Definition FPArray.h:214
int size() const
Return logical size of this array.
Definition FPArray.h:221
FPArray< Data, Capacity > & operator=(FPArray< Data, Capacity > const &other)
Assignment, element by element.
Definition FPArray.h:177
Forward iterator for a PArray.
void setEnd(Data **ptr)
Set the value of the end pointer.
void setCurrent(Data **ptr)
Set the current pointer value.
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:49
Utility classes for scientific computation.