-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptrarray.h
191 lines (143 loc) · 3.7 KB
/
ptrarray.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
179
180
181
182
183
184
185
186
187
188
189
190
//+-------------------------------------------------------------------------
//
// Microsoft Windows
// Copyright (C) Microsoft Corporation, 1995
//
// File: ptrarray.h
//
// Contents: Handles dynamic arrays of void *
//
// History: 7-13-95 Davepl Created
//
//--------------------------------------------------------------------------
#pragma once
class CPtrArray
{
public:
//
// Constructor / Destructor
//
CPtrArray();
CPtrArray(HANDLE hHeap);
virtual ~CPtrArray();
//
// Attributes
//
int GetSize() const
{
return m_nSize;
}
int GetUpperBound() const
{
return m_nSize-1;
}
BOOL SetSize(int nNewSize, int nGrowBy = -1);
BOOL FreeExtra();
BOOL RemoveAll()
{
return SetSize(0);
}
void* GetAt(int nIndex) const
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_pData[nIndex];
}
void SetAt(int nIndex, void* newElement)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
m_pData[nIndex] = newElement;
}
void*& ElementAt(int nIndex)
{
ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_pData[nIndex];
}
// Direct Access to the element data (may return NULL)
const void** GetData() const
{
return (const void**)m_pData;
}
void** GetData()
{
return (void**)m_pData;
}
// Potentially growing the array
BOOL SetAtGrow(int nIndex, void* newElement)
{
ASSERT(nIndex >= 0);
if (nIndex >= m_nSize)
{
if (FALSE == SetSize(nIndex+1))
{
return FALSE;
}
}
m_pData[nIndex] = newElement;
return TRUE;
}
BOOL Add(void* newElement, int * pIndex = NULL)
{
if (pIndex)
{
*pIndex = m_nSize;
}
return SetAtGrow(m_nSize, newElement);
}
BOOL Append(const CPtrArray& src, int * pOldSize = NULL)
{
ASSERT(this != &src); // cannot append to itself
int nOldSize = m_nSize;
if (FALSE == SetSize(m_nSize + src.m_nSize))
{
return TRUE;
}
CopyMemory(m_pData + nOldSize, src.m_pData, src.m_nSize * sizeof(*m_pData));
if (pOldSize)
{
*pOldSize = nOldSize;
}
return TRUE;
}
BOOL Copy(const CPtrArray& src)
{
ASSERT(this != &src); // cannot append to itself
if (FALSE == SetSize(src.m_nSize))
{
return FALSE;
}
CopyMemory(m_pData, src.m_pData, src.m_nSize * sizeof(*m_pData));
return TRUE;
}
// overloaded operator helpers
void* operator[](int nIndex) const
{
return GetAt(nIndex);
}
void*& operator[](int nIndex)
{
return ElementAt(nIndex);
}
// Operations that move elements around
BOOL InsertAt(int nIndex, void* newElement, int nCount = 1);
BOOL InsertAt(int nStartIndex, CPtrArray* pNewArray);
void RemoveAt(int nIndex, int nCount)
{
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
if (nMoveCount > 0)
{
CopyMemory(&m_pData[nIndex], &m_pData[nIndex + nCount], nMoveCount * sizeof(*m_pData));
}
m_nSize -= nCount;
}
// Implementation
protected:
void** m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount
HANDLE m_hHeap; // heap to allocate from
};