forked from orangeduck/Motion-Matching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
182 lines (152 loc) · 5.35 KB
/
array.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
#pragma once
#include <assert.h>
#include <string.h>
#include <stdio.h>
//--------------------------------------
// Basic type representing a pointer to some
// data and the size of the data. `__restrict__`
// here is used to indicate the data should not
// alias against any other input parameters and
// can sometimes produce important performance
// gains.
template<typename T>
struct slice1d
{
int size;
T* __restrict__ data;
slice1d(int _size, T* _data) : size(_size), data(_data) {}
void zero() { memset((char*)data, 0, sizeof(T) * size); }
void set(const T& x) { for (int i = 0; i < size; i++) { data[i] = x; } }
inline T& operator()(int i) const { assert(i >= 0 && i < size); return data[i]; }
};
// Same but for a 2d array of data.
template<typename T>
struct slice2d
{
int rows, cols;
T* __restrict__ data;
slice2d(int _rows, int _cols, T* _data) : rows(_rows), cols(_cols), data(_data) {}
void zero() { memset((char*)data, 0, sizeof(T) * rows * cols); }
void set(const T& x) { for (int i = 0; i < rows * cols; i++) { data[i] = x; } }
inline slice1d<T> operator()(int i) const { assert(i >= 0 && i < rows); return slice1d<T>(cols, &data[i * cols]); }
inline T& operator()(int i, int j) const { assert(i >= 0 && i < rows && j >= 0 && j < cols); return data[i * cols + j]; }
};
//--------------------------------------
// These types are used for the storage of arrays of data.
// They implicitly cast to slices so can be given directly
// as inputs to functions requiring them.
template<typename T>
struct array1d
{
int size;
T* data;
array1d() : size(0), data(NULL) {}
array1d(int _size) : array1d() { resize(_size); }
array1d(const slice1d<T>& rhs) : array1d() { resize(rhs.size); memcpy(data, rhs.data, rhs.size * sizeof(T)); }
array1d(const array1d<T>& rhs) : array1d() { resize(rhs.size); memcpy(data, rhs.data, rhs.size * sizeof(T)); }
~array1d() { resize(0); }
array1d& operator=(const slice1d<T>& rhs) { resize(rhs.size); memcpy(data, rhs.data, rhs.size * sizeof(T)); return *this; };
array1d& operator=(const array1d<T>& rhs) { resize(rhs.size); memcpy(data, rhs.data, rhs.size * sizeof(T)); return *this; };
inline T& operator()(int i) const { assert(i >= 0 && i < size); return data[i]; }
operator slice1d<T>() const { return slice1d<T>(size, data); }
void zero() { memset(data, 0, sizeof(T) * size); }
void set(const T& x) { for (int i = 0; i < size; i++) { data[i] = x; } }
void resize(int _size)
{
if (_size == 0 && size != 0)
{
free(data);
data = NULL;
size = 0;
}
else if (_size > 0 && size == 0)
{
data = (T*)malloc(_size * sizeof(T));
size = _size;
assert(data != NULL);
}
else if (_size > 0 && size > 0 && _size != size)
{
data = (T*)realloc(data, _size * sizeof(T));
size = _size;
assert(data != NULL);
}
}
};
template<typename T>
void array1d_write(const array1d<T>& arr, FILE* f)
{
fwrite(&arr.size, sizeof(int), 1, f);
size_t num = fwrite(arr.data, sizeof(T), arr.size, f);
assert((int)num == arr.size);
}
template<typename T>
void array1d_read(array1d<T>& arr, FILE* f)
{
int size;
fread(&size, sizeof(int), 1, f);
arr.resize(size);
size_t num = fread(arr.data, sizeof(T), size, f);
assert((int)num == size);
}
// Similar type but for 2d data
template<typename T>
struct array2d
{
int rows, cols;
T* data;
array2d() : rows(0), cols(0), data(NULL) {}
array2d(int _rows, int _cols) : array2d() { resize(_rows, _cols); }
~array2d() { resize(0, 0); }
array2d& operator=(const array2d<T>& rhs) { resize(rhs.rows, rhs.cols); memcpy(data, rhs.data, rhs.rows * rhs.cols * sizeof(T)); return *this; };
array2d& operator=(const slice2d<T>& rhs) { resize(rhs.rows, rhs.cols); memcpy(data, rhs.data, rhs.rows * rhs.cols * sizeof(T)); return *this; };
inline slice1d<T> operator()(int i) const { assert(i >= 0 && i < rows); return slice1d<T>(cols, &data[i * cols]); }
inline T& operator()(int i, int j) const { assert(i >= 0 && i < rows && j >= 0 && j < cols); return data[i * cols + j]; }
operator slice2d<T>() const { return slice2d<T>(rows, cols, data); }
void zero() { memset(data, 0, sizeof(T) * rows * cols); }
void set(const T& x) { for (int i = 0; i < rows * cols; i++) { data[i] = x; } }
void resize(int _rows, int _cols)
{
int _size = _rows * _cols;
int size = rows * cols;
if (_size == 0 && size != 0)
{
free(data);
data = NULL;
rows = 0;
cols = 0;
}
else if (_size > 0 && size == 0)
{
data = (T*)malloc(_size * sizeof(T));
rows = _rows;
cols = _cols;
assert(data != NULL);
}
else if (_size > 0 && size > 0 && _size != size)
{
data = (T*)realloc(data, _size * sizeof(T));
rows = _rows;
cols = _cols;
assert(data != NULL);
}
}
};
template<typename T>
void array2d_write(const array2d<T>& arr, FILE* f)
{
fwrite(&arr.rows, sizeof(int), 1, f);
fwrite(&arr.cols, sizeof(int), 1, f);
size_t num = fwrite(arr.data, sizeof(T), arr.rows * arr.cols, f);
assert((int)num == arr.rows * arr.cols);
}
template<typename T>
void array2d_read(array2d<T>& arr, FILE* f)
{
int rows, cols;
fread(&rows, sizeof(int), 1, f);
fread(&cols, sizeof(int), 1, f);
arr.resize(rows, cols);
size_t num = fread(arr.data, sizeof(T), rows * cols, f);
assert((int)num == rows * cols);
}