forked from derandark/DungeonViewerAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DArray.h
127 lines (88 loc) · 1.97 KB
/
DArray.h
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
#pragma once
template<class T>
class DArray
{
public:
DArray(DWORD initial_size, DWORD grow_size);
DArray(DWORD initial_size);
~DArray();
void safe_add(T *pdata, DWORD index);
void shrink(DWORD new_size);
void grow(DWORD new_size);
DWORD GetMaxCount();
T *array_data;
DWORD grow_size;
DWORD num_used;
DWORD alloc_size;
};
template<class T>
DArray<T>::DArray(DWORD initial_size, DWORD grow_size)
{
array_data = new T[ initial_size ];
grow_size = grow_size;
num_used = 0;
alloc_size = initial_size;
}
template<class T>
DArray<T>::DArray(DWORD grow_size)
{
array_data = NULL;
grow_size = grow_size;
num_used = 0;
alloc_size = 0;
}
template<class T>
DArray<T>::~DArray()
{
delete [] array_data;
}
template<class T>
void DArray<T>::grow(DWORD new_size)
{
if (new_size > alloc_size)
{
T *new_data = new T[ new_size ];
for (DWORD i = 0; i < alloc_size; i++)
new_data[i] = array_data[i];
delete [] array_data;
array_data = new_data;
alloc_size = new_size;
}
else
shrink(new_size);
}
template<class T>
void DArray<T>::shrink(DWORD new_size)
{
if (new_size <= alloc_size)
{
if (!new_size)
{
delete [] array_data;
array_data = NULL;
alloc_size = 0;
}
else
{
T *new_data = new T[ new_size ];
for (DWORD i = 0; i < new_size; i++)
new_data[i] = array_data[i];
delete [] array_data;
array_data = new_data;
alloc_size = new_size;
}
if (num_used > alloc_size)
num_used = alloc_size;
}
else
grow(new_size);
}
template<class T>
void DArray<T>::safe_add(T *pdata, DWORD index)
{
if (index >= alloc_size)
grow(grow_size + index);
array_data[index] = *pdata;
if (index > num_used)
num_used = index + 1;
}