1 |
bearsoft |
1.1 |
#ifndef __ObjectArray_H__
|
2 |
|
|
#define __ObjectArray_H__
|
3 |
|
|
|
4 |
|
|
#include "../Element/ObjectElement.h"
|
5 |
|
|
#include "../System/SystemDefine.h"
|
6 |
|
|
#include "../String/Parse.h"
|
7 |
|
|
#include "../String/StringTools.h"
|
8 |
|
|
//#include "../Sort/ObjectArrayQuickSort.h"
|
9 |
|
|
|
10 |
|
|
template <class ClassName>
|
11 |
|
|
|
12 |
|
|
class ObjectArray
|
13 |
|
|
{
|
14 |
|
|
|
15 |
|
|
public:
|
16 |
|
|
|
17 |
|
|
ObjectArray()
|
18 |
|
|
{
|
19 |
|
|
buffer=null;
|
20 |
|
|
size=0;
|
21 |
|
|
stringTools = new StringTools();
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
ObjectArray(int iSize)
|
25 |
|
|
{
|
26 |
|
|
stringTools = new StringTools();
|
27 |
|
|
|
28 |
|
|
if (iSize > 0 )
|
29 |
|
|
{
|
30 |
|
|
buffer=(ObjectElement<ClassName>**)malloc(sizeof(ObjectElement<ClassName>*)*iSize);
|
31 |
|
|
size=iSize;
|
32 |
|
|
|
33 |
|
|
for ( int i=0; i<size ; i++ )
|
34 |
|
|
{
|
35 |
|
|
buffer[i]=null;
|
36 |
|
|
}
|
37 |
|
|
}
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
~ObjectArray()
|
41 |
|
|
{
|
42 |
|
|
for ( int r=0 ; r<size ; r++ )
|
43 |
|
|
{
|
44 |
|
|
ObjectElement<ClassName> *objectElement=buffer[r];
|
45 |
|
|
if (objectElement != null)
|
46 |
|
|
{
|
47 |
|
|
delete objectElement;
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
free(buffer);
|
52 |
|
|
|
53 |
|
|
if (stringTools!=null)
|
54 |
|
|
{
|
55 |
|
|
delete stringTools;
|
56 |
|
|
}
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
void resetArray()
|
60 |
|
|
{
|
61 |
|
|
for ( int i=0; i<size ; i++ )
|
62 |
|
|
{
|
63 |
|
|
ObjectElement<ClassName> *objectElement=buffer[i];
|
64 |
|
|
|
65 |
|
|
if (objectElement!=null)
|
66 |
|
|
{
|
67 |
|
|
objectElement->classNameElement=null;
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
void setObject(ClassName *object, int index)
|
73 |
|
|
{
|
74 |
|
|
Parse parse;
|
75 |
|
|
|
76 |
|
|
if (index >= 0 && index < size )
|
77 |
|
|
{
|
78 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
79 |
|
|
|
80 |
|
|
if ( objectElement != null )
|
81 |
|
|
{
|
82 |
|
|
objectElement->remove();
|
83 |
|
|
objectElement->classNameElement=object;
|
84 |
|
|
int length=0;
|
85 |
|
|
char *name=parse.intToString(index,&length);
|
86 |
|
|
objectElement->name=new String(name);
|
87 |
|
|
free(name);
|
88 |
|
|
}
|
89 |
|
|
else
|
90 |
|
|
{
|
91 |
|
|
int length=0;
|
92 |
|
|
char *name=parse.intToString(index, &length);
|
93 |
|
|
buffer[index]=new ObjectElement<ClassName>(object, name);
|
94 |
|
|
free(name);
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
void setObject(ClassName *object, char *iName, int index)
|
100 |
|
|
{
|
101 |
|
|
if (index >= 0 && index < size )
|
102 |
|
|
{
|
103 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
104 |
|
|
|
105 |
|
|
if ( objectElement != null )
|
106 |
|
|
{
|
107 |
|
|
objectElement->remove();
|
108 |
|
|
objectElement->classNameElement=object;
|
109 |
|
|
objectElement->name=new String(iName);
|
110 |
|
|
}
|
111 |
|
|
else
|
112 |
|
|
{
|
113 |
|
|
buffer[index]=new ObjectElement<ClassName>(object, iName);
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
119 |
|
|
//have to do with the quicksort
|
120 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
121 |
|
|
|
122 |
|
|
void setElement(ObjectElement<ClassName> *iObjectElement, int index)
|
123 |
|
|
{
|
124 |
|
|
if (index >= 0 && index < size )
|
125 |
|
|
{
|
126 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
127 |
|
|
objectElement->classNameElement=iObjectElement->classNameElement;
|
128 |
|
|
objectElement->name->setString(iObjectElement->name->buffer);
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
133 |
|
|
//have to do with the quicksort
|
134 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
135 |
|
|
|
136 |
|
|
void setElementPivot(ClassName *object, char *iName, int index)
|
137 |
|
|
{
|
138 |
|
|
if (index >= 0 && index < size )
|
139 |
|
|
{
|
140 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
141 |
|
|
objectElement->classNameElement=object;
|
142 |
|
|
objectElement->name->setString(iName);
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
char *getObjectName(int index)
|
147 |
|
|
{
|
148 |
|
|
if (index >= 0 && index < size )
|
149 |
|
|
{
|
150 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
151 |
|
|
stringLength=objectElement->name->length;
|
152 |
|
|
return objectElement->name->buffer;
|
153 |
|
|
}
|
154 |
|
|
return null;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
ClassName *getObject(int index)
|
158 |
|
|
{
|
159 |
|
|
if (index >= 0 && index < size )
|
160 |
|
|
{
|
161 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
162 |
|
|
|
163 |
|
|
if (objectElement!=null)
|
164 |
|
|
{
|
165 |
|
|
return objectElement->classNameElement;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
return null;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
ObjectElement<ClassName> *getObjectElement(int index)
|
173 |
|
|
{
|
174 |
|
|
if (index >= 0 && index < size )
|
175 |
|
|
{
|
176 |
|
|
ObjectElement<ClassName> *objectElement=buffer[index];
|
177 |
|
|
|
178 |
|
|
return objectElement;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
return null;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
void addLast(ClassName *object)
|
185 |
|
|
{
|
186 |
|
|
Parse parse;
|
187 |
|
|
|
188 |
|
|
if (buffer==null)
|
189 |
|
|
{
|
190 |
|
|
buffer=(ObjectElement<ClassName>**)malloc(sizeof(ObjectElement<ClassName>*)*1);
|
191 |
|
|
buffer[0]=new ObjectElement<ClassName>(object, "0");
|
192 |
|
|
size=1;
|
193 |
|
|
}
|
194 |
|
|
else
|
195 |
|
|
{
|
196 |
|
|
int length=0;
|
197 |
|
|
char *name=parse.intToString(size+1, &length);
|
198 |
|
|
ObjectElement<ClassName> **tempBuffer=addObjectToArray(buffer,size,new ObjectElement<ClassName>(object, name));
|
199 |
|
|
free(name);
|
200 |
|
|
free(buffer);
|
201 |
|
|
buffer=tempBuffer;
|
202 |
|
|
size++;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
void addLast(ClassName *object, char *iName)
|
207 |
|
|
{
|
208 |
|
|
if (buffer==0)
|
209 |
|
|
{
|
210 |
|
|
buffer=(ObjectElement<ClassName>**)malloc(sizeof(ObjectElement<ClassName>*)*1);
|
211 |
|
|
buffer[0]=(int)new ObjectElement<ClassName>(object, iName);
|
212 |
|
|
size=1;
|
213 |
|
|
}
|
214 |
|
|
else
|
215 |
|
|
{
|
216 |
|
|
ObjectElement<ClassName> **tempBuffer=addObjectToArray(buffer,size,new ObjectElement<ClassName>(object, iName));
|
217 |
|
|
free(buffer);
|
218 |
|
|
buffer=tempBuffer;
|
219 |
|
|
size++;
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
224 |
|
|
//add object together
|
225 |
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
226 |
|
|
|
227 |
|
|
ObjectElement<ClassName> **addObjectToArray(ObjectElement<ClassName> **source, int sourceSize, ObjectElement<ClassName> *objectPointer)
|
228 |
|
|
{
|
229 |
|
|
int forward=0;
|
230 |
|
|
|
231 |
|
|
ObjectElement<ClassName> **buffer=(ObjectElement<ClassName>**)malloc(sourceSize*sizeof(ObjectElement<ClassName>*)+sizeof(ObjectElement<ClassName>*));
|
232 |
|
|
|
233 |
|
|
for ( int s=0 ; s<sourceSize ; s++ )
|
234 |
|
|
{
|
235 |
|
|
buffer[forward]=source[s];
|
236 |
|
|
forward++;
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
buffer[forward]=objectPointer;
|
240 |
|
|
|
241 |
|
|
return buffer;
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
245 |
|
|
//sort element in linkedlist
|
246 |
|
|
////////////////////////////////////////////////////////////////////////////////////
|
247 |
|
|
|
248 |
|
|
void sortLargestOrder()
|
249 |
|
|
{
|
250 |
|
|
sortLargestOrder(this, 0, size-1, 1);
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
void sortSmallestOrder()
|
254 |
|
|
{
|
255 |
|
|
sortSmallestOrder(this, 0, size-1, 0);
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
int partitionOrder(ObjectArray<ClassName> *list, int start, int top, int statement)
|
260 |
|
|
{
|
261 |
|
|
|
262 |
|
|
ObjectElement<ClassName> *tempPivot = list->getObjectElement(top);
|
263 |
|
|
ClassName *pointer=tempPivot->classNameElement;
|
264 |
|
|
String pivotString=tempPivot->name->buffer;
|
265 |
|
|
char *pivotBuffer = pivotString.buffer; // Partition around the last value
|
266 |
|
|
int pivotLength = pivotString.length;
|
267 |
|
|
|
268 |
|
|
int bottom = start-1; // Start outside the area to be partitioned
|
269 |
|
|
bool done=true;
|
270 |
|
|
|
271 |
|
|
while (done) // Until all elements are partitioned...
|
272 |
|
|
{
|
273 |
|
|
while (done) // Until we find an out of place element...
|
274 |
|
|
{
|
275 |
|
|
bottom++; // ... move the bottom up.
|
276 |
|
|
if (bottom == top) // If we hit the top...
|
277 |
|
|
{
|
278 |
|
|
done = false; // ... we are done.
|
279 |
|
|
break;
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
if (statement==0)
|
283 |
|
|
{
|
284 |
|
|
ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
|
285 |
|
|
char *stringBuffer=objectElement->name->buffer;
|
286 |
|
|
int stringLength=objectElement->name->length;
|
287 |
|
|
|
288 |
|
|
if (stringTools->isStringSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
|
289 |
|
|
{
|
290 |
|
|
list->setElement(objectElement,top); // Then put at the top... 6=7
|
291 |
|
|
break;
|
292 |
|
|
} // ... and start searching from the top.
|
293 |
|
|
}
|
294 |
|
|
else
|
295 |
|
|
{
|
296 |
|
|
ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
|
297 |
|
|
char *stringBuffer=objectElement->name->buffer;
|
298 |
|
|
int stringLength=objectElement->name->length;
|
299 |
|
|
|
300 |
|
|
if (stringTools->isStringLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
|
301 |
|
|
{ // Is the bottom out of place?
|
302 |
|
|
list->setElement(objectElement,top); // Then put at the top... 6=7
|
303 |
|
|
break;
|
304 |
|
|
} // ... and start searching from the top.
|
305 |
|
|
}
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
while (done) // Until we find an out of place element...
|
309 |
|
|
{
|
310 |
|
|
--top; // ... move the top down.
|
311 |
|
|
|
312 |
|
|
if (top == bottom) // If we hit the bottom...
|
313 |
|
|
{
|
314 |
|
|
done = false; // ... we are done.
|
315 |
|
|
break;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
if (statement==0)
|
319 |
|
|
{
|
320 |
|
|
ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
|
321 |
|
|
char *stringBuffer=objectElement->name->buffer;
|
322 |
|
|
int stringLength=objectElement->name->length;
|
323 |
|
|
|
324 |
|
|
if (stringTools->isStringLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
|
325 |
|
|
{
|
326 |
|
|
list->setElement(objectElement,bottom); // Then put it at the bottom...
|
327 |
|
|
break;
|
328 |
|
|
} // ...and start searching from the bottom.
|
329 |
|
|
}
|
330 |
|
|
else
|
331 |
|
|
{
|
332 |
|
|
ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
|
333 |
|
|
char *stringBuffer=objectElement->name->buffer;
|
334 |
|
|
int stringLength=objectElement->name->length;
|
335 |
|
|
|
336 |
|
|
if (stringTools->isStringSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
|
337 |
|
|
{
|
338 |
|
|
list->setElement(objectElement,bottom); // Then put it at the bottom...
|
339 |
|
|
break;
|
340 |
|
|
} // ...and start searching from the bottom.
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
list->setElementPivot(pointer,pivotString.buffer, top);
|
346 |
|
|
return top; // Return the split point
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
void sortLargestOrder(ObjectArray<ClassName> *list, int start, int end, int statement)
|
350 |
|
|
{
|
351 |
|
|
if ( start < end )
|
352 |
|
|
{ // If there are two or more elements...
|
353 |
|
|
int split = partitionOrder(list, start, end, statement); //... partition the sublist...
|
354 |
|
|
sortLargestOrder(list, start, split-1, statement); // ... and sort both halves.
|
355 |
|
|
sortLargestOrder(list, split+1, end, statement);
|
356 |
|
|
}
|
357 |
|
|
}
|
358 |
|
|
|
359 |
|
|
void sortSmallestOrder(ObjectArray<ClassName> *list, int start, int end, int statement)
|
360 |
|
|
{
|
361 |
|
|
if ( start < end )
|
362 |
|
|
{ // If there are two or more elements...
|
363 |
|
|
int split = partitionOrder(list,start,end, statement); //... partition the sublist...
|
364 |
|
|
sortSmallestOrder(list, start, split-1, statement ); // ... and sort both halves.
|
365 |
|
|
sortSmallestOrder(list, split+1, end, statement);
|
366 |
|
|
|
367 |
|
|
}
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
ObjectElement<ClassName> **buffer;
|
371 |
|
|
int size;
|
372 |
|
|
int stringLength;
|
373 |
|
|
StringTools *stringTools;
|
374 |
|
|
|
375 |
|
|
};
|
376 |
|
|
|
377 |
|
|
#endif
|