-
Notifications
You must be signed in to change notification settings - Fork 1
/
multidimmatrix.h
158 lines (130 loc) · 3.57 KB
/
multidimmatrix.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef JWUTIL_MULTIDIMMATRIX_H
#define JWUTIL_MULTIDIMMATRIX_H
#include <assert.h>
#include <type_traits>
#include <array>
#include "fastmath.h"
#ifdef MULTIDIMMATRIX_INIT_GARBAGE
#include <cstdlib>
#endif
namespace jw_util
{
namespace
{
enum Operator {
OpSet,
OpAdd,
OpSub
};
}
template <typename MultiDimMatrixType>
class _MultiDimMatrixBase
{
public:
template <typename SetType>
MultiDimMatrixType &operator=(const SetType &set)
{
MultiDimMatrixType &child = *static_cast<MultiDimMatrixType*>(this);
child.template accumulate<OpSet, SetType>(set);
return child;
}
template <typename AddType>
MultiDimMatrixType &operator+=(const AddType &add)
{
MultiDimMatrixType &child = *static_cast<MultiDimMatrixType*>(this);
child.template accumulate<OpAdd, AddType>(add);
return child;
}
template <typename SubType>
MultiDimMatrixType &operator-=(const SubType &sub)
{
MultiDimMatrixType &child = *static_cast<MultiDimMatrixType*>(this);
child.template accumulate<OpSub, SubType>(sub);
return child;
}
};
template <unsigned int size, unsigned int dims, typename BaseType = float>
class MultiDimMatrix : public _MultiDimMatrixBase<MultiDimMatrix<size, dims, BaseType>>
{
typedef _MultiDimMatrixBase<MultiDimMatrix<size, dims, BaseType>> ParentClass;
friend ParentClass;
private:
static_assert(dims != 0, "Special case MultiDimMatrix<size, 0> not handled by template specialization");
typedef MultiDimMatrix<size, dims - 1, BaseType> ChildMatrixType;
typedef std::array<ChildMatrixType, size> DataType;
DataType data;
public:
MultiDimMatrix()
{
//static_assert(sizeof(MultiDimMatrix<size, dims>) == FastMath::pow<dims>(size) * sizeof(float), "Unexpected size for MultiDimMatrix");
}
void set_zero()
{
for (unsigned int i = 0; i < size; i++)
{
data[i].set_zero();
}
}
ChildMatrixType &operator[](unsigned int i)
{
assert(i < size);
return data[i];
}
const ChildMatrixType &operator[](unsigned int i) const
{
assert(i < size);
return data[i];
}
using ParentClass::operator =;
using ParentClass::operator +=;
using ParentClass::operator -=;
private:
template <Operator op, typename OperandType>
void accumulate(const OperandType &operand)
{
for (unsigned int i = 0; i < size; i++)
{
data[i].template accumulate<op>(operand[i]);
}
}
};
template <unsigned int size, typename BaseType>
class MultiDimMatrix<size, 0, BaseType> : public _MultiDimMatrixBase<MultiDimMatrix<size, 0, BaseType>>
{
typedef _MultiDimMatrixBase<MultiDimMatrix<size, 0, BaseType>> ParentClass;
friend ParentClass;
friend class MultiDimMatrix<size, 1, BaseType>;
private:
BaseType data;
public:
MultiDimMatrix()
{
#ifdef MULTIDIMMATRIX_INIT_GARBAGE
data = static_cast<BaseType>(rand());
#endif
}
void set_zero()
{
data = static_cast<BaseType>(0);
}
operator BaseType() const
{
return data;
}
using ParentClass::operator =;
using ParentClass::operator +=;
using ParentClass::operator -=;
private:
template <Operator op, typename OperandType>
void accumulate(const OperandType &operand)
{
switch (op)
{
case OpSet: data = operand; break;
case OpAdd: data += operand; break;
case OpSub: data -= operand; break;
}
}
};
}
#endif // JWUTIL_MULTIDIMMATRIX_H