-
Notifications
You must be signed in to change notification settings - Fork 99
/
Vector.h
207 lines (180 loc) · 3.65 KB
/
Vector.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
/*
* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#pragma once
#include "NvFlexTypes.h"
#include <new>
namespace NvFlex
{
template<class T, NvFlexUint staticCapacity>
struct VectorCached : public Allocable
{
static const NvFlexUint s_staticCapacity = staticCapacity;
T* m_data;
NvFlexUint m_capacity;
T m_cache[staticCapacity];
NvFlexUint m_size = 0u;
T* allocate(NvFlexUint capacity)
{
T* ptr = (T*)operator new[](capacity*sizeof(T));
for (NvFlexUint i = 0; i < capacity; i++)
{
new(ptr + i) T;
}
return ptr;
}
void cleanup(T* data, NvFlexUint capacity)
{
if (m_cache != data)
{
for (NvFlexUint i = 0; i < capacity; i++)
{
data[i].~T();
}
operator delete[](data);
}
}
VectorCached(NvFlexUint capacity)
{
m_capacity = capacity;
if (capacity > staticCapacity)
{
m_data = allocate(capacity);
}
else
{
m_data = m_cache;
}
}
~VectorCached()
{
if (m_capacity > staticCapacity)
{
cleanup(m_data, m_capacity);
}
m_data = nullptr;
}
T& operator[](unsigned int idx)
{
return m_data[idx];
}
NvFlexUint allocateBack()
{
// resize if needed
if (m_size + 1 > m_capacity)
{
NvFlexUint capacity = 2 * m_capacity;
T* newData = allocate(capacity);
// copy to new
for (NvFlexUint i = 0; i < m_size; i++)
{
new(&newData[i]) T(m_data[i]);
}
// cleanup old
cleanup(m_data, m_capacity);
// commit new
m_data = newData;
m_capacity = capacity;
}
m_size++;
return m_size - 1;
}
void reserve(NvFlexUint capacity)
{
if (capacity > m_capacity)
{
T* newData = allocate(capacity);
// copy to new
for (NvFlexUint i = 0; i < m_size; i++)
{
new(&newData[i]) T(m_data[i]);
}
// cleanup old
cleanup(m_data, m_capacity);
// commit new
m_data = newData;
m_capacity = capacity;
}
}
};
template <class T>
struct Image3D : public Allocable
{
T* m_data = nullptr;
NvFlexDim m_dim = { 0u, 0u, 0u };
NvFlexUint m_capacity = 0u;
Image3D() {}
void init(NvFlexDim dim)
{
m_dim = dim;
m_capacity = m_dim.x * m_dim.y * m_dim.z;
T* ptr = (T*)operator new[](m_capacity*sizeof(T));
for (NvFlexUint i = 0; i < m_capacity; i++)
{
new(ptr + i) T;
}
m_data = ptr;
}
~Image3D()
{
if (m_data)
{
for (NvFlexUint i = 0; i < m_capacity; i++)
{
m_data[i].~T();
}
operator delete[](m_data);
m_data = nullptr;
}
}
T& operator[](unsigned int idx)
{
return m_data[idx];
}
T& operator()(NvFlexUint i, NvFlexUint j, NvFlexUint k)
{
return m_data[(k*m_dim.y + j) * m_dim.x + i];
}
};
template <class T>
struct Image1D : public Allocable
{
T* m_data = nullptr;
NvFlexUint m_dim = 0u;
NvFlexUint m_capacity = 0u;
Image1D() {}
void init(NvFlexUint dim)
{
m_dim = dim;
m_capacity = m_dim;
T* ptr = (T*)operator new[](m_capacity*sizeof(T));
for (NvFlexUint i = 0; i < m_capacity; i++)
{
new(ptr + i) T;
}
m_data = ptr;
}
~Image1D()
{
if (m_data)
{
for (NvFlexUint i = 0; i < m_capacity; i++)
{
m_data[i].~T();
}
operator delete[](m_data);
m_data = nullptr;
}
}
T& operator[](unsigned int idx)
{
return m_data[idx];
}
};
}