subview_field_meat.hpp
Go to the documentation of this file.
1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2008-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12 
13 
16 
17 
18 template<typename oT>
19 inline
21  {
23  }
24 
25 
26 
27 template<typename oT>
30  (
31  const field<oT>& in_f,
32  const uword in_row1,
33  const uword in_col1,
34  const uword in_n_rows,
35  const uword in_n_cols
36  )
37  : f(in_f)
38  , f_ptr(0)
39  , aux_row1(in_row1)
40  , aux_col1(in_col1)
41  , n_rows(in_n_rows)
42  , n_cols(in_n_cols)
43  , n_elem(in_n_rows*in_n_cols)
44  {
46  }
47 
48 
49 
50 template<typename oT>
53  (
54  field<oT>& in_f,
55  const uword in_row1,
56  const uword in_col1,
57  const uword in_n_rows,
58  const uword in_n_cols
59  )
60  : f(in_f)
61  , f_ptr(&in_f)
62  , aux_row1(in_row1)
63  , aux_col1(in_col1)
64  , n_rows(in_n_rows)
65  , n_cols(in_n_cols)
66  , n_elem(in_n_rows*in_n_cols)
67  {
69  }
70 
71 
72 
73 template<typename oT>
74 inline
75 void
77  {
79 
80  subview_field<oT>& t = *this;
81 
82  arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
83 
84  for(uword col=0; col<t.n_cols; ++col)
85  {
86  for(uword row=0; row<t.n_rows; ++row)
87  {
88  t.at(row,col) = x.at(row,col);
89  }
90  }
91  }
92 
93 
94 
96 template<typename oT>
97 inline
98 void
100  {
102 
103  const bool overlap = check_overlap(x_in);
104 
105  field<oT>* tmp_field = overlap ? new field<oT>(x_in.f) : 0;
106  const subview_field<oT>* tmp_subview = overlap ? new subview_field<oT>(*tmp_field, x_in.aux_row1, x_in.aux_col1, x_in.n_rows, x_in.n_cols) : 0;
107  const subview_field<oT>& x = overlap ? (*tmp_subview) : x_in;
108 
109  subview_field<oT>& t = *this;
110 
111  arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
112 
113  for(uword col=0; col<t.n_cols; ++col)
114  {
115  for(uword row=0; row<t.n_rows; ++row)
116  {
117  t.at(row,col) = x.at(row,col);
118  }
119  }
120 
121  if(overlap)
122  {
123  delete tmp_subview;
124  delete tmp_field;
125  }
126  }
127 
128 
129 
130 template<typename oT>
132 oT&
134  {
135  arma_check( (f_ptr == 0), "subview_field::operator[]: field is read-only");
136 
137  const uword in_col = i / n_rows;
138  const uword in_row = i % n_rows;
139 
140  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
141 
142  return *((*f_ptr).mem[index]);
143  }
144 
145 
146 
147 template<typename oT>
149 const oT&
151  {
152  const uword in_col = i / n_rows;
153  const uword in_row = i % n_rows;
154 
155  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
156 
157  return *(f.mem[index]);
158  }
159 
160 
161 
162 template<typename oT>
164 oT&
166  {
167  arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
168  arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
169 
170  const uword in_col = i / n_rows;
171  const uword in_row = i % n_rows;
172 
173  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
174 
175  return *((*f_ptr).mem[index]);
176  }
177 
178 
179 
180 template<typename oT>
182 const oT&
184  {
185  arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
186 
187  const uword in_col = i / n_rows;
188  const uword in_row = i % n_rows;
189 
190  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
191 
192  return *(f.mem[index]);
193  }
194 
195 
196 
197 template<typename oT>
199 oT&
200 subview_field<oT>::operator()(const uword in_row, const uword in_col)
201  {
202  arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
203  arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
204 
205  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
206 
207  return *((*f_ptr).mem[index]);
208  }
209 
210 
211 
212 template<typename oT>
214 const oT&
215 subview_field<oT>::operator()(const uword in_row, const uword in_col) const
216  {
217  arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
218 
219  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
220 
221  return *(f.mem[index]);
222  }
223 
224 
225 
226 template<typename oT>
228 oT&
229 subview_field<oT>::at(const uword in_row, const uword in_col)
230  {
231  //arma_extra_debug_sigprint();
232 
233  arma_check( (f_ptr == 0), "subview_field::at(): field is read-only");
234 
235  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
236 
237  return *((*f_ptr).mem[index]);
238  }
239 
240 
241 
242 template<typename oT>
244 const oT&
245 subview_field<oT>::at(const uword in_row, const uword in_col) const
246  {
247  //arma_extra_debug_sigprint();
248 
249  const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
250 
251  return *(f.mem[index]);
252  }
253 
254 
255 
256 template<typename oT>
257 inline
258 bool
260  {
261  const subview_field<oT>& t = *this;
262 
263  if(&t.f != &x.f)
264  {
265  return false;
266  }
267  else
268  {
269  if( (t.n_elem == 0) || (x.n_elem == 0) )
270  {
271  return false;
272  }
273  else
274  {
275  const uword t_row_start = t.aux_row1;
276  const uword t_row_end_p1 = t_row_start + t.n_rows;
277 
278  const uword t_col_start = t.aux_col1;
279  const uword t_col_end_p1 = t_col_start + t.n_cols;
280 
281 
282  const uword x_row_start = x.aux_row1;
283  const uword x_row_end_p1 = x_row_start + x.n_rows;
284 
285  const uword x_col_start = x.aux_col1;
286  const uword x_col_end_p1 = x_col_start + x.n_cols;
287 
288 
289  const bool outside_rows = ( (x_row_start >= t_row_end_p1) || (t_row_start >= x_row_end_p1) );
290  const bool outside_cols = ( (x_col_start >= t_col_end_p1) || (t_col_start >= x_col_end_p1) );
291 
292  return ( (outside_rows == false) && (outside_cols == false) );
293  }
294  }
295  }
296 
297 
298 
300 template<typename oT>
301 inline
302 void
304  {
306 
307  //
308  const bool alias = (&actual_out == &in.f);
309 
310  field<oT>* tmp = (alias) ? new field<oT> : 0;
311  field<oT>& out = (alias) ? (*tmp) : actual_out;
312 
313  //
314 
315  const uword n_rows = in.n_rows;
316  const uword n_cols = in.n_cols;
317 
318  out.set_size(n_rows, n_cols);
319 
320  arma_extra_debug_print(arma_boost::format("out.n_rows = %d out.n_cols = %d in.m.n_rows = %d in.m.n_cols = %d") % out.n_rows % out.n_cols % in.f.n_rows % in.f.n_cols );
321 
322  for(uword col = 0; col<n_cols; ++col)
323  {
324  for(uword row = 0; row<n_rows; ++row)
325  {
326  out.at(row,col) = in.at(row,col);
327  }
328  }
329 
330 
331  if(alias)
332  {
333  actual_out = out;
334  delete tmp;
335  }
336 
337  }
338 
339 
340 
arma_inline oT & operator[](const uword i)
const uword n_rows
number of rows in the field (read-only)
Definition: field_bones.hpp:37
const uword n_cols
number of columns in the field (read-only)
Definition: field_bones.hpp:38
void set_size(const uword n_obj_in)
Definition: field_meat.hpp:155
#define arma_extra_debug_print
Definition: debug.hpp:1118
u32 uword
Definition: typedef.hpp:85
arma_inline oT & operator()(const uword i)
#define arma_debug_check
Definition: debug.hpp:1084
subview_field< oT > col(const uword col_num)
creation of subview_field (column of a field)
Definition: field_meat.hpp:359
arma_inline oT & at(const uword row, const uword col)
bool check_overlap(const subview_field &x) const
const field< oT > & f
#define arma_extra_debug_sigprint
Definition: debug.hpp:1116
static void extract(field< oT > &out, const subview_field &in)
X = Y.subfield(...)
subview_field< oT > row(const uword row_num)
creation of subview_field (row of a field)
Definition: field_meat.hpp:329
#define arma_inline
const uword n_elem
number of elements in the field (read-only)
Definition: field_bones.hpp:39
void operator=(const field< oT > &x)
void arma_hot arma_check(const bool state, const T1 &x)
if state is true, abort program
Definition: debug.hpp:342
arma_inline oT & at(const uword i)
linear element accessor (treats the field as a vector); no bounds check
Definition: field_meat.hpp:217


armadillo_matrix
Author(s):
autogenerated on Fri Apr 16 2021 02:31:59