PSCF v1.1
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
15namespace Util
16{
17
32 template <typename Data>
33 class Array
34 {
35
36 public:
37
38 // Protected default constructor, to prohibit direct instantiation.
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
64 void begin(ArrayIterator<Data>& iterator);
65
74 void begin(ConstArrayIterator<Data>& iterator) const;
75
84 Data& operator [] (int i);
85
94 Data const & operator [] (int i) const;
95
99 Data* cArray();
100
104 Data const * cArray() const;
105
106 protected:
107
109 Data* data_;
110
113
120
121 private:
122
128 Array(Array const & other);
129
135 Array<Data>& operator = (Array<Data> const & other);
136
137 };
138
139 /*
140 * Default constructor.
141 */
142 template <typename Data>
144 : data_(0),
145 capacity_(0)
146 {}
147
148 /*
149 * Destructor.
150 */
151 template <typename Data>
153 {}
154
155 /*
156 * Return allocated size.
157 */
158 template <typename Data>
159 inline int Array<Data>::capacity() const
160 { return capacity_; }
161
162 /*
163 * Set an ArrayIterator to begin this Array.
164 */
165 template <typename Data>
167 {
168 assert(data_ != 0);
169 assert(capacity_ > 0);
170 iterator.setCurrent(data_);
171 iterator.setEnd(data_ + capacity_);
172 }
173
174 /*
175 * Set a ConstArrayIterator to begin this Array.
176 */
177 template <typename Data>
178 inline void Array<Data>::begin(ConstArrayIterator<Data> &iterator) const
179 {
180 assert(data_ != 0);
181 assert(capacity_ > 0);
182 iterator.setCurrent(data_);
183 iterator.setEnd(data_ + capacity_);
184 }
185
186 /*
187 * Get an element by reference (C-array subscripting)
188 */
189 template <typename Data>
190 inline Data& Array<Data>::operator [] (int i)
191 {
192 assert(data_ != 0);
193 assert(i >= 0);
194 assert(i < capacity_);
195 return *(data_ + i);
196 }
197
198 /*
199 * Get an element by const reference (C-array subscripting)
200 */
201 template <typename Data>
202 inline Data const & Array<Data>::operator [] (int i) const
203 {
204 assert(data_ != 0);
205 assert(i >= 0 );
206 assert(i < capacity_);
207 return *(data_ + i);
208 }
209
210 /*
211 * Get a pointer to the underlying C array.
212 */
213 template <typename Data>
214 inline Data* Array<Data>::cArray()
215 { return data_; }
216
217 /*
218 * Get a pointer to const to the underlying C array.
219 */
220 template <typename Data>
221 inline Data const * Array<Data>::cArray() const
222 { return data_; }
223
224}
225#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
Array container class template.
Definition: Array.h:34
void begin(ConstArrayIterator< Data > &iterator) const
Set a const iterator to begin this Array.
Definition: Array.h:178
Data * cArray()
Return a pointer to the underlying C array.
Definition: Array.h:214
Data * data_
Pointer to an array of Data elements.
Definition: Array.h:109
int capacity() const
Return allocated size.
Definition: Array.h:159
Data const * cArray() const
Return pointer to const to the underlying C array.
Definition: Array.h:221
void begin(ArrayIterator< Data > &iterator)
Set an iterator to begin this Array.
Definition: Array.h:166
Data & operator[](int i)
Get an element by non-const reference.
Definition: Array.h:190
virtual ~Array()
Destructor.
Definition: Array.h:152
int capacity_
Allocated size of the data_ array.
Definition: Array.h:112
Array()
Default constructor.
Definition: Array.h:143
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.
File containing preprocessor macros for error handling.
Utility classes for scientific computation.
Definition: accumulators.mod:1