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