OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FArray1D.h
Go to the documentation of this file.
1 #ifndef CLASSIC_FArray1D_HH
2 #define CLASSIC_FArray1D_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: FArray1D.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1.2.3 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: FArray1D<T,N>
13 //
14 // ------------------------------------------------------------------------
15 // Class category: FixedAlgebra
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2004/11/18 22:18:06 $
19 // $Author: jsberg $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include "Utilities/CLRangeError.h"
24 #include "Utilities/SizeError.h"
25 #include <algorithm>
26 #include <initializer_list>
27 #include <ostream>
28 
29 
30 // Template class FArray1D<T,N>
31 // ------------------------------------------------------------------------
33 // This version has fixed dimensions.
34 // It implements array access, but contains no arithmetic operations.
35 // The destructor generated by the compiler performs the correct operation.
36 // For speed reasons it is not implemented.
37 
38 template<class T, int N>
39 class FArray1D {
40 
41 public:
42 
44  typedef T value_type;
45 
47  typedef T *iterator;
48 
50  typedef const T *const_iterator;
51 
53  // Constructs a zero array.
54  FArray1D();
55 
57  // Set all array elements to [b]t[/b].
58  explicit FArray1D(const T &t);
59 
61  FArray1D(const FArray1D &);
62 
64  FArray1D(const std::initializer_list<T>&);
65 
67  const FArray1D &operator=(const FArray1D &);
68 
70  // Return a reference to element [b]n[/b].
71  // Throw RangeError, if [b]n[/b] is out of range.
72  T &operator()(int n);
73 
75  // Return a constant reference to element [b]n[/b].
76  // Throw RangeError, if [b]n[/b] is out of range.
77  const T &operator()(int n) const;
78 
80  // Return a reference to element [b]n[/b].
81  // Result is undefined, if [b]n[/b] is out of range.
82  T &operator[](int n);
83 
85  // Return a reference to element [b]n[/b].
86  // Result is undefined, if [b]n[/b] is out of range.
87  const T &operator[](int n) const;
88 
90  // Version for non-constant array.
91  iterator begin();
92 
94  // Version for non-constant array.
95  iterator end();
96 
98  // Version for constant array.
99  const_iterator begin() const;
100 
102  // Version for constant array.
103  const_iterator end() const;
104 
106  int size() const;
107 
108 protected:
109 
110  // The array data.
111  T data[N];
112 };
113 
114 
115 // Template function implementation.
116 // ------------------------------------------------------------------------
117 
118 template<class T, int N>
120  std::fill(begin(), end(), T(0));
121 }
122 
123 
124 template<class T, int N>
126  std::copy(rhs.begin(), rhs.end(), begin());
127 }
128 
129 
130 template<class T, int N>
132  std::fill(begin(), end(), val);
133 }
134 
135 #include <iostream>
136 
137 template<class T, int N>
138 FArray1D<T, N>::FArray1D(const std::initializer_list<T>& init) {
139 
140 
141  if (init.size() > N) {
142  throw SizeError("FArray1D<T,N>(const std::initializer_list<T>&)","Size exceeds array dimension.");
143  }
144 
145  // copy list to array
146  std::copy(init.begin(), init.end(), begin());
147 
148  // if initializer list smaller than array dimension, fill rest with zero
149  if (init.size() < N) {
150  std::fill(begin()+init.size(), end(), T(0));
151  }
152 }
153 
154 template<class T, int N>
156  std::copy(rhs.begin(), rhs.end(), begin());
157  return *this;
158 }
159 
160 
161 template<class T, int N> inline
163  return data[i];
164 }
165 
166 
167 template<class T, int N> inline
168 const T &FArray1D<T, N>::operator[](int i) const {
169  return data[i];
170 }
171 
172 
173 template<class T, int N>
175  if(i >= N) {
176  throw CLRangeError("FArray1D::operator()", "Index out of range.");
177  }
178  return data[i];
179 }
180 
181 
182 template<class T, int N>
183 const T &FArray1D<T, N>::operator()(int i) const {
184  if(i >= N) {
185  throw CLRangeError("FArray1D::operator()", "Index out of range.");
186  }
187  return data[i];
188 }
189 
190 
191 template<class T, int N> inline
193  return data;
194 }
195 
196 
197 template<class T, int N> inline
199  return data + N;
200 }
201 
202 
203 template<class T, int N> inline
205  return data;
206 }
207 
208 
209 template<class T, int N> inline
211  return data + N;
212 }
213 
214 
215 template<class T, int N> inline
216 int FArray1D<T, N>::size() const {
217  return N;
218 }
219 
220 
221 template <class T, int N>
222 std::ostream &operator<<(std::ostream &os, const FArray1D<T, N> &v) {
223  for(int i = 0; i < N; ++i) {
224  os << v[i] << " ";
225  }
226 
227  os << std::endl;
228  return os;
229 }
230 
231 #endif // CLASSIC_FArray1D_HH
T & operator[](int n)
Get element.
Definition: FArray1D.h:162
Definition: rbendmap.h:8
Size error exception.
Definition: SizeError.h:33
const FArray1D & operator=(const FArray1D &)
Assignment.
Definition: FArray1D.h:155
T * iterator
Iterator for the array.
Definition: FArray1D.h:47
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
T data[N]
Definition: FArray1D.h:111
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
const T * const_iterator
Iterator for constant array.
Definition: FArray1D.h:50
T & operator()(int n)
Get element.
Definition: FArray1D.h:174
Range error.
Definition: CLRangeError.h:33
A templated representation for one-dimensional arrays.
Definition: FArray1D.h:39
int size() const
Get array size.
Definition: FArray1D.h:216
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
T value_type
The value type of this array.
Definition: FArray1D.h:44
FArray1D()
Default constructor.
Definition: FArray1D.h:119