ARTS  1.0.222
array.h
Go to the documentation of this file.
1 /* Copyright (C) 2001 Stefan Buehler <sbuehler@uni-bremen.de>
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 <iomanip>
32 #include <iostream>
33 #include <climits>
34 #include "arts.h"
35 
46 template<class base>
47 class Array : public std::vector<base>
48 {
49 public:
50  // Constructors:
51  Array() : std::vector<base>() { /* Nothing to do here. */ };
52  explicit Array(Index n) : std::vector<base>(n) { /* Nothing to do here. */ };
53  Array(Index n, const base& fill);
54  Array(const Array<base>& A) : std::vector<base>(A) { /* Nothing to do here. */ };
55 
56  // Assignment operators:
57  Array& operator=(base x);
59 
60  // Number of elements:
61  Index nelem() const;
62 
63  // Index operators:
64  const base& operator[](Index n) const;
65  base& operator[](Index n);
66 };
67 
68 
69 
70 // Member functions for Array:
71 
73 template<class base>
74 inline Array<base>::Array(Index n, const base& fill) :
75  std::vector<base>(n)
76 {
77  // Use std::fill to fill.
78  std::fill(this->begin(),this->end(),fill);
79 };
80 
81 
83 template<class base>
85 {
86  std::fill(this->begin(),this->end(),x);
87  return *this;
88 }
89 
100 template<class base>
102 {
103  // cout << "size this / A = " << size() << " / " << A.size() << "\n";
104  if ( 0==this->size() )
105  this->resize(A.size()); // Adjust if previously empty.
106  else
107  assert( this->size()==A.size() ); // Otherwise check that sizes are compatible.
108 
109  std::copy( A.begin(), A.end(), this->begin() );
110  return *this;
111 }
112 
114 template<class base>
115 inline Index Array<base>::nelem() const
116 {
117  size_t s = this->size();
118  assert(s<LONG_MAX);
119  return static_cast<INDEX>(s);
120 }
121 
124 template<class base>
125 inline const base& Array<base>::operator[](Index n) const
126 {
127  assert(0<=n);
128  assert(n<nelem());
129  return std::vector<base>::operator[](n);
130 }
131 
134 template<class base>
136 {
137  assert(0<=n);
138  assert(n<nelem());
139  return std::vector<base>::operator[](n);
140 }
141 
142 // Non-member functions:
143 
145 template<class base>
146 inline std::ostream& operator<<(std::ostream& os, const Array<base>& v)
147 {
148  typename Array<base>::const_iterator i = v.begin();
149  const typename Array<base>::const_iterator end = v.end();
150 
151  if ( i!=end )
152  {
153  os << setw(3) << *i;
154  ++i;
155  }
156 
157  for ( ; i!=end; ++i )
158  {
159  os << "\n" << setw(3) << *i;
160  }
161 
162  return os;
163 }
164 
166 template<class base>
167 inline base max(const Array<base>& x)
168 {
169  // Initial value for max:
170  base max = x[0];
171 
172  typename Array<base>::const_iterator xi = x.begin();
173  const typename Array<base>::const_iterator xe = x.end();
174 
175  for ( ; xi!=xe ; ++xi )
176  {
177  if ( *xi > max )
178  max = *xi;
179  }
180 
181  return max;
182 }
183 
185 template<class base>
186 inline base min(const Array<base>& x)
187 {
188  // Initial value for min:
189  base min = x[0];
190 
191  typename Array<base>::const_iterator xi = x.begin();
192  const typename Array<base>::const_iterator xe = x.end();
193 
194  for ( ; xi!=xe ; ++xi )
195  {
196  if ( *xi < min )
197  min = *xi;
198  }
199 
200  return min;
201 }
202 
203 
204 // It is not a good idea to put all the predefined array types in one
205 // place. If I do this than a file cannot use one without defining all
206 // the others.
207 
208 #endif // array_h
Array::operator[]
base & operator[](Index n)
Non-constant index operator.
Definition: array.h:135
copy
void copy(ConstIterator1D origin, const ConstIterator1D &end, Iterator1D target)
Copy data between begin and end to target.
Definition: matpackI.h:1319
Array::Array
Array()
Definition: array.h:51
Array::Array
Array(Index n)
Definition: array.h:52
Array
This can be used to make arrays out of anything.
Definition: array.h:48
Array::operator[]
const base & operator[](Index n) const
Constant index operator.
Definition: array.h:125
Array::operator=
Array & operator=(base x)
Assignment from base type (fill entire Array with this value).
Definition: array.h:84
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
max
base max(const Array< base > &x)
Max function.
Definition: array.h:167
operator<<
std::ostream & operator<<(std::ostream &os, const Array< base > &v)
Output operator.
Definition: array.h:146
Array::operator=
Array & operator=(const Array< base > &A)
Assignment from another Array.
Definition: array.h:101
Array::Array
Array(const Array< base > &A)
Definition: array.h:54
Array::Array
Array(Index n, const base &fill)
Constructor filling with constant value.
Definition: array.h:74
min
base min(const Array< base > &x)
Min function.
Definition: array.h:186
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:115
arts.h
The global header file for ARTS.