PSCF v1.1
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, The Regents of the University of Minnesota
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
39 template <typename Data, int Capacity>
40 class FPArray
41 {
42
43 public:
44
48 FPArray();
49
57 FPArray(FPArray<Data, Capacity> const & other);
58
62 ~FPArray();
63
70
76 void append(Data &data);
77
81 void clear();
82
86 int capacity() const;
87
91 int size() const;
92
98 void begin(PArrayIterator<Data> &iterator);
99
105 void begin(ConstPArrayIterator<Data> &iterator) const;
106
113 Data& operator[] (int i);
114
121 Data const & operator[] (int i) const;
122
123 protected:
124
126 Data* ptrs_[Capacity];
127
129 int size_;
130
131 };
132
133 // Method definitions
134
138 template <typename Data, int Capacity>
140 : size_(0)
141 {}
142
143 /*
144 * Copy constructor, copy all pointers.
145 */
146 template<typename Data, int Capacity>
148 {
149
150 // Copy pointer values
151 int i;
152 for (i = 0; i < other.size_; ++i) {
153 ptrs_[i] = other.ptrs_[i];
154 }
155 size_ = other.size_;
156
157 // Nullify any unused elements
158 if (Capacity > size_) {
159 for (i = size_; i < Capacity; ++i) {
160 ptrs_[i] = 0;
161 }
162 }
163
164 }
165
166 /*
167 * Assignment, element by element.
168 */
169 template <typename Data, int Capacity>
172 {
173
174 // Check for self assignment
175 if (this == &other) return *this;
176
177 // Precondition
178 if (Capacity < other.size_) {
179 UTIL_THROW("LHS FPArray is too small");
180 }
181
182 // Copy pointers
183 for (int i = 0; i < other.size_; ++i) {
184 ptrs_[i] = other[i];
185 }
186 size_ = other.size_;
187
188 // Nullify any unused elements
189 if (Capacity > size_) {
190 for (int i = size_; i < Capacity; ++i) {
191 ptrs_[i] = 0;
192 }
193 }
194
195 return *this;
196 }
197
201 template <typename Data, int Capacity>
203 {}
204
208 template <typename Data, int Capacity>
210 { return Capacity; }
211
212 /*
213 * Return logical size of this array.
214 */
215 template <typename Data, int Capacity>
217 { return size_; }
218
219 /*
220 * Set a PArrayIterator to the beginning of this Array.
221 */
222 template <typename Data, int Capacity>
223 inline
225 {
226 iterator.setCurrent(const_cast<Data**>(ptrs_));
227 iterator.setEnd(const_cast<Data**>(ptrs_ + size_));
228 }
229
230 /*
231 * Set a PArrayIterator to the beginning of this Array.
232 */
233 template <typename Data, int Capacity>
234 inline
236 {
237 iterator.setCurrent(const_cast<Data**>(ptrs_));
238 iterator.setEnd(const_cast<Data**>(ptrs_ + size_));
239 }
240
241 /*
242 * Mimic C array subscripting.
243 *
244 * \param i array index
245 * \return reference to element i
246 */
247 template <typename Data, int Capacity>
249 {
250 assert(i < size_);
251 assert(i >= 0);
252 return *ptrs_[i];
253 }
254
255 /*
256 * Mimic C array subscripting.
257 *
258 * \param i array index
259 * \return const reference to element i
260 */
261 template <typename Data, int Capacity>
262 inline Data const & FPArray<Data, Capacity>::operator[] (int i) const
263 {
264 assert(i < size_);
265 assert(i >= 0 );
266 return *ptrs_[i];
267 }
268
269 /*
270 * Append data to the end of the array.
271 *
272 * \param data Data to add to end of array.
273 */
274 template <typename Data, int Capacity>
275 inline void FPArray<Data, Capacity>::append(Data &data)
276 {
277 if (size_ == Capacity) {
278 UTIL_THROW("Attempt to add to full FPArray");
279 }
280 ptrs_[size_] = &data;
281 ++size_;
282 }
283
284 /*
285 * Set logical size to zero.
286 */
287 template <typename Data, int Capacity>
289 {
290 size_ = 0;
291 for (int i=0; i < Capacity; ++i) {
292 ptrs_[i] = 0;
293 }
294 }
295
296}
297#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.
Statically allocated pointer array.
Definition: FPArray.h:41
Data & operator[](int i)
Get an element by reference (mimic C-array subscripting).
Definition: FPArray.h:248
Data * ptrs_[Capacity]
Array of pointers to Data objects.
Definition: FPArray.h:126
void clear()
Set logical size to zero and nullify all elements.
Definition: FPArray.h:288
~FPArray()
Destructor.
Definition: FPArray.h:202
FPArray()
Default constructor.
Definition: FPArray.h:139
int size_
Logical size of array (number of elements used).
Definition: FPArray.h:129
void append(Data &data)
Append an element to the end of the array.
Definition: FPArray.h:275
void begin(PArrayIterator< Data > &iterator)
Set an iterator to begin this container.
Definition: FPArray.h:224
int capacity() const
Return physical capacity of array.
Definition: FPArray.h:209
int size() const
Return logical size of this array.
Definition: FPArray.h:216
FPArray< Data, Capacity > & operator=(FPArray< Data, Capacity > const &other)
Assignment, element by element.
Definition: FPArray.h:171
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:51
Utility classes for scientific computation.
Definition: accumulators.mod:1