ARTS  2.2.66
array.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2012 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 
64 
67 
69 
72 
75 
77 
80 
82 
85 
88 
90 
93 
94 
105 template<class base>
106 class Array : public std::vector<base>
107 {
108 public:
109  // Constructors:
110  Array() : std::vector<base>() { /* Nothing to do here. */ }
111  explicit Array(Index n) : std::vector<base>(n) { /* Nothing to do here. */ }
112  Array(Index n, const base& fillvalue);
113  Array(const Array<base>& A) : std::vector<base>(A) { /* Nothing to do here. */ }
114 
115  // Assignment operators:
116  Array& operator=(base x);
117  Array& operator=(const Array<base>& A);
118 
119  // Number of elements:
120  Index nelem() const;
121 
122  // Index operators:
123  const base& operator[](Index n) const;
124  base& operator[](Index n);
125 };
126 
127 
128 
129 // Member functions for Array:
130 
132 template<class base>
133 inline Array<base>::Array(Index n, const base& fillvalue) :
134  std::vector<base>(n)
135 {
136  // Use fill to fill.
137  fill(this->begin(),this->end(),fillvalue);
138 }
139 
140 
142 template<class base>
144 {
145  fill(this->begin(),this->end(),x);
146  return *this;
147 }
148 
150 
165 template<class base>
167 {
168  // cout << "size this / A = " << size() << " / " << A.size() << "\n";
169  this->resize(A.size());
170  copy( A.begin(), A.end(), this->begin() );
171  return *this;
172 }
173 
175 template<class base>
176 inline Index Array<base>::nelem() const
177 {
178  size_t s = this->size();
179  assert(s<LONG_MAX);
180  return static_cast<long>(s);
181 }
182 
185 template<class base>
186 inline const base& Array<base>::operator[](Index n) const
187 {
188  assert(0<=n);
189  assert(n<nelem());
190  return std::vector<base>::operator[](n);
191 }
192 
195 template<class base>
197 {
198  assert(0<=n);
199  assert(n<nelem());
200  return std::vector<base>::operator[](n);
201 }
202 
203 
204 // Non-member functions:
205 
207 template<class base>
208 inline std::ostream& operator<<(std::ostream& os, const Array<base>& v)
209 {
210  typename Array<base>::const_iterator i = v.begin();
211  const typename Array<base>::const_iterator end = v.end();
212 
213  if ( i!=end )
214  {
215  os << std::setw(3) << *i;
216  ++i;
217  }
218 
219  for ( ; i!=end; ++i )
220  {
221  os << " " << std::setw(3) << *i;
222  }
223 
224  return os;
225 }
226 
228 template<class base>
229 inline base max(const Array<base>& x)
230 {
231  // Initial value for max:
232  base max = x[0];
233 
234  typename Array<base>::const_iterator xi = x.begin();
235  const typename Array<base>::const_iterator xe = x.end();
236 
237  for ( ; xi!=xe ; ++xi )
238  {
239  if ( *xi > max )
240  max = *xi;
241  }
242 
243  return max;
244 }
245 
247 template<class base>
248 inline base min(const Array<base>& x)
249 {
250  // Initial value for min:
251  base min = x[0];
252 
253  typename Array<base>::const_iterator xi = x.begin();
254  const typename Array<base>::const_iterator xe = x.end();
255 
256  for ( ; xi!=xe ; ++xi )
257  {
258  if ( *xi < min )
259  min = *xi;
260  }
261 
262  return min;
263 }
264 
265 
267 
280 template <class base>
282  const base& w )
283 {
284  for ( Index i=0; i<x.nelem(); ++i )
285  if ( w == x[i] )
286  return i;
287 
288  return -1;
289 }
290 
292 
305 template <class base>
307  const Array<base>& x,
308  const base& w )
309 {
310  pos.resize(0);
311  for ( Index i=0; i<x.nelem(); ++i )
312  if ( w == x[i] )
313  pos.push_back(i);
314 }
315 
316 
317 
318 // It is not a good idea to put all the predefined array types in one
319 // place. If I do this than a file cannot use one without defining all
320 // the others.
321 
322 #endif // array_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
Index find_first(const Array< base > &x, const base &w)
Find first occurance.
Definition: array.h:281
Array(const Array< base > &A)
Definition: array.h:113
Array< ArrayOfMatrix > ArrayOfArrayOfMatrix
Definition: array.h:68
Array< Numeric > ArrayOfNumeric
An array of Numeric.
Definition: array.h:48
base min(const Array< base > &x)
Min function.
Definition: array.h:248
Index nelem() const
Number of elements.
Definition: array.h:176
The Vector class.
Definition: matpackI.h:556
void copy(ConstIterator1D origin, const ConstIterator1D &end, Iterator1D target)
Copy data between begin and end to target.
Definition: matpackI.cc:612
The Sparse class.
Definition: matpackII.h:55
The Tensor4 class.
Definition: matpackIV.h:383
cmplx FADDEEVA() w(cmplx z, double relerr)
Definition: Faddeeva.cc:679
The Tensor7 class.
Definition: matpackVII.h:1931
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
Array()
Definition: array.h:110
Array< Index > ArrayOfIndex
An array of Index.
Definition: array.h:40
The Tensor3 class.
Definition: matpackIII.h:348
const base & operator[](Index n) const
Constant index operator.
Definition: array.h:186
Array< ArrayOfTensor6 > ArrayOfArrayOfTensor6
Definition: array.h:89
Array(Index n)
Definition: array.h:111
Array< Sparse > ArrayOfSparse
An array of sparse matrices.
Definition: array.h:71
Array< Tensor4 > ArrayOfTensor4
An array of Tensor4.
Definition: array.h:79
The Matrix class.
Definition: matpackI.h:788
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:66
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:143
This can be used to make arrays out of anything.
Definition: array.h:40
Array< ArrayOfVector > ArrayOfArrayOfVector
Definition: array.h:63
The Tensor6 class.
Definition: matpackVI.h:950
Array< ArrayOfIndex > ArrayOfArrayOfIndex
Definition: array.h:45
base max(const Array< base > &x)
Max function.
Definition: array.h:229
Array< Tensor7 > ArrayOfTensor7
An array of Tensor7.
Definition: array.h:92
Array< Tensor3 > ArrayOfTensor3
An array of Tensor3.
Definition: array.h:74
Array< Tensor6 > ArrayOfTensor6
An array of Tensor6.
Definition: array.h:87
Array< ArrayOfTensor4 > ArrayOfArrayOfTensor4
Definition: array.h:81
void find_all(ArrayOfIndex &pos, const Array< base > &x, const base &w)
Find all occurances.
Definition: array.h:306
Array< Tensor5 > ArrayOfTensor5
An array of Tensor5.
Definition: array.h:84
Array< ArrayOfTensor3 > ArrayOfArrayOfTensor3
Definition: array.h:76
The Tensor5 class.
Definition: matpackV.h:451