| 1 | /* | 
|---|
| 2 | *    vmg - a versatile multigrid solver | 
|---|
| 3 | *    Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn | 
|---|
| 4 | * | 
|---|
| 5 | *  vmg is free software: you can redistribute it and/or modify | 
|---|
| 6 | *  it under the terms of the GNU General Public License as published by | 
|---|
| 7 | *  the Free Software Foundation, either version 3 of the License, or | 
|---|
| 8 | *  (at your option) any later version. | 
|---|
| 9 | * | 
|---|
| 10 | *  vmg is distributed in the hope that it will be useful, | 
|---|
| 11 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 13 | *  GNU General Public License for more details. | 
|---|
| 14 | * | 
|---|
| 15 | *  You should have received a copy of the GNU General Public License | 
|---|
| 16 | *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | /** | 
|---|
| 20 | * @file   grid_iterator.hpp | 
|---|
| 21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de> | 
|---|
| 22 | * @date   Wed Apr 20 12:24:09 2011 | 
|---|
| 23 | * | 
|---|
| 24 | * @brief  Iterator class to iterate over subgrids. | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | #ifndef GRID_ITERATOR_HPP_ | 
|---|
| 29 | #define GRID_ITERATOR_HPP_ | 
|---|
| 30 |  | 
|---|
| 31 | #include <list> | 
|---|
| 32 |  | 
|---|
| 33 | #include "base/index.hpp" | 
|---|
| 34 | #include "base/object.hpp" | 
|---|
| 35 |  | 
|---|
| 36 | namespace VMG | 
|---|
| 37 | { | 
|---|
| 38 |  | 
|---|
| 39 | class GridIterator : public Object | 
|---|
| 40 | { | 
|---|
| 41 | public: | 
|---|
| 42 | GridIterator() : | 
|---|
| 43 | index(0), | 
|---|
| 44 | begin(0), | 
|---|
| 45 | end(0) | 
|---|
| 46 | {} | 
|---|
| 47 |  | 
|---|
| 48 | GridIterator(const Index& index, const Index& begin, const Index& end) : | 
|---|
| 49 | index(index), | 
|---|
| 50 | begin(begin), | 
|---|
| 51 | end(end) | 
|---|
| 52 | {} | 
|---|
| 53 |  | 
|---|
| 54 | GridIterator(const GridIterator& rhs) : | 
|---|
| 55 | index(rhs.index), | 
|---|
| 56 | begin(rhs.begin), | 
|---|
| 57 | end(rhs.end) | 
|---|
| 58 | {} | 
|---|
| 59 |  | 
|---|
| 60 | virtual ~GridIterator() | 
|---|
| 61 | {} | 
|---|
| 62 |  | 
|---|
| 63 | GridIterator& operator=(const GridIterator& rhs) | 
|---|
| 64 | { | 
|---|
| 65 | this->index = rhs.index; | 
|---|
| 66 | this->begin = rhs.begin; | 
|---|
| 67 | this->end = rhs.end; | 
|---|
| 68 | return *this; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | Index& operator*(); | 
|---|
| 72 | Index* operator->(); | 
|---|
| 73 |  | 
|---|
| 74 | bool operator==(const GridIterator& rhs) const; | 
|---|
| 75 | bool operator==(const Index& rhs) const; | 
|---|
| 76 | bool operator==(const int& rhs) const; | 
|---|
| 77 |  | 
|---|
| 78 | bool operator!=(const GridIterator& rhs) const; | 
|---|
| 79 | bool operator!=(const Index& rhs) const; | 
|---|
| 80 | bool operator!=(const int& rhs) const; | 
|---|
| 81 |  | 
|---|
| 82 | GridIterator& operator++(); | 
|---|
| 83 | GridIterator operator++(int); | 
|---|
| 84 |  | 
|---|
| 85 | GridIterator& operator--(); | 
|---|
| 86 | GridIterator operator--(int); | 
|---|
| 87 |  | 
|---|
| 88 | GridIterator& operator+=(const int& steps); | 
|---|
| 89 |  | 
|---|
| 90 | const Index& GetIndex() const {return index;} | 
|---|
| 91 | const Index& GetBegin() const {return begin;} | 
|---|
| 92 | const Index& GetEnd() const {return end;} | 
|---|
| 93 |  | 
|---|
| 94 | private: | 
|---|
| 95 | Index index; | 
|---|
| 96 | Index begin, end; | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | inline Index& GridIterator::operator*() | 
|---|
| 100 | { | 
|---|
| 101 | return this->index; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | inline Index* GridIterator::operator->() | 
|---|
| 105 | { | 
|---|
| 106 | return &*(static_cast<GridIterator>(*this)); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | inline bool GridIterator::operator==(const GridIterator& rhs) const | 
|---|
| 110 | { | 
|---|
| 111 | return this->index == rhs.index; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | inline bool GridIterator::operator==(const Index& rhs) const | 
|---|
| 115 | { | 
|---|
| 116 | return this->index == rhs; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | inline bool GridIterator::operator==(const int& rhs) const | 
|---|
| 120 | { | 
|---|
| 121 | return this->index[0] == rhs && | 
|---|
| 122 | this->index[1] == rhs && | 
|---|
| 123 | this->index[2] == rhs; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | inline bool GridIterator::operator!=(const GridIterator& rhs) const | 
|---|
| 127 | { | 
|---|
| 128 | return this->index != rhs.index; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | inline bool GridIterator::operator!=(const Index& rhs) const | 
|---|
| 132 | { | 
|---|
| 133 | return this->index != rhs; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | inline bool GridIterator::operator!=(const int& rhs) const | 
|---|
| 137 | { | 
|---|
| 138 | return this->index[0] != rhs || | 
|---|
| 139 | this->index[1] != rhs || | 
|---|
| 140 | this->index[2] != rhs; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | inline GridIterator& GridIterator::operator++() | 
|---|
| 144 | { | 
|---|
| 145 | if (++index.Z() >= end.Z()) { | 
|---|
| 146 | index.Z() = begin.Z(); | 
|---|
| 147 | if (++index.Y() >= end.Y()) { | 
|---|
| 148 | index.Y() = begin.Y(); | 
|---|
| 149 | if (++index.X() >= end.X()) | 
|---|
| 150 | index = -1; | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | return *this; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | inline GridIterator GridIterator::operator++(int) | 
|---|
| 158 | { | 
|---|
| 159 | GridIterator result = *this; | 
|---|
| 160 | ++(*this); | 
|---|
| 161 | return result; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | inline GridIterator& GridIterator::operator--() | 
|---|
| 165 | { | 
|---|
| 166 | if (--index.Z() < begin.Z()) { | 
|---|
| 167 | index.Z() = end.Z() - 1; | 
|---|
| 168 | if (--index.Y() < begin.Y()) { | 
|---|
| 169 | index.Y() = end.Y() - 1; | 
|---|
| 170 | if (--index.X() < begin.X()) | 
|---|
| 171 | index = -1; | 
|---|
| 172 | } | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | return *this; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | inline GridIterator GridIterator::operator--(int) | 
|---|
| 179 | { | 
|---|
| 180 | GridIterator result = *this; | 
|---|
| 181 | --(*this); | 
|---|
| 182 | return result; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | inline GridIterator& GridIterator::operator+=(const int& steps) | 
|---|
| 186 | { | 
|---|
| 187 | index.Z() += steps; | 
|---|
| 188 | if (index.Z() >= end.Z()) { | 
|---|
| 189 | index.Z() = begin.Z(); | 
|---|
| 190 | index.Y() += steps; | 
|---|
| 191 | if (index.Y() >= end.Y()) { | 
|---|
| 192 | index.Y() = begin.Y(); | 
|---|
| 193 | index.X() += steps; | 
|---|
| 194 | if (index.X() >= end.X()) | 
|---|
| 195 | index = -1; | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | return *this; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | #endif /* GRID_ITERATOR_HPP_ */ | 
|---|