/[cvs]/api/Classes/LinkedList/ObjectLinkedList.h
ViewVC logotype

Contents of /api/Classes/LinkedList/ObjectLinkedList.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Sun Jul 1 20:47:58 2001 UTC (22 years, 10 months ago) by bearsoft
Branch: lazy, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
First import

1 #ifndef __ObjectLinkedList_H__
2 #define __ObjectLinkedList_H__
3
4 #include "../Element/ObjectElement.h"
5
6 template <class ClassName>
7
8 class ObjectLinkedList
9 {
10
11 public:
12
13 ObjectLinkedList()
14 {
15 howfar=0;
16 stringTools = new StringTools();
17 }
18
19 ~ObjectLinkedList()
20 {
21 if (stringTools!=null)
22 {
23 delete stringTools;
24 }
25
26 removeAllElement();
27 }
28
29 ////////////////////////////////////////////////////////////////////////////////////
30 //free memory for all Elements that exsist in the LinkedList
31 ////////////////////////////////////////////////////////////////////////////////////
32
33 void removeAllElement()
34 {
35 if ( howfar != 0 )
36 {
37 ObjectElement<ClassName> *element=first;
38
39 for ( int s=0; s<howfar ; s++ )
40 {
41 ObjectElement<ClassName> *tempElement=element->next;
42 delete element;
43 element=tempElement;
44 }
45 }
46 }
47
48
49 ClassName *addLast(ClassName *iObjectElement)
50 {
51 Parse parse;
52
53 if (howfar == 0)
54 {
55 int length=0;
56 char *iName=parse.intToString(howfar, &length);
57 first = new ObjectElement<ClassName>(iObjectElement,iName);
58 free(iName);
59 last=first;
60 }
61 else
62 {
63 int length=0;
64 char *iName=parse.intToString(howfar, &length);
65 last->next = new ObjectElement<ClassName>(iObjectElement,iName);
66 free(iName);
67 last->next->previos=last;
68 last=last->next;
69 }
70
71 howfar++;
72
73 return last->classNameElement;
74 }
75
76 ////////////////////////////////////////////////////////////////////////////////////
77 //How many Elements is it in the LinkedList
78 ////////////////////////////////////////////////////////////////////////////////////
79
80 int size()
81 {
82 return howfar;
83 }
84
85 ////////////////////////////////////////////////////////////////////////////////////
86 //add a element to a linkedlist, with a element name
87 ////////////////////////////////////////////////////////////////////////////////////
88
89 ClassName *addLast(ClassName *iObjectElement, char *iName)
90 {
91 if (howfar == 0)
92 {
93 first = new ObjectElement<ClassName>(iObjectElement,iName);
94 last=first;
95 }
96 else
97 {
98 last->next = new ObjectElement<ClassName>(iObjectElement, iName);
99 last->next->previos=last;
100 last=last->next;
101 }
102
103 howfar++;
104
105 return last->classNameElement;
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////////
109 //add a element to a linkedlist, with a element name
110 ////////////////////////////////////////////////////////////////////////////////////
111
112 ClassName *addLast(ClassName *iObjectElement, const String &iName)
113 {
114 if (howfar == 0)
115 {
116 first = new ObjectElement<ClassName>(iObjectElement,iName);
117 last=first;
118 }
119 else
120 {
121 last->next = new ObjectElement<ClassName>(iObjectElement, iName);
122 last->next->previos=last;
123 last=last->next;
124 }
125
126 howfar++;
127
128 return last->classNameElement;
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////////
132 //add a element to a linkedlist, with a element name, if the element name already exist
133 //dont input this element take the element that already exist.
134 ////////////////////////////////////////////////////////////////////////////////////
135
136 ClassName *addLastWithNoDoubleName(ClassName *iObjectElement, char *iName)
137 {
138 ClassName *objectElement=checkIfElementNameExist(iName);
139
140 if(objectElement!=null)
141 {
142 return objectElement;
143 }
144
145 if (howfar == 0)
146 {
147 first = new ObjectElement<ClassName>(iObjectElement,iName);
148 last=first;
149 }
150 else
151 {
152 last->next = new ObjectElement<ClassName>(iObjectElement, iName);
153 last->next->previos=last;
154 last=last->next;
155 }
156
157 howfar++;
158
159 return last->classNameElement;
160 }
161
162 ////////////////////////////////////////////////////////////////////////////////////
163 //check if a element with same name exist
164 ////////////////////////////////////////////////////////////////////////////////////
165
166 ClassName *checkIfElementNameExist(char *name)
167 {
168 ObjectElement<ClassName> *element=first;
169
170 for ( int s=0; s<howfar ; s++ )
171 {
172 if (element->name->equals(name))
173 {
174 return (ClassName*)element;
175 }
176 element=element->next;
177 }
178
179 return (ClassName*)null;
180 }
181
182
183 ////////////////////////////////////////////////////////////////////////////////////
184 //get a Element from the LinkedList
185 ////////////////////////////////////////////////////////////////////////////////////
186
187 ClassName *getElement(int index)
188 {
189 ObjectElement<ClassName> *element=first;
190
191 for ( int s=0; s<index ; s++ )
192 {
193 element=element->next;
194 }
195
196 return element->classNameElement;
197 }
198
199
200 ////////////////////////////////////////////////////////////////////////////////////
201 //get a ObjectName from the LinkedList
202 ////////////////////////////////////////////////////////////////////////////////////
203
204 char *getObjectName(int index)
205 {
206 ObjectElement<ClassName> *element=first;
207
208 for ( int s=0; s<index ; s++ )
209 {
210 element=element->next;
211 }
212
213 stringLength=element->name->length;
214
215 return element->name->buffer;
216 }
217
218
219 ////////////////////////////////////////////////////////////////////////////////////
220 //get a ObjectElement from the LinkedList
221 ////////////////////////////////////////////////////////////////////////////////////
222
223 ObjectElement<ClassName> *getObjectElement(int index)
224 {
225 ObjectElement<ClassName> *element=first;
226
227 for ( int s=0; s<index ; s++ )
228 {
229 element=element->next;
230 }
231
232 return (ObjectElement<ClassName>*)element;
233 }
234
235 ////////////////////////////////////////////////////////////////////////////////////
236 //set a Element in the LinkedList
237 ////////////////////////////////////////////////////////////////////////////////////
238
239 void setElement(ClassName *iObjectElement, int index)
240 {
241 if ( index < howfar && index >= 0)
242 {
243 ObjectElement<ClassName> *element=first;
244
245 for ( int s=0; s<index ; s++ )
246 {
247 element=element->next;
248 }
249
250 element->remove();
251 element->classNameElement=iObjectElement;
252 }
253 }
254
255 void setElement(ClassName *iObjectElement, char *iName, int index)
256 {
257 if ( index < howfar && index >= 0)
258 {
259 ObjectElement<ClassName> *element=first;
260
261 for ( int s=0; s<index ; s++ )
262 {
263 element=element->next;
264 }
265
266 element->remove();
267 element->classNameElement=iObjectElement;
268 element->name->setString(iName);
269 }
270 }
271
272 ////////////////////////////////////////////////////////////////////////////////////
273 //insert a new object here, used with sorting
274 ////////////////////////////////////////////////////////////////////////////////////
275
276 void setElement(ObjectElement<ClassName> *iObjectElement, int index)
277 {
278 ObjectElement<ClassName> *element=first;
279
280 for ( int s=0; s<index ; s++ )
281 {
282 element=element->next;
283 }
284
285 element->classNameElement=iObjectElement->classNameElement;
286 element->name->setString(iObjectElement->name->buffer);
287 element->sideNext=iObjectElement->sideNext;
288 element->sidePrevios=iObjectElement->sidePrevios;
289 }
290
291 void setElementPivot(ClassName *iObjectElement, ObjectElement<ClassName> *iSidePrevios, ObjectElement<ClassName> *iSideNext, char *iName, int index)
292 {
293 ObjectElement<ClassName> *element=first;
294
295 for ( int s=0; s<index ; s++ )
296 {
297 element=element->next;
298 }
299
300 element->classNameElement=iObjectElement;
301 element->name->setString(iName);
302 element->sidePrevios=iSidePrevios;
303 element->sideNext=iSideNext;
304 }
305
306 ////////////////////////////////////////////////////////////////////////////////////
307 //remove a element from the linkedlist
308 ////////////////////////////////////////////////////////////////////////////////////
309
310 bool removeElement(int index)
311 {
312 if ( index < howfar && index >= 0)
313 {
314 int temphowfar=howfar;
315 --howfar;
316 ObjectElement<ClassName> *lista = first;
317
318 for ( int w=0; w<index ; w++ )
319 {
320 lista=lista->next;
321 }
322
323 if ( lista == first && temphowfar == 1 )
324 {
325 delete first;
326 return true;
327 }
328
329 if ( lista == first && temphowfar != 1 )
330 {
331 bool s=false;
332
333 if ( lista->next == last )
334 {
335 s=true;
336 }
337
338 ObjectElement<ClassName> *temp=first->next;
339 delete first;
340 first=temp;
341
342 if (s == true)
343 {
344 last=first;
345 }
346
347 return true;
348 }
349
350 if ( lista == last )
351 {
352 ObjectElement<ClassName> *temp=last->previos;
353 delete last;
354 last=temp;
355 return true;
356 }
357
358 int t=0;
359
360 if ( lista->next == last && lista->previos == first )
361 {
362 t=1;
363 }
364
365
366 ObjectElement<ClassName> *tempnext=lista->previos;
367 ObjectElement<ClassName> *tempprevios=lista->next;
368 delete lista;
369 tempnext->next=tempprevios;
370 tempprevios->previos=tempnext;
371
372 if ( t == 1 )
373 {
374 first=tempnext;
375 last=tempprevios;
376 }
377
378 return true;
379 }
380
381 return false;
382 }
383
384 ////////////////////////////////////////////////////////////////////////////////////
385 //sort element in the linkedlist
386 ////////////////////////////////////////////////////////////////////////////////////
387
388 void sortLargestOrder()
389 {
390 sortLargestOrder(this, 0, howfar-1, 1);
391 }
392
393 void sortSmallestOrder()
394 {
395 sortSmallestOrder(this, 0, howfar-1, 0);
396 }
397
398 /////////////////////////////////////////////////////////////////
399 // string sorting
400 /////////////////////////////////////////////////////////////////
401
402 int partitionOrder(ObjectLinkedList<ClassName> *list, int start, int top, int statement)
403 {
404
405 ObjectElement<ClassName> *tempPivot = list->getObjectElement(top);
406 ClassName *pointer=tempPivot->classNameElement;
407 String pivotString=tempPivot->name->buffer;
408 ObjectElement<ClassName> *sidePrevios=tempPivot->sidePrevios;
409 ObjectElement<ClassName> *sideNext=tempPivot->sideNext;
410 char *pivotBuffer = pivotString.buffer; // Partition around the last value
411 int pivotLength = pivotString.length;
412
413 int bottom = start-1; // Start outside the area to be partitioned
414 bool done=true;
415
416 while (done) // Until all elements are partitioned...
417 {
418 while (done) // Until we find an out of place element...
419 {
420 bottom++; // ... move the bottom up.
421 if (bottom == top) // If we hit the top...
422 {
423 done = false; // ... we are done.
424 break;
425 }
426
427 if (statement==0)
428 {
429 ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
430 char *stringBuffer=objectElement->name->buffer;
431 int stringLength=objectElement->name->length;
432
433 if (stringTools->isStringSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
434 {
435 list->setElement(objectElement,top); // Then put at the top... 6=7
436 break;
437 } // ... and start searching from the top.
438 }
439 else
440 {
441 ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
442 char *stringBuffer=objectElement->name->buffer;
443 int stringLength=objectElement->name->length;
444
445 if (stringTools->isStringLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
446 { // Is the bottom out of place?
447 list->setElement(objectElement,top); // Then put at the top... 6=7
448 break;
449 } // ... and start searching from the top.
450 }
451 }
452
453 while (done) // Until we find an out of place element...
454 {
455 --top; // ... move the top down.
456
457 if (top == bottom) // If we hit the bottom...
458 {
459 done = false; // ... we are done.
460 break;
461 }
462
463 if (statement==0)
464 {
465 ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
466 char *stringBuffer=objectElement->name->buffer;
467 int stringLength=objectElement->name->length;
468
469 if (stringTools->isStringLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
470 {
471 list->setElement(objectElement,bottom); // Then put it at the bottom...
472 break;
473 } // ...and start searching from the bottom.
474 }
475 else
476 {
477 ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
478 char *stringBuffer=objectElement->name->buffer;
479 int stringLength=objectElement->name->length;
480
481 if (stringTools->isStringSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
482 {
483 list->setElement(objectElement,bottom); // Then put it at the bottom...
484 break;
485 } // ...and start searching from the bottom.
486 }
487 }
488 }
489
490 list->setElementPivot(pointer, sidePrevios, sideNext, pivotString.buffer, top);
491 return top; // Return the split point
492 }
493
494 void sortLargestOrder(ObjectLinkedList<ClassName> *list, int start, int end, int statement)
495 {
496 if ( start < end )
497 { // If there are two or more elements...
498 int split = partitionOrder(list, start, end, statement); //... partition the sublist...
499 sortLargestOrder(list, start, split-1, statement); // ... and sort both halves.
500 sortLargestOrder(list, split+1, end, statement);
501 }
502 }
503
504 void sortSmallestOrder(ObjectLinkedList<ClassName> *list, int start, int end, int statement)
505 {
506 if ( start < end )
507 { // If there are two or more elements...
508 int split = partitionOrder(list, start, end, statement); //... partition the sublist...
509 sortSmallestOrder(list, start, split-1, statement); // ... and sort both halves.
510 sortSmallestOrder(list, split+1, end, statement);
511
512 }
513 }
514
515
516 ////////////////////////////////////////////////////////////////////////////////////
517 //sort element in the linkedlist
518 ////////////////////////////////////////////////////////////////////////////////////
519
520 void sortLargestValueOrder()
521 {
522 sortLargestValueOrder(this, 0, howfar-1, 1);
523 }
524
525 void sortSmallestValueOrder()
526 {
527 sortSmallestValueOrder(this, 0, howfar-1, 0);
528 }
529
530 /////////////////////////////////////////////////////////////////
531 // string sorting
532 /////////////////////////////////////////////////////////////////
533
534 int partitionValueOrder(ObjectLinkedList<ClassName> *list, int start, int top, int statement)
535 {
536
537 ObjectElement<ClassName> *tempPivot = list->getObjectElement(top);
538 ClassName *pointer=tempPivot->classNameElement;
539 String pivotString=tempPivot->name->buffer;
540 ObjectElement<ClassName> *sidePrevios=tempPivot->sidePrevios;
541 ObjectElement<ClassName> *sideNext=tempPivot->sideNext;
542 char *pivotBuffer = pivotString.buffer; // Partition around the last value
543 int pivotLength = pivotString.length;
544
545 int bottom = start-1; // Start outside the area to be partitioned
546 bool done=true;
547
548 while (done) // Until all elements are partitioned...
549 {
550 while (done) // Until we find an out of place element...
551 {
552 bottom++; // ... move the bottom up.
553 if (bottom == top) // If we hit the top...
554 {
555 done = false; // ... we are done.
556 break;
557 }
558
559 if (statement==0)
560 {
561 ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
562 char *stringBuffer=objectElement->name->buffer;
563 int stringLength=objectElement->name->length;
564
565 if (stringTools->isStringValueSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
566 {
567 list->setElement(objectElement,top); // Then put at the top... 6=7
568 break;
569 } // ... and start searching from the top.
570 }
571 else
572 {
573 ObjectElement<ClassName> *objectElement=list->getObjectElement(bottom);
574 char *stringBuffer=objectElement->name->buffer;
575 int stringLength=objectElement->name->length;
576
577 if (stringTools->isStringValueLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
578 { // Is the bottom out of place?
579 list->setElement(objectElement,top); // Then put at the top... 6=7
580 break;
581 } // ... and start searching from the top.
582 }
583 }
584
585 while (done) // Until we find an out of place element...
586 {
587 --top; // ... move the top down.
588
589 if (top == bottom) // If we hit the bottom...
590 {
591 done = false; // ... we are done.
592 break;
593 }
594
595 if (statement==0)
596 {
597 ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
598 char *stringBuffer=objectElement->name->buffer;
599 int stringLength=objectElement->name->length;
600
601 if (stringTools->isStringValueLarger(stringBuffer,stringLength,pivotBuffer,pivotLength))
602 {
603 list->setElement(objectElement,bottom); // Then put it at the bottom...
604 break;
605 } // ...and start searching from the bottom.
606 }
607 else
608 {
609 ObjectElement<ClassName> *objectElement=list->getObjectElement(top);
610 char *stringBuffer=objectElement->name->buffer;
611 int stringLength=objectElement->name->length;
612
613 if (stringTools->isStringValueSmaller(stringBuffer,stringLength,pivotBuffer,pivotLength))
614 {
615 list->setElement(objectElement,bottom); // Then put it at the bottom...
616 break;
617 } // ...and start searching from the bottom.
618 }
619 }
620 }
621
622 list->setElementPivot(pointer, sidePrevios, sideNext, pivotString.buffer, top);
623 return top; // Return the split point
624 }
625
626 void sortLargestValueOrder(ObjectLinkedList<ClassName> *list, int start, int end, int statement)
627 {
628 if ( start < end )
629 { // If there are two or more elements...
630 int split = partitionValueOrder(list, start, end, statement); //... partition the sublist...
631 sortLargestValueOrder(list, start, split-1, statement); // ... and sort both halves.
632 sortLargestValueOrder(list, split+1, end, statement);
633 }
634 }
635
636 void sortSmallestValueOrder(ObjectLinkedList<ClassName> *list, int start, int end, int statement)
637 {
638 if ( start < end )
639 { // If there are two or more elements...
640 int split = partitionValueOrder(list, start, end, statement); //... partition the sublist...
641 sortSmallestValueOrder(list, start, split-1, statement); // ... and sort both halves.
642 sortSmallestValueOrder(list, split+1, end, statement);
643 }
644 }
645
646
647 ObjectElement<ClassName> *first;
648 ObjectElement<ClassName> *last;
649
650 int stringLength;
651
652 private:
653 int howfar;
654 StringTools *stringTools;
655 };
656
657 #endif

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26