| 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   index.hpp | 
|---|
| 21 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de> | 
|---|
| 22 | * @date   Mon Apr 18 12:19:49 2011 | 
|---|
| 23 | * | 
|---|
| 24 | * @brief  Header file for the class VMG::Index. | 
|---|
| 25 | * | 
|---|
| 26 | */ | 
|---|
| 27 |  | 
|---|
| 28 | #ifndef INDEX_HPP_ | 
|---|
| 29 | #define INDEX_HPP_ | 
|---|
| 30 |  | 
|---|
| 31 | #include <algorithm> | 
|---|
| 32 | #include <cmath> | 
|---|
| 33 | #include <cstdlib> | 
|---|
| 34 | #include <cstring> | 
|---|
| 35 | #include <iostream> | 
|---|
| 36 |  | 
|---|
| 37 | namespace VMG | 
|---|
| 38 | { | 
|---|
| 39 |  | 
|---|
| 40 | class Vector; | 
|---|
| 41 |  | 
|---|
| 42 | class Index | 
|---|
| 43 | { | 
|---|
| 44 | public: | 
|---|
| 45 | Index(const Index& rhs); | 
|---|
| 46 | Index(const Vector& vec); | 
|---|
| 47 | Index(int x, int y, int z) {i[0]=x;i[1]=y;i[2]=z;} | 
|---|
| 48 | Index(int val) {i[0]=val;i[1]=val;i[2]=val;} | 
|---|
| 49 | Index(int* arr) {i[0]=arr[0];i[1]=arr[1];i[2]=arr[2];} | 
|---|
| 50 | Index() {i[0]=0;i[1]=0;i[2]=0;} | 
|---|
| 51 |  | 
|---|
| 52 | int& operator[](const int& index) {return i[index];} | 
|---|
| 53 | const int& operator[](const int& index) const {return i[index];} | 
|---|
| 54 |  | 
|---|
| 55 | int& X() {return i[0];} | 
|---|
| 56 | int& Y() {return i[1];} | 
|---|
| 57 | int& Z() {return i[2];} | 
|---|
| 58 |  | 
|---|
| 59 | const int& X() const {return i[0];} | 
|---|
| 60 | const int& Y() const {return i[1];} | 
|---|
| 61 | const int& Z() const {return i[2];} | 
|---|
| 62 |  | 
|---|
| 63 | Index Abs() {return Index(abs(i[0]), abs(i[1]), abs(i[2]));} | 
|---|
| 64 | int Max() const {return std::max(std::max(i[0],i[1]),i[2]);} | 
|---|
| 65 | int Min() const {return std::min(std::min(i[0],i[1]),i[2]);} | 
|---|
| 66 | int Product() const {return i[0]*i[1]*i[2];} | 
|---|
| 67 | int Sum() const {return i[0]+i[1]+i[2];} | 
|---|
| 68 |  | 
|---|
| 69 | bool IsInBounds(const Index& begin, const Index& end) const | 
|---|
| 70 | { | 
|---|
| 71 | return i[0] >= begin[0] && i[0] < end[0] && | 
|---|
| 72 | i[1] >= begin[1] && i[1] < end[1] && | 
|---|
| 73 | i[2] >= begin[2] && i[2] < end[2]; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | bool IsComponentwiseLess(const Index& other) const | 
|---|
| 77 | { | 
|---|
| 78 | return this->i[0] < other.i[0] | 
|---|
| 79 | && this->i[1] < other.i[1] | 
|---|
| 80 | && this->i[2] < other.i[2]; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | bool IsComponentwiseLessOrEqual(const Index& other) const | 
|---|
| 84 | { | 
|---|
| 85 | return this->i[0] <= other.i[0] | 
|---|
| 86 | && this->i[1] <= other.i[1] | 
|---|
| 87 | && this->i[2] <= other.i[2]; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | bool IsComponentwiseGreater(const Index& other) const | 
|---|
| 91 | { | 
|---|
| 92 | return this->i[0] > other.i[0] | 
|---|
| 93 | && this->i[1] > other.i[1] | 
|---|
| 94 | && this->i[2] > other.i[2]; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | bool IsComponentwiseGreaterOrEqual(const Index& other) const | 
|---|
| 98 | { | 
|---|
| 99 | return this->i[0] >= other.i[0] | 
|---|
| 100 | && this->i[1] >= other.i[1] | 
|---|
| 101 | && this->i[2] >= other.i[2]; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | Index MaxComponentwise(const Index& rhs) const | 
|---|
| 105 | { | 
|---|
| 106 | return Index(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2])); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | Index MinComponentwise(const Index& rhs) const | 
|---|
| 110 | { | 
|---|
| 111 | return Index(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2])); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | Index Clamp(const Index& lower_bound, const Index& upper_bound) const | 
|---|
| 115 | { | 
|---|
| 116 | Index index(*this); | 
|---|
| 117 | for (int j=0; j<3; ++j) { | 
|---|
| 118 | index.i[j] = std::max(index.i[j], lower_bound[j]); | 
|---|
| 119 | index.i[j] = std::min(index.i[j], upper_bound[j]); | 
|---|
| 120 | } | 
|---|
| 121 | return index; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | int* vec() {return i;} | 
|---|
| 125 | const int* vec() const {return i;} | 
|---|
| 126 |  | 
|---|
| 127 | Index& operator=(const Index& rhs) | 
|---|
| 128 | { | 
|---|
| 129 | i[0] = rhs.X(); | 
|---|
| 130 | i[1] = rhs.Y(); | 
|---|
| 131 | i[2] = rhs.Z(); | 
|---|
| 132 | return *this; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | Index& operator=(const int& rhs) | 
|---|
| 136 | { | 
|---|
| 137 | i[0] = rhs; | 
|---|
| 138 | i[1] = rhs; | 
|---|
| 139 | i[2] = rhs; | 
|---|
| 140 | return *this; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | bool operator==(const Index& other) const | 
|---|
| 144 | { | 
|---|
| 145 | return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z()); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | bool operator<(const Index& other) const | 
|---|
| 149 | { | 
|---|
| 150 | for (int j=0; j<3; ++j) { | 
|---|
| 151 | if (this->i[j] < other.i[j]) return true; | 
|---|
| 152 | if (this->i[j] != other.i[j]) return false; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | return false; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | bool operator!=(const Index& other) const | 
|---|
| 159 | { | 
|---|
| 160 | return !(*this == other); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | Index& operator+=(const Index& rhs) | 
|---|
| 164 | { | 
|---|
| 165 | i[0] += rhs.X(); | 
|---|
| 166 | i[1] += rhs.Y(); | 
|---|
| 167 | i[2] += rhs.Z(); | 
|---|
| 168 | return *this; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | Index& operator-=(const Index& rhs) | 
|---|
| 172 | { | 
|---|
| 173 | i[0] -= rhs.X(); | 
|---|
| 174 | i[1] -= rhs.Y(); | 
|---|
| 175 | i[2] -= rhs.Z(); | 
|---|
| 176 | return *this; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | Index& operator*=(const Index& rhs) | 
|---|
| 180 | { | 
|---|
| 181 | i[0] *= rhs.X(); | 
|---|
| 182 | i[1] *= rhs.Y(); | 
|---|
| 183 | i[2] *= rhs.Z(); | 
|---|
| 184 | return *this; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | Index& operator/=(const Index& rhs) | 
|---|
| 188 | { | 
|---|
| 189 | i[0] /= rhs.X(); | 
|---|
| 190 | i[1] /= rhs.Y(); | 
|---|
| 191 | i[2] /= rhs.Z(); | 
|---|
| 192 | return *this; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | Index& operator%=(const Index& rhs) | 
|---|
| 196 | { | 
|---|
| 197 | i[0] %= rhs.X(); | 
|---|
| 198 | i[1] %= rhs.Y(); | 
|---|
| 199 | i[2] %= rhs.Z(); | 
|---|
| 200 | return *this; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | Index& operator+=(const int& rhs) | 
|---|
| 204 | { | 
|---|
| 205 | i[0] += rhs; | 
|---|
| 206 | i[1] += rhs; | 
|---|
| 207 | i[2] += rhs; | 
|---|
| 208 | return *this; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | Index& operator-=(const int& rhs) | 
|---|
| 212 | { | 
|---|
| 213 | i[0] -= rhs; | 
|---|
| 214 | i[1] -= rhs; | 
|---|
| 215 | i[2] -= rhs; | 
|---|
| 216 | return *this; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | Index& operator*=(const int& rhs) | 
|---|
| 220 | { | 
|---|
| 221 | i[0] *= rhs; | 
|---|
| 222 | i[1] *= rhs; | 
|---|
| 223 | i[2] *= rhs; | 
|---|
| 224 | return *this; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | Index& operator/=(const int& rhs) | 
|---|
| 228 | { | 
|---|
| 229 | i[0] /= rhs; | 
|---|
| 230 | i[1] /= rhs; | 
|---|
| 231 | i[2] /= rhs; | 
|---|
| 232 | return *this; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | Index& operator%=(const int& rhs) | 
|---|
| 236 | { | 
|---|
| 237 | i[0] %= rhs; | 
|---|
| 238 | i[1] %= rhs; | 
|---|
| 239 | i[2] %= rhs; | 
|---|
| 240 | return *this; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | Index operator+(const Index& rhs) const | 
|---|
| 244 | { | 
|---|
| 245 | return Index(*this) += rhs; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | Index operator-(const Index& rhs) const | 
|---|
| 249 | { | 
|---|
| 250 | return Index(*this) -= rhs; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | Index operator*(const Index& rhs) const | 
|---|
| 254 | { | 
|---|
| 255 | return Index(*this) *= rhs; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | Index operator/(const Index& rhs) const | 
|---|
| 259 | { | 
|---|
| 260 | return Index(*this) /= rhs; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | Index operator%(const Index& rhs) const | 
|---|
| 264 | { | 
|---|
| 265 | return Index(*this) %= rhs; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | Index operator+(const int& rhs) const | 
|---|
| 269 | { | 
|---|
| 270 | return Index(*this) += rhs; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | Index operator-(const int& rhs) const | 
|---|
| 274 | { | 
|---|
| 275 | return Index(*this) -= rhs; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | Index operator*(const int& rhs) const | 
|---|
| 279 | { | 
|---|
| 280 | return Index(*this) *= rhs; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | Index operator/(const int& rhs) const | 
|---|
| 284 | { | 
|---|
| 285 | return Index(*this) /= rhs; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | Index operator%(const int& rhs) const | 
|---|
| 289 | { | 
|---|
| 290 | return Index(*this) %= rhs; | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | Vector operator+(const Vector& rhs) const; | 
|---|
| 294 | Vector operator-(const Vector& rhs) const; | 
|---|
| 295 | Vector operator*(const Vector& rhs) const; | 
|---|
| 296 | Vector operator/(const Vector& rhs) const; | 
|---|
| 297 |  | 
|---|
| 298 | Vector operator+(const vmg_float& rhs) const; | 
|---|
| 299 | Vector operator-(const vmg_float& rhs) const; | 
|---|
| 300 | Vector operator*(const vmg_float& rhs) const; | 
|---|
| 301 | Vector operator/(const vmg_float& rhs) const; | 
|---|
| 302 |  | 
|---|
| 303 | private: | 
|---|
| 304 | int i[3]; | 
|---|
| 305 | }; | 
|---|
| 306 |  | 
|---|
| 307 | inline Index operator+(const int& lhs, const Index& rhs) | 
|---|
| 308 | { | 
|---|
| 309 | return Index(lhs) += rhs; | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | inline Index operator-(const int& lhs, const Index& rhs) | 
|---|
| 313 | { | 
|---|
| 314 | return Index(lhs) -= rhs; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | inline Index operator*(const int& lhs, const Index& rhs) | 
|---|
| 318 | { | 
|---|
| 319 | return Index(lhs) *= rhs; | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | inline Index operator/(const int& lhs, const Index& rhs) | 
|---|
| 323 | { | 
|---|
| 324 | return Index(lhs) /= rhs; | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 | inline Index operator%(const int& lhs, const Index& rhs) | 
|---|
| 328 | { | 
|---|
| 329 | return Index(lhs) %= rhs; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | const Index Max(const Index& index1, const Index& index2); | 
|---|
| 333 | std::ostream& operator<<(std::ostream& out, const Index& index); | 
|---|
| 334 |  | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | #endif /* INDEX_HPP_ */ | 
|---|