Newton Dynamics  4.00
ndArray.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /****************************************************************************
23 *
24 * Visual C++ 6.0 created by: Julio Jerez
25 *
26 ****************************************************************************/
27 #ifndef __ND_ARRAY_H__
28 #define __ND_ARRAY_H__
29 
30 #include "ndCoreStdafx.h"
31 #include "ndTypes.h"
32 #include "ndRand.h"
33 #include "ndUtils.h"
34 #include "ndVector.h"
35 #include "ndClassAlloc.h"
36 
40 template<class T>
41 class ndArray: public ndClassAlloc
42 {
43  public:
47  ndArray(ndInt32 count);
49  ndArray(const ndArray& source);
50 
53 
55  ndInt32 GetCount() const;
56 
60  void SetCount(ndInt32 count);
61 
62  //void Clear();
66  void Resize(ndInt32 count);
67 
69  ndInt32 GetCapacity() const;
70 
73  T& operator[] (ndInt32 i);
74 
77  const T& operator[] (ndInt32 i) const;
78 
81  void Swap(ndArray& other);
82 
85  void PushBack(const T& element);
86 
88  void RandomShuffle(ndInt32 count);
89 
92  void ResetMembers();
93 
96  void SetMembers(ndInt32 size, void* const memory);
97 
98  private:
99  void CopyData(T* const dst, const T* const src, ndInt32 elements);
100 
101  protected:
102  T* m_array;
103  ndInt32 m_size;
104  ndInt32 m_capacity;
105 };
106 
107 template<class T>
109  :ndClassAlloc()
110  ,m_array(nullptr)
111  ,m_size(0)
112  ,m_capacity(0)
113 {
114 }
115 
116 template<class T>
117 ndArray<T>::ndArray(ndInt32 count)
118  :ndClassAlloc()
119  ,m_array(nullptr)
120  ,m_size(0)
121  ,m_capacity(0)
122 {
123  Resize(count);
124 }
125 
126 template<class T>
128  :ndClassAlloc()
129  ,m_array(nullptr)
130  ,m_size(0)
131  ,m_capacity(0)
132 {
133  if (source.m_array)
134  {
135  Resize(source.m_capacity);
136  SetCount(source.m_size);
137  for (ndInt32 i = 0; i < source.m_size; ++i)
138  {
139  m_array[i] = source[i];
140  }
141  }
142 }
143 
144 template<class T>
146 {
147  if (m_array)
148  {
149  ndMemory::Free(m_array);
150  }
151 }
152 
153 template<class T>
154 const T& ndArray<T>::operator[] (ndInt32 i) const
155 {
156  ndAssert(i >= 0);
157  ndAssert(i < m_size);
158  return m_array[i];
159 }
160 
161 template<class T>
163 {
164  ndAssert(i >= 0);
165  ndAssert(i < m_size);
166  return m_array[i];
167 }
168 
169 template<class T>
170 void ndArray<T>::PushBack(const T& element)
171 {
172  ndAssert(m_size <= m_capacity);
173  if (m_size == m_capacity)
174  {
175  Resize(m_capacity * 2);
176  }
177  m_array[m_size] = element;
178  m_size++;
179 }
180 
181 template<class T>
182 ndInt32 ndArray<T>::GetCount() const
183 {
184  return m_size;
185 }
186 
187 template<class T>
188 void ndArray<T>::SetCount(ndInt32 count)
189 {
190  while (count > m_capacity)
191  {
192  Resize(m_capacity * 2);
193  }
194  m_size = count;
195 }
196 
197 template<class T>
198 ndInt32 ndArray<T>::GetCapacity() const
199 {
200  return m_capacity;
201 }
202 
203 template<class T>
204 void ndArray<T>::CopyData(T* const dstPtr, const T* const srcPtr, ndInt32 elements)
205 {
206  const ndInt32 sizeInBytes = ndInt32 (elements * sizeof(T));
207  const ndInt32 size16 = ndInt32(sizeInBytes / sizeof(ndVector));
208 
209  ndVector* const dst = (ndVector*)dstPtr;
210  const ndVector* const src = (ndVector*)srcPtr;
211  for (ndInt32 i = 0; i < size16; ++i)
212  {
213  dst[i] = src[i];
214  }
215  char* const dstBytes = (char*)dst;
216  const char* const srcBytes = (char*)src;
217  for (ndInt32 i = ndInt32(size16 * sizeof(ndVector)); i < sizeInBytes; ++i)
218  {
219  dstBytes[i] = srcBytes[i];
220  }
221 }
222 
223 template<class T>
224 void ndArray<T>::Resize(ndInt32 newSize)
225 {
226  // note: I know some tolls will detect a warning here
227  // because it is copy object from one array
228  // to another with out calling the copy constructor
229  // and destructor, but the ndArray is designed for
230  // high performance memory resizing of struct,
231  // if an application needs to use an array with
232  // for general purpose classes,
233  // please use standart lib std::vector
234  if (newSize > m_capacity || (m_capacity == 0))
235  {
236  newSize = ndMax(newSize, 16);
237  T* const newArray = (T*)ndMemory::Malloc(size_t(sizeof(T) * newSize));
238  if (m_array)
239  {
240  CopyData(newArray, m_array, m_size);
241  ndMemory::Free(m_array);
242  }
243  m_array = newArray;
244  m_capacity = newSize;
245  }
246  else if (newSize < m_capacity)
247  {
248  newSize = ndMax(newSize, 16);
249  T* const newArray = (T*)ndMemory::Malloc(size_t(sizeof(T) * newSize));
250  if (m_array)
251  {
252  CopyData(newArray, m_array, newSize);
253  ndMemory::Free(m_array);
254  }
255  m_size = newSize;
256  m_array = newArray;
257  m_capacity = newSize;
258  }
259 }
260 
261 template<class T>
263 {
264  ndSwap(m_array, other.m_array);
265  ndSwap(m_size, other.m_size);
266  ndSwap(m_capacity, other.m_capacity);
267 }
268 
269 template<class T>
270 void ndArray<T>::SetMembers(ndInt32 size, void* const memory)
271 {
272  m_size = size;
273  m_capacity = size + 1;
274  m_array = (T*)memory;
275 }
276 
277 template<class T>
279 {
280  m_size = 0;
281  m_capacity = 0;
282  m_array = nullptr;
283 }
284 
285 template<class T>
286 void ndArray<T>::RandomShuffle(ndInt32 count)
287 {
288  const ndInt32 size = ndMin (count, GetCount());
289  for (ndInt32 i = size - 1; i != 0; --i)
290  {
291  ndUnsigned32 j = ndUnsigned32(ndRandInt()) % size;
292  ndSwap (m_array[i], m_array[j]);
293  }
294 }
295 
296 #endif
297 
298 
299 
300 
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndMemory::Malloc
static D_CORE_API void * Malloc(size_t size)
General Memory allocation function.
Definition: ndMemory.cpp:46
ndArray
Generic template vector.
Definition: ndArray.h:42
ndArray::Swap
void Swap(ndArray &other)
Interchange all the information with other.
Definition: ndArray.h:262
ndArray::ndArray
ndArray()
constructor, set count and capacity to zero, not memory is allocated.
Definition: ndArray.h:108
ndArray::PushBack
void PushBack(const T &element)
Add element to the end of the buffer.
Definition: ndArray.h:170
ndArray::RandomShuffle
void RandomShuffle(ndInt32 count)
Randomize the vector entries.
Definition: ndArray.h:286
ndArray::ResetMembers
void ResetMembers()
set all member to 0.
Definition: ndArray.h:278
ndArray::GetCount
ndInt32 GetCount() const
return the size of the array.
Definition: ndArray.h:182
ndArray::ndArray
ndArray(ndInt32 count)
constructor, set count and capacity, allocated space for count elements.
Definition: ndArray.h:117
ndArray::Resize
void Resize(ndInt32 count)
Set a new size.
Definition: ndArray.h:224
ndArray::SetMembers
void SetMembers(ndInt32 size, void *const memory)
assign all members.
Definition: ndArray.h:270
ndArray::ndArray
ndArray(const ndArray &source)
copy constructor, allocate and copy only m_size elements from source.
Definition: ndArray.h:127
ndArray::~ndArray
~ndArray()
deallocate all memory, dos not call destructor on any of th elements.
Definition: ndArray.h:145
ndArray::GetCapacity
ndInt32 GetCapacity() const
return the capacity of the array.
Definition: ndArray.h:198
ndArray::SetCount
void SetCount(ndInt32 count)
Set a new size.
Definition: ndArray.h:188
ndArray::operator[]
T & operator[](ndInt32 i)
Get the i element for the array.
Definition: ndArray.h:162
ndVector
Definition: ndVectorArmNeon.h:41
ndMemory::Free
static D_CORE_API void Free(void *const ptr)
Destroy a memory buffer previously allocated by Malloc.
Definition: ndMemory.cpp:61