Beatmup
array.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program 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  This program 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 #pragma once
20 #include <vector>
21 
22 namespace Beatmup {
23  /**
24  Multidimensional array container with data access facilities
25  The number of dimensions is compile time-known.
26  */
27  template<typename datatype, size_t numdim> class Array {
28  private:
29  std::vector<datatype> storage;
30  size_t dims[numdim];
31  public:
32  Array(size_t length) :
33  storage(length)
34  {
35  static_assert(numdim == 1, "Dimensions mismatch");
36  dims[0] = length;
37  }
38 
39  Array(size_t width, size_t height) :
41  {
42  static_assert(numdim == 2, "Dimensions mismatch");
43  dims[0] = width;
44  dims[1] = height;
45  }
46 
47  Array(size_t width, size_t height, size_t depth):
48  storage(width * height * depth)
49  {
50  static_assert(numdim == 3, "Dimensions mismatch");
51  dims[0] = width;
52  dims[1] = height;
53  dims[2] = depth;
54  }
55 
56  Array(size_t channels, size_t width, size_t height, size_t depth) :
57  storage(channels * width * height * depth)
58  {
59  static_assert(numdim == 4, "Dimensions mismatch");
60  dims[0] = channels;
61  dims[1] = width;
62  dims[2] = height;
63  dims[3] = depth;
64  }
65 
66  datatype* data() { return storage.data(); }
67  size_t size() const { return storage.size(); }
68 
69  datatype& operator()(size_t i) {
70  static_assert(numdim == 1, "Dimensions mismatch");
71  return storage[i];
72  }
73 
74  datatype& operator()(size_t x, size_t y) {
75  static_assert(numdim == 2, "Dimensions mismatch");
76  return storage[y * dims[0] + x];
77  }
78 
79  datatype& operator()(size_t x, size_t y, size_t z) {
80  static_assert(numdim == 3, "Dimensions mismatch");
81  return storage[(z * dims[1] + y) * dims[0] + x];
82  }
83 
84  datatype& operator()(size_t c, size_t x, size_t y, size_t z) {
85  static_assert(numdim == 4, "Dimensions mismatch");
86  return storage[((z * dims[2] + y) * dims[1] + x) * dims[0] + c];
87  }
88 
89  const datatype& operator()(size_t c, size_t x, size_t y, size_t z) const {
90  static_assert(numdim == 4, "Dimensions mismatch");
91  return storage[((z * dims[2] + y) * dims[1] + x) * dims[0] + c];
92  }
93  };
94 }
Multidimensional array container with data access facilities The number of dimensions is compile time...
Definition: array.h:27
datatype & operator()(size_t x, size_t y)
Definition: array.h:74
size_t size() const
Definition: array.h:67
Array(size_t channels, size_t width, size_t height, size_t depth)
Definition: array.h:56
datatype & operator()(size_t i)
Definition: array.h:69
const datatype & operator()(size_t c, size_t x, size_t y, size_t z) const
Definition: array.h:89
Array(size_t length)
Definition: array.h:32
std::vector< datatype > storage
Definition: array.h:29
Array(size_t width, size_t height, size_t depth)
Definition: array.h:47
Array(size_t width, size_t height)
Definition: array.h:39
datatype & operator()(size_t x, size_t y, size_t z)
Definition: array.h:79
size_t dims[numdim]
Definition: array.h:30
datatype & operator()(size_t c, size_t x, size_t y, size_t z)
Definition: array.h:84
datatype * data()
Definition: array.h:66
JNIEnv jobject jint jint jint channels
jlong jstring jint jint jint z
jobject jlong jint jint y
jlong jint width
jlong jint jint height
jobject jlong jint x