-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathByteArray.cpp
More file actions
254 lines (197 loc) · 5.36 KB
/
Copy pathByteArray.cpp
File metadata and controls
254 lines (197 loc) · 5.36 KB
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
// ByteArray.cpp: implementation of the CByteArray class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ByteArray.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define ASSERT_VALID( exp )
CByteArray::CByteArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
CByteArray::~CByteArray()
{
ASSERT_VALID(this);
delete[] (BYTE*)m_pData;
}
void CByteArray::SetSize(int nNewSize, int nGrowBy)
{
ASSERT_VALID(this);
ASSERT(nNewSize >= 0);
if (nGrowBy != -1)
m_nGrowBy = nGrowBy; // set new size
if (nNewSize == 0)
{
// shrink to nothing
delete[] (BYTE*)m_pData;
m_pData = NULL;
m_nSize = m_nMaxSize = 0;
}
else if (m_pData == NULL)
{
// create one with exact size
#ifdef SIZE_T_MAX
ASSERT(nNewSize <= SIZE_T_MAX/sizeof(BYTE)); // no overflow
#endif
m_pData = (BYTE*) new BYTE[nNewSize * sizeof(BYTE)];
memset(m_pData, 0, nNewSize * sizeof(BYTE)); // zero fill
m_nSize = m_nMaxSize = nNewSize;
}
else if (nNewSize <= m_nMaxSize)
{
// it fits
if (nNewSize > m_nSize)
{
// initialize the new elements
memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(BYTE));
}
m_nSize = nNewSize;
}
else
{
// otherwise, grow array
int nGrowBy = m_nGrowBy;
if (nGrowBy == 0)
{
// heuristically determine growth when nGrowBy == 0
// (this avoids heap fragmentation in many situations)
nGrowBy = min(1024, max(4, m_nSize / 8));
}
int nNewMax;
if (nNewSize < m_nMaxSize + nGrowBy)
nNewMax = m_nMaxSize + nGrowBy; // granularity
else
nNewMax = nNewSize; // no slush
ASSERT(nNewMax >= m_nMaxSize); // no wrap around
#ifdef SIZE_T_MAX
ASSERT(nNewMax <= SIZE_T_MAX/sizeof(BYTE)); // no overflow
#endif
BYTE* pNewData = (BYTE*) new BYTE[nNewMax * sizeof(BYTE)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(BYTE));
// construct remaining elements
ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(BYTE));
// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
int CByteArray::Append(const CByteArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself
int nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);
memcpy(m_pData + nOldSize, src.m_pData, src.m_nSize * sizeof(BYTE));
return nOldSize;
}
int CByteArray::Append(BYTE *newElement, int length)
{
int nOldSize = m_nSize;
SetSize(m_nSize + length);
memcpy(m_pData + nOldSize, newElement, length * sizeof(BYTE));
return nOldSize;
}
BYTE * CByteArray::GetWritePtr(int length)
{
int nOldSize = m_nSize;
SetSize(m_nSize + length);
return m_pData+nOldSize;
}
void CByteArray::Copy(const CByteArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself
SetSize(src.m_nSize);
memcpy(m_pData, src.m_pData, src.m_nSize * sizeof(BYTE));
}
void CByteArray::FreeExtra()
{
ASSERT_VALID(this);
if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
ASSERT(m_nSize <= SIZE_T_MAX/sizeof(BYTE)); // no overflow
#endif
BYTE* pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (BYTE*) new BYTE[m_nSize * sizeof(BYTE)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(BYTE));
}
// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
/////////////////////////////////////////////////////////////////////////////
void CByteArray::SetAtGrow(int nIndex, BYTE newElement)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
SetSize(nIndex+1);
m_pData[nIndex] = newElement;
}
void CByteArray::InsertAt(int nIndex, BYTE newElement, int nCount)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0); // will expand to meet need
ASSERT(nCount > 0); // zero or negative size not allowed
if (nIndex >= m_nSize)
{
// adding after the end of the array
SetSize(nIndex + nCount); // grow so nIndex is valid
}
else
{
// inserting in the middle of the array
int nOldSize = m_nSize;
SetSize(m_nSize + nCount); // grow it to new size
// shift old data up to fill gap
memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
(nOldSize-nIndex) * sizeof(BYTE));
// re-init slots we copied from
memset(&m_pData[nIndex], 0, nCount * sizeof(BYTE));
}
// insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
// copy elements into the empty space
while (nCount--)
m_pData[nIndex++] = newElement;
}
void CByteArray::RemoveAt(int nIndex, int nCount)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
if (nMoveCount)
memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
nMoveCount * sizeof(BYTE));
m_nSize -= nCount;
}
void CByteArray::InsertAt(int nStartIndex, CByteArray* pNewArray)
{
ASSERT_VALID(this);
ASSERT(pNewArray != NULL);
ASSERT_VALID(pNewArray);
ASSERT(nStartIndex >= 0);
if (pNewArray->GetSize() > 0)
{
InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
for (int i = 0; i < pNewArray->GetSize(); i++)
SetAt(nStartIndex + i, pNewArray->GetAt(i));
}
}