comparison armadillo-2.4.4/include/armadillo_bits/subview_field_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
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
14 //! \addtogroup subview_field
15 //! @{
16
17
18 template<typename oT>
19 inline
20 subview_field<oT>::~subview_field()
21 {
22 arma_extra_debug_sigprint();
23 }
24
25
26
27 template<typename oT>
28 arma_inline
29 subview_field<oT>::subview_field
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 {
45 arma_extra_debug_sigprint();
46 }
47
48
49
50 template<typename oT>
51 arma_inline
52 subview_field<oT>::subview_field
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 {
68 arma_extra_debug_sigprint();
69 }
70
71
72
73 template<typename oT>
74 inline
75 void
76 subview_field<oT>::operator= (const field<oT>& x)
77 {
78 arma_extra_debug_sigprint();
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
95 //! x.subfield(...) = y.subfield(...)
96 template<typename oT>
97 inline
98 void
99 subview_field<oT>::operator= (const subview_field<oT>& x_in)
100 {
101 arma_extra_debug_sigprint();
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>
131 arma_inline
132 oT&
133 subview_field<oT>::operator[](const uword i)
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>
148 arma_inline
149 const oT&
150 subview_field<oT>::operator[](const uword i) const
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>
163 arma_inline
164 oT&
165 subview_field<oT>::operator()(const uword i)
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>
181 arma_inline
182 const oT&
183 subview_field<oT>::operator()(const uword i) const
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>
198 arma_inline
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>
213 arma_inline
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>
227 arma_inline
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>
243 arma_inline
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
259 subview_field<oT>::check_overlap(const subview_field<oT>& x) const
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
299 //! X = Y.subfield(...)
300 template<typename oT>
301 inline
302 void
303 subview_field<oT>::extract(field<oT>& actual_out, const subview_field<oT>& in)
304 {
305 arma_extra_debug_sigprint();
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
341 //! @}