My Project: dynamics.mod/NewtonDynamics/source/core/dgArray.h Source File

My Project

dgArray.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <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 __dgArray__
28 #define __dgArray__
29
30 #include "dgStdafx.h"
31
32 template<class T>
33 class dgArray
34 {
35 public:
36 dgArray (dgInt32 granulatitySize, dgMemoryAllocator* const allocator, dgInt32 aligmentInBytes = DG_MEMORY_GRANULARITY);
37 ~dgArray ();
38
39 DG_CLASS_ALLOCATOR(allocator)
40
41
42 T& operator[] (dgInt32 i);
43 const T& operator[] (dgInt32 i) const;
44 void Resize (dgInt32 size) const;
45
46 dgInt32 GetElementSize() const;
47 dgInt32 GetBytesCapacity () const;
48 dgInt32 GetElementsCapacity () const;
49
50 bool ExpandCapacityIfNeessesary (dgInt32 index, dgInt32 stride) const;
51
52 private:
53 dgInt32 m_granulatity;
54 dgInt32 m_aligmentInByte;
55 mutable dgInt32 m_maxSize;
56 mutable T *m_array;
57 dgMemoryAllocator* m_allocator;
58 };
59
60
61 template<class T>
62 dgArray<T>::dgArray (dgInt32 granulatitySize, dgMemoryAllocator* const allocator, dgInt32 aligmentInBytes)
63 :m_granulatity(granulatitySize), m_aligmentInByte(aligmentInBytes), m_maxSize(0), m_array(NULL), m_allocator(allocator)
64 {
65 if (m_aligmentInByte <= 0) {
66 m_aligmentInByte = 8;
67 }
68 m_aligmentInByte = 1 << dgExp2(m_aligmentInByte);
69 }
70
71 template<class T>
72 dgArray<T>::~dgArray ()
73 {
74 if (m_array) {
75 m_allocator->FreeLow (m_array);
76 }
77 }
78
79
80 template<class T>
81 const T& dgArray<T>::operator[] (dgInt32 i) const
82 {
83 dgAssert (i >= 0);
84 while (i >= m_maxSize) {
85 Resize (i);
86 }
87 return m_array[i];
88 }
89
90
91 template<class T>
92 T& dgArray<T>::operator[] (dgInt32 i)
93 {
94 dgAssert (i >= 0);
95 while (i >= m_maxSize) {
96 Resize (i);
97 }
98 return m_array[i];
99 }
100
101 template<class T>
102 dgInt32 dgArray<T>::GetElementSize() const
103 {
104 return sizeof (T);
105 }
106
107 template<class T>
108 dgInt32 dgArray<T>::GetElementsCapacity () const
109 {
110 return m_maxSize;
111 }
112
113
114 template<class T>
115 dgInt32 dgArray<T>::GetBytesCapacity () const
116 {
117 return m_maxSize * GetElementSize();
118 }
119
120
121
122 template<class T>
123 void dgArray<T>::Resize (dgInt32 size) const
124 {
125 if (size >= m_maxSize) {
126 size = size + m_granulatity - (size + m_granulatity) % m_granulatity;
127 T* const newArray = (T*) m_allocator->MallocLow (GetElementSize() * size, m_aligmentInByte);
128 if (m_array) {
129 for (dgInt32 i = 0; i < m_maxSize; i ++) {
130 newArray[i] = m_array[i];
131 }
132 m_allocator->FreeLow (m_array);
133 }
134 m_array = newArray;
135 m_maxSize = size;
136 } else if (size < m_maxSize) {
137 size = size + m_granulatity - (size + m_granulatity) % m_granulatity;
138 T* const newArray = (T*) m_allocator->MallocLow (GetElementSize() * size, m_aligmentInByte);
139 if (m_array) {
140 for (dgInt32 i = 0; i < size; i ++) {
141 newArray[i] = m_array[i];
142 }
143 m_allocator->FreeLow (m_array);
144 }
145 m_array = newArray;
146 m_maxSize = size;
147 }
148 }
149
150 template<class T>
151 bool dgArray<T>::ExpandCapacityIfNeessesary (dgInt32 index, dgInt32 stride) const
152 {
153 bool ret = false;
154 dgInt32 size = (index + 1) * stride;
155 while (size >= m_maxSize) {
156 ret = true;
157 Resize (m_maxSize * 2);
158 }
159 return ret;
160 }
161
162 #endif
163
164
165
166
dgExp2
DG_INLINE dgInt32 dgExp2(dgInt32 x)
Definition: dgTypes.h:283
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgArray::GetBytesCapacity
dgInt32 GetBytesCapacity() const
Definition: dgArray.h:115
dgArray::GetElementsCapacity
dgInt32 GetElementsCapacity() const
Definition: dgArray.h:108
dgMemoryAllocator::FreeLow
void FreeLow(void *const retPtr)
Definition: dgMemory.cpp:156
DG_CLASS_ALLOCATOR
#define DG_CLASS_ALLOCATOR(allocator)
Definition: dgMemory.h:60
dgArray::operator[]
T & operator[](dgInt32 i)
Definition: dgArray.h:92
dgStdafx.h
dgArray::~dgArray
~dgArray()
Definition: dgArray.h:72
dgArray
Definition: dgArray.h:33
dgMemoryAllocator::MallocLow
void * MallocLow(dgInt32 size, dgInt32 alignment=DG_MEMORY_GRANULARITY)
Definition: dgMemory.cpp:138
dgArray::Resize
void Resize(dgInt32 size) const
Definition: dgArray.h:123
dgArray::ExpandCapacityIfNeessesary
bool ExpandCapacityIfNeessesary(dgInt32 index, dgInt32 stride) const
Definition: dgArray.h:151
dgMemoryAllocator
Definition: dgMemory.h:73
dgArray::GetElementSize
dgInt32 GetElementSize() const
Definition: dgArray.h:102
DG_MEMORY_GRANULARITY
#define DG_MEMORY_GRANULARITY
Definition: dgMemory.h:81
dgArray::dgArray
dgArray(dgInt32 granulatitySize, dgMemoryAllocator *const allocator, dgInt32 aligmentInBytes=DG_MEMORY_GRANULARITY)
Definition: dgArray.h:62