| [dfed1c] | 1 | /**
|
|---|
| 2 | * @file grid_iterator.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Wed Apr 20 12:24:09 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Iterator class to iterate over subgrids.
|
|---|
| 7 | *
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #ifndef GRID_ITERATOR_HPP_
|
|---|
| 11 | #define GRID_ITERATOR_HPP_
|
|---|
| 12 |
|
|---|
| 13 | #include <list>
|
|---|
| 14 |
|
|---|
| 15 | #include "base/index.hpp"
|
|---|
| 16 | #include "base/object.hpp"
|
|---|
| 17 |
|
|---|
| 18 | namespace VMG
|
|---|
| 19 | {
|
|---|
| 20 |
|
|---|
| 21 | class GridIterator : public Object
|
|---|
| 22 | {
|
|---|
| 23 | public:
|
|---|
| 24 | GridIterator()
|
|---|
| 25 | {
|
|---|
| 26 | this->index = 0;
|
|---|
| 27 | this->begin = 0;
|
|---|
| 28 | this->end = 0;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | GridIterator(const Index& index, const Index& begin, const Index& end)
|
|---|
| 32 | {
|
|---|
| 33 | this->index = index;
|
|---|
| 34 | this->begin = begin;
|
|---|
| 35 | this->end = end;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | GridIterator(const GridIterator& rhs)
|
|---|
| 39 | {
|
|---|
| 40 | this->index = rhs.index;
|
|---|
| 41 | this->begin = rhs.begin;
|
|---|
| 42 | this->end = rhs.end;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | virtual ~GridIterator()
|
|---|
| 46 | {}
|
|---|
| 47 |
|
|---|
| 48 | GridIterator& operator=(const GridIterator& rhs)
|
|---|
| 49 | {
|
|---|
| 50 | this->index = rhs.index;
|
|---|
| 51 | this->begin = rhs.begin;
|
|---|
| 52 | this->end = rhs.end;
|
|---|
| 53 |
|
|---|
| 54 | return *this;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | Index& operator*();
|
|---|
| 58 | Index* operator->();
|
|---|
| 59 |
|
|---|
| 60 | bool operator==(const GridIterator& rhs) const;
|
|---|
| 61 | bool operator==(const Index& rhs) const;
|
|---|
| 62 | bool operator==(const int& rhs) const;
|
|---|
| 63 |
|
|---|
| 64 | bool operator!=(const GridIterator& rhs) const;
|
|---|
| 65 | bool operator!=(const Index& rhs) const;
|
|---|
| 66 | bool operator!=(const int& rhs) const;
|
|---|
| 67 |
|
|---|
| 68 | GridIterator& operator++();
|
|---|
| 69 | GridIterator operator++(int);
|
|---|
| 70 |
|
|---|
| 71 | GridIterator& operator--();
|
|---|
| 72 | GridIterator operator--(int);
|
|---|
| 73 |
|
|---|
| 74 | GridIterator& operator+=(const int& steps);
|
|---|
| 75 |
|
|---|
| 76 | const Index& GetIndex() const {return index;}
|
|---|
| 77 | const Index& GetBegin() const {return begin;}
|
|---|
| 78 | const Index& GetEnd() const {return end;}
|
|---|
| 79 |
|
|---|
| 80 | private:
|
|---|
| 81 | Index index;
|
|---|
| 82 | Index begin, end;
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | inline Index& GridIterator::operator*()
|
|---|
| 86 | {
|
|---|
| 87 | return this->index;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | inline Index* GridIterator::operator->()
|
|---|
| 91 | {
|
|---|
| 92 | return &*(static_cast<GridIterator>(*this));
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | inline bool GridIterator::operator==(const GridIterator& rhs) const
|
|---|
| 96 | {
|
|---|
| 97 | return this->index == rhs.index;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | inline bool GridIterator::operator==(const Index& rhs) const
|
|---|
| 101 | {
|
|---|
| 102 | return this->index == rhs;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | inline bool GridIterator::operator==(const int& rhs) const
|
|---|
| 106 | {
|
|---|
| 107 | return this->index[0] == rhs &&
|
|---|
| 108 | this->index[1] == rhs &&
|
|---|
| 109 | this->index[2] == rhs;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | inline bool GridIterator::operator!=(const GridIterator& rhs) const
|
|---|
| 113 | {
|
|---|
| 114 | return this->index != rhs.index;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | inline bool GridIterator::operator!=(const Index& rhs) const
|
|---|
| 118 | {
|
|---|
| 119 | return this->index != rhs;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | inline bool GridIterator::operator!=(const int& rhs) const
|
|---|
| 123 | {
|
|---|
| 124 | return this->index[0] != rhs ||
|
|---|
| 125 | this->index[1] != rhs ||
|
|---|
| 126 | this->index[2] != rhs;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | inline GridIterator& GridIterator::operator++()
|
|---|
| 130 | {
|
|---|
| 131 | if (++index.Z() >= end.Z()) {
|
|---|
| 132 | index.Z() = begin.Z();
|
|---|
| 133 | if (++index.Y() >= end.Y()) {
|
|---|
| 134 | index.Y() = begin.Y();
|
|---|
| 135 | if (++index.X() >= end.X())
|
|---|
| 136 | index = -1;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | return *this;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | inline GridIterator GridIterator::operator++(int)
|
|---|
| 144 | {
|
|---|
| 145 | GridIterator result = *this;
|
|---|
| 146 | ++(*this);
|
|---|
| 147 | return result;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | inline GridIterator& GridIterator::operator--()
|
|---|
| 151 | {
|
|---|
| 152 | if (--index.Z() < begin.Z()) {
|
|---|
| 153 | index.Z() = end.Z() - 1;
|
|---|
| 154 | if (--index.Y() < begin.Y()) {
|
|---|
| 155 | index.Y() = end.Y() - 1;
|
|---|
| 156 | if (--index.X() < begin.X())
|
|---|
| 157 | index = -1;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | return *this;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | inline GridIterator GridIterator::operator--(int)
|
|---|
| 165 | {
|
|---|
| 166 | GridIterator result = *this;
|
|---|
| 167 | --(*this);
|
|---|
| 168 | return result;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | inline GridIterator& GridIterator::operator+=(const int& steps)
|
|---|
| 172 | {
|
|---|
| 173 | index.Z() += steps;
|
|---|
| 174 | if (index.Z() >= end.Z()) {
|
|---|
| 175 | index.Z() = begin.Z();
|
|---|
| 176 | index.Y() += steps;
|
|---|
| 177 | if (index.Y() >= end.Y()) {
|
|---|
| 178 | index.Y() = begin.Y();
|
|---|
| 179 | index.X() += steps;
|
|---|
| 180 | if (index.X() >= end.X())
|
|---|
| 181 | index = -1;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | return *this;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | #endif /* GRID_ITERATOR_HPP_ */
|
|---|