forked from BlueQuartzSoftware/SIMPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicListArray.hpp
More file actions
executable file
·334 lines (305 loc) · 9.84 KB
/
DynamicListArray.hpp
File metadata and controls
executable file
·334 lines (305 loc) · 9.84 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* ============================================================================
* Copyright (c) 2009-2016 BlueQuartz Software, LLC
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of BlueQuartz Software, the US Air Force, nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The code contained herein was partially funded by the followig contracts:
* United States Air Force Prime Contract FA8650-07-D-5800
* United States Air Force Prime Contract FA8650-10-D-5210
* United States Prime Contract Navy N00173-07-C-2068
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
#pragma once
#include <vector>
//-- DREAM3D Includes
#include "SIMPLib/Common/SIMPLibSetGetMacros.h"
#include "SIMPLib/SIMPLib.h"
/**
* @brief The MeshFaceNeighbors class contains arrays of Faces for each Node in the mesh. This allows quick query to the node
* to determine what Cells the node is a part of.
*/
template <typename T, typename K> class DynamicListArray
{
public:
SIMPL_SHARED_POINTERS(DynamicListArray)
SIMPL_STATIC_NEW_MACRO(DynamicListArray)
SIMPL_TYPE_MACRO(DynamicListArray)
class ElementList
{
public:
T ncells;
K* cells;
};
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
virtual ~DynamicListArray()
{
// This makes sure we deallocate any lists that have been created
for(size_t i = 0; i < this->m_Size; i++)
{
if(this->m_Array[i].cells != nullptr)
{
delete[] this->m_Array[i].cells;
}
}
// Now delete all the "NeighborLists" structures
if(this->m_Array != nullptr)
{
delete[] this->m_Array;
}
}
/**
* @brief size
* @return
*/
size_t size() const
{
return m_Size;
}
/**
* @brief deepCopy
* @param forceNoAllocate
* @return
*/
Pointer deepCopy(bool forceNoAllocate = false) const
{
DynamicListArray::Pointer copy = DynamicListArray::New();
std::vector<T> linkCounts(m_Size, 0);
if(forceNoAllocate)
{
copy->allocateLists(linkCounts);
return copy;
}
// Figure out how many entries, and for each entry, how many cells
for(size_t ptId = 0; ptId < m_Size; ptId++)
{
linkCounts[ptId] = this->m_Array[ptId].ncells;
}
// Allocate all that in the copy
copy->allocateLists(linkCounts);
// Copy the data from the original to the new
for(size_t ptId = 0; ptId < m_Size; ptId++)
{
ElementList& elementList = getElementList(ptId);
copy->setElementList(ptId, elementList);
}
return copy;
}
/**
* @brief insertCellReference
* @param ptId
* @param pos
* @param cellId
*/
inline void insertCellReference(size_t ptId, size_t pos, size_t cellId)
{
this->m_Array[ptId].cells[pos] = cellId;
}
/**
* @brief Get a link structure given a point id.
* @param ptId
* @return
*/
ElementList& getElementList(size_t ptId) const
{
return this->m_Array[ptId];
}
/**
* @brief setElementList
* @param ptId
* @param nCells
* @param data
* @return
*/
bool setElementList(size_t ptId, T nCells, K* data)
{
if(ptId >= m_Size)
{
return false;
}
if(nullptr != m_Array[ptId].cells && m_Array[ptId].ncells > 0)
{
m_Array[ptId].cells = nullptr;
m_Array[ptId].ncells = 0;
}
m_Array[ptId].ncells = nCells;
// If nCells is huge then there could be problems with this
this->m_Array[ptId].cells = new K[nCells];
::memcpy(m_Array[ptId].cells, data, sizeof(K) * nCells);
return true;
}
/**
* @brief setElementList
* @param ptId
* @param list
* @return
*/
bool setElementList(size_t ptId, ElementList& list)
{
T nCells = list.ncells;
K* data = list.cells;
if(ptId >= m_Size)
{
return false;
}
if(nullptr != m_Array[ptId].cells && m_Array[ptId].ncells > 0)
{
m_Array[ptId].cells = nullptr;
m_Array[ptId].ncells = 0;
}
m_Array[ptId].ncells = nCells;
// If nCells is huge then there could be problems with this
this->m_Array[ptId].cells = new K[nCells];
::memcpy(m_Array[ptId].cells, data, sizeof(K) * nCells);
return true;
}
/**
* @brief Get the number of cells using the point specified by ptId.
* @param ptId
* @return
*/
T getNumberOfElements(size_t ptId) const
{
return this->m_Array[ptId].ncells;
}
/**
* @brief Return a list of cell ids using the point.
* @param ptId
* @return
*/
K* getElementListPointer(size_t ptId) const
{
return this->m_Array[ptId].cells;
}
/**
* @brief deserializeLinks
* @param buffer
* @param nElements
*/
void deserializeLinks(QVector<uint8_t>& buffer, size_t nElements)
{
size_t offset = 0;
allocate(nElements); // Allocate all the links with 0 and nullptr;
uint8_t* bufPtr = buffer.data();
// Walk the array and allocate all the array links to Zero and nullptr
T* ncells = nullptr;
for(size_t i = 0; i < nElements; ++i)
{
ncells = reinterpret_cast<T*>(bufPtr + offset);
this->m_Array[i].ncells = *ncells; // Set the number of cells in this link
offset += 2;
this->m_Array[i].cells = new K[(*ncells)]; // Allocate a new chunk of memory to store the list
::memcpy(this->m_Array[i].cells, bufPtr + offset, (*ncells) * sizeof(K)); // Copy from the buffer into the new list memory
offset += (*ncells) * sizeof(K); // Increment the offset
}
}
/**
* @brief deserializeLinks
* @param buffer
* @param nElements
*/
void deserializeLinks(std::vector<uint8_t>& buffer, size_t nElements)
{
size_t offset = 0;
allocate(nElements); // Allocate all the links with 0 and nullptr;
uint8_t* bufPtr = &(buffer.front());
// Walk the array and allocate all the array links to Zero and nullptr
T* ncells = nullptr;
// int32_t* cells = nullptr;
for(size_t i = 0; i < nElements; ++i)
{
ncells = reinterpret_cast<T*>(bufPtr + offset);
this->m_Array[i].ncells = *ncells; // Set the number of cells in this link
offset += 2;
this->m_Array[i].cells = new K[(*ncells)]; // Allocate a new chunk of memory to store the list
::memcpy(this->m_Array[i].cells, bufPtr + offset, (*ncells) * sizeof(K)); // Copy from the buffer into the new list memory
offset += (*ncells) * sizeof(K); // Increment the offset
}
}
/**
* @brief allocateLists
* @param linkCounts
*/
void allocateLists(QVector<T>& linkCounts)
{
allocate(linkCounts.size());
for(typename std::vector<T>::size_type i = 0; i < linkCounts.size(); i++)
{
this->m_Array[i].ncells = linkCounts[i];
this->m_Array[i].cells = new K[this->m_Array[i].ncells];
}
}
/**
* @brief allocateLists
* @param linkCounts
*/
void allocateLists(std::vector<T>& linkCounts)
{
allocate(linkCounts.size());
for(typename std::vector<T>::size_type i = 0; i < linkCounts.size(); i++)
{
this->m_Array[i].ncells = linkCounts[i];
this->m_Array[i].cells = new K[this->m_Array[i].ncells];
}
}
protected:
DynamicListArray() = default;
//----------------------------------------------------------------------------
// This will allocate memory to hold all the NeighborList structures where each
// structure is initialized to Zero Entries and a nullptr Pointer
void allocate(size_t sz, size_t ext = 1000)
{
static typename DynamicListArray<T, K>::ElementList linkInit = {0, nullptr};
// This makes sure we deallocate any lists that have been created
for(size_t i = 0; i < this->m_Size; i++)
{
if(this->m_Array[i].cells != nullptr)
{
delete[] this->m_Array[i].cells;
}
}
// Now delete all the "NeighborLists" structures
if(this->m_Array != nullptr)
{
delete[] this->m_Array;
}
this->m_Size = sz;
// Allocate a whole new set of structures
this->m_Array = new typename DynamicListArray<T, K>::ElementList[sz];
// Initialize each structure to have 0 entries and nullptr pointer.
for(size_t i = 0; i < sz; i++)
{
this->m_Array[i] = linkInit;
}
}
private:
ElementList* m_Array = nullptr; // pointer to data
size_t m_Size = 0;
};
typedef DynamicListArray<int32_t, int32_t> Int32Int32DynamicListArray;
typedef DynamicListArray<uint16_t, int64_t> UInt16Int64DynamicListArray;
typedef DynamicListArray<int64_t, int64_t> Int64Int64DynamicListArray;