Simpatico  v1.10
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 
15 namespace Util
16 {
17 
36  template <typename Data, int Capacity>
37  class FSArray
38  {
39 
40  public:
41 
45  FSArray();
46 
52  FSArray(const FSArray<Data, Capacity>& other);
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  const Data& operator[] (int i) const;
107 
113  void append(const Data &data);
114 
118  void clear();
119 
126  template <class Archive>
127  void serialize(Archive& ar, const unsigned int version);
128 
132  int packedSize();
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  const Data& 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(const Data &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 void FSArray<Data, Capacity>::serialize(Archive& ar,
280  const unsigned int version)
281  {
282  ar & size_;
283  if (size_ > Capacity) {
284  UTIL_THROW("FSArray<Data, Capacity> with size > Capacity");
285  }
286  for (int i = 0; i < size_; ++i) {
287  ar & data_[i];
288  }
289  }
290 
291  /*
292  * Packed size of FSArray in a MemoryArchive, in bytes.
293  */
294  template <typename Data, int Capacity>
296  { return Capacity*sizeof(Data) + sizeof(int); }
297 
298 }
299 #endif
void clear()
Set logical size to zero.
Definition: FSArray.h:271
void setCurrent(Data *ptr)
Set the current pointer value.
Definition: ArrayIterator.h:59
void append(const Data &data)
Append data to the end of the array.
Definition: FSArray.h:258
File containing preprocessor macros for error handling.
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition: FSArray.h:279
FSArray< Data, Capacity > & operator=(const FSArray< Data, Capacity > &other)
Assignment, element by element.
Definition: FSArray.h:175
int capacity() const
Return physical capacity of array.
Definition: FSArray.h:200
int packedSize()
Packed size of FSArray in a MemoryArchive, in bytes.
Definition: FSArray.h:295
FSArray()
Constructor.
Definition: FSArray.h:148
Forward const iterator for an Array or a C array.
A fixed capacity (static) contiguous array with a variable logical size.
Definition: FSArray.h:37
void setEnd(Data *ptr)
Set the value of the end pointer.
int size_
Logical size of array (number of elements used).
Definition: FSArray.h:140
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Data & operator[](int i)
Mimic C array subscripting.
Definition: FSArray.h:236
Utility classes for scientific computation.
Definition: accumulators.mod:1
void begin(ArrayIterator< Data > &iterator)
Set an ArrayIterator to the beginning of this container.
Definition: FSArray.h:216
Data data_[Capacity]
Array of Data elements.
Definition: FSArray.h:137
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
void setEnd(Data *ptr)
Set the value of the end pointer.
Definition: ArrayIterator.h:67
void setCurrent(Data *ptr)
Set the current pointer value.
virtual ~FSArray()
Destructor.
Definition: FSArray.h:193
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207