| 1 | /* |
| 2 | * vector_map_iterator.hpp |
| 3 | * |
| 4 | * Created on: Apr 1, 2017 |
| 5 | * Author: i-bird |
| 6 | */ |
| 7 | |
| 8 | #ifndef OPENFPM_DATA_SRC_VECTOR_VECTOR_MAP_ITERATOR_HPP_ |
| 9 | #define OPENFPM_DATA_SRC_VECTOR_VECTOR_MAP_ITERATOR_HPP_ |
| 10 | |
| 11 | namespace openfpm |
| 12 | { |
| 13 | /*! \brief Vector iterator |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | class vector_key_iterator |
| 18 | { |
| 19 | //! Linearized end element |
| 20 | size_t end; |
| 21 | |
| 22 | protected: |
| 23 | |
| 24 | //! Actual key |
| 25 | size_t gk; |
| 26 | |
| 27 | public: |
| 28 | |
| 29 | /*! \brief Constructor require the size of the vector |
| 30 | * |
| 31 | * \param end point |
| 32 | * \param start starting point |
| 33 | * |
| 34 | */ |
| 35 | vector_key_iterator(size_t end, size_t start = 0) |
| 36 | : end(end),gk(start) |
| 37 | {} |
| 38 | |
| 39 | |
| 40 | /*! \brief Get the next element |
| 41 | * |
| 42 | * Get the next element |
| 43 | * |
| 44 | * \return the next grid_key |
| 45 | * |
| 46 | */ |
| 47 | vector_key_iterator & operator++() |
| 48 | { |
| 49 | //! increment the first index |
| 50 | |
| 51 | gk++; |
| 52 | |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | /*! \brief Set the dimension |
| 57 | * |
| 58 | * \param d is the dimension (IGNORED is by default 0) |
| 59 | * \param sz set the counter to sz |
| 60 | * |
| 61 | */ |
| 62 | void set(int d, size_t sz) |
| 63 | { |
| 64 | // set the counter dim to sz |
| 65 | |
| 66 | gk = sz; |
| 67 | } |
| 68 | |
| 69 | /*! \brief Check if there is the next element |
| 70 | * |
| 71 | * Check if there is the next element |
| 72 | * |
| 73 | * \return true if there is the next, false otherwise |
| 74 | * |
| 75 | */ |
| 76 | bool isNext() const |
| 77 | { |
| 78 | if (gk < end) |
| 79 | { |
| 80 | //! we did not reach the end of the grid |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | //! we reach the end of the grid |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /*! \brief Get the actual key |
| 90 | * |
| 91 | * Get the actual key |
| 92 | * |
| 93 | * \return the actual key |
| 94 | * |
| 95 | */ |
| 96 | size_t get() const |
| 97 | { |
| 98 | return gk; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | /*! \brief Vector iterator |
| 103 | * |
| 104 | * Vector iterator over a predefined sequence |
| 105 | * |
| 106 | */ |
| 107 | |
| 108 | template<typename lid> |
| 109 | class vector_key_iterator_seq |
| 110 | { |
| 111 | openfpm::vector<lid> & dp; |
| 112 | |
| 113 | protected: |
| 114 | |
| 115 | //! Actual key |
| 116 | size_t gk; |
| 117 | |
| 118 | public: |
| 119 | |
| 120 | /*! \brief Constructor require the sequence |
| 121 | * |
| 122 | * \param dp |
| 123 | * |
| 124 | */ |
| 125 | vector_key_iterator_seq(openfpm::vector<lid> & dp) |
| 126 | :dp(dp),gk(0) |
| 127 | {} |
| 128 | |
| 129 | |
| 130 | /*! \brief Get the next element |
| 131 | * |
| 132 | * Get the next element |
| 133 | * |
| 134 | * \return the next grid_key |
| 135 | * |
| 136 | */ |
| 137 | vector_key_iterator_seq<lid> & operator++() |
| 138 | { |
| 139 | //! increment the first index |
| 140 | |
| 141 | gk++; |
| 142 | |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | /*! \brief Set the dimension |
| 147 | * |
| 148 | * \param d is the dimension (IGNORED is by default 0) |
| 149 | * \param sz set the counter to sz |
| 150 | * |
| 151 | */ |
| 152 | void set(int d, size_t sz) |
| 153 | { |
| 154 | // set the counter dim to sz |
| 155 | |
| 156 | gk = sz; |
| 157 | } |
| 158 | |
| 159 | /*! \brief Check if there is the next element |
| 160 | * |
| 161 | * Check if there is the next element |
| 162 | * |
| 163 | * \return true if there is the next, false otherwise |
| 164 | * |
| 165 | */ |
| 166 | bool isNext() const |
| 167 | { |
| 168 | if (gk < dp.size()) |
| 169 | { |
| 170 | //! we did not reach the end of the grid |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | //! we reach the end of the grid |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | /*! \brief Get the actual key |
| 180 | * |
| 181 | * Get the actual key |
| 182 | * |
| 183 | * \return the actual key |
| 184 | * |
| 185 | */ |
| 186 | size_t get() const |
| 187 | { |
| 188 | return dp.get(gk); |
| 189 | } |
| 190 | }; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | #endif /* OPENFPM_DATA_SRC_VECTOR_VECTOR_MAP_ITERATOR_HPP_ */ |
| 195 | |