Newton Dynamics  4.00
dArray.h
1 /* Copyright (c) <2003-2019> <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 __D_ARRAY_H__
28 #define __D_ARRAY_H__
29 
30 #include "dCoreStdafx.h"
31 #include "dTypes.h"
32 #include "dMemory.h"
33 
34 template<class T>
35 class dArray
36 {
37  public:
38  dArray();
39  dArray(dInt32 count);
40  dArray(const dArray& source);
41 
42  ~dArray ();
43 
44  const dInt32 GetCount() const;
45  void SetCount(dInt32 count);
46 
47  void Clear();
48  void Resize(dInt32 count);
49  dInt32 GetCapacity() const;
50 
51  T& operator[] (dInt32 i);
52  const T& operator[] (dInt32 i) const;
53 
54  void Swap(dArray& other);
55  void PushBack(const T& element);
56 
57  protected:
58  T* m_array;
59 
60  private:
61  dInt32 m_size;
62  dInt32 m_capacity;
63 };
64 
65 template<class T>
67  :m_array(nullptr)
68  ,m_size(0)
69  ,m_capacity(0)
70 {
71 }
72 
73 template<class T>
74 dArray<T>::dArray(dInt32 count)
75  :m_array(nullptr)
76  ,m_size(0)
77  ,m_capacity(0)
78 {
79  Resize(count);
80 }
81 
82 template<class T>
83 dArray<T>::dArray(const dArray& source)
84  :m_array(nullptr)
85  ,m_size(0)
86  ,m_capacity(0)
87 {
88  if (source.m_array)
89  {
90  Resize(source.m_capacity);
91  SetCount(source.m_size);
92  for (dInt32 i = 0; i < source.m_size; i++)
93  {
94  m_array[i] = source[i];
95  }
96  }
97 }
98 
99 template<class T>
101 {
102  if (m_array)
103  {
104  dMemory::Free(m_array);
105  }
106 }
107 
108 template<class T>
109 const T& dArray<T>::operator[] (dInt32 i) const
110 {
111  dAssert(i >= 0);
112  dAssert(i < m_size);
113  return m_array[i];
114 }
115 
116 template<class T>
117 T& dArray<T>::operator[] (dInt32 i)
118 {
119  dAssert(i >= 0);
120  dAssert(i < m_size);
121  return m_array[i];
122 }
123 
124 template<class T>
125 void dArray<T>::PushBack(const T& element)
126 {
127  dAssert(m_size <= m_capacity);
128  if (m_size == m_capacity)
129  {
130  Resize(m_capacity * 2);
131  }
132  dInt32 index = m_size;
133  m_size++;
134  (*this)[index] = element;
135 }
136 
137 template<class T>
138 const dInt32 dArray<T>::GetCount() const
139 {
140  return m_size;
141 }
142 
143 template<class T>
144 void dArray<T>::SetCount(dInt32 count)
145 {
146  m_size = count;
147  while (m_size > m_capacity)
148  {
149  Resize(m_capacity * 2);
150  }
151 }
152 
153 template<class T>
154 dInt32 dArray<T>::GetCapacity() const
155 {
156  return m_capacity;
157 }
158 
159 template<class T>
160 void dArray<T>::Clear()
161 {
162  m_size = 0;
163 }
164 
165 template<class T>
166 void dArray<T>::Resize(dInt32 size)
167 {
168  if (size >= m_capacity)
169  {
170  size = dMax(size, 16);
171  T* const newArray = (T*)dMemory::Malloc(dInt32(sizeof(T) * size));
172  if (m_array)
173  {
174  for (dInt32 i = 0; i < m_capacity; i++)
175  {
176  memcpy(&newArray[i], &m_array[i], sizeof(T));
177  }
178  dMemory::Free(m_array);
179  }
180  m_array = newArray;
181  m_capacity = size;
182  }
183  else if (size < m_capacity)
184  {
185  size = dMax(size, 16);
186  T* const newArray = (T*)dMemory::Malloc(dInt32(sizeof(T) * size));
187  if (m_array)
188  {
189  for (dInt32 i = 0; i < size; i++)
190  {
191  memcpy(&newArray[i], &m_array[i], sizeof(T));
192  }
193  dMemory::Free(m_array);
194  }
195  m_array = newArray;
196  m_capacity = size;
197  }
198 }
199 
200 template<class T>
201 void dArray<T>::Swap(dArray& other)
202 {
203  dSwap(m_array, other.m_array);
204  dSwap(m_size, other.m_size);
205  dSwap(m_capacity, other.m_capacity);
206 }
207 
208 #endif
209 
210 
211 
212 
dArray
Definition: dArray.h:36
dMemory::Free
static D_CORE_API void Free(void *const ptr)
Destroy a memory buffer previously allocated by Malloc.
Definition: dMemory.cpp:58
dMemory::Malloc
static D_CORE_API void * Malloc(size_t size)
General Memory allocation function.
Definition: dMemory.cpp:44