ScummVM API documentation
dgArray.h
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 public:
35  dgArray(dgInt32 granulatitySize, dgMemoryAllocator *const allocator);
36  ~dgArray();
37 
38  DG_CLASS_ALLOCATOR(allocator)
39 
40  T &operator[](dgInt32 i);
41  const T &operator[](dgInt32 i) const;
42  void Resize(dgInt32 size) const;
43 
44 private:
45  dgInt32 m_granulatity;
46  mutable dgInt32 m_maxSize;
47  mutable T *m_array;
48  dgMemoryAllocator *m_allocator;
49 };
50 
51 
52 template<class T>
53 dgArray<T>::dgArray(dgInt32 granulatitySize, dgMemoryAllocator *const allocator) {
54  m_maxSize = 0;
55  m_granulatity = granulatitySize;
56  m_array = NULL;
57  m_allocator = allocator;
58 }
59 
60 template<class T>
62  if (m_array) {
63  m_allocator->FreeLow(m_array);
64  }
65 }
66 
67 
68 template<class T>
69 const T &dgArray<T>::operator[](dgInt32 i) const {
70  NEWTON_ASSERT(i >= 0);
71  while (i >= m_maxSize) {
72  Resize(i);
73  }
74  return m_array[i];
75 }
76 
77 
78 template<class T>
79 T &dgArray<T>::operator[](dgInt32 i) {
80  NEWTON_ASSERT(i >= 0);
81  while (i >= m_maxSize) {
82  Resize(i);
83  }
84  return m_array[i];
85 }
86 
87 template<class T>
88 void dgArray<T>::Resize(dgInt32 size) const {
89  if (size >= m_maxSize) {
90  size = size + m_granulatity - (size + m_granulatity) % m_granulatity;
91  T *const newArray = (T *) m_allocator->MallocLow(dgInt32(sizeof(T) * size));
92  if (m_array) {
93  for (dgInt32 i = 0; i < m_maxSize; i ++) {
94  newArray[i] = m_array[i];
95  }
96  m_allocator->FreeLow(m_array);
97  }
98  m_array = newArray;
99  m_maxSize = size;
100  } else if (size < m_maxSize) {
101  size = size + m_granulatity - (size + m_granulatity) % m_granulatity;
102  T *const newArray = (T *) m_allocator->MallocLow(dgInt32(sizeof(T) * size));
103  if (m_array) {
104  for (dgInt32 i = 0; i < size; i ++) {
105  newArray[i] = m_array[i];
106  }
107  m_allocator->FreeLow(m_array);
108  }
109  m_array = newArray;
110  m_maxSize = size;
111  }
112 }
113 
114 
115 
116 #endif
117 
118 
119 
120 
Definition: dgArray.h:33
Definition: dgMemory.h:80