-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2DynArray.h
178 lines (136 loc) · 3.02 KB
/
p2DynArray.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ----------------------------------------------------
// Array that resizes dynamically -------------------
// ----------------------------------------------------
#ifndef __P2DYNARRAY_H__
#define __P2DYNARRAY_H__
#include <assert.h>
#define DYN_ARRAY_BLOCK_SIZE 16
template<class VALUE>
class p2DynArray
{
private:
VALUE* data;
unsigned int mem_capacity;
unsigned int num_elements;
public:
// Constructors
p2DynArray() : mem_capacity(0), num_elements(0), data(NULL)
{
Alloc(DYN_ARRAY_BLOCK_SIZE);
}
p2DynArray(unsigned int capacity) : mem_capacity(0), num_elements(0), data(NULL)
{
Alloc(capacity);
}
p2DynArray(const p2DynArray& array) : mem_capacity(0), num_elements(0), data(NULL)
{
Alloc(array.num_elements);
for(unsigned int i = 0; i < array.num_elements; ++i)
PushBack(array.data[i]);
}
// Destructor
~p2DynArray()
{
delete[] data;
}
// Operators
VALUE& operator[](unsigned int index)
{
assert(index < num_elements);
return data[index];
}
const VALUE& operator[](unsigned int index) const
{
assert(index < num_elements);
return data[index];
}
const p2DynArray<VALUE>& operator=(const p2DynArray<VALUE>& other)
{
Alloc(other.num_elements);
for(unsigned int i = 0; i < other.num_elements; ++i)
PushBack(other.data[i]);
return *this;
}
// Data Management
VALUE PushBack(const VALUE& element)
{
if(num_elements >= mem_capacity)
{
Alloc(mem_capacity + DYN_ARRAY_BLOCK_SIZE);
}
data[num_elements++] = element;
return element;
}
bool Pop(VALUE& value)
{
if(num_elements > 0)
{
value = data[--num_elements];
return true;
}
return false;
}
void Clear()
{
num_elements = 0;
}
bool Insert(const VALUE& element, unsigned int position)
{
if(position > num_elements)
return false;
if(position == num_elements)
{
PushBack(element);
return true;
}
if(num_elements + 1 > mem_capacity)
Alloc(mem_capacity + DYN_ARRAY_BLOCK_SIZE);
for(unsigned int i = num_elements; i > position; --i)
{
data[i] = data[i - 1];
}
data[position] = element;
++num_elements;
return true;
}
VALUE* At(unsigned int index)
{
VALUE* result = NULL;
if(index < num_elements)
return result = &data[index];
return result;
}
const VALUE* At(unsigned int index) const
{
VALUE* result = NULL;
if(index < num_elements)
return result = &data[index];
return result;
}
// Utils
unsigned int GetCapacity() const
{
return mem_capacity;
}
unsigned int Count() const
{
return num_elements;
}
private:
// Private Utils
void Alloc(unsigned int mem)
{
VALUE* tmp = data;
mem_capacity = mem;
data = new VALUE[mem_capacity];
if(num_elements > mem_capacity)
num_elements = mem_capacity;
if(tmp != NULL)
{
for(unsigned int i = 0; i < num_elements; ++i)
data[i] = tmp[i];
delete[] tmp;
}
}
};
#endif // __P2DYNARRAY_H__