ARTS  2.0.49
array.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2008 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
27 #ifndef array_h
28 #define array_h
29 
30 #include <vector>
31 #include <iostream>
32 #include <iomanip>
33 #include <cassert>
34 #include <climits>
35 #include "matpack.h"
36 
37 
38 // Declare the existance of class Array:
39 template<class base>
40 class Array;
41 
44 
46 
49 
50 // Declare the existance of Vector/Matrix/Tensor classes:
51 class Vector;
52 class Matrix;
53 class Sparse;
54 class Tensor3;
55 class Tensor4;
56 class Tensor5;
57 class Tensor6;
58 class Tensor7;
59 
62 
65 
67 
70 
73 
75 
78 
81 
84 
86 
89 
90 
101 template<class base>
102 class Array : public vector<base>
103 {
104 public:
105  // Constructors:
106  Array() : vector<base>() { /* Nothing to do here. */ }
107  explicit Array(Index n) : vector<base>(n) { /* Nothing to do here. */ }
108  Array(Index n, const base& fillvalue);
109  Array(const Array<base>& A) : vector<base>(A) { /* Nothing to do here. */ }
110 
111  // Assignment operators:
112  Array& operator=(base x);
114 
115  // Number of elements:
116  Index nelem() const;
117 
118  // Index operators:
119  const base& operator[](Index n) const;
120  base& operator[](Index n);
121 };
122 
123 
124 
125 // Member functions for Array:
126 
128 template<class base>
129 inline Array<base>::Array(Index n, const base& fillvalue) :
130  vector<base>(n)
131 {
132  // Use fill to fill.
133  fill(this->begin(),this->end(),fillvalue);
134 }
135 
136 
138 template<class base>
140 {
141  fill(this->begin(),this->end(),x);
142  return *this;
143 }
144 
146 
161 template<class base>
163 {
164  // cout << "size this / A = " << size() << " / " << A.size() << "\n";
165  this->resize(A.size());
166  copy( A.begin(), A.end(), this->begin() );
167  return *this;
168 }
169 
171 template<class base>
172 inline Index Array<base>::nelem() const
173 {
174  size_t s = this->size();
175  assert(s<LONG_MAX);
176  return static_cast<long>(s);
177 }
178 
181 template<class base>
182 inline const base& Array<base>::operator[](Index n) const
183 {
184  assert(0<=n);
185  assert(n<nelem());
186  return vector<base>::operator[](n);
187 }
188 
191 template<class base>
193 {
194  assert(0<=n);
195  assert(n<nelem());
196  return vector<base>::operator[](n);
197 }
198 
199 
200 // Non-member functions:
201 
203 template<class base>
204 inline ostream& operator<<(ostream& os, const Array<base>& v)
205 {
206  typename Array<base>::const_iterator i = v.begin();
207  const typename Array<base>::const_iterator end = v.end();
208 
209  if ( i!=end )
210  {
211  os << setw(3) << *i;
212  ++i;
213  }
214 
215  for ( ; i!=end; ++i )
216  {
217  os << " " << setw(3) << *i;
218  }
219 
220  return os;
221 }
222 
224 template<class base>
225 inline base max(const Array<base>& x)
226 {
227  // Initial value for max:
228  base max = x[0];
229 
230  typename Array<base>::const_iterator xi = x.begin();
231  const typename Array<base>::const_iterator xe = x.end();
232 
233  for ( ; xi!=xe ; ++xi )
234  {
235  if ( *xi > max )
236  max = *xi;
237  }
238 
239  return max;
240 }
241 
243 template<class base>
244 inline base min(const Array<base>& x)
245 {
246  // Initial value for min:
247  base min = x[0];
248 
249  typename Array<base>::const_iterator xi = x.begin();
250  const typename Array<base>::const_iterator xe = x.end();
251 
252  for ( ; xi!=xe ; ++xi )
253  {
254  if ( *xi < min )
255  min = *xi;
256  }
257 
258  return min;
259 }
260 
261 
263 
276 template <class base>
278  const base& w )
279 {
280  for ( Index i=0; i<x.nelem(); ++i )
281  if ( w == x[i] )
282  return i;
283 
284  return -1;
285 }
286 
288 
301 template <class base>
303  const Array<base>& x,
304  const base& w )
305 {
306  pos.resize(0);
307  for ( Index i=0; i<x.nelem(); ++i )
308  if ( w == x[i] )
309  pos.push_back(i);
310 }
311 
312 
313 
314 // It is not a good idea to put all the predefined array types in one
315 // place. If I do this than a file cannot use one without defining all
316 // the others.
317 
318 #endif // array_h
Matrix
The Matrix class.
Definition: matpackI.h:767
operator<<
ostream & operator<<(ostream &os, const Array< base > &v)
Output operator.
Definition: array.h:204
Array::operator[]
base & operator[](Index n)
Non-constant index operator.
Definition: array.h:192
matpack.h
ArrayOfNumeric
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:48
Tensor3
The Tensor3 class.
Definition: matpackIII.h:340
ArrayOfTensor4
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:77
ArrayOfTensor3
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:72
Sparse
The Sparse class.
Definition: matpackII.h:55
Array::Array
Array()
Definition: array.h:106
Tensor4
The Tensor4 class.
Definition: matpackIV.h:375
Array::Array
Array(Index n)
Definition: array.h:107
ArrayOfVector
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
Array
This can be used to make arrays out of anything.
Definition: array.h:103
Array::Array
Array(Index n, const base &fillvalue)
Constructor filling with constant value.
Definition: array.h:129
ArrayOfArrayOfTensor3
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:74
ArrayOfArrayOfIndex
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:45
ArrayOfIndex
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
Array::operator[]
const base & operator[](Index n) const
Constant index operator.
Definition: array.h:182
ArrayOfMatrix
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:64
ArrayOfTensor7
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:88
Tensor5
The Tensor5 class.
Definition: matpackV.h:443
ArrayOfSparse
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:69
Array::operator=
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:139
ArrayOfTensor5
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:80
find_all
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:302
max
base max(const Array< base > &x)
Max function.
Definition: array.h:225
Array::operator=
Array & operator=(const Array< base > &A)
Assignment from another Array.
Definition: array.h:162
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Array::Array
Array(const Array< base > &A)
Definition: array.h:109
find_first
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:277
Tensor6
The Tensor6 class.
Definition: matpackVI.h:937
ArrayOfArrayOfMatrix
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:66
Vector
The Vector class.
Definition: matpackI.h:555
ArrayOfTensor6
Array< Tensor6 > ArrayOfTensor6
An array of Tensor6.
Definition: array.h:83
min
base min(const Array< base > &x)
Min function.
Definition: array.h:244
copy
void copy(ConstIterator1D origin, const ConstIterator1D &end, Iterator1D target)
Copy data between begin and end to target.
Definition: matpackI.cc:597
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:172
Tensor7
The Tensor7 class.
Definition: matpackVII.h:1912
ArrayOfArrayOfTensor6
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:85