PSCF v1.1
FSArray.h
1#ifndef UTIL_FS_ARRAY_H
2#define UTIL_FS_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/ArrayIterator.h>
12#include <util/containers/ConstArrayIterator.h>
13#include <util/global.h>
14
15namespace Util
16{
17
36 template <typename Data, int Capacity>
37 class FSArray
38 {
39
40 public:
41
46
53
62
66 virtual ~FSArray();
67
71 int capacity() const;
72
76 int size() const;
77
83 void begin(ArrayIterator<Data> &iterator);
84
90 void begin(ConstArrayIterator<Data> &iterator) const;
91
98 Data& operator[] (int i);
99
106 Data const & operator[] (int i) const;
107
113 void append(Data const & data);
114
118 void clear();
119
126 template <class Archive>
127 void serialize(Archive& ar, const unsigned int version);
128
133
134 protected:
135
137 Data data_[Capacity];
138
140 int size_;
141
142 };
143
144 /*
145 * Constructor.
146 */
147 template <class Data, int Capacity>
149 : size_(0)
150 {}
151
152 /*
153 * Copy constructor.
154 *
155 *\param other the FSArray to be copied.
156 */
157 template <class Data, int Capacity>
159 {
160 size_ = other.size_;
161 for (int i = 0; i < size_; ++i) {
162 data_[i] = other.data_[i];
163 }
164 }
165
166 /*
167 * Assignment, element by element.
168 *
169 * Capacity of LHS FSArray must be >= size of RHS FSArray.
170 *
171 * \param other the RHS FSArray
172 */
173 template <class Data, int Capacity>
176 {
177
178 // Check for self assignment
179 if (this == &other) return *this;
180
181 // Copy elements
182 size_ = other.size_;
183 for (int i = 0; i < size_; ++i) {
184 data_[i] = other[i];
185 }
186 return *this;
187 }
188
189 /*
190 * Destructor.
191 */
192 template <class Data, int Capacity>
194 {}
195
196 /*
197 * Return physical capacity of array.
198 */
199 template <class Data, int Capacity>
201 { return Capacity; }
202
203 /*
204 * Return logical size of this array (i.e., number of elements).
205 */
206 template <class Data, int Capacity>
208 { return size_; }
209
210 /*
211 * Set an ArrayIterator to the beginning of this Array.
212 *
213 * \param iterator ArrayIterator, initialized on output.
214 */
215 template <class Data, int Capacity>
217 {
218 iterator.setCurrent(data_);
219 iterator.setEnd(data_ + size_);
220 }
221
222 /*
223 * Set a ConstArrayIterator to the beginning of this Array.
224 */
225 template <class Data, int Capacity>
227 {
228 iterator.setCurrent(data_);
229 iterator.setEnd(data_ + size_);
230 }
231
232 /*
233 * Mimic C array subscripting.
234 */
235 template <class Data, int Capacity>
237 {
238 assert(i < size_);
239 assert(i >= 0);
240 return data_[i];
241 }
242
243 /*
244 * Mimic C array subscripting.
245 */
246 template <class Data, int Capacity>
247 Data const & FSArray<Data, Capacity>::operator[] (int i) const
248 {
249 assert(i < size_);
250 assert(i >= 0);
251 return data_[i];
252 }
253
254 /*
255 * Append data to the end of the array.
256 */
257 template <class Data, int Capacity>
258 inline void FSArray<Data, Capacity>::append(Data const & data)
259 {
260 if (size_ == Capacity) {
261 UTIL_THROW("Attempt to add to full FSArray");
262 }
263 data_[size_] = data;
264 ++size_;
265 }
266
267 /*
268 * Set logical size to zero.
269 */
270 template <class Data, int Capacity>
272 { size_ = 0; }
273
274 /*
275 * Serialize a FSArray to/from an Archive.
276 */
277 template <class Data, int Capacity>
278 template <class Archive>
279 inline
281 const unsigned int version)
282 {
283 ar & size_;
284 if (size_ > Capacity) {
285 UTIL_THROW("FSArray<Data, Capacity> with size > Capacity");
286 }
287 for (int i = 0; i < size_; ++i) {
288 ar & data_[i];
289 }
290 }
291
292 /*
293 * Packed size of FSArray in a MemoryArchive, in bytes.
294 */
295 template <typename Data, int Capacity>
297 { return Capacity*sizeof(Data) + sizeof(int); }
298
299}
300#endif
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:37
void setEnd(Data *ptr)
Set the value of the end pointer.
Definition: ArrayIterator.h:64
void setCurrent(Data *ptr)
Set the current pointer value.
Definition: ArrayIterator.h:56
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.
A fixed capacity (static) contiguous array with a variable logical size.
Definition: FSArray.h:38
Data & operator[](int i)
Mimic C array subscripting.
Definition: FSArray.h:236
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition: FSArray.h:280
FSArray()
Constructor.
Definition: FSArray.h:148
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
Data data_[Capacity]
Array of Data elements.
Definition: FSArray.h:137
void begin(ArrayIterator< Data > &iterator)
Set an ArrayIterator to the beginning of this container.
Definition: FSArray.h:216
int packedSize()
Packed size of FSArray in a MemoryArchive, in bytes.
Definition: FSArray.h:296
void clear()
Set logical size to zero.
Definition: FSArray.h:271
FSArray(FSArray< Data, Capacity > const &other)
Copy constructor.
Definition: FSArray.h:158
void begin(ConstArrayIterator< Data > &iterator) const
Set a ConstArrayIterator to the beginning of this container.
Definition: FSArray.h:226
int size_
Logical size of array (number of elements used).
Definition: FSArray.h:140
int capacity() const
Return physical capacity of array.
Definition: FSArray.h:200
void append(Data const &data)
Append data to the end of the array.
Definition: FSArray.h:258
FSArray< Data, Capacity > & operator=(FSArray< Data, Capacity > const &other)
Assignment, element by element.
Definition: FSArray.h:175
virtual ~FSArray()
Destructor.
Definition: FSArray.h:193
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