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
|
/*
* Collections.cpp
*
* Created by Lou Amadio on 9/17/08.
* Copyright 2008 OoeyGUI. All rights reserved.
*
*/
#include <WProgram.h>
#include "Collections.h"
DArray::DArray()
: _array(NULL)
, _count(0)
{
}
DArray::~DArray()
{
if (_array)
free(_array);
}
void* DArray::item(size_t i)
{
if (i < _count)
{
return _array[i];
}
return NULL;
}
void DArray::push(void* item)
{
insert(_count, item);
}
void* DArray::dequeue()
{
void* item = NULL;
if (_count)
{
item = _array[0];
}
remove(0);
return item;
}
void* DArray::pop()
{
void* item = NULL;
if (_count)
{
item = _array[_count - 1];
remove(_count - 1);
}
return item;
}
void DArray::set(size_t i, void* item)
{
if (i < _count)
{
_array[i] = item;
}
}
bool DArray::insert(size_t i, void* item)
{
size_t needed = max(i, _count + 1);
if (_array)
{
void** newArray = (void**)realloc(_array, needed * sizeof(void*));
if (newArray)
_array = newArray;
else
return false;
}
else
{
_array = (void**)malloc(needed * sizeof(void*));
if (!_array)
return false;
}
if (i < _count)
{
memmove(&_array[i + 1], &_array[i], (_count - i) * sizeof(void*));
}
_array[i] = item;
_count = needed;
return true;
}
void DArray::remove(size_t i)
{
if (i < _count)
{
memmove(&_array[i], &_array[i + 1], (_count - i) * sizeof(void*));
// We don't need to reallocate this, because realloc will handle this correctly.
_count--;
}
}
bool DArray::find(void* itemToFind, size_t* at)
{
for (size_t index = 0; index < count(); index++)
{
void* i = this->item(index);
if (i == itemToFind)
{
if (at)
{
*at = index;
}
return true;
}
}
return false;
}
void DArray::foreach(DArrayForEach cb, void* context)
{
for (size_t index = 0; index < count(); index++)
{
cb(_array[index], context);
}
}
void DArray::sort(DArraySortCallback cb)
{
qsort(_array, _count, sizeof(void*), cb);
}
DArray* DArray::shallowClone()
{
DArray* newArray = new DArray();
if (newArray)
{
newArray->_count = _count;
newArray->_array = (void**)malloc(_count * sizeof(void**));
if (newArray->_array)
{
memmove(newArray->_array, _array, _count * sizeof(void*));
}
}
return newArray;
}
bool testCollections()
{
// {
int i = 0;
DArray a;
a.pushValue(i++);
a.pushValue(i++);
a.pushValue(i++);
a.pushValue(i++);
a.pushValue(i++);
Serial.print("DArray: Count:");
Serial.println(a.count());
Serial.println("DArray: poping values");
while (a.count())
{
if (a.popValue() != --i)
{
Serial.println("DArray: poping not in order");
return false;
}
}
a.insertValue(0, 3);
a.insertValue(0, 3);
a.insertValue(0, 1);
a.insertValue(0, 1);
a.insertValue(0, 4);
a.insertValue(0, 4);
Serial.print("Inserted 6 items, got Count:");
Serial.println(a.count());
int tests[] = { 4, 4, 1, 1, 3, 3};
for (int index = 0; index < a.count(); index++)
{
if (tests[index] != a.value(index))
{
Serial.println("DArray: inserted values not in order");
for (index = 0; index < a.count(); index++)
{
Serial.print((long)a[index]);
Serial.print(", ");
}
return false;
}
}
DArray b;
b.pushValue(0);
b.pushValue(1);
b.pushValue(2);
b.pushValue(3);
b.pushValue(4);
Serial.print("pushed 5 values got Count:");
Serial.println(a.count());
size_t index;
Serial.println("Finding value 1");
bool found = b.findValue(1, &index);
if (found)
{
Serial.println("Found value 1");
}
else
{
Serial.println("Did not find value 1");
}
if (!found || index != 1)
{
Serial.println("DArray: values not found correctly");
return false;
}
// }
Serial.println("DArray: test succeeded");
return true;
}
|