-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSPtrArray.cpp
More file actions
270 lines (230 loc) · 5.75 KB
/
Copy pathSPtrArray.cpp
File metadata and controls
270 lines (230 loc) · 5.75 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (C) 1991 - 1999 Rational Software Corporation
#include "stdafx.h"
#include "SPtrArray.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//##ModelId=4049F71402AF
CSPtrArray::CSPtrArray()
{
// TODO: Add your specialized code here.
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
//##ModelId=4049F738001C
CSPtrArray::~CSPtrArray()
{
// TODO: Add your specialized code here.
if (m_pData != NULL)
{
DeletePtr(m_pData, m_nSize);
delete[] (BYTE*)m_pData;
}
}
//##ModelId=4049F7D20349
void* CSPtrArray::GetAt(int nIndex) const
{
// TODO: Add your specialized code here.
ASSERT(nIndex<m_nSize);
return m_pData[nIndex];
}
void CSPtrArray::InsertAt(int nIndex, void * newElement, int nCount /*=1*/)
{
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, -1); // grow so nIndex is valid
}
else
{
// inserting in the middle of the array
int nOldSize = m_nSize;
SetSize(m_nSize + nCount, -1); // grow it to new size
// destroy intial data before copying over it
DeletePtr(&m_pData[nOldSize], nCount);
// shift old data up to fill gap
memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
(nOldSize-nIndex) * sizeof(void *));
// re-init slots we copied from
//memset(&m_pData[nIndex],0, nCount);
}
// insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
m_pData[nIndex++] = newElement;
}
//##ModelId=4049F7440286
void CSPtrArray::SetSize(int nNewSize, int nGrowBy)
{
// TODO: Add your specialized code here.
ASSERT(nNewSize >= 0);
if (nGrowBy != -1) m_nGrowBy = nGrowBy; // set new size
if (nNewSize == 0)
{
// shrink to nothing
if (m_pData != NULL)
{
DeletePtr(m_pData, m_nSize);
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(void)); // no overflow
#endif
m_pData = (void**) new BYTE[nNewSize * sizeof(void *)];
memset(m_pData,0,nNewSize * sizeof(void *));
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(void*));
}
else if (m_nSize > nNewSize)
{
// destroy the old elements
DeletePtr(&m_pData[nNewSize],m_nSize-nNewSize);
memset(&m_pData[nNewSize],0, (m_nSize-nNewSize)*sizeof(void*));
}
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 = m_nSize / 8;
nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
}
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(TYPE)); // no overflow
#endif
void** pNewData = (void **) new BYTE[nNewMax * sizeof(void*)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(void *));
// construct remaining elements
ASSERT(nNewSize > m_nSize);
memset(&pNewData[m_nSize], 0,(nNewSize-m_nSize)*sizeof(void*));
// get rid of old stuff (note: no destructors called)
delete[] m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}
//##ModelId=4049F7A3032D
void*& CSPtrArray::ElementAt(int nIndex)
{
// TODO: Add your specialized code here.
// NOTE: Requires a correct return value to compile.
ASSERT(nIndex<m_nSize);
return m_pData[nIndex];
}
//##ModelId=404C958903D9
void CSPtrArray::RemoveAll()
{
// TODO: Add your specialized code here.
SetSize(0);
}
//##ModelId=404C95C1002C
void CSPtrArray::RemoveAt(int nIndex,int nCount)
{
// TODO: Add your specialized code here.
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
DeletePtr(&m_pData[nIndex], nCount);
if (nMoveCount)
memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
nMoveCount * sizeof(void *));
m_nSize -= nCount;
}
//##ModelId=4060357101C5
int CSPtrArray::SetAt(int nIndex,void * newElement)
{
ASSERT(nIndex >= 0);
if (nIndex >= m_nSize) SetSize(nIndex+1);
m_pData[nIndex] = newElement;
return nIndex;
}
void CSPtrArray::DeletePtr(void **ptr,int nLength)
{
}
CSPtrStack::CSPtrStack():sp(-1)
{
SetSize(10);
}
//##ModelId=4060357100F4
CSPtrStack::~CSPtrStack()
{
}
void* CSPtrStack::Push(void* newElement)
{
SetAt(++sp,newElement);
return m_pData[sp];
}
//##ModelId=4048ACFF0071
void* CSPtrStack::Pop()
{
// TODO: Add your specialized code here.
if( sp>=0 ){
void * tmp =m_pData[sp+1];
RemoveAt(sp--);
return tmp;
}
return NULL;
}
//##ModelId=4048AD230113
void* CSPtrStack::Head()
{
// TODO: Add your specialized code here.
if( sp<0 ) return 0;
return m_pData[sp];
}
//##ModelId=4048AD30039C
void* CSPtrStack::Tail()
{
// TODO: Add your specialized code here.
if( sp<0 ) return 0;
return m_pData[0];
}
//##ModelId=4049DD330245
void* CSPtrStack::operator[](int nIndex) const
{
// TODO: Add your specialized code here.
ASSERT(nIndex<=sp);
return GetAt(nIndex);
}
//##ModelId=4049DD3302D1
void* CSPtrStack::GetAt(int nIndex) const
{
// TODO: Add your specialized code here.'
ASSERT(nIndex<=sp);
return CSPtrArray::GetAt(nIndex);
}