PSCF v1.4.0
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
38 template <typename Data>
39 class Array
40 {
41
42 public:
43
44 Array<Data>& operator = (Array<Data> const & other) = delete;
45 Array(Array const & other) = delete;
46
52 int capacity() const;
53
62 void begin(ArrayIterator<Data>& iterator);
63
72 void begin(ConstArrayIterator<Data>& iterator) const;
73
82 Data& operator [] (int i);
83
92 Data const & operator [] (int i) const;
93
97 Data* cArray();
98
102 Data const * cArray() const;
103
104 protected:
105
107 Data* data_;
108
111
115 Array();
116
121
122 };
123
124 /*
125 * Default constructor.
126 */
127 template <typename Data>
129 : data_(nullptr),
130 capacity_(0)
131 {}
132
133 /*
134 * Destructor (do-hothing)
135 */
136 template <typename Data>
139
140 /*
141 * Return allocated size.
142 */
143 template <typename Data>
144 inline int Array<Data>::capacity() const
145 { return capacity_; }
146
147 /*
148 * Set an ArrayIterator to begin this Array.
149 */
150 template <typename Data>
152 {
153 assert(data_);
154 assert(capacity_ > 0);
155 iterator.setCurrent(data_);
156 iterator.setEnd(data_ + capacity_);
157 }
158
159 /*
160 * Set a ConstArrayIterator to begin this Array.
161 */
162 template <typename Data>
163 inline void Array<Data>::begin(ConstArrayIterator<Data> &iterator) const
164 {
165 assert(data_);
166 assert(capacity_ > 0);
167 iterator.setCurrent(data_);
168 iterator.setEnd(data_ + capacity_);
169 }
170
171 /*
172 * Get an element by reference (C-array subscripting)
173 */
174 template <typename Data>
175 inline Data& Array<Data>::operator [] (int i)
176 {
177 assert(data_);
178 assert(i >= 0);
179 assert(i < capacity_);
180 return *(data_ + i);
181 }
182
183 /*
184 * Get an element by const reference (C-array subscripting)
185 */
186 template <typename Data>
187 inline Data const & Array<Data>::operator [] (int i) const
188 {
189 assert(data_);
190 assert(i >= 0 );
191 assert(i < capacity_);
192 return *(data_ + i);
193 }
194
195 /*
196 * Get a pointer to the underlying C array.
197 */
198 template <typename Data>
199 inline Data* Array<Data>::cArray()
200 { return data_; }
201
202 /*
203 * Get a pointer to const to the underlying C array.
204 */
205 template <typename Data>
206 inline Data const * Array<Data>::cArray() const
207 { return data_; }
208
209}
210#endif
Forward 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.
Data * cArray()
Return a pointer to the underlying C array.
Definition Array.h:199
Data * data_
Pointer to an array of Data elements.
Definition Array.h:107
int capacity() const
Return allocated size.
Definition Array.h:144
void begin(ArrayIterator< Data > &iterator)
Set an iterator to begin this Array.
Definition Array.h:151
Data & operator[](int i)
Get an element by non-const reference.
Definition Array.h:175
~Array()
Destructor (protected to prevent direct destruction).
Definition Array.h:137
int capacity_
Allocated size of the data_ array.
Definition Array.h:110
Array()
Constructor (protected to provent direct instantiation).
Definition Array.h:128
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.