annotate armadillo-2.4.4/include/armadillo_bits/subview_field_meat.hpp @ 18:8d046a9d36aa slimline

Back out rev 13:ac07c60aa798. Like an idiot, I committed a whole pile of unrelated changes in the guise of a single typo fix. Will re-commit in stages
author Chris Cannam
date Thu, 10 May 2012 10:45:44 +0100
parents 8b6102e2a9b0
children
rev   line source
max@0 1 // Copyright (C) 2008-2011 NICTA (www.nicta.com.au)
max@0 2 // Copyright (C) 2008-2011 Conrad Sanderson
max@0 3 //
max@0 4 // This file is part of the Armadillo C++ library.
max@0 5 // It is provided without any warranty of fitness
max@0 6 // for any purpose. You can redistribute this file
max@0 7 // and/or modify it under the terms of the GNU
max@0 8 // Lesser General Public License (LGPL) as published
max@0 9 // by the Free Software Foundation, either version 3
max@0 10 // of the License or (at your option) any later version.
max@0 11 // (see http://www.opensource.org/licenses for more info)
max@0 12
max@0 13
max@0 14 //! \addtogroup subview_field
max@0 15 //! @{
max@0 16
max@0 17
max@0 18 template<typename oT>
max@0 19 inline
max@0 20 subview_field<oT>::~subview_field()
max@0 21 {
max@0 22 arma_extra_debug_sigprint();
max@0 23 }
max@0 24
max@0 25
max@0 26
max@0 27 template<typename oT>
max@0 28 arma_inline
max@0 29 subview_field<oT>::subview_field
max@0 30 (
max@0 31 const field<oT>& in_f,
max@0 32 const uword in_row1,
max@0 33 const uword in_col1,
max@0 34 const uword in_n_rows,
max@0 35 const uword in_n_cols
max@0 36 )
max@0 37 : f(in_f)
max@0 38 , f_ptr(0)
max@0 39 , aux_row1(in_row1)
max@0 40 , aux_col1(in_col1)
max@0 41 , n_rows(in_n_rows)
max@0 42 , n_cols(in_n_cols)
max@0 43 , n_elem(in_n_rows*in_n_cols)
max@0 44 {
max@0 45 arma_extra_debug_sigprint();
max@0 46 }
max@0 47
max@0 48
max@0 49
max@0 50 template<typename oT>
max@0 51 arma_inline
max@0 52 subview_field<oT>::subview_field
max@0 53 (
max@0 54 field<oT>& in_f,
max@0 55 const uword in_row1,
max@0 56 const uword in_col1,
max@0 57 const uword in_n_rows,
max@0 58 const uword in_n_cols
max@0 59 )
max@0 60 : f(in_f)
max@0 61 , f_ptr(&in_f)
max@0 62 , aux_row1(in_row1)
max@0 63 , aux_col1(in_col1)
max@0 64 , n_rows(in_n_rows)
max@0 65 , n_cols(in_n_cols)
max@0 66 , n_elem(in_n_rows*in_n_cols)
max@0 67 {
max@0 68 arma_extra_debug_sigprint();
max@0 69 }
max@0 70
max@0 71
max@0 72
max@0 73 template<typename oT>
max@0 74 inline
max@0 75 void
max@0 76 subview_field<oT>::operator= (const field<oT>& x)
max@0 77 {
max@0 78 arma_extra_debug_sigprint();
max@0 79
max@0 80 subview_field<oT>& t = *this;
max@0 81
max@0 82 arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
max@0 83
max@0 84 for(uword col=0; col<t.n_cols; ++col)
max@0 85 {
max@0 86 for(uword row=0; row<t.n_rows; ++row)
max@0 87 {
max@0 88 t.at(row,col) = x.at(row,col);
max@0 89 }
max@0 90 }
max@0 91 }
max@0 92
max@0 93
max@0 94
max@0 95 //! x.subfield(...) = y.subfield(...)
max@0 96 template<typename oT>
max@0 97 inline
max@0 98 void
max@0 99 subview_field<oT>::operator= (const subview_field<oT>& x_in)
max@0 100 {
max@0 101 arma_extra_debug_sigprint();
max@0 102
max@0 103 const bool overlap = check_overlap(x_in);
max@0 104
max@0 105 field<oT>* tmp_field = overlap ? new field<oT>(x_in.f) : 0;
max@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;
max@0 107 const subview_field<oT>& x = overlap ? (*tmp_subview) : x_in;
max@0 108
max@0 109 subview_field<oT>& t = *this;
max@0 110
max@0 111 arma_debug_check( (t.n_rows != x.n_rows) || (t.n_cols != x.n_cols), "incompatible field dimensions");
max@0 112
max@0 113 for(uword col=0; col<t.n_cols; ++col)
max@0 114 {
max@0 115 for(uword row=0; row<t.n_rows; ++row)
max@0 116 {
max@0 117 t.at(row,col) = x.at(row,col);
max@0 118 }
max@0 119 }
max@0 120
max@0 121 if(overlap)
max@0 122 {
max@0 123 delete tmp_subview;
max@0 124 delete tmp_field;
max@0 125 }
max@0 126 }
max@0 127
max@0 128
max@0 129
max@0 130 template<typename oT>
max@0 131 arma_inline
max@0 132 oT&
max@0 133 subview_field<oT>::operator[](const uword i)
max@0 134 {
max@0 135 arma_check( (f_ptr == 0), "subview_field::operator[]: field is read-only");
max@0 136
max@0 137 const uword in_col = i / n_rows;
max@0 138 const uword in_row = i % n_rows;
max@0 139
max@0 140 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 141
max@0 142 return *((*f_ptr).mem[index]);
max@0 143 }
max@0 144
max@0 145
max@0 146
max@0 147 template<typename oT>
max@0 148 arma_inline
max@0 149 const oT&
max@0 150 subview_field<oT>::operator[](const uword i) const
max@0 151 {
max@0 152 const uword in_col = i / n_rows;
max@0 153 const uword in_row = i % n_rows;
max@0 154
max@0 155 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 156
max@0 157 return *(f.mem[index]);
max@0 158 }
max@0 159
max@0 160
max@0 161
max@0 162 template<typename oT>
max@0 163 arma_inline
max@0 164 oT&
max@0 165 subview_field<oT>::operator()(const uword i)
max@0 166 {
max@0 167 arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
max@0 168 arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
max@0 169
max@0 170 const uword in_col = i / n_rows;
max@0 171 const uword in_row = i % n_rows;
max@0 172
max@0 173 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 174
max@0 175 return *((*f_ptr).mem[index]);
max@0 176 }
max@0 177
max@0 178
max@0 179
max@0 180 template<typename oT>
max@0 181 arma_inline
max@0 182 const oT&
max@0 183 subview_field<oT>::operator()(const uword i) const
max@0 184 {
max@0 185 arma_debug_check( (i >= n_elem), "subview_field::operator(): index out of bounds");
max@0 186
max@0 187 const uword in_col = i / n_rows;
max@0 188 const uword in_row = i % n_rows;
max@0 189
max@0 190 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 191
max@0 192 return *(f.mem[index]);
max@0 193 }
max@0 194
max@0 195
max@0 196
max@0 197 template<typename oT>
max@0 198 arma_inline
max@0 199 oT&
max@0 200 subview_field<oT>::operator()(const uword in_row, const uword in_col)
max@0 201 {
max@0 202 arma_check( (f_ptr == 0), "subview_field::operator(): field is read-only");
max@0 203 arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
max@0 204
max@0 205 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 206
max@0 207 return *((*f_ptr).mem[index]);
max@0 208 }
max@0 209
max@0 210
max@0 211
max@0 212 template<typename oT>
max@0 213 arma_inline
max@0 214 const oT&
max@0 215 subview_field<oT>::operator()(const uword in_row, const uword in_col) const
max@0 216 {
max@0 217 arma_debug_check( ((in_row >= n_rows) || (in_col >= n_cols)), "subview_field::operator(): index out of bounds");
max@0 218
max@0 219 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 220
max@0 221 return *(f.mem[index]);
max@0 222 }
max@0 223
max@0 224
max@0 225
max@0 226 template<typename oT>
max@0 227 arma_inline
max@0 228 oT&
max@0 229 subview_field<oT>::at(const uword in_row, const uword in_col)
max@0 230 {
max@0 231 //arma_extra_debug_sigprint();
max@0 232
max@0 233 arma_check( (f_ptr == 0), "subview_field::at(): field is read-only");
max@0 234
max@0 235 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 236
max@0 237 return *((*f_ptr).mem[index]);
max@0 238 }
max@0 239
max@0 240
max@0 241
max@0 242 template<typename oT>
max@0 243 arma_inline
max@0 244 const oT&
max@0 245 subview_field<oT>::at(const uword in_row, const uword in_col) const
max@0 246 {
max@0 247 //arma_extra_debug_sigprint();
max@0 248
max@0 249 const uword index = (in_col + aux_col1)*f.n_rows + aux_row1 + in_row;
max@0 250
max@0 251 return *(f.mem[index]);
max@0 252 }
max@0 253
max@0 254
max@0 255
max@0 256 template<typename oT>
max@0 257 inline
max@0 258 bool
max@0 259 subview_field<oT>::check_overlap(const subview_field<oT>& x) const
max@0 260 {
max@0 261 const subview_field<oT>& t = *this;
max@0 262
max@0 263 if(&t.f != &x.f)
max@0 264 {
max@0 265 return false;
max@0 266 }
max@0 267 else
max@0 268 {
max@0 269 if( (t.n_elem == 0) || (x.n_elem == 0) )
max@0 270 {
max@0 271 return false;
max@0 272 }
max@0 273 else
max@0 274 {
max@0 275 const uword t_row_start = t.aux_row1;
max@0 276 const uword t_row_end_p1 = t_row_start + t.n_rows;
max@0 277
max@0 278 const uword t_col_start = t.aux_col1;
max@0 279 const uword t_col_end_p1 = t_col_start + t.n_cols;
max@0 280
max@0 281
max@0 282 const uword x_row_start = x.aux_row1;
max@0 283 const uword x_row_end_p1 = x_row_start + x.n_rows;
max@0 284
max@0 285 const uword x_col_start = x.aux_col1;
max@0 286 const uword x_col_end_p1 = x_col_start + x.n_cols;
max@0 287
max@0 288
max@0 289 const bool outside_rows = ( (x_row_start >= t_row_end_p1) || (t_row_start >= x_row_end_p1) );
max@0 290 const bool outside_cols = ( (x_col_start >= t_col_end_p1) || (t_col_start >= x_col_end_p1) );
max@0 291
max@0 292 return ( (outside_rows == false) && (outside_cols == false) );
max@0 293 }
max@0 294 }
max@0 295 }
max@0 296
max@0 297
max@0 298
max@0 299 //! X = Y.subfield(...)
max@0 300 template<typename oT>
max@0 301 inline
max@0 302 void
max@0 303 subview_field<oT>::extract(field<oT>& actual_out, const subview_field<oT>& in)
max@0 304 {
max@0 305 arma_extra_debug_sigprint();
max@0 306
max@0 307 //
max@0 308 const bool alias = (&actual_out == &in.f);
max@0 309
max@0 310 field<oT>* tmp = (alias) ? new field<oT> : 0;
max@0 311 field<oT>& out = (alias) ? (*tmp) : actual_out;
max@0 312
max@0 313 //
max@0 314
max@0 315 const uword n_rows = in.n_rows;
max@0 316 const uword n_cols = in.n_cols;
max@0 317
max@0 318 out.set_size(n_rows, n_cols);
max@0 319
max@0 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 );
max@0 321
max@0 322 for(uword col = 0; col<n_cols; ++col)
max@0 323 {
max@0 324 for(uword row = 0; row<n_rows; ++row)
max@0 325 {
max@0 326 out.at(row,col) = in.at(row,col);
max@0 327 }
max@0 328 }
max@0 329
max@0 330
max@0 331 if(alias)
max@0 332 {
max@0 333 actual_out = out;
max@0 334 delete tmp;
max@0 335 }
max@0 336
max@0 337 }
max@0 338
max@0 339
max@0 340
max@0 341 //! @}