-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArray.cpp
267 lines (225 loc) · 5.95 KB
/
Array.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: ARRAY.CPP
** COMPONENT: Windows C++ Library.
** DESCRIPTION: CArray class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "Array.hpp"
/******************************************************************************
** Method: Constructor.
**
** Description: .
**
** Parameters: nItemSize The size of an item.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CArray::CArray(size_t nItemSize)
: m_pData(NULL)
, m_nSize(0)
, m_nAllocSize(0)
, m_nItemSize(nItemSize)
{
ASSERT(m_nItemSize > 0);
}
/******************************************************************************
** Method: Copy constructor.
**
** Description: Copies the other array.
**
** Parameters: .
**
** Returns: Nothing.
**
*******************************************************************************
*/
CArray::CArray(const CArray& rArray)
: m_pData(NULL)
, m_nSize(rArray.m_nSize)
, m_nAllocSize(rArray.m_nAllocSize)
, m_nItemSize(rArray.m_nItemSize)
{
// Array not empty?
if (rArray.m_pData != NULL)
{
// Calculate number of bytes to allocate.
size_t nBytes = m_nAllocSize * m_nItemSize;
// Allocate the array.
m_pData = static_cast<byte*>(realloc(m_pData, nBytes));
// Copy the data.
memcpy(m_pData, rArray.m_pData, nBytes);
}
}
/******************************************************************************
** Method: Destructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CArray::~CArray()
{
RemoveAll();
}
/******************************************************************************
** Method: Reserve()
**
** Description: Reserve space for at least the number of items requested.
**
** Parameters: nSize The number of items to reserve space for.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::Reserve(size_t nSize)
{
// Buffer already big enough?
if (nSize <= m_nAllocSize)
return;
// Round size to a multiple of 4.
m_nAllocSize = (nSize + 3) & ~3;
// Calculate number of bytes to allocate.
size_t nBytes = m_nAllocSize * m_nItemSize;
// Allocate it...
m_pData = static_cast<byte*>(realloc(m_pData, nBytes));
ASSERT(m_pData);
}
/******************************************************************************
** Method: Set()
**
** Description: Sets an item in the array.
**
** Parameters: nIndex The item to set.
** pItem A pointer to the items value.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::Set(size_t nIndex, const void* pItem)
{
ASSERT(nIndex <= m_nSize);
// Calculate offset to the position.
byte* pPos = m_pData + (nIndex * m_nItemSize);
// Copy the new item into the array.
memcpy(pPos, pItem, m_nItemSize);
}
/******************************************************************************
** Method: Add()
**
** Description: Appends an item to the array.
**
** Parameters: pItem A pointer to the item to add.
**
** Returns: The position where the item was added.
**
*******************************************************************************
*/
size_t CArray::Add(const void* pItem)
{
// Increase buffer by 1.
Reserve(m_nSize+1);
// Calculate offset of end of array.
byte* pPos = m_pData + (m_nSize * m_nItemSize);
// Copy the item.
memcpy(pPos, pItem, m_nItemSize);
return m_nSize++;
}
/******************************************************************************
** Method: Insert()
**
** Description: Inserts an item into the array.
**
** Parameters: nIndex The index where to insert at.
** pItem A pointer to the item to insert.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::Insert(size_t nIndex, const void* pItem)
{
ASSERT(nIndex <= m_nSize);
// Increase buffer by 1.
Reserve(m_nSize+1);
// Calculate offset to insert position.
byte* pPos = m_pData + (nIndex * m_nItemSize);
// Calculate the size of bytes to move up.
size_t nBytes = (m_nSize - nIndex) * m_nItemSize;
// Move all existing items up one.
memmove(pPos + m_nItemSize, pPos, nBytes);
// Copy the new item into the array.
memcpy(pPos, pItem, m_nItemSize);
++m_nSize;
}
/******************************************************************************
** Method: Remove()
**
** Description: Removes an item from the array.
**
** Parameters: nIndex The index of the item to remove.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::Remove(size_t nIndex)
{
ASSERT(nIndex < m_nSize);
// Calculate offset to the item.
byte* pPos = m_pData + (nIndex * m_nItemSize);
// Calculate the size of bytes to move down.
size_t nBytes = (m_nSize - nIndex - 1) * m_nItemSize;
// Move all existing items down one.
memmove(pPos, pPos + m_nItemSize, nBytes);
m_nSize--;
}
/******************************************************************************
** Method: RemoveAll()
**
** Description: Free up resources.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::RemoveAll()
{
// Free the array buffer.
if (m_pData != NULL)
{
free(m_pData);
m_pData = NULL;
}
// Reset size members.
m_nSize = 0;
m_nAllocSize = 0;
}
/******************************************************************************
** Method: Sort()
**
** Description: Sort the array using a quicksort.
**
** Parameters: pfnCompare The compare function.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CArray::Sort(PFNQSCOMPARE pfnCompare)
{
qsort(m_pData, m_nSize, m_nItemSize, pfnCompare);
}