summaryrefslogtreecommitdiff
path: root/src/cuboid.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuboid.h')
-rw-r--r--src/cuboid.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/cuboid.h b/src/cuboid.h
new file mode 100644
index 0000000..c4ff803
--- /dev/null
+++ b/src/cuboid.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <array>
+
+class Cuboid {
+private:
+ const std::array<std::size_t,3> _size;
+ const std::size_t _volume;
+
+public:
+ Cuboid(std::size_t nX, std::size_t nY, std::size_t nZ):
+ _size{nX, nY, nZ},
+ _volume(nX*nY*nZ) { }
+
+ std::size_t volume() const {
+ return _volume;
+ }
+
+ std::size_t index(unsigned iX, unsigned iY, unsigned iZ) const {
+ return iX*_size[1]*_size[2] + iY*_size[2] + iZ;
+ }
+
+ int operator[](unsigned i) const {
+ return _size[i];
+ }
+
+ template <typename F>
+ void traverse(F f) {
+ for (int iX = 0; iX < _size[0]; ++iX) {
+ for (int iY = 0; iY < _size[1]; ++iY) {
+ for (int iZ = 0; iZ < _size[2]; ++iZ) {
+ f(iX, iY, iZ, index(iX,iY,iZ));
+ }
+ }
+ }
+ }
+};