├── LICENSE ├── README.md └── orca_array.hpp /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Pramod Gupta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | #orca_array 3 | 4 | Author: Pramod Gupta, Department of Astronomy, University of Washington 5 | 6 | Last Modified: 2020 Oct 26 7 | 8 | orca_array consists of multi-dimensional array template classes array1d to array7d 9 | with compile time options for array bounds checking and 10 | for accessing array elements via Fortran order or C order. 11 | 12 | See orca_array.hpp for the code. 13 | 14 | 15 | **(1) Why are orca_array arrays only up to only upto 7 dimensions?** 16 | 17 | * In classical physics, to describe a particle you need 3 space coordinates (x,y,z), 3 velocities (v_x, v_y, v_z) and time t. 18 | * More than 7 dimensional arrays are very rare since memory usage increases exponentially like L^d 19 | where L is the size of one dimension and d is the number of dimensions. 20 | * If you need more dimensions, the array7d code can easily be modified for arrayNd. 21 | 22 | **(2) What compilers can compile orca_array.hpp?** 23 | 24 | orca_array.hpp has been compiled with g++, clang++, and icpc. 25 | 26 | 27 | **(3) How can one choose between column-major and row-major order of accessing arrays?** 28 | 29 | Choose one of below options in orca_array.hpp: 30 | 31 | ```C++ 32 | //First index changes fastest 33 | #define FORTRAN_ORDER 1 34 | 35 | //Last index changes fastest (C order) 36 | #define FORTRAN_ORDER 0 37 | 38 | ``` 39 | 40 | 41 | **(4) Can you show examples of using orca_array arrays?** 42 | 43 | ```C++ 44 | #include "orca_array.hpp" 45 | using namespace orca_array; 46 | 47 | //defining 2D array 48 | array2d b(10,8); 49 | 50 | //using 2D array 51 | b.at(3,4)=3.14; 52 | 53 | //Copy constructor and assignment operator are private. 54 | //Hence pass all orca_arrays to a function by reference. 55 | 56 | //passing 2D array to function 57 | //m, n are optional 58 | double trace(int m, int n, array2d & myarray2d){ 59 | . . . 60 | } 61 | 62 | ``` 63 | 64 | 65 | 66 | **(5) How can one turn on or turn off array bounds checking?** 67 | 68 | Choose one of the below options in orca_array.hpp: 69 | 70 | ```C++ 71 | #define ARRAY_BOUNDS_CHECK 1 72 | 73 | #define ARRAY_BOUNDS_CHECK 0 74 | ``` 75 | 76 | If ARRAY_BOUNDS_CHECK is 1 and during run time an array index is out of bounds then the program will encounter a segmentation fault. 77 | 78 | Compile the program with the -g option and run in the debugger to identify the line of code as shown below. 79 | 80 | Suppose a program xyz.cpp uses orca_array.hpp. 81 | If the program xyz.cpp has a bug and it runs into a segmentation fault 82 | due to array out of bounds or negative sizes of dimensions then use the 83 | below steps to identify the buggy line of code. 84 | 85 | 86 | GNU Compiler: 87 | ``` 88 | g++ -g xyz.cpp -o xyz 89 | gdb xyz 90 | run 91 | bt (to get the backtrace and find the line of code) 92 | 93 | ``` 94 | -------------------------------------------------------------------------------- /orca_array.hpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////// 2 | // 3 | // File: orca_array.hpp 4 | // Author: Pramod Gupta, Department of Astronomy, University of Washington 5 | // Last Modified: 2020 Oct 26 6 | // 7 | // Multi-dimensional array template classes array1d to array7d 8 | // Compile time option for array bounds checking and 9 | // for accessing array elements via Fortran order or C order. 10 | // 11 | // See orca_array_readme for usage. 12 | /////////////////////////////////////////////////////////////////////////// 13 | 14 | #ifndef ORCA_ARRAY 15 | #define ORCA_ARRAY 16 | 17 | // Choose 0 or 1 for below options 18 | #define ARRAY_BOUNDS_CHECK 0 19 | #define FORTRAN_ORDER 0 20 | 21 | ////////////////////////////////////////////////////////////////////////////// 22 | // Notes: 23 | // Copy constructor and assignment operator are private. 24 | // Hence pass all orca_arrays to a function by reference . 25 | 26 | // All member functions are defined within the class so they are inline 27 | // by default. However, we still label the at() function inline as a 28 | // reminder. 29 | /////////////////////////////////////////////////////////////////////////////// 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | namespace orca_array { 39 | 40 | //////////////// start class array1d ///////////////////// 41 | template class array1d { 42 | 43 | private: 44 | int size1; 45 | array_element_type *internal_array; 46 | 47 | public: 48 | inline int length1(void) const { return size1; } 49 | 50 | inline array_element_type &at(int x1) { 51 | #if ARRAY_BOUNDS_CHECK == 1 52 | 53 | if ((x1 < 0) || (x1 >= size1)) { 54 | 55 | printf("index x1 is less than 0 or equal to size1 or greater than " 56 | "size1\n"); 57 | printf("x1=%d \n", x1); 58 | printf("size1=%d \n", size1); 59 | printf("file %s, line %d.\n", __FILE__, __LINE__); 60 | raise(SIGSEGV); 61 | } 62 | #endif 63 | 64 | return internal_array[x1]; 65 | } 66 | 67 | // overloaded at() const 68 | inline const array_element_type &at(int x1) const { 69 | #if ARRAY_BOUNDS_CHECK == 1 70 | 71 | if ((x1 < 0) || (x1 >= size1)) { 72 | 73 | printf("index x1 is less than 0 or equal to size1 or greater than " 74 | "size1\n"); 75 | printf("x1=%d \n", x1); 76 | printf("size1=%d \n", size1); 77 | printf("file %s, line %d.\n", __FILE__, __LINE__); 78 | raise(SIGSEGV); 79 | } 80 | #endif 81 | 82 | return internal_array[x1]; 83 | } 84 | 85 | // constructor 86 | array1d(int dim1) { 87 | 88 | if (dim1 <= 0) { 89 | printf("dim1 is less than or equal to 0\n"); 90 | printf("dim1=%d \n", dim1); 91 | printf("file %s, line %d.\n", __FILE__, __LINE__); 92 | raise(SIGSEGV); 93 | } else { 94 | size1 = dim1; 95 | internal_array = new array_element_type[size1]; 96 | } 97 | } 98 | 99 | // destructor 100 | ~array1d() { delete[] internal_array; } 101 | 102 | // note that even though array1d is a template, inside defintion of array1d 103 | // array1d means same as array1d 104 | private: 105 | // prohibit copy constructor 106 | array1d(array1d &); 107 | 108 | // prohibit assignment operator 109 | array1d &operator=(array1d &); 110 | }; 111 | 112 | ////////////// end class array1d ///////////////////// 113 | 114 | //////////////// start class array2d ///////////////////// 115 | 116 | template class array2d { 117 | 118 | private: 119 | // number of rows 120 | int size1; 121 | // number of columns 122 | int size2; 123 | 124 | array_element_type *internal_array; 125 | 126 | public: 127 | inline int length1(void) const { return size1; } 128 | 129 | inline int length2(void) const { return size2; } 130 | 131 | inline array_element_type &at(int x1, int x2) { 132 | 133 | #if ARRAY_BOUNDS_CHECK == 1 134 | 135 | if ((x1 < 0) || (x1 >= size1)) { 136 | 137 | printf("index x1 is less than 0 or equal to size1 or greater than " 138 | "size1\n"); 139 | printf("x1=%d \n", x1); 140 | printf("size1=%d \n", size1); 141 | printf("file %s, line %d.\n", __FILE__, __LINE__); 142 | raise(SIGSEGV); 143 | } 144 | 145 | if ((x2 < 0) || (x2 >= size2)) { 146 | 147 | printf("index x2 is less than 0 or equal to size2 or greater than " 148 | "size2\n"); 149 | printf("x2=%d \n", x2); 150 | printf("size2=%d \n", size2); 151 | printf("file %s, line %d.\n", __FILE__, __LINE__); 152 | raise(SIGSEGV); 153 | } 154 | 155 | #endif 156 | 157 | // x1 is row number and x2 is column number 158 | 159 | #if FORTRAN_ORDER == 1 160 | // fortran convention 161 | // first index changes fastest 162 | return internal_array[x2 * size1 + x1]; 163 | #else 164 | // C convention 165 | // last index changes fastest 166 | return internal_array[x1 * size2 + x2]; 167 | #endif 168 | } 169 | 170 | // overloaded at() const 171 | inline const array_element_type &at(int x1, int x2) const { 172 | 173 | #if ARRAY_BOUNDS_CHECK == 1 174 | 175 | if ((x1 < 0) || (x1 >= size1)) { 176 | 177 | printf("index x1 is less than 0 or equal to size1 or greater than " 178 | "size1\n"); 179 | printf("x1=%d \n", x1); 180 | printf("size1=%d \n", size1); 181 | printf("file %s, line %d.\n", __FILE__, __LINE__); 182 | raise(SIGSEGV); 183 | } 184 | 185 | if ((x2 < 0) || (x2 >= size2)) { 186 | 187 | printf("index x2 is less than 0 or equal to size2 or greater than " 188 | "size2\n"); 189 | printf("x2=%d \n", x2); 190 | printf("size2=%d \n", size2); 191 | printf("file %s, line %d.\n", __FILE__, __LINE__); 192 | raise(SIGSEGV); 193 | } 194 | 195 | #endif 196 | 197 | // x1 is row number and x2 is column number 198 | 199 | #if FORTRAN_ORDER == 1 200 | // fortran convention 201 | // first index changes fastest 202 | return internal_array[x2 * size1 + x1]; 203 | #else 204 | // C convention 205 | // last index changes fastest 206 | return internal_array[x1 * size2 + x2]; 207 | #endif 208 | } 209 | 210 | // constructor 211 | array2d(int dim1, int dim2) { 212 | 213 | if (dim1 <= 0) { 214 | printf("dim1 is less than or equal to 0\n"); 215 | printf("dim1=%d \n", dim1); 216 | printf("file %s, line %d.\n", __FILE__, __LINE__); 217 | raise(SIGSEGV); 218 | } else if (dim2 <= 0) { 219 | printf("dim2 is less than or equal to 0\n"); 220 | printf("dim2=%d \n", dim2); 221 | printf("file %s, line %d.\n", __FILE__, __LINE__); 222 | raise(SIGSEGV); 223 | } else { 224 | size1 = dim1; 225 | size2 = dim2; 226 | internal_array = new array_element_type[size1 * size2]; 227 | } 228 | } 229 | 230 | // destructor 231 | ~array2d() { delete[] internal_array; } 232 | 233 | // note that even though array2d is a template, inside defintion of array2d 234 | // array2d means same as array2d 235 | private: 236 | // prohibit copy constructor 237 | array2d(array2d &); 238 | 239 | // prohibit assignment operator 240 | array2d &operator=(array2d &); 241 | }; 242 | 243 | ////////////// end class array2d ///////////////////// 244 | 245 | //////////////// start class array3d ///////////////////// 246 | template class array3d { 247 | 248 | private: 249 | // x-axis 250 | int size1; 251 | // y-axis 252 | int size2; 253 | // z-axis (think of like floor in a building) 254 | int size3; 255 | 256 | // size2size1 and size2size3 did not improve performance 257 | ////but this approach may improve performance for higher dimensions 258 | // size2size1=size2*size1 in constructor 259 | // int size2size1; 260 | 261 | // size2size3=size2*size3 in constructor 262 | // int size2size3; 263 | 264 | array_element_type *internal_array; 265 | 266 | // factors for Fortran order 267 | int F1, F2, F3; 268 | 269 | // factors for C order 270 | int C1, C2, C3; 271 | 272 | public: 273 | inline int length1(void) const { return size1; } 274 | 275 | inline int length2(void) const { return size2; } 276 | 277 | inline int length3(void) const { return size3; } 278 | 279 | inline array_element_type &at(int x1, int x2, int x3) { 280 | 281 | #if ARRAY_BOUNDS_CHECK == 1 282 | 283 | if ((x1 < 0) || (x1 >= size1)) { 284 | 285 | printf("index x1 is less than 0 or equal to size1 or greater than " 286 | "size1\n"); 287 | printf("x1=%d \n", x1); 288 | printf("size1=%d \n", size1); 289 | printf("file %s, line %d.\n", __FILE__, __LINE__); 290 | raise(SIGSEGV); 291 | } 292 | 293 | if ((x2 < 0) || (x2 >= size2)) { 294 | 295 | printf("index x2 is less than 0 or equal to size2 or greater than " 296 | "size2\n"); 297 | printf("x2=%d \n", x2); 298 | printf("size2=%d \n", size2); 299 | printf("file %s, line %d.\n", __FILE__, __LINE__); 300 | raise(SIGSEGV); 301 | } 302 | 303 | if ((x3 < 0) || (x3 >= size3)) { 304 | 305 | printf("index x3 is less than 0 or equal to size3 or greater than " 306 | "size3\n"); 307 | printf("x3=%d \n", x3); 308 | printf("size3=%d \n", size3); 309 | printf("file %s, line %d.\n", __FILE__, __LINE__); 310 | raise(SIGSEGV); 311 | } 312 | 313 | #endif 314 | 315 | #if FORTRAN_ORDER == 1 316 | // fortran convention 317 | // first index changes fastest 318 | // return internal_array[x3*size2*size1 + x2*size1 + x1]; 319 | return internal_array[x3 * F3 + x2 * F2 + x1 * F1]; 320 | #else 321 | // C convention 322 | // last index changes fastest 323 | // return internal_array[x1*size2*size3 + x2*size3 + x3]; 324 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3]; 325 | 326 | #endif 327 | } 328 | 329 | // overloaded at() const 330 | inline const array_element_type &at(int x1, int x2, int x3) const { 331 | 332 | #if ARRAY_BOUNDS_CHECK == 1 333 | 334 | if ((x1 < 0) || (x1 >= size1)) { 335 | 336 | printf("index x1 is less than 0 or equal to size1 or greater than " 337 | "size1\n"); 338 | printf("x1=%d \n", x1); 339 | printf("size1=%d \n", size1); 340 | printf("file %s, line %d.\n", __FILE__, __LINE__); 341 | raise(SIGSEGV); 342 | } 343 | 344 | if ((x2 < 0) || (x2 >= size2)) { 345 | 346 | printf("index x2 is less than 0 or equal to size2 or greater than " 347 | "size2\n"); 348 | printf("x2=%d \n", x2); 349 | printf("size2=%d \n", size2); 350 | printf("file %s, line %d.\n", __FILE__, __LINE__); 351 | raise(SIGSEGV); 352 | } 353 | 354 | if ((x3 < 0) || (x3 >= size3)) { 355 | 356 | printf("index x3 is less than 0 or equal to size3 or greater than " 357 | "size3\n"); 358 | printf("x3=%d \n", x3); 359 | printf("size3=%d \n", size3); 360 | printf("file %s, line %d.\n", __FILE__, __LINE__); 361 | raise(SIGSEGV); 362 | } 363 | 364 | #endif 365 | 366 | #if FORTRAN_ORDER == 1 367 | // fortran convention 368 | // first index changes fastest 369 | // return internal_array[x3*size2*size1 + x2*size1 + x1]; 370 | return internal_array[x3 * F3 + x2 * F2 + x1 * F1]; 371 | #else 372 | // C convention 373 | // last index changes fastest 374 | // return internal_array[x1*size2*size3 + x2*size3 + x3]; 375 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3]; 376 | 377 | #endif 378 | } 379 | 380 | // constructor 381 | array3d(int dim1, int dim2, int dim3) { 382 | 383 | if (dim1 <= 0) { 384 | printf("dim1 is less than or equal to 0\n"); 385 | printf("dim1=%d \n", dim1); 386 | printf("file %s, line %d.\n", __FILE__, __LINE__); 387 | raise(SIGSEGV); 388 | } else if (dim2 <= 0) { 389 | printf("dim2 is less than or equal to 0\n"); 390 | printf("dim2=%d \n", dim2); 391 | printf("file %s, line %d.\n", __FILE__, __LINE__); 392 | raise(SIGSEGV); 393 | } else if (dim3 <= 0) { 394 | printf("dim3 is less than or equal to 0\n"); 395 | printf("dim3=%d \n", dim3); 396 | printf("file %s, line %d.\n", __FILE__, __LINE__); 397 | raise(SIGSEGV); 398 | } else { 399 | size1 = dim1; 400 | size2 = dim2; 401 | size3 = dim3; 402 | 403 | F3 = size2 * size1; 404 | F2 = size1; 405 | F1 = 1; 406 | 407 | C1 = size2 * size3; 408 | C2 = size3; 409 | C3 = 1; 410 | 411 | internal_array = new array_element_type[size1 * size2 * size3]; 412 | } 413 | } 414 | 415 | // destructor 416 | ~array3d() { delete[] internal_array; } 417 | 418 | // note that even though array3d is a template, inside defintion of array3d 419 | // array3d means same as array3d 420 | private: 421 | // prohibit copy constructor 422 | array3d(array3d &); 423 | 424 | // prohibit assignment operator 425 | array3d &operator=(array3d &); 426 | }; 427 | 428 | ////////////// end class array3d ///////////////////// 429 | 430 | //////////////// start class array4d ///////////////////// 431 | template class array4d { 432 | 433 | private: 434 | // x-axis 435 | int size1; 436 | // y-axis 437 | int size2; 438 | // z-axis (think of like floor in a building) 439 | int size3; 440 | // t-axis 441 | int size4; 442 | 443 | array_element_type *internal_array; 444 | 445 | // factors for Fortran order 446 | int F1, F2, F3, F4; 447 | 448 | // factors for C order 449 | int C1, C2, C3, C4; 450 | 451 | public: 452 | inline int length1(void) const { return size1; } 453 | 454 | inline int length2(void) const { return size2; } 455 | 456 | inline int length3(void) const { return size3; } 457 | 458 | inline int length4(void) const { return size4; } 459 | 460 | inline array_element_type &at(int x1, int x2, int x3, int x4) { 461 | 462 | #if ARRAY_BOUNDS_CHECK == 1 463 | 464 | if ((x1 < 0) || (x1 >= size1)) { 465 | 466 | printf("index x1 is less than 0 or equal to size1 or greater than " 467 | "size1\n"); 468 | printf("x1=%d \n", x1); 469 | printf("size1=%d \n", size1); 470 | printf("file %s, line %d.\n", __FILE__, __LINE__); 471 | raise(SIGSEGV); 472 | } 473 | 474 | if ((x2 < 0) || (x2 >= size2)) { 475 | 476 | printf("index x2 is less than 0 or equal to size2 or greater than " 477 | "size2\n"); 478 | printf("x2=%d \n", x2); 479 | printf("size2=%d \n", size2); 480 | printf("file %s, line %d.\n", __FILE__, __LINE__); 481 | raise(SIGSEGV); 482 | } 483 | 484 | if ((x3 < 0) || (x3 >= size3)) { 485 | 486 | printf("index x3 is less than 0 or equal to size3 or greater than " 487 | "size3\n"); 488 | printf("x3=%d \n", x3); 489 | printf("size3=%d \n", size3); 490 | printf("file %s, line %d.\n", __FILE__, __LINE__); 491 | raise(SIGSEGV); 492 | } 493 | 494 | if ((x4 < 0) || (x4 >= size4)) { 495 | 496 | printf("index x4 is less than 0 or equal to size4 or greater than " 497 | "size4\n"); 498 | printf("x4=%d \n", x4); 499 | printf("size4=%d \n", size4); 500 | printf("file %s, line %d.\n", __FILE__, __LINE__); 501 | raise(SIGSEGV); 502 | exit(1); 503 | } 504 | 505 | #endif 506 | 507 | #if FORTRAN_ORDER == 1 508 | // fortran convention 509 | // first index changes fastest 510 | // return internal_array[x4*size3*size2*size1 + x3*size2*size1 + 511 | // x2*size1 + x1]; 512 | 513 | return internal_array[x4 * F4 + x3 * F3 + x2 * F2 + x1 * F1]; 514 | #else 515 | // C convention 516 | // last index changes fastest 517 | // return internal_array[x1*size2*size3*size4 + x2*size3*size4 518 | // +x3*size4 + x4]; 519 | 520 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4]; 521 | #endif 522 | } 523 | 524 | // overloaded at() const 525 | inline const array_element_type &at(int x1, int x2, int x3, int x4) const { 526 | 527 | #if ARRAY_BOUNDS_CHECK == 1 528 | 529 | if ((x1 < 0) || (x1 >= size1)) { 530 | 531 | printf("index x1 is less than 0 or equal to size1 or greater than " 532 | "size1\n"); 533 | printf("x1=%d \n", x1); 534 | printf("size1=%d \n", size1); 535 | printf("file %s, line %d.\n", __FILE__, __LINE__); 536 | raise(SIGSEGV); 537 | } 538 | 539 | if ((x2 < 0) || (x2 >= size2)) { 540 | 541 | printf("index x2 is less than 0 or equal to size2 or greater than " 542 | "size2\n"); 543 | printf("x2=%d \n", x2); 544 | printf("size2=%d \n", size2); 545 | printf("file %s, line %d.\n", __FILE__, __LINE__); 546 | raise(SIGSEGV); 547 | } 548 | 549 | if ((x3 < 0) || (x3 >= size3)) { 550 | 551 | printf("index x3 is less than 0 or equal to size3 or greater than " 552 | "size3\n"); 553 | printf("x3=%d \n", x3); 554 | printf("size3=%d \n", size3); 555 | printf("file %s, line %d.\n", __FILE__, __LINE__); 556 | raise(SIGSEGV); 557 | } 558 | 559 | if ((x4 < 0) || (x4 >= size4)) { 560 | 561 | printf("index x4 is less than 0 or equal to size4 or greater than " 562 | "size4\n"); 563 | printf("x4=%d \n", x4); 564 | printf("size4=%d \n", size4); 565 | printf("file %s, line %d.\n", __FILE__, __LINE__); 566 | raise(SIGSEGV); 567 | exit(1); 568 | } 569 | 570 | #endif 571 | 572 | #if FORTRAN_ORDER == 1 573 | // fortran convention 574 | // first index changes fastest 575 | // return internal_array[x4*size3*size2*size1 + x3*size2*size1 + 576 | // x2*size1 + x1]; 577 | 578 | return internal_array[x4 * F4 + x3 * F3 + x2 * F2 + x1 * F1]; 579 | #else 580 | // C convention 581 | // last index changes fastest 582 | // return internal_array[x1*size2*size3*size4 + x2*size3*size4 583 | // +x3*size4 + x4]; 584 | 585 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4]; 586 | #endif 587 | } 588 | 589 | // constructor 590 | array4d(int dim1, int dim2, int dim3, int dim4) { 591 | 592 | if (dim1 <= 0) { 593 | printf("dim1 is less than or equal to 0\n"); 594 | printf("dim1=%d \n", dim1); 595 | printf("file %s, line %d.\n", __FILE__, __LINE__); 596 | raise(SIGSEGV); 597 | } else if (dim2 <= 0) { 598 | printf("dim2 is less than or equal to 0\n"); 599 | printf("dim2=%d \n", dim2); 600 | printf("file %s, line %d.\n", __FILE__, __LINE__); 601 | raise(SIGSEGV); 602 | } else if (dim3 <= 0) { 603 | printf("dim3 is less than or equal to 0\n"); 604 | printf("dim3=%d \n", dim3); 605 | printf("file %s, line %d.\n", __FILE__, __LINE__); 606 | raise(SIGSEGV); 607 | } else if (dim4 <= 0) { 608 | printf("dim4 is less than or equal to 0\n"); 609 | printf("dim4=%d \n", dim4); 610 | printf("file %s, line %d.\n", __FILE__, __LINE__); 611 | raise(SIGSEGV); 612 | } else { 613 | size1 = dim1; 614 | size2 = dim2; 615 | size3 = dim3; 616 | size4 = dim4; 617 | 618 | internal_array = 619 | new array_element_type[size1 * size2 * size3 * size4]; 620 | 621 | F4 = size3 * size2 * size1; 622 | F3 = size2 * size1; 623 | F2 = size1; 624 | F1 = 1; 625 | 626 | C1 = size2 * size3 * size4; 627 | C2 = size3 * size4; 628 | C3 = size4; 629 | C4 = 1; 630 | } 631 | } 632 | 633 | // destructor 634 | ~array4d() { delete[] internal_array; } 635 | 636 | // note that even though array4d is a template, inside defintion of array4d 637 | // array4d means same as array4d 638 | private: 639 | // prohibit copy constructor 640 | array4d(array4d &); 641 | 642 | // prohibit assignment operator 643 | array4d &operator=(array4d &); 644 | }; 645 | 646 | ////////////// end class array4d ///////////////////// 647 | 648 | //////////////// start class array5d ///////////////////// 649 | template class array5d { 650 | 651 | private: 652 | // x-axis 653 | int size1; 654 | // y-axis 655 | int size2; 656 | // z-axis (think of like floor in a building) 657 | int size3; 658 | // t-axis 659 | int size4; 660 | // 661 | int size5; 662 | 663 | array_element_type *internal_array; 664 | 665 | // factors for Fortran order 666 | int F1, F2, F3, F4, F5; 667 | 668 | // factors for C order 669 | int C1, C2, C3, C4, C5; 670 | 671 | public: 672 | inline int length1(void) const { return size1; } 673 | 674 | inline int length2(void) const { return size2; } 675 | 676 | inline int length3(void) const { return size3; } 677 | 678 | inline int length4(void) const { return size4; } 679 | 680 | inline int length5(void) const { return size5; } 681 | 682 | inline array_element_type &at(int x1, int x2, int x3, int x4, int x5) { 683 | 684 | #if ARRAY_BOUNDS_CHECK == 1 685 | 686 | if ((x1 < 0) || (x1 >= size1)) { 687 | 688 | printf("index x1 is less than 0 or equal to size1 or greater than " 689 | "size1\n"); 690 | printf("x1=%d \n", x1); 691 | printf("size1=%d \n", size1); 692 | printf("file %s, line %d.\n", __FILE__, __LINE__); 693 | raise(SIGSEGV); 694 | } 695 | 696 | if ((x2 < 0) || (x2 >= size2)) { 697 | 698 | printf("index x2 is less than 0 or equal to size2 or greater than " 699 | "size2\n"); 700 | printf("x2=%d \n", x2); 701 | printf("size2=%d \n", size2); 702 | printf("file %s, line %d.\n", __FILE__, __LINE__); 703 | raise(SIGSEGV); 704 | } 705 | 706 | if ((x3 < 0) || (x3 >= size3)) { 707 | 708 | printf("index x3 is less than 0 or equal to size3 or greater than " 709 | "size3\n"); 710 | printf("x3=%d \n", x3); 711 | printf("size3=%d \n", size3); 712 | printf("file %s, line %d.\n", __FILE__, __LINE__); 713 | raise(SIGSEGV); 714 | } 715 | 716 | if ((x4 < 0) || (x4 >= size4)) { 717 | 718 | printf("index x4 is less than 0 or equal to size4 or greater than " 719 | "size4\n"); 720 | printf("x4=%d \n", x4); 721 | printf("size4=%d \n", size4); 722 | printf("file %s, line %d.\n", __FILE__, __LINE__); 723 | raise(SIGSEGV); 724 | } 725 | 726 | if ((x5 < 0) || (x5 >= size5)) { 727 | 728 | printf("index x5 is less than 0 or equal to size5 or greater than " 729 | "size5\n"); 730 | printf("x5=%d \n", x5); 731 | printf("size5=%d \n", size5); 732 | printf("file %s, line %d.\n", __FILE__, __LINE__); 733 | raise(SIGSEGV); 734 | } 735 | #endif 736 | 737 | #if FORTRAN_ORDER == 1 738 | // fortran convention 739 | // first index changes fastest 740 | // return internal_array[x5*size4*size3*size2*size1 + 741 | // x4*size3*size2*size1 + x3*size2*size1 + x2*size1 + x1]; 742 | 743 | return internal_array[x5 * F5 + x4 * F4 + x3 * F3 + x2 * F2 + x1 * F1]; 744 | 745 | #else 746 | // C convention 747 | // last index changes fastest 748 | // return internal_array[x1*size2*size3*size4*size5 + 749 | // x2*size3*size4*size5 + 750 | // x3*size4*size5 + 751 | // x4*size5 + x5]; 752 | 753 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5]; 754 | 755 | #endif 756 | } 757 | 758 | // overloaded at() const 759 | inline const array_element_type &at(int x1, int x2, int x3, int x4, 760 | int x5) const { 761 | 762 | #if ARRAY_BOUNDS_CHECK == 1 763 | 764 | if ((x1 < 0) || (x1 >= size1)) { 765 | 766 | printf("index x1 is less than 0 or equal to size1 or greater than " 767 | "size1\n"); 768 | printf("x1=%d \n", x1); 769 | printf("size1=%d \n", size1); 770 | printf("file %s, line %d.\n", __FILE__, __LINE__); 771 | raise(SIGSEGV); 772 | } 773 | 774 | if ((x2 < 0) || (x2 >= size2)) { 775 | 776 | printf("index x2 is less than 0 or equal to size2 or greater than " 777 | "size2\n"); 778 | printf("x2=%d \n", x2); 779 | printf("size2=%d \n", size2); 780 | printf("file %s, line %d.\n", __FILE__, __LINE__); 781 | raise(SIGSEGV); 782 | } 783 | 784 | if ((x3 < 0) || (x3 >= size3)) { 785 | 786 | printf("index x3 is less than 0 or equal to size3 or greater than " 787 | "size3\n"); 788 | printf("x3=%d \n", x3); 789 | printf("size3=%d \n", size3); 790 | printf("file %s, line %d.\n", __FILE__, __LINE__); 791 | raise(SIGSEGV); 792 | } 793 | 794 | if ((x4 < 0) || (x4 >= size4)) { 795 | 796 | printf("index x4 is less than 0 or equal to size4 or greater than " 797 | "size4\n"); 798 | printf("x4=%d \n", x4); 799 | printf("size4=%d \n", size4); 800 | printf("file %s, line %d.\n", __FILE__, __LINE__); 801 | raise(SIGSEGV); 802 | } 803 | 804 | if ((x5 < 0) || (x5 >= size5)) { 805 | 806 | printf("index x5 is less than 0 or equal to size5 or greater than " 807 | "size5\n"); 808 | printf("x5=%d \n", x5); 809 | printf("size5=%d \n", size5); 810 | printf("file %s, line %d.\n", __FILE__, __LINE__); 811 | raise(SIGSEGV); 812 | } 813 | #endif 814 | 815 | #if FORTRAN_ORDER == 1 816 | // fortran convention 817 | // first index changes fastest 818 | // return internal_array[x5*size4*size3*size2*size1 + 819 | // x4*size3*size2*size1 + x3*size2*size1 + x2*size1 + x1]; 820 | 821 | return internal_array[x5 * F5 + x4 * F4 + x3 * F3 + x2 * F2 + x1 * F1]; 822 | 823 | #else 824 | // C convention 825 | // last index changes fastest 826 | // return internal_array[x1*size2*size3*size4*size5 + 827 | // x2*size3*size4*size5 + 828 | // x3*size4*size5 + 829 | // x4*size5 + x5]; 830 | 831 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5]; 832 | 833 | #endif 834 | } 835 | 836 | // constructor 837 | array5d(int dim1, int dim2, int dim3, int dim4, int dim5) { 838 | 839 | if (dim1 <= 0) { 840 | printf("dim1 is less than or equal to 0\n"); 841 | printf("dim1=%d \n", dim1); 842 | printf("file %s, line %d.\n", __FILE__, __LINE__); 843 | raise(SIGSEGV); 844 | } else if (dim2 <= 0) { 845 | printf("dim2 is less than or equal to 0\n"); 846 | printf("dim2=%d \n", dim2); 847 | printf("file %s, line %d.\n", __FILE__, __LINE__); 848 | raise(SIGSEGV); 849 | } else if (dim3 <= 0) { 850 | printf("dim3 is less than or equal to 0\n"); 851 | printf("dim3=%d \n", dim3); 852 | printf("file %s, line %d.\n", __FILE__, __LINE__); 853 | raise(SIGSEGV); 854 | } else if (dim4 <= 0) { 855 | printf("dim4 is less than or equal to 0\n"); 856 | printf("dim4=%d \n", dim4); 857 | printf("file %s, line %d.\n", __FILE__, __LINE__); 858 | raise(SIGSEGV); 859 | } else if (dim5 <= 0) { 860 | printf("dim5 is less than or equal to 0\n"); 861 | printf("dim5=%d \n", dim5); 862 | printf("file %s, line %d.\n", __FILE__, __LINE__); 863 | raise(SIGSEGV); 864 | } else { 865 | size1 = dim1; 866 | size2 = dim2; 867 | size3 = dim3; 868 | size4 = dim4; 869 | size5 = dim5; 870 | 871 | internal_array = 872 | new array_element_type[size1 * size2 * size3 * size4 * size5]; 873 | 874 | F5 = size4 * size3 * size2 * size1; 875 | F4 = size3 * size2 * size1; 876 | F3 = size2 * size1; 877 | F2 = size1; 878 | F1 = 1; 879 | 880 | C1 = size2 * size3 * size4 * size5; 881 | C2 = size3 * size4 * size5; 882 | C3 = size4 * size5; 883 | C4 = size5; 884 | C5 = 1; 885 | } 886 | } 887 | // destructor 888 | ~array5d() { delete[] internal_array; } 889 | 890 | // note that even though array5d is a template, inside defintion of array5d 891 | // array5d means same as array5d 892 | private: 893 | // prohibit copy constructor 894 | array5d(array5d &); 895 | 896 | // prohibit assignment operator 897 | array5d &operator=(array5d &); 898 | }; 899 | 900 | ////////////// end class array5d ///////////////////// 901 | 902 | //////////////// start class array6d ///////////////////// 903 | template class array6d { 904 | 905 | private: 906 | // x-axis 907 | int size1; 908 | // y-axis 909 | int size2; 910 | // z-axis (think of like floor in a building) 911 | int size3; 912 | // t-axis 913 | int size4; 914 | // 915 | int size5; 916 | 917 | int size6; 918 | 919 | array_element_type *internal_array; 920 | 921 | // factors for Fortran order 922 | int F1, F2, F3, F4, F5, F6; 923 | 924 | // factors for C order 925 | int C1, C2, C3, C4, C5, C6; 926 | 927 | public: 928 | inline int length1(void) const { return size1; } 929 | 930 | inline int length2(void) const { return size2; } 931 | 932 | inline int length3(void) const { return size3; } 933 | 934 | inline int length4(void) const { return size4; } 935 | 936 | inline int length5(void) const { return size5; } 937 | 938 | inline int length6(void) const { return size6; } 939 | 940 | inline array_element_type &at(int x1, int x2, int x3, int x4, int x5, 941 | int x6) { 942 | 943 | #if ARRAY_BOUNDS_CHECK == 1 944 | 945 | if ((x1 < 0) || (x1 >= size1)) { 946 | 947 | printf("index x1 is less than 0 or equal to size1 or greater than " 948 | "size1\n"); 949 | printf("x1=%d \n", x1); 950 | printf("size1=%d \n", size1); 951 | printf("file %s, line %d.\n", __FILE__, __LINE__); 952 | raise(SIGSEGV); 953 | } 954 | 955 | if ((x2 < 0) || (x2 >= size2)) { 956 | 957 | printf("index x2 is less than 0 or equal to size2 or greater than " 958 | "size2\n"); 959 | printf("x2=%d \n", x2); 960 | printf("size2=%d \n", size2); 961 | printf("file %s, line %d.\n", __FILE__, __LINE__); 962 | raise(SIGSEGV); 963 | } 964 | 965 | if ((x3 < 0) || (x3 >= size3)) { 966 | 967 | printf("index x3 is less than 0 or equal to size3 or greater than " 968 | "size3\n"); 969 | printf("x3=%d \n", x3); 970 | printf("size3=%d \n", size3); 971 | printf("file %s, line %d.\n", __FILE__, __LINE__); 972 | raise(SIGSEGV); 973 | } 974 | 975 | if ((x4 < 0) || (x4 >= size4)) { 976 | 977 | printf("index x4 is less than 0 or equal to size4 or greater than " 978 | "size4\n"); 979 | printf("x4=%d \n", x4); 980 | printf("size4=%d \n", size4); 981 | printf("file %s, line %d.\n", __FILE__, __LINE__); 982 | raise(SIGSEGV); 983 | } 984 | 985 | if ((x5 < 0) || (x5 >= size5)) { 986 | 987 | printf("index x5 is less than 0 or equal to size5 or greater than " 988 | "size5\n"); 989 | printf("x5=%d \n", x5); 990 | printf("size5=%d \n", size5); 991 | printf("file %s, line %d.\n", __FILE__, __LINE__); 992 | raise(SIGSEGV); 993 | } 994 | 995 | if ((x6 < 0) || (x6 >= size6)) { 996 | 997 | printf("index x6 is less than 0 or equal to size6 or greater than " 998 | "size6\n"); 999 | printf("x6=%d \n", x6); 1000 | printf("size6=%d \n", size6); 1001 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1002 | raise(SIGSEGV); 1003 | } 1004 | 1005 | #endif 1006 | 1007 | #if FORTRAN_ORDER == 1 1008 | // fortran convention 1009 | // first index changes fastest 1010 | // return internal_array[x6*size5*size4*size3*size2*size1 + 1011 | // x5*size4*size3*size2*size1 + 1012 | // x4*size3*size2*size1 + x3*size2*size1 + 1013 | // x2*size1 + x1]; 1014 | 1015 | return internal_array[x6 * F6 + x5 * F5 + x4 * F4 + x3 * F3 + x2 * F2 + 1016 | x1 * F1]; 1017 | 1018 | #else 1019 | // C convention 1020 | // last index changes fastest 1021 | // return internal_array[x1*size2*size3*size4*size5*size6 + 1022 | // x2*size3*size4*size5*size6 + 1023 | // x3*size4*size5*size6 + 1024 | // x4*size5*size6 + 1025 | // x5*size6 + x6]; 1026 | 1027 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5 + 1028 | x6 * C6]; 1029 | 1030 | #endif 1031 | } 1032 | 1033 | // overloaded at() const 1034 | inline const array_element_type &at(int x1, int x2, int x3, int x4, int x5, 1035 | int x6) const { 1036 | 1037 | #if ARRAY_BOUNDS_CHECK == 1 1038 | 1039 | if ((x1 < 0) || (x1 >= size1)) { 1040 | 1041 | printf("index x1 is less than 0 or equal to size1 or greater than " 1042 | "size1\n"); 1043 | printf("x1=%d \n", x1); 1044 | printf("size1=%d \n", size1); 1045 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1046 | raise(SIGSEGV); 1047 | } 1048 | 1049 | if ((x2 < 0) || (x2 >= size2)) { 1050 | 1051 | printf("index x2 is less than 0 or equal to size2 or greater than " 1052 | "size2\n"); 1053 | printf("x2=%d \n", x2); 1054 | printf("size2=%d \n", size2); 1055 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1056 | raise(SIGSEGV); 1057 | } 1058 | 1059 | if ((x3 < 0) || (x3 >= size3)) { 1060 | 1061 | printf("index x3 is less than 0 or equal to size3 or greater than " 1062 | "size3\n"); 1063 | printf("x3=%d \n", x3); 1064 | printf("size3=%d \n", size3); 1065 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1066 | raise(SIGSEGV); 1067 | } 1068 | 1069 | if ((x4 < 0) || (x4 >= size4)) { 1070 | 1071 | printf("index x4 is less than 0 or equal to size4 or greater than " 1072 | "size4\n"); 1073 | printf("x4=%d \n", x4); 1074 | printf("size4=%d \n", size4); 1075 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1076 | raise(SIGSEGV); 1077 | } 1078 | 1079 | if ((x5 < 0) || (x5 >= size5)) { 1080 | 1081 | printf("index x5 is less than 0 or equal to size5 or greater than " 1082 | "size5\n"); 1083 | printf("x5=%d \n", x5); 1084 | printf("size5=%d \n", size5); 1085 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1086 | raise(SIGSEGV); 1087 | } 1088 | 1089 | if ((x6 < 0) || (x6 >= size6)) { 1090 | 1091 | printf("index x6 is less than 0 or equal to size6 or greater than " 1092 | "size6\n"); 1093 | printf("x6=%d \n", x6); 1094 | printf("size6=%d \n", size6); 1095 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1096 | raise(SIGSEGV); 1097 | } 1098 | 1099 | #endif 1100 | 1101 | #if FORTRAN_ORDER == 1 1102 | // fortran convention 1103 | // first index changes fastest 1104 | // return internal_array[x6*size5*size4*size3*size2*size1 + 1105 | // x5*size4*size3*size2*size1 + 1106 | // x4*size3*size2*size1 + x3*size2*size1 + 1107 | // x2*size1 + x1]; 1108 | 1109 | return internal_array[x6 * F6 + x5 * F5 + x4 * F4 + x3 * F3 + x2 * F2 + 1110 | x1 * F1]; 1111 | 1112 | #else 1113 | // C convention 1114 | // last index changes fastest 1115 | // return internal_array[x1*size2*size3*size4*size5*size6 + 1116 | // x2*size3*size4*size5*size6 + 1117 | // x3*size4*size5*size6 + 1118 | // x4*size5*size6 + 1119 | // x5*size6 + x6]; 1120 | 1121 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5 + 1122 | x6 * C6]; 1123 | 1124 | #endif 1125 | } 1126 | 1127 | // constructor 1128 | array6d(int dim1, int dim2, int dim3, int dim4, int dim5, int dim6) { 1129 | 1130 | if (dim1 <= 0) { 1131 | printf("dim1 is less than or equal to 0\n"); 1132 | printf("dim1=%d \n", dim1); 1133 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1134 | raise(SIGSEGV); 1135 | } else if (dim2 <= 0) { 1136 | printf("dim2 is less than or equal to 0\n"); 1137 | printf("dim2=%d \n", dim2); 1138 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1139 | raise(SIGSEGV); 1140 | } else if (dim3 <= 0) { 1141 | printf("dim3 is less than or equal to 0\n"); 1142 | printf("dim3=%d \n", dim3); 1143 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1144 | raise(SIGSEGV); 1145 | } else if (dim4 <= 0) { 1146 | printf("dim4 is less than or equal to 0\n"); 1147 | printf("dim4=%d \n", dim4); 1148 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1149 | raise(SIGSEGV); 1150 | } else if (dim5 <= 0) { 1151 | printf("dim5 is less than or equal to 0\n"); 1152 | printf("dim5=%d \n", dim5); 1153 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1154 | raise(SIGSEGV); 1155 | } else if (dim6 <= 0) { 1156 | printf("dim6 is less than or equal to 0\n"); 1157 | printf("dim6=%d \n", dim6); 1158 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1159 | raise(SIGSEGV); 1160 | } else { 1161 | size1 = dim1; 1162 | size2 = dim2; 1163 | size3 = dim3; 1164 | size4 = dim4; 1165 | size5 = dim5; 1166 | size6 = dim6; 1167 | 1168 | internal_array = new array_element_type[size1 * size2 * size3 * 1169 | size4 * size5 * size6]; 1170 | 1171 | F6 = size5 * size4 * size3 * size2 * size1; 1172 | F5 = size4 * size3 * size2 * size1; 1173 | F4 = size3 * size2 * size1; 1174 | F3 = size2 * size1; 1175 | F2 = size1; 1176 | F1 = 1; 1177 | 1178 | C1 = size2 * size3 * size4 * size5 * size6; 1179 | C2 = size3 * size4 * size5 * size6; 1180 | C3 = size4 * size5 * size6; 1181 | C4 = size5 * size6; 1182 | C5 = size6; 1183 | C6 = 1; 1184 | } 1185 | } 1186 | 1187 | // destructor 1188 | ~array6d() { delete[] internal_array; } 1189 | 1190 | // note that even though array6d is a template, inside defintion of array6d 1191 | // array6d means same as array6d 1192 | private: 1193 | // prohibit copy constructor 1194 | array6d(array6d &); 1195 | 1196 | // prohibit assignment operator 1197 | array6d &operator=(array6d &); 1198 | }; 1199 | 1200 | ////////////// end class array6d ///////////////////// 1201 | 1202 | //////////////// start class array7d ///////////////////// 1203 | template class array7d { 1204 | 1205 | private: 1206 | // x-axis 1207 | int size1; 1208 | // y-axis 1209 | int size2; 1210 | // z-axis (think of like floor in a building) 1211 | int size3; 1212 | // t-axis 1213 | int size4; 1214 | // 1215 | int size5; 1216 | 1217 | int size6; 1218 | 1219 | int size7; 1220 | 1221 | array_element_type *internal_array; 1222 | 1223 | // factors for Fortran order 1224 | int F1, F2, F3, F4, F5, F6, F7; 1225 | 1226 | // factors for C order 1227 | int C1, C2, C3, C4, C5, C6, C7; 1228 | 1229 | public: 1230 | inline int length1(void) const { return size1; } 1231 | 1232 | inline int length2(void) const { return size2; } 1233 | 1234 | inline int length3(void) const { return size3; } 1235 | 1236 | inline int length4(void) const { return size4; } 1237 | 1238 | inline int length5(void) const { return size5; } 1239 | 1240 | inline int length6(void) const { return size6; } 1241 | 1242 | inline int length7(void) const { return size7; } 1243 | 1244 | inline array_element_type &at(int x1, int x2, int x3, int x4, int x5, 1245 | int x6, int x7) { 1246 | 1247 | #if ARRAY_BOUNDS_CHECK == 1 1248 | 1249 | if ((x1 < 0) || (x1 >= size1)) { 1250 | 1251 | printf("index x1 is less than 0 or equal to size1 or greater than " 1252 | "size1\n"); 1253 | printf("x1=%d \n", x1); 1254 | printf("size1=%d \n", size1); 1255 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1256 | raise(SIGSEGV); 1257 | } 1258 | 1259 | if ((x2 < 0) || (x2 >= size2)) { 1260 | 1261 | printf("index x2 is less than 0 or equal to size2 or greater than " 1262 | "size2\n"); 1263 | printf("x2=%d \n", x2); 1264 | printf("size2=%d \n", size2); 1265 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1266 | raise(SIGSEGV); 1267 | } 1268 | 1269 | if ((x3 < 0) || (x3 >= size3)) { 1270 | 1271 | printf("index x3 is less than 0 or equal to size3 or greater than " 1272 | "size3\n"); 1273 | printf("x3=%d \n", x3); 1274 | printf("size3=%d \n", size3); 1275 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1276 | raise(SIGSEGV); 1277 | } 1278 | 1279 | if ((x4 < 0) || (x4 >= size4)) { 1280 | 1281 | printf("index x4 is less than 0 or equal to size4 or greater than " 1282 | "size4\n"); 1283 | printf("x4=%d \n", x4); 1284 | printf("size4=%d \n", size4); 1285 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1286 | raise(SIGSEGV); 1287 | } 1288 | 1289 | if ((x5 < 0) || (x5 >= size5)) { 1290 | 1291 | printf("index x5 is less than 0 or equal to size5 or greater than " 1292 | "size5\n"); 1293 | printf("x5=%d \n", x5); 1294 | printf("size5=%d \n", size5); 1295 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1296 | raise(SIGSEGV); 1297 | } 1298 | 1299 | if ((x6 < 0) || (x6 >= size6)) { 1300 | 1301 | printf("index x6 is less than 0 or equal to size6 or greater than " 1302 | "size6\n"); 1303 | printf("x6=%d \n", x6); 1304 | printf("size6=%d \n", size6); 1305 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1306 | raise(SIGSEGV); 1307 | } 1308 | 1309 | if ((x7 < 0) || (x7 >= size7)) { 1310 | 1311 | printf("index x7 is less than 0 or equal to size7 or greater than " 1312 | "size7\n"); 1313 | printf("x7=%d \n", x7); 1314 | printf("size7=%d \n", size7); 1315 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1316 | raise(SIGSEGV); 1317 | } 1318 | 1319 | #endif 1320 | 1321 | #if FORTRAN_ORDER == 1 1322 | // fortran convention 1323 | // first index changes fastest 1324 | // return internal_array[x7*size6*size5*size4*size3*size2*size1 + 1325 | // x6*size5*size4*size3*size2*size1 + 1326 | // x5*size4*size3*size2*size1 + 1327 | // x4*size3*size2*size1 + x3*size2*size1 + 1328 | // x2*size1 + x1]; 1329 | 1330 | return internal_array[x7 * F7 + x6 * F6 + x5 * F5 + x4 * F4 + x3 * F3 + 1331 | x2 * F2 + x1 * F1]; 1332 | #else 1333 | // C convention 1334 | // last index changes fastest 1335 | // return internal_array[x1*size2*size3*size4*size5*size6*size7 + 1336 | // x2*size3*size4*size5*size6*size7 + 1337 | // x3*size4*size5*size6*size7 + 1338 | // x4*size5*size6*size7 + 1339 | // x5*size6*size7 + 1340 | // x6*size7 + x7]; 1341 | 1342 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5 + 1343 | x6 * C6 + x7 * C7]; 1344 | #endif 1345 | } 1346 | 1347 | // overloaded at() const 1348 | inline const array_element_type &at(int x1, int x2, int x3, int x4, int x5, 1349 | int x6, int x7) const { 1350 | 1351 | #if ARRAY_BOUNDS_CHECK == 1 1352 | 1353 | if ((x1 < 0) || (x1 >= size1)) { 1354 | 1355 | printf("index x1 is less than 0 or equal to size1 or greater than " 1356 | "size1\n"); 1357 | printf("x1=%d \n", x1); 1358 | printf("size1=%d \n", size1); 1359 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1360 | raise(SIGSEGV); 1361 | } 1362 | 1363 | if ((x2 < 0) || (x2 >= size2)) { 1364 | 1365 | printf("index x2 is less than 0 or equal to size2 or greater than " 1366 | "size2\n"); 1367 | printf("x2=%d \n", x2); 1368 | printf("size2=%d \n", size2); 1369 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1370 | raise(SIGSEGV); 1371 | } 1372 | 1373 | if ((x3 < 0) || (x3 >= size3)) { 1374 | 1375 | printf("index x3 is less than 0 or equal to size3 or greater than " 1376 | "size3\n"); 1377 | printf("x3=%d \n", x3); 1378 | printf("size3=%d \n", size3); 1379 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1380 | raise(SIGSEGV); 1381 | } 1382 | 1383 | if ((x4 < 0) || (x4 >= size4)) { 1384 | 1385 | printf("index x4 is less than 0 or equal to size4 or greater than " 1386 | "size4\n"); 1387 | printf("x4=%d \n", x4); 1388 | printf("size4=%d \n", size4); 1389 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1390 | raise(SIGSEGV); 1391 | } 1392 | 1393 | if ((x5 < 0) || (x5 >= size5)) { 1394 | 1395 | printf("index x5 is less than 0 or equal to size5 or greater than " 1396 | "size5\n"); 1397 | printf("x5=%d \n", x5); 1398 | printf("size5=%d \n", size5); 1399 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1400 | raise(SIGSEGV); 1401 | } 1402 | 1403 | if ((x6 < 0) || (x6 >= size6)) { 1404 | 1405 | printf("index x6 is less than 0 or equal to size6 or greater than " 1406 | "size6\n"); 1407 | printf("x6=%d \n", x6); 1408 | printf("size6=%d \n", size6); 1409 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1410 | raise(SIGSEGV); 1411 | } 1412 | 1413 | if ((x7 < 0) || (x7 >= size7)) { 1414 | 1415 | printf("index x7 is less than 0 or equal to size7 or greater than " 1416 | "size7\n"); 1417 | printf("x7=%d \n", x7); 1418 | printf("size7=%d \n", size7); 1419 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1420 | raise(SIGSEGV); 1421 | } 1422 | 1423 | #endif 1424 | 1425 | #if FORTRAN_ORDER == 1 1426 | // fortran convention 1427 | // first index changes fastest 1428 | // return internal_array[x7*size6*size5*size4*size3*size2*size1 + 1429 | // x6*size5*size4*size3*size2*size1 + 1430 | // x5*size4*size3*size2*size1 + 1431 | // x4*size3*size2*size1 + x3*size2*size1 + 1432 | // x2*size1 + x1]; 1433 | 1434 | return internal_array[x7 * F7 + x6 * F6 + x5 * F5 + x4 * F4 + x3 * F3 + 1435 | x2 * F2 + x1 * F1]; 1436 | #else 1437 | // C convention 1438 | // last index changes fastest 1439 | // return internal_array[x1*size2*size3*size4*size5*size6*size7 + 1440 | // x2*size3*size4*size5*size6*size7 + 1441 | // x3*size4*size5*size6*size7 + 1442 | // x4*size5*size6*size7 + 1443 | // x5*size6*size7 + 1444 | // x6*size7 + x7]; 1445 | 1446 | return internal_array[x1 * C1 + x2 * C2 + x3 * C3 + x4 * C4 + x5 * C5 + 1447 | x6 * C6 + x7 * C7]; 1448 | #endif 1449 | } 1450 | 1451 | // constructor 1452 | array7d(int dim1, int dim2, int dim3, int dim4, int dim5, int dim6, 1453 | int dim7) { 1454 | 1455 | if (dim1 <= 0) { 1456 | printf("dim1 is less than or equal to 0\n"); 1457 | printf("dim1=%d \n", dim1); 1458 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1459 | raise(SIGSEGV); 1460 | } else if (dim2 <= 0) { 1461 | printf("dim2 is less than or equal to 0\n"); 1462 | printf("dim2=%d \n", dim2); 1463 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1464 | raise(SIGSEGV); 1465 | } else if (dim3 <= 0) { 1466 | printf("dim3 is less than or equal to 0\n"); 1467 | printf("dim3=%d \n", dim3); 1468 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1469 | raise(SIGSEGV); 1470 | } else if (dim4 <= 0) { 1471 | printf("dim4 is less than or equal to 0\n"); 1472 | printf("dim4=%d \n", dim4); 1473 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1474 | raise(SIGSEGV); 1475 | } else if (dim5 <= 0) { 1476 | printf("dim5 is less than or equal to 0\n"); 1477 | printf("dim5=%d \n", dim5); 1478 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1479 | raise(SIGSEGV); 1480 | } else if (dim6 <= 0) { 1481 | printf("dim6 is less than or equal to 0\n"); 1482 | printf("dim6=%d \n", dim6); 1483 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1484 | raise(SIGSEGV); 1485 | } else if (dim7 <= 0) { 1486 | printf("dim7 is less than or equal to 0\n"); 1487 | printf("dim7=%d \n", dim7); 1488 | printf("file %s, line %d.\n", __FILE__, __LINE__); 1489 | raise(SIGSEGV); 1490 | } else { 1491 | size1 = dim1; 1492 | size2 = dim2; 1493 | size3 = dim3; 1494 | size4 = dim4; 1495 | size5 = dim5; 1496 | size6 = dim6; 1497 | size7 = dim7; 1498 | 1499 | // Fortran convention 1500 | F7 = size6 * size5 * size4 * size3 * size2 * size1; 1501 | F6 = size5 * size4 * size3 * size2 * size1; 1502 | F5 = size4 * size3 * size2 * size1; 1503 | F4 = size3 * size2 * size1; 1504 | F3 = size2 * size1; 1505 | F2 = size1; 1506 | F1 = 1; 1507 | 1508 | // C convention 1509 | // last index changes fastest 1510 | C1 = size2 * size3 * size4 * size5 * size6 * size7; 1511 | C2 = size3 * size4 * size5 * size6 * size7; 1512 | C3 = size4 * size5 * size6 * size7; 1513 | C4 = size5 * size6 * size7; 1514 | C5 = size6 * size7; 1515 | C6 = size7; 1516 | C7 = 1; 1517 | 1518 | internal_array = 1519 | new array_element_type[size1 * size2 * size3 * size4 * size5 * 1520 | size6 * size7]; 1521 | } 1522 | } 1523 | 1524 | // destructor 1525 | ~array7d() { delete[] internal_array; } 1526 | 1527 | // note that even though array7d is a template, inside defintion of array7d 1528 | // array7d means same as array7d 1529 | private: 1530 | // prohibit copy constructor 1531 | array7d(array7d &); 1532 | 1533 | // prohibit assignment operator 1534 | array7d &operator=(array7d &); 1535 | }; 1536 | 1537 | ////////////// end class array7d ///////////////////// 1538 | 1539 | } // namespace orca_array 1540 | 1541 | // endif ORCA_ARRAY 1542 | #endif 1543 | --------------------------------------------------------------------------------