ARTS  2.2.66
m_append.h
Go to the documentation of this file.
1 /* Copyright (C) 2002-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 
29 #ifndef m_append_h
30 #define m_append_h
31 
32 #include "array.h"
33 #include "exceptions.h"
34 #include "matpackI.h"
35 
36 
37 /* Implementations for supported types follow. */
38 
39 /* Implementation for array types */
40 template< class T >
41 void Append(// WS Generic Output:
42  Array<T>& out,
43  // WS Generic Input:
44  const Array<T>& in,
45  const String& direction _U_,
46  const Verbosity&)
47 {
48  const Array<T>* in_pnt;
49  Array<T> in_copy;
50 
51  if (&in == &out)
52  {
53  in_copy = in;
54  in_pnt = &in_copy;
55  }
56  else
57  in_pnt = &in;
58 
59  const Array<T>& in_ref = *in_pnt;
60 
61  // Reserve memory in advance to avoid reallocations:
62  out.reserve(out.nelem() + in_ref.nelem());
63  // Append in to end of out:
64  for (Index i = 0; i < in_ref.nelem(); ++i)
65  out.push_back(in_ref[i]);
66 }
67 
68 
69 /* Implementation for array types to append single element */
70 template< class T >
71 void Append(// WS Generic Output:
72  Array<T>& out,
73  // WS Generic Input:
74  const T& in,
75  const String& direction _U_,
76  const Verbosity&)
77 {
78  // Append in to end of out:
79  out.push_back(in);
80 }
81 
82 
83 /* Implementation for Vector */
84 void Append(// WS Generic Output:
85  Vector& out,
86  // WS Generic Input:
87  const Vector& in,
88  const String& direction _U_,
89  const Verbosity&)
90 {
91  const Vector* in_pnt;
92  Vector in_copy;
93 
94  if (&in == &out)
95  {
96  in_copy = in;
97  in_pnt = &in_copy;
98  }
99  else
100  in_pnt = &in;
101 
102  const Vector &in_ref = *in_pnt;
103 
104  // Get backup of out:
105  Vector dummy = out;
106 
107  // Make out the right size:
108  out.resize(dummy.nelem() + in_ref.nelem());
109 
110  // Copy dummy to first part of out:
111  if (dummy.nelem())
112  out[Range(0, dummy.nelem())] = dummy;
113 
114  // Copy in to last part of out:
115  if (in_ref.nelem())
116  out[Range(dummy.nelem(), in_ref.nelem())] = in_ref;
117 }
118 
119 
120 /* Implementation for Matrix */
121 void Append(// WS Generic Output:
122  Matrix& out,
123  // WS Generic Input:
124  const Matrix& in,
125  const String& direction,
126  const Verbosity&)
127 {
128  const Matrix* in_pnt;
129  Matrix in_copy;
130 
131  if (&in == &out)
132  {
133  in_copy = in;
134  in_pnt = &in_copy;
135  }
136  else
137  in_pnt = &in;
138 
139  const Matrix &in_ref = *in_pnt;
140 
141  // Get backup of out:
142  Matrix dummy = out;
143 
144  if (direction == "leading")
145  {
146  if (out.ncols() != in_ref.ncols())
147  throw runtime_error("Input and output matrix must have the same number of columns.");
148 
149  out.resize(dummy.nrows() + in_ref.nrows(), dummy.ncols());
150 
151  if (dummy.nrows() && dummy.ncols())
152  out(Range(0, dummy.nrows()), Range(0, dummy.ncols())) = dummy;
153  if (dummy.nrows() && in_ref.nrows() && in_ref.ncols())
154  out(Range(dummy.nrows(), in_ref.nrows()), Range(0, in_ref.ncols())) = in_ref;
155  }
156  else if (direction == "trailing")
157  {
158  if (out.nrows() != in_ref.nrows())
159  throw runtime_error("Input and output matrix must have the same number of rows.");
160 
161  out.resize(dummy.nrows(), dummy.ncols() + in_ref.ncols());
162  if (dummy.nrows() && dummy.ncols())
163  out(Range(0, dummy.nrows()), Range(0, dummy.ncols())) = dummy;
164  if (dummy.ncols() && in_ref.nrows() && in_ref.ncols())
165  out(Range(0, in_ref.nrows()), Range(dummy.ncols(), in_ref.ncols())) = in_ref;
166  }
167  else throw runtime_error("Dimension must be either \"leading\" or \"trailing\".");
168 }
169 
170 
171 /* Implementation for Matrix/Vector */
172 void Append(// WS Generic Output:
173  Matrix& out,
174  // WS Generic Input:
175  const Vector& in,
176  const String& direction,
177  const Verbosity&)
178 {
179  // Get backup of out:
180  Matrix dummy = out;
181 
182  if (direction == "leading")
183  {
184  if (out.ncols() != in.nelem())
185  throw runtime_error("Number of elements in the input Vector has to match "
186  "the number of columns in the output Matrix.");
187 
188  out.resize(dummy.nrows() + 1, dummy.ncols());
189  out(Range(0, dummy.nrows()), Range(0, dummy.ncols())) = dummy;
190  out(Range(dummy.nrows(), 1), Range(0, in.nelem())) = transpose(in);
191  }
192  else if (direction == "trailing")
193  {
194  if (in.nelem())
195  {
196  if (out.nrows() != in.nelem())
197  throw runtime_error("Number of elements in the input Vector has to match "
198  "the number of rows in the output Matrix.");
199 
200  out.resize(dummy.nrows(), dummy.ncols() + 1);
201  out(Range(0, dummy.nrows()), Range(0, dummy.ncols())) = dummy;
202  out(Range(0, in.nelem()), Range(dummy.ncols(), 1)) = in;
203  }
204  }
205  else throw runtime_error("Dimension must be either \"leading\" or \"trailing\".");
206 }
207 
208 
209 /* Implementation for Vector/Numeric */
210 void Append(// WS Generic Output:
211  Vector& out,
212  // WS Generic Input:
213  const Numeric& in,
214  const String& direction _U_,
215  const Verbosity&)
216 {
217  // Get backup of out:
218  Vector dummy = out;
219 
220  // Make out the right size:
221  out.resize(dummy.nelem() + 1);
222 
223  // Copy dummy to first part of out:
224  if (dummy.nelem())
225  out[Range(0, dummy.nelem())] = dummy;
226 
227  // Copy in to last part of out:
228  out[Range(dummy.nelem(), 1)] = in;
229 }
230 
231 
232 /* Implementation for Tensor4 */
233 void Append(// WS Generic Output:
234  Tensor4& out,
235  // WS Generic Input:
236  const Tensor4& in,
237 // const String& direction,
238  const String& direction _U_,
239  const Verbosity&)
240 {
241  const Tensor4* in_pnt;
242  Tensor4 in_copy;
243 
244  if (&in == &out)
245  {
246  in_copy = in;
247  in_pnt = &in_copy;
248  }
249  else
250  in_pnt = &in;
251 
252  const Tensor4 &in_ref = *in_pnt;
253 
254  // Get backup of out:
255  Tensor4 dummy = out;
256 
257  if (out.npages() != in_ref.npages() || out.nrows() != in_ref.nrows() ||
258  out.ncols() != in_ref.ncols())
259  throw runtime_error("Input and output Tensor4 must have the same number"
260  "of books.");
261 
262  out.resize(dummy.nbooks() + in_ref.nbooks(), dummy.npages(),
263  dummy.nrows(), dummy.ncols());
264 
265  if (dummy.nbooks() && dummy.npages() && dummy.nrows() && dummy.ncols())
266  out(Range(0, dummy.nbooks()), Range(0, dummy.npages()),
267  Range(0, dummy.nrows()), Range(0, dummy.ncols())) = dummy;
268  if (dummy.nbooks() && in_ref.nbooks() && in_ref.npages() &&
269  in_ref.nrows() && in_ref.ncols())
270  out(Range(dummy.nbooks(), in_ref.nbooks()), Range(0, in_ref.npages()),
271  Range(0, in_ref.nrows()), Range(0, in_ref.ncols())) = in_ref;
272 }
273 
274 
275 /* Implementation for String */
276 void Append(// WS Generic Output:
277  String& out,
278  // WS Generic Input:
279  const String& in,
280  const String& direction _U_,
281  const Verbosity&)
282 {
283  // String stream for easy string operations:
284  ostringstream os;
285 
286  os << out << in;
287 
288  out = os.str();
289 }
290 
291 #endif // m_append_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
ConstMatrixView transpose(ConstMatrixView m)
Const version of transpose.
Definition: matpackI.cc:1799
Index nelem() const
Number of elements.
Definition: array.h:176
The Vector class.
Definition: matpackI.h:556
The Tensor4 class.
Definition: matpackIV.h:383
The range class.
Definition: matpackI.h:148
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:69
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:75
void Append(Array< T > &out, const Array< T > &in, const String &direction, const Verbosity &)
Definition: m_append.h:41
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
This file contains the definition of Array.
The implementation for String, the ARTS string class.
Definition: mystring.h:63
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
The declarations of all the exception classes.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:29
The Matrix class.
Definition: matpackI.h:788
This can be used to make arrays out of anything.
Definition: array.h:40
void resize(Index n)
Assignment operator from VectorView.
Definition: matpackI.cc:798
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:63
#define _U_
Definition: config.h:167
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:81
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1403
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1580