Simpatico  v1.10
Array.h
1 #ifndef UTIL_ARRAY_H
2 #define UTIL_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 
32  template <typename Data>
33  class Array
34  {
35 
36  public:
37 
38  // Protected default constructor
39 
40  // Private unimplemented copy constructor, to prohibit copying.
41 
45  virtual ~Array();
46 
47  // Private unimplemented assignment operator, to prohibit assignment.
48 
54  int capacity() const;
55 
61  void begin(ArrayIterator<Data>& iterator);
62 
68  void begin(ConstArrayIterator<Data>& iterator) const;
69 
78  Data& operator [] (int i);
79 
88  const Data& operator [] (int i) const;
89 
93  Data* cArray();
94 
98  const Data* cArray() const;
99 
100  protected:
101 
103  Data* data_;
104 
107 
113  Array();
114 
115  private:
116 
122  Array(const Array& other);
123 
129  Array<Data>& operator = (const Array<Data>& other);
130 
131  };
132 
133  /*
134  * Default constructor.
135  */
136  template <typename Data>
138  : data_(0),
139  capacity_(0)
140  {}
141 
142  /*
143  * Destructor.
144  */
145  template <typename Data>
147  {}
148 
149  /*
150  * Return allocated size.
151  */
152  template <typename Data>
153  inline int Array<Data>::capacity() const
154  { return capacity_; }
155 
156  /*
157  * Set an ArrayIterator to the beginning of this Array.
158  */
159  template <typename Data>
161  {
162  assert(data_ != 0);
163  assert(capacity_ > 0);
164  iterator.setCurrent(data_);
165  iterator.setEnd(data_ + capacity_);
166  }
167 
168  /*
169  * Set a ConstArrayIterator to the beginning of this Array.
170  */
171  template <typename Data>
172  inline void Array<Data>::begin(ConstArrayIterator<Data> &iterator) const
173  {
174  assert(data_ != 0);
175  assert(capacity_ > 0);
176  iterator.setCurrent(data_);
177  iterator.setEnd(data_ + capacity_);
178  }
179 
180  /*
181  * Get an element by reference (C-array subscripting)
182  */
183  template <typename Data>
184  inline Data& Array<Data>::operator [] (int i)
185  {
186  assert(data_ != 0);
187  assert(i >= 0);
188  assert(i < capacity_);
189  return *(data_ + i);
190  }
191 
192  /*
193  * Get an element by const reference (C-array subscripting)
194  */
195  template <typename Data>
196  inline const Data& Array<Data>::operator [] (int i) const
197  {
198  assert(data_ != 0);
199  assert(i >= 0 );
200  assert(i < capacity_);
201  return *(data_ + i);
202  }
203 
204  /*
205  * Get a pointer to the underlying C array.
206  */
207  template <typename Data>
208  inline Data* Array<Data>::cArray()
209  { return data_; }
210 
211  /*
212  * Get a pointer to const to the underlying C array.
213  */
214  template <typename Data>
215  inline const Data* Array<Data>::cArray() const
216  { return data_; }
217 
218 }
219 #endif
Data * data_
Pointer to an array of Data elements.
Definition: Array.h:103
void begin(ArrayIterator< Data > &iterator)
Set an iterator to begin this Array.
Definition: Array.h:160
Data * cArray()
Return pointer to underlying C array.
Definition: Array.h:208
int capacity_
Allocated size of the data_ array.
Definition: Array.h:106
void setCurrent(Data *ptr)
Set the current pointer value.
Definition: ArrayIterator.h:59
Array container class template.
Definition: AutoCorrArray.h:28
File containing preprocessor macros for error handling.
Forward const iterator for an Array or a C array.
void setEnd(Data *ptr)
Set the value of the end pointer.
Utility classes for scientific computation.
Definition: accumulators.mod:1
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
Data & operator[](int i)
Get an element by non-const reference.
Definition: Array.h:184
void setCurrent(Data *ptr)
Set the current pointer value.
virtual ~Array()
Destructor.
Definition: Array.h:146
Array()
Default constructor.
Definition: Array.h:137
int capacity() const
Return allocated size.
Definition: Array.h:153