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
|
#include <stdio.h>
#include <string.h>
#include "h_collection.h"
HCollection::HCollection(ccIndex aLimit, ccIndex aDelta) :
count(0),
limit(0),
items(NULL),
delta(aDelta),
shouldDelete(false),
lastItem(-1)
{
setLimit(aLimit);
}
HCollection::~HCollection()
{
if (shouldDelete)
freeAll();
delete items;
}
void* HCollection::at(ccIndex pIndex)
{
if (pIndex < 0 || pIndex >= count)
return NULL;
lastItem = pIndex;
return items[pIndex];
}
bool HCollection::atFree(ccIndex pIndex)
{
void* item = at(pIndex);
if (!atRemove(pIndex))
return false;
freeItem(item);
return true;
}
bool HCollection::atInsert(ccIndex pIndex, void* item)
{
if (pIndex < 0)
return false;
if (count == limit) {
if (delta == 0)
return false;
setLimit(count + delta);
}
if (pIndex < count)
shiftItem(pIndex, 1);
count++;
items[pIndex] = (uchar*)item;
lastItem = pIndex;
return true;
}
bool HCollection::atPut(ccIndex pIndex, void* item)
{
if (pIndex >= count) return false;
items[pIndex] = (uchar*)item;
return true;
}
bool HCollection::atRemove(ccIndex pIndex)
{
if (pIndex < 0 || pIndex >= count)
return false;
count--;
if (count > pIndex)
shiftItem(pIndex, -1);
lastItem = pIndex;
return true;
}
void HCollection::remove(void* item)
{
atRemove(indexOf(item));
}
void HCollection::removeAll()
{
count = 0;
}
void* HCollection::firstThat(ccTestFunc Test, void* arg)
{
for (ccIndex i = 0; i < count; i++)
if (Test(items[i], arg) == true) {
lastItem = i;
return items[i];
}
return NULL;
}
void* HCollection::lastThat(ccTestFunc Test, void* arg)
{
for (ccIndex i = count - 1; i >= 0; i--)
if (Test(items[i], arg) == true) {
lastItem = i;
return items[i];
}
return NULL;
}
void HCollection::forEach(ccAppFunc action, void* arg)
{
for (ccIndex i = 0; i < count; i++)
action(items[i], arg);
lastItem = count - 1;
}
void HCollection::free(void* item)
{
remove(item);
freeItem(item);
}
void HCollection::freeAll()
{
for (ccIndex i = 0; i < count; i++)
freeItem(at(i));
count = 0;
}
void HCollection::freeItem(void* item)
{
delete item;
}
ccIndex HCollection::indexOf(const void* item)
{
for (ccIndex i = 0; i < count; i++)
if (item == items[i])
return i;
return -1;
}
bool HCollection::insert(void* item)
{
return atInsert(count, item);
}
void HCollection::pack()
{
uchar** curDst = items;
uchar** curSrc = items;
uchar** last = items + count;
while (curSrc < last) {
if (*curSrc != 0)
*curDst++ = *curSrc;
*curSrc++;
}
}
void HCollection::setLimit(ccIndex aLimit)
{
if (aLimit < count)
aLimit = count;
if (aLimit > maxCollectionSize)
aLimit = maxCollectionSize;
if (aLimit != limit) {
uchar** aItems;
if (aLimit == 0) aItems = NULL;
else {
aItems = new uchar*[aLimit];
if (count != 0 && aItems != NULL && items != NULL)
memcpy(aItems, items, count* sizeof(void*));
}
delete items;
items = aItems;
limit = aLimit;
}
}
bool HCollection::shiftItem(ccIndex pItemNo, int direction)
{
if (items == NULL || pItemNo >= limit)
return false;
switch (direction) {
case 1: // Раздвинуть массив на один элемент
memmove(items + pItemNo + 1, items + pItemNo, sizeof(void*)*(count - pItemNo));
items[pItemNo] = NULL;
return true;
case -1: // Сдвинуть массив на один элемент
memmove(items + pItemNo, items + pItemNo + 1, sizeof(void*)*(count - pItemNo));
items[count] = NULL;
return true;
default: return false;
}
}
void HCollection::tide()
{
delete items; items = NULL;
count = 0;
}
|