| 1 | /* |
| 2 | * grid_key_dx_iterator.hpp |
| 3 | * |
| 4 | * Created on: Dec 15, 2015 |
| 5 | * Author: i-bird |
| 6 | */ |
| 7 | |
| 8 | #ifndef OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_ |
| 9 | #define OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_ |
| 10 | |
| 11 | #include "Grid/grid_sm.hpp" |
| 12 | #include "stencil_type.hpp" |
| 13 | |
| 14 | /** |
| 15 | * |
| 16 | * Grid key class iterator, iterate through the grid element |
| 17 | * |
| 18 | * \tparam dim dimensionality of the grid |
| 19 | * \tparam stencil stencil type iterator |
| 20 | * |
| 21 | * \note if you have a grid you can get this object from getIterator() |
| 22 | * |
| 23 | * ### Grid iterator declaration and usage ### |
| 24 | * \snippet grid_sm_unit_tests.hpp Grid iterator test usage |
| 25 | * |
| 26 | */ |
| 27 | template<unsigned int dim, typename stencil=no_stencil, typename linearizer = grid_sm<dim,void>> |
| 28 | class grid_key_dx_iterator |
| 29 | { |
| 30 | #ifdef SE_CLASS1 |
| 31 | // Actual status of the iterator, when the iterator is not initialized cannot be used |
| 32 | // and reinitialize must be called |
| 33 | bool initialized = false; |
| 34 | #endif |
| 35 | |
| 36 | //! information of the grid where this iterator iterate |
| 37 | linearizer grid_base; |
| 38 | |
| 39 | /*! \brief return the index i of the gk key |
| 40 | * |
| 41 | * \param i index to get |
| 42 | * |
| 43 | * \return index value |
| 44 | * |
| 45 | */ |
| 46 | |
| 47 | inline size_t get_gk(size_t i) const |
| 48 | { |
| 49 | return gk.get(i); |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | |
| 54 | //! Actual key |
| 55 | grid_key_dx<dim> gk; |
| 56 | |
| 57 | //! Additional operation and information in case we do stencil |
| 58 | //! operations |
| 59 | stencil stl_code; |
| 60 | |
| 61 | public: |
| 62 | |
| 63 | /*! \brief Default constructor |
| 64 | * |
| 65 | * \warning entremly unsafe |
| 66 | * Before use the iterator you have call reinitialize |
| 67 | * |
| 68 | */ |
| 69 | grid_key_dx_iterator() |
| 70 | { |
| 71 | #ifdef SE_CLASS1 |
| 72 | initialized = false; |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | /*! \brief Constructor from a grid_key_dx_iterator<dim> |
| 77 | * |
| 78 | * \param g_it grid_key_dx_iterator<dim> |
| 79 | */ |
| 80 | grid_key_dx_iterator(const grid_key_dx_iterator<dim> & g_it) |
| 81 | : grid_base(g_it.grid_base) |
| 82 | { |
| 83 | //! Initialize to 0 the index |
| 84 | |
| 85 | for (size_t i = 0 ; i < dim ; i++) |
| 86 | { |
| 87 | gk.set_d(i,g_it.get_gk(i)); |
| 88 | } |
| 89 | |
| 90 | stl_code = g_it.stl_code; |
| 91 | |
| 92 | #ifdef SE_CLASS1 |
| 93 | initialized = true; |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | /*! \brief Constructor require a grid_sm<dim,T> |
| 98 | * |
| 99 | * \param g info of the grid on which iterate |
| 100 | * \param stencil_pnt stencil points |
| 101 | * |
| 102 | */ |
| 103 | template<typename grid_lin> |
| 104 | grid_key_dx_iterator(const grid_lin & g, |
| 105 | const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp]) |
| 106 | :grid_base(g) |
| 107 | { |
| 108 | reset(); |
| 109 | |
| 110 | grid_key_dx<dim> zero; |
| 111 | for (size_t i = 0 ; i < dim ; i++) {zero.set_d(i,0);} |
| 112 | |
| 113 | // calculate the offsets for the stencil code |
| 114 | stl_code.calc_offsets(g,zero,stencil_pnt); |
| 115 | |
| 116 | #ifdef SE_CLASS1 |
| 117 | initialized = true; |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | /*! \brief Constructor |
| 122 | * |
| 123 | * Using this constructor you must call reinitialize |
| 124 | * |
| 125 | * \param stencil_pnt stencil points |
| 126 | * |
| 127 | */ |
| 128 | grid_key_dx_iterator(const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp]) |
| 129 | { |
| 130 | // calculate the offsets for the stencil code |
| 131 | stl_code.set_stencil(stencil_pnt); |
| 132 | } |
| 133 | |
| 134 | /*! \brief Constructor require a grid_sm<dim,T> |
| 135 | * |
| 136 | * \param g info of the grid on which iterate |
| 137 | */ |
| 138 | grid_key_dx_iterator(const linearizer & g) |
| 139 | : grid_base(g) |
| 140 | { |
| 141 | reset(); |
| 142 | |
| 143 | #ifdef SE_CLASS1 |
| 144 | initialized = true; |
| 145 | #endif |
| 146 | } |
| 147 | |
| 148 | /*! \brief Constructor from another grid_key_dx_iterator |
| 149 | * |
| 150 | * \param key_it grid_key_dx_iterator |
| 151 | * |
| 152 | * \return itself |
| 153 | * |
| 154 | */ |
| 155 | inline grid_key_dx_iterator<dim> operator=(const grid_key_dx_iterator<dim> & key_it) |
| 156 | { |
| 157 | grid_base = key_it.grid_base; |
| 158 | |
| 159 | //! Initialize the index using key_it |
| 160 | |
| 161 | for (size_t i = 0 ; i < dim ; i++) |
| 162 | {gk.set_d(i,key_it.get_gk(i));} |
| 163 | |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | /*! \brief Get the next element |
| 168 | * |
| 169 | * \return the next grid_key |
| 170 | * |
| 171 | */ |
| 172 | |
| 173 | inline grid_key_dx_iterator<dim,stencil,linearizer> & operator++() |
| 174 | { |
| 175 | //! increment the first index |
| 176 | |
| 177 | size_t id = gk.get(0); |
| 178 | gk.set_d(0,id+1); |
| 179 | |
| 180 | stl_code.increment(); |
| 181 | |
| 182 | //! check the overflow of all the index with exception of the last dimensionality |
| 183 | |
| 184 | size_t i = 0; |
| 185 | for ( ; i < dim-1 ; i++) |
| 186 | { |
| 187 | /* coverity[dead_error_begin] */ |
| 188 | size_t id = gk.get(i); |
| 189 | if (id >= grid_base.size(i)) |
| 190 | { |
| 191 | // ! overflow, increment the next index |
| 192 | |
| 193 | size_t idr = gk.get(i); |
| 194 | gk.set_d(i,0); |
| 195 | id = gk.get(i+1); |
| 196 | gk.set_d(i+1,id+1); |
| 197 | |
| 198 | stl_code.adjust_offset(i,idr,grid_base); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return *this; |
| 207 | } |
| 208 | |
| 209 | /*! \brief Set the dimension |
| 210 | * |
| 211 | * Set the dimension |
| 212 | * |
| 213 | * \param d is the dimension |
| 214 | * \param sz set the counter to sz |
| 215 | * |
| 216 | */ |
| 217 | inline void set(int d, size_t sz) |
| 218 | { |
| 219 | // set the counter dim to sz |
| 220 | |
| 221 | gk.set_d(d,sz); |
| 222 | } |
| 223 | |
| 224 | /*! \brief Get the actual position |
| 225 | * |
| 226 | * Get the actual position |
| 227 | * |
| 228 | * \return the actual key |
| 229 | * |
| 230 | */ |
| 231 | template<unsigned int id> inline size_t getStencil() const |
| 232 | { |
| 233 | return stl_code.template getStencil<id>(); |
| 234 | } |
| 235 | |
| 236 | /*! \brief Check if there is the next element |
| 237 | * |
| 238 | * Check if there is the next element |
| 239 | * |
| 240 | * \return true if there is the next, false otherwise |
| 241 | * |
| 242 | */ |
| 243 | |
| 244 | inline bool isNext() |
| 245 | { |
| 246 | if (gk.get(dim-1) < (long int)grid_base.size(dim-1)) |
| 247 | { |
| 248 | //! we did not reach the end of the grid |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | //! we reach the end of the grid |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | /*! \brief Get the actual key |
| 258 | * |
| 259 | * Get the actual key |
| 260 | * |
| 261 | * \return the actual key |
| 262 | * |
| 263 | */ |
| 264 | inline const grid_key_dx<dim> & get() const |
| 265 | { |
| 266 | return gk; |
| 267 | } |
| 268 | |
| 269 | /*! \brief Reinitialize the grid_key_dx_iterator |
| 270 | * |
| 271 | * \param key form |
| 272 | * |
| 273 | */ |
| 274 | inline void reinitialize(const grid_key_dx_iterator<dim> & key) |
| 275 | { |
| 276 | grid_base = key.getGridInfo(); |
| 277 | reset(); |
| 278 | } |
| 279 | |
| 280 | /*! \brief Get the information about the grid |
| 281 | * |
| 282 | * \return the grid info |
| 283 | * |
| 284 | */ |
| 285 | inline const linearizer & getGridInfo() const |
| 286 | { |
| 287 | return grid_base; |
| 288 | } |
| 289 | |
| 290 | /*! \brief Reset the iterator (it restart from the beginning) |
| 291 | * |
| 292 | */ |
| 293 | inline void reset() |
| 294 | { |
| 295 | // Initialize to 0 the index |
| 296 | |
| 297 | for (size_t i = 0 ; i < dim ; i++) |
| 298 | {gk.set_d(i,0);} |
| 299 | |
| 300 | // here we check if grid have a size equal to zero or negative |
| 301 | // in this case the grid has no points |
| 302 | |
| 303 | for (size_t i = 0 ; i < dim ; i++) |
| 304 | { |
| 305 | // If the size of the grid is zero in any dimension set the iterator |
| 306 | // to the end point |
| 307 | if (grid_base.size(i) == 0) |
| 308 | gk.set_d(dim-1,grid_base.size(dim-1)); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /*! \brief Calculate the stencil offset |
| 313 | * |
| 314 | * \param start_p starting point |
| 315 | * |
| 316 | */ |
| 317 | void calc_stencil_offset(const grid_key_dx<dim> & start_p) |
| 318 | { |
| 319 | // calculate the offsets for the stencil code |
| 320 | stl_code.calc_offsets(grid_base,start_p); |
| 321 | } |
| 322 | |
| 323 | /*! \brief Return the starting point of the iteration |
| 324 | * |
| 325 | * it is always the point (0,0) in 2D (0,0,0) in 3D ... |
| 326 | * |
| 327 | */ |
| 328 | grid_key_dx<dim> getStart() |
| 329 | { |
| 330 | grid_key_dx<dim> zero; |
| 331 | zero.zero(); |
| 332 | return zero; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /*! \brief Return the stop point of the iteration |
| 337 | * |
| 338 | * |
| 339 | */ |
| 340 | grid_key_dx<dim> getStop() |
| 341 | { |
| 342 | grid_key_dx<dim> stop; |
| 343 | |
| 344 | for (size_t i = 0 ; i < dim ; i++) |
| 345 | { |
| 346 | stop.set_d(i,grid_base.size(i) - 1); |
| 347 | } |
| 348 | |
| 349 | return stop; |
| 350 | } |
| 351 | |
| 352 | /*! \brief Get the volume spanned by this sub-grid iterator |
| 353 | * |
| 354 | * \return the volume |
| 355 | * |
| 356 | */ |
| 357 | inline size_t getVolume() |
| 358 | { |
| 359 | return Box<dim,long int>::getVolumeKey(getStart().k, getStop().k); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | }; |
| 364 | |
| 365 | |
| 366 | #endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_ */ |
| 367 | |