forked from molovol/MoloVol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer3d.h
69 lines (57 loc) · 2.27 KB
/
container3d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef CONTAINER3D_H
#define CONTAINER3D_H
#include <iostream>
#include <cassert>
#include <array>
#include <vector>
template <class T>
class Container3D{
public:
// constructors
Container3D() = default;
Container3D(const unsigned long int x, const unsigned long int y, const unsigned long int z){
_data = std::vector(x*y*z,T());
_n_elements[0] = x;
_n_elements[1] = y;
_n_elements[2] = z;
}
Container3D(const std::array<unsigned long int,3> steps){
_data = std::vector(steps[0]*steps[1]*steps[2],T());
_n_elements = steps;
}
Container3D(const std::array<unsigned int,3> steps)
: Container3D((unsigned long) steps[0], (unsigned long) steps[1], (unsigned long) steps[2]){}
// get element
// single integer
T& getElement(const unsigned long int i){return _data[i];}
// three integers
T& getElement(const unsigned long int x, const unsigned long int y, const unsigned long int z){
// check if element is out of bounds
// assert(x < _n_elements[0]);
// assert(y < _n_elements[1]);
// assert(z < _n_elements[2]);
return _data[z * _n_elements[0] * _n_elements[1] + y * _n_elements[0] + x];
}
// arrays
T& getElement(const std::array<unsigned long int,3> coord){
return _data[coord[2] * _n_elements[0] * _n_elements[1] + coord[1] * _n_elements[0] + coord[0]];}
T& getElement(const std::array<unsigned int,3> coord){
return _data[coord[2] * _n_elements[0] * _n_elements[1] + coord[1] * _n_elements[0] + coord[0]];}
T& getElement(const std::array<long int,3> coord){
return _data[coord[2] * _n_elements[0] * _n_elements[1] + coord[1] * _n_elements[0] + coord[0]];}
T& getElement(const std::array<int,3> coord){
return _data[coord[2] * _n_elements[0] * _n_elements[1] + coord[1] * _n_elements[0] + coord[0]];}
// get boundaries
template <typename Q = unsigned long>
std::array<Q,3> getNumElements() const {
std::array<Q,3> arr;
for (char i = 0; i < 3; ++i){
arr[i] = static_cast<Q>(_n_elements[i]);
}
return arr;
}
private:
std::vector<T> _data;
std::array<unsigned long int,3> _n_elements;
};
#endif