-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdimensions.h
219 lines (189 loc) · 5.53 KB
/
dimensions.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// dimensions.h
// jpcnn
//
// Created by Peter Warden on 1/9/14.
// Copyright (c) 2014 Jetpac, Inc. All rights reserved.
//
#ifndef INCLUDE_DIMENSIONS_H
#define INCLUDE_DIMENSIONS_H
#include "stdlib.h"
#include "stdio.h"
#define MAX_DEBUG_STRING_LEN (1024)
#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif // MAX
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif // MIN
#define DIMENSIONS_MAX_LENGTH (5)
class Dimensions {
public:
Dimensions(int a);
Dimensions(int a, int b);
Dimensions(int a, int b, int c);
Dimensions(int a, int b, int c, int d);
Dimensions(int a, int b, int c, int d, int e);
Dimensions(const int* dims, int length);
Dimensions(const Dimensions & other);
int elementCount() const;
int byteCount() const;
int last() const;
int operator[](const int index) const;
int offset(int aIndex) const;
int offset(int aIndex, int bIndex) const;
int offset(int aIndex, int bIndex, int cIndex) const;
int offset(int aIndex, int bIndex, int cIndex, int dIndex) const;
int offset(int aIndex, int bIndex, int cIndex, int dIndex, int eIndex) const;
Dimensions removeDimensions(int howMany) const;
bool operator==(const Dimensions & other) const;
char* debugString() const;
int _length;
int _dims[DIMENSIONS_MAX_LENGTH];
};
inline Dimensions::Dimensions(int a) {
_dims[0] = a;
_length = 1;
}
inline Dimensions::Dimensions(int a, int b) {
_dims[0] = a;
_dims[1] = b;
_length = 2;
}
inline Dimensions::Dimensions(int a, int b, int c) {
_dims[0] = a;
_dims[1] = b;
_dims[2] = c;
_length = 3;
}
inline Dimensions::Dimensions(int a, int b, int c, int d) {
_dims[0] = a;
_dims[1] = b;
_dims[2] = c;
_dims[3] = d;
_length = 4;
}
inline Dimensions::Dimensions(int a, int b, int c, int d, int e) {
_dims[0] = a;
_dims[1] = b;
_dims[2] = c;
_dims[3] = d;
_dims[4] = e;
_length = 5;
}
inline Dimensions::Dimensions(const int* dims, int length) {
if (length > DIMENSIONS_MAX_LENGTH) {
fprintf(stderr, "Length too large in Dimensions::Dimensions(): %d, max is %d", length, DIMENSIONS_MAX_LENGTH);
}
for (int i = 0; i < MIN(length, DIMENSIONS_MAX_LENGTH); i += 1) {
_dims[i] = dims[i];
}
_length = length;
}
inline Dimensions::Dimensions(const Dimensions & other) {
for (int i = 0; i < other._length; i += 1) {
_dims[i] = other._dims[i];
}
_length = other._length;
}
inline int Dimensions::last() const {
return _dims[_length - 1];
}
inline int Dimensions::elementCount() const {
int result = 1;
for (int index = 0; index < _length; index += 1) {
result *= _dims[index];
}
return result;
}
inline int Dimensions::byteCount() const {
return elementCount() * sizeof(float);
}
inline int Dimensions::operator[](const int index) const {
if (index >= _length) {
return -1;
}
return _dims[index];
}
inline int Dimensions::offset(int aIndex) const {
const int size0 = 1;
return (aIndex * size0);
}
inline int Dimensions::offset(int aIndex, int bIndex) const {
const int size0 = 1;
const int size1 = (_dims[1] * size0);
return ((aIndex * size1) + (bIndex * size0));
}
inline int Dimensions::offset(int aIndex, int bIndex, int cIndex) const {
const int size0 = 1;
const int size1 = (_dims[2] * size0);
const int size2 = (_dims[1] * size1);
return ((aIndex * size2) + (bIndex * size1) + (cIndex * size0));
}
inline int Dimensions::offset(int aIndex, int bIndex, int cIndex, int dIndex) const {
const int size0 = 1;
const int size1 = (_dims[3] * size0);
const int size2 = (_dims[2] * size1);
const int size3 = (_dims[1] * size2);
return ((aIndex * size3) + (bIndex * size2) + (cIndex * size1) + (dIndex * size0));
}
inline int Dimensions::offset(int aIndex, int bIndex, int cIndex, int dIndex, int eIndex) const {
const int size0 = 1;
const int size1 = (_dims[4] * size0);
const int size2 = (_dims[3] * size1);
const int size3 = (_dims[2] * size2);
const int size4 = (_dims[1] * size3);
return ((aIndex * size4) + (bIndex * size3) + (cIndex * size2) + (dIndex * size1) + (eIndex * size0));
}
inline char* Dimensions::debugString() const {
// Will be leaked, but don't care during debugging
char* result = (char*)(malloc(MAX_DEBUG_STRING_LEN));
switch (_length) {
case 1:
snprintf(result, MAX_DEBUG_STRING_LEN, "(%d)",
_dims[0]);
break;
case 2:
snprintf(result, MAX_DEBUG_STRING_LEN, "(%d, %d)",
_dims[0], _dims[1]);
break;
case 3:
snprintf(result, MAX_DEBUG_STRING_LEN, "(%d, %d, %d)",
_dims[0], _dims[1], _dims[2]);
break;
case 4:
snprintf(result, MAX_DEBUG_STRING_LEN, "(%d, %d, %d, %d)",
_dims[0], _dims[1], _dims[2], _dims[3]);
break;
case 5:
snprintf(result, MAX_DEBUG_STRING_LEN, "(%d, %d, %d, %d, %d)",
_dims[0], _dims[1], _dims[2], _dims[3], _dims[4]);
break;
default:
snprintf(result, MAX_DEBUG_STRING_LEN, "Bad length in Dimensions::debugString(): %d", _length);
break;
}
return result;
}
inline Dimensions Dimensions::removeDimensions(int howMany) const {
if ((_length - howMany) <= 0) {
return Dimensions(0);
}
int newDims[DIMENSIONS_MAX_LENGTH];
for (int i = howMany; i < _length; i += 1) {
newDims[i - howMany] = _dims[i];
}
return Dimensions(newDims, (_length - howMany));
}
inline bool Dimensions::operator==(const Dimensions & other) const {
if (_length != other._length) {
return false;
}
for (int index = 0; index < _length; index += 1) {
if (_dims[index] != other._dims[index]) {
return false;
}
}
return true;
}
#endif // INCLUDE_DIMENSIONS_H