ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GList.hh
Revision: 104
Committed: Mon Oct 10 19:34:20 2011 UTC (7 years, 9 months ago) by gpertea
File size: 41138 byte(s)
Log Message:
fixed GVec, GArray to use new[] instead of malloc

Line User Rev File contents
1 gpertea 2 //---------------------------------------------------------------------------
2     /*
3     Sortable collection of pointers to objects
4     */
5    
6     #ifndef GListHH
7     #define GListHH
8    
9     #include "GBase.h"
10     //#include "assert.h"
11 gpertea 16
12 gpertea 2 #ifdef __LINE__
13 gpertea 16 #define SLISTINDEX_ERR "GList error (%s:%d):Invalid list index: %d\n"
14 gpertea 2 #define TEST_INDEX(x) \
15     if (x<0 || x>=fCount) GError(SLISTINDEX_ERR, __FILE__,__LINE__, x)
16     #else
17 gpertea 16 #define SLISTINDEX_ERR "GList error:Invalid list index: %d\n"
18 gpertea 2 #define TEST_INDEX(x) \
19     if (x<0 || x>=fCount) GError(SLISTINDEX_ERR, x, __FILE__,__LINE__)
20     #endif
21    
22 gpertea 16 #define SLISTCAPACITY_ERR "GList error: invalid capacity: %d\n"
23     #define SLISTCOUNT_ERR "GList error: invalid count: %d\n"
24     #define SLISTSORTED_ERR "Operation not allowed on a sorted list!\n"
25     #define SLISTUNSORTED_ERR "Operation not allowed on an unsorted list!\n"
26 gpertea 2
27     // ------ macros:
28     #define BE_UNSORTED if (fCompareProc!=NULL) { GError(SLISTSORTED_ERR); return; }
29     #define BE_SORTED if (fCompareProc==NULL) { GError(SLISTUNSORTED_ERR); return; }
30    
31     #define MAXLISTSIZE INT_MAX-1
32    
33     #define SORTED (fCompareProc!=NULL)
34     #define UNSORTED (fCompareProc==NULL)
35     #define FREEDATA (fFreeProc!=NULL)
36     /* #define TEST_INDEX(x) assert(x>=0 && x<fCount); \
37     if (x<0 || x>=fCount) GError(SLISTINDEX_ERR, x) */
38    
39    
40 gpertea 16 //template for array of objects; GVec is not sortable,
41     // so it doesn't require comparison operators defined
42     template <class OBJ> class GVec {
43 gpertea 2 protected:
44     OBJ* fArray;
45     int fCount;
46     int fCapacity;
47 gpertea 16 public:
48     GVec(int init_capacity=20);
49     GVec(GVec<OBJ>& array); //copy constructor
50     const GVec<OBJ>& operator=(GVec& array); //copy operator
51     virtual ~GVec();
52     void idxInsert(int idx, OBJ& item);
53     void Grow();
54     void Grow(int idx, OBJ& item);
55     void Reverse(); //WARNING: will break the sort order if SORTED!
56     int Add(OBJ* item); // simply append to the end of fArray, reallocating as needed
57     int Add(OBJ& item) { return Add(&item); } //both will CREATE a new OBJ and COPY to it
58     // using OBJ new operator=
59     void Add(GVec<OBJ>& list); //append copies of all items from another list
60     OBJ& Get(int idx) {
61     TEST_INDEX(idx);
62     return fArray[idx];
63     }
64     OBJ& operator[](int i) {
65     TEST_INDEX(i);
66     return fArray[i];
67     }
68     void Clear();
69     void Insert(int idx, OBJ* item);
70     void Delete(int index);
71     void Replace(int idx, OBJ& item); //Put, use operator= to copy
72     void Exchange(int idx1, int idx2);
73     void Swap(int idx1, int idx2) { Exchange(idx1, idx2); }
74     int Capacity() { return fCapacity; }
75     //this will reject identical items in sorted lists only!
76     void setCapacity(int NewCapacity);
77     int Count() { return fCount; }
78     void setCount(int NewCount); //?! - will trim or expand the array as needed
79     void Move(int curidx, int newidx);
80     };
81    
82 gpertea 104 // GArray is the sortable collection, but requires the comparison operators to be defined
83 gpertea 16 template <class OBJ> class GArray:public GVec<OBJ> {
84     protected:
85 gpertea 2 bool fUnique;
86     static int DefaultCompareProc(OBJ& item1, OBJ& item2) {
87     //the comparison operators MUST be defined for OBJ class!
88     if ( item1 > item2) return 1;
89     else return (item2 > item1) ? -1 : 0 ;
90     }
91     public:
92     typedef int CompareProc(OBJ& item1, OBJ& item2);
93     protected:
94     CompareProc* fCompareProc;
95     void qSort(int L, int R);
96     public:
97     GArray(CompareProc* cmpFunc=NULL);
98     GArray(bool sorted, bool unique=false);
99     GArray(int init_capacity, bool sorted, bool unique=false);
100     GArray(GArray<OBJ>& array); //copy constructor
101     const GArray<OBJ>& operator=(GArray& array);
102 gpertea 16 //~GArray();
103 gpertea 2 //assignment operator
104     void setSorted(CompareProc* cmpFunc);
105     //sort the array if cmpFunc not NULL or changes
106     int Add(OBJ* item); // specific implementation if sorted
107     int Add(OBJ& item) { return Add(&item); } //both will CREATE a new OBJ and COPY to it
108     // using OBJ new operator=
109     void Add(GArray<OBJ>& list); //add copies of all items from another list
110     //this will reject identical items in sorted lists only!
111     void setUnique(bool beUnique) { fUnique = beUnique; };
112     void Sort(); //explicit sort may be requested
113     bool Sorted() { return fCompareProc!=NULL; }
114 gpertea 16 void Replace(int idx, OBJ& item); //Put, use operator= to copy
115     int Unique() { return fUnique; }
116 gpertea 2 int IndexOf(OBJ& item);
117     //this needs the == operator to have been defined for OBJ
118     bool Found(OBJ& item, int& idx); // for sorted arrays only;
119     //search by content; if found, returns true and idx will be the index
120     //of the first item found matching for which CompareProc returns 0
121     bool Exists(OBJ& item); //same as above without existing index info
122     //unsorted only, place item at position idx:
123 gpertea 16 void Move(int curidx, int newidx);
124 gpertea 2 void Insert(int idx, OBJ* item);
125     void Insert(int idx, OBJ& item) { Insert(idx,&item); }
126     };
127    
128     //------- template for array of pointers to objects ---------
129 gpertea 16 template <class OBJ> class GPVec {
130 gpertea 2 protected:
131     OBJ** fList; //pointer to an array of pointers to objects
132     int fCount; //total number of entries in list
133     int fCapacity; //current allocated size
134 gpertea 16 GFreeProc* fFreeProc; //useful for deleting objects
135     //---
136     void Expand();
137     void Grow();
138     void Grow(int idx, OBJ* newitem);
139     void setCount(int NewCount); //will trim/expand the array as needed
140     public:
141     static void DefaultFreeProc(pointer item) {
142     delete (OBJ*)item;
143     }
144     virtual ~GPVec();
145     GPVec(int init_capacity=10, bool free_elements=true); //also the default constructor
146     GPVec(GPVec<OBJ>& list); //copy constructor?
147     GPVec(GPVec<OBJ>* list); //kind of a copy constructor
148     const GPVec<OBJ>& operator=(GPVec& list);
149     OBJ* Get(int i);
150     OBJ* operator[](int i) { return this->Get(i); }
151     void Reverse(); //reverse pointer array; WARNING: will break the sort order if sorted!
152     void freeItem(int idx); //calls fFreeProc (or DefaultFreeProc) on fList[idx] and sets NULL there, doesn't pack!
153     //it will free even if fFreeProc is NULL!
154     void setFreeItem(GFreeProc *freeProc) { fFreeProc=freeProc; }
155     void setFreeItem(bool doFree) {
156     if (doFree) fFreeProc=DefaultFreeProc;
157     else fFreeProc=NULL;
158     }
159     // -- stack usage:
160     int Push(OBJ* item) { return Add(item); }
161     OBJ* Pop();// Stack use; removes and returns last item,but does NOT FREE it
162     OBJ* Shift(); //Queue use: removes and returns first item, but does NOT FREE it
163     void deallocate_item(OBJ* item); //forcefully call fFreeProc or delete on item
164     void Clear();
165     void Exchange(int idx1, int idx2);
166 gpertea 60 void Swap(int idx1, int idx2) { Exchange(idx1, idx2); }
167 gpertea 16 OBJ* First() { return (fCount>0)?fList[0]:NULL; }
168     OBJ* Last() { return (fCount>0)?fList[fCount-1]:NULL;}
169     bool isEmpty() { return fCount==0; }
170     bool notEmpty() { return fCount>0; }
171     int Capacity() { return fCapacity; }
172     int Count() { return fCount; }
173     void setCapacity(int NewCapacity);
174     int Add(OBJ* item); //simply append the pointer copy
175     void Add(GPVec<OBJ>& list); //add all pointers from another list
176     void Insert(int idx, OBJ* item);
177     void Move(int curidx, int newidx);
178     void Put(int idx, OBJ* item);
179     void Pack();
180     void Delete(int index); //also frees the item if fFreeProc!=NULL, and shifts the successor items
181     void Forget(int idx); //simply places a NULL at fList[idx], nothing else
182     int RemovePtr(pointer item); //always use linear search to find the pointer! calls Delete() if found
183     int IndexOf(pointer item); //a linear search for pointer address only!
184     };
185    
186     template <class OBJ> class GList:public GPVec<OBJ> {
187     protected:
188 gpertea 2 bool fUnique;
189     GCompareProc* fCompareProc; //a pointer to a Compare function
190     static int DefaultCompareProc(const pointer item1, const pointer item2) {
191     //the comparison operators MUST be defined for OBJ class!
192     if (*((OBJ*)item1) > *((OBJ*)item2)) return 1;
193     else if (*((OBJ*)item2) > *((OBJ*)item1)) return -1;
194     else return 0;
195     }
196     void QuickSort(int L, int R);
197     public:
198     void sortInsert(int idx, OBJ* item);
199     GList(GCompareProc* compareProc=NULL); //free by default
200     GList(GCompareProc* compareProc, //unsorted by default
201     GFreeProc *freeProc,
202     bool beUnique=false);
203     GList(bool sorted, bool free_elements=true, bool beUnique=false);
204     GList(int init_capacity, bool sorted, bool free_elements=true, bool beUnique=false);
205     GList(GList<OBJ>& list); //copy constructor?
206     GList(GList<OBJ>* list); //kind of a copy constructor
207 gpertea 16 const GList<OBJ>& operator=(GList& list);
208     //void Clear();
209     //~GList();
210 gpertea 2 void setSorted(GCompareProc* compareProc);
211     //sorted if compareProc not NULL; sort the list if compareProc changes !
212     bool Sorted() { return fCompareProc!=NULL; }
213     void setSorted(bool sorted) {
214     if (sorted) {
215     if (fCompareProc!=&DefaultCompareProc) {
216     fCompareProc=&DefaultCompareProc;
217     Sort();
218     }
219     }
220     else fCompareProc=NULL;
221     }
222     int Add(OBJ* item); //-- specific implementation if sorted
223     void Add(GList<OBJ>& list); //add all pointers from another list
224    
225     OBJ* AddIfNew(OBJ* item, bool deleteIfFound=true, int* fidx=NULL);
226     // default: delete item if Found() (and pointers are not equal)!
227     //returns the equal (==) object if it's in the list already
228 gpertea 16 //or the item itself if it is unique and actually added
229 gpertea 2
230 gpertea 16 int AddedIfNew(OBJ* item);
231     // if Found(item) (and pointers are not equal) delete item and returns -1
232     // if added, returns the new item index
233    
234    
235 gpertea 2 int Unique() { return fUnique; }
236     //this will reject identical items in sorted lists only!
237     void setUnique(bool beUnique) { fUnique = beUnique; };
238    
239     GCompareProc* GetCompareProc() {return fCompareProc;}
240     int IndexOf(OBJ* item); //this has a specific implementation for sorted lists
241     //if list is sorted, item data is located by binary search
242     //based on the Compare function
243     //if not, a linear search is performed, but
244     //this needs the == operator to have been defined for OBJ
245 gpertea 16
246     void Put(int idx, OBJ* item, bool re_sort=false);
247 gpertea 2 bool Found(OBJ* item, int & idx); // sorted only;
248     //search by content; if found, returns true and idx will be the index
249     //of the first item found matching for which GTCompareProc returns 0
250     bool Exists(OBJ* item); //same as above without existing index info
251     bool Exists(OBJ& item); //same as above without existing index info
252 gpertea 16 void Sort(); //explicit sort may be requested using this function
253     int Remove(OBJ* item); //search for pointer, using binary search if sorted
254 gpertea 2 void Insert(int idx, OBJ* item); //unsorted only, place item at position idx
255     void Move(int curidx, int newidx);
256 gpertea 16 }; //GList
257 gpertea 2
258    
259 gpertea 16 //basic template for a Stack of pointers (implemented as a linked list)
260 gpertea 2 template <class OBJ> class GStack {
261     protected:
262     struct StackOBJ {
263     OBJ* obj;
264     StackOBJ* prev;
265     };
266     int fCount; //total number of elements in stack
267     StackOBJ* base;
268     StackOBJ* top;
269     public:
270     GStack(OBJ* po=NULL) {
271     base=NULL;
272     top=NULL;
273     fCount=0;
274     if (po!=NULL) Push(po);
275     }
276     ~GStack() {
277     while (fCount>0) Pop();
278     }
279     bool isEmpty() { return fCount==0; }
280     int Size() { return fCount; }
281     int Count() { return fCount; }
282     OBJ* Pop() {
283     if (top==NULL) return NULL;
284     fCount--;
285     StackOBJ* ctop=top;
286     if (top==base) base=NULL;
287     OBJ* r=top->obj;
288     top=top->prev;
289     GFREE(ctop);
290     return r;
291     }
292     OBJ* Push(OBJ* o) {
293     fCount++;
294     StackOBJ* ctop=top; //could be NULL
295     GMALLOC(top, sizeof(StackOBJ));
296     top->obj=o;
297     top->prev=ctop;
298     if (base==NULL) base=top;
299     return o;
300     }
301     OBJ* Top() { return ((top==NULL)? NULL : top->obj); }
302     OBJ* Base() { return ((base==NULL)? NULL : base->obj); }
303     };
304    
305     //-------------------- TEMPLATE IMPLEMENTATION-------------------------------
306    
307 gpertea 16 template <class OBJ> GVec<OBJ>::GVec(int init_capacity) {
308     fCount=0;
309     fCapacity=0;
310     fArray=NULL;
311     setCapacity(init_capacity);
312     }
313    
314     template <class OBJ> GVec<OBJ>::GVec(GVec<OBJ>& array) { //copy constructor
315     this->fCount=array.fCount;
316     this->fCapacity=array.fCapacity;
317     this->fArray=NULL;
318     if (this->fCapacity>0) {
319 gpertea 104 //GMALLOC(fArray, fCapacity*sizeof(OBJ));
320     fArray=new OBJ[this->fCapacity];
321 gpertea 2 }
322 gpertea 16 this->fCount=array.fCount;
323     // uses OBJ operator=
324     for (int i=0;i<this->fCount;i++) fArray[i]=array[i];
325     }
326    
327     template <class OBJ> GArray<OBJ>::GArray(GArray<OBJ>& array):GVec<OBJ>(0) { //copy constructor
328     this->fCount=array.fCount;
329     this->fCapacity=array.fCapacity;
330     this->fArray=NULL;
331     if (this->fCapacity>0) {
332 gpertea 104 //GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
333     this->fArray=new OBJ[this->fCapacity];
334 gpertea 16 }
335     this->fCount=array.fCount;
336 gpertea 2 fUnique=array.fUnique;
337     fCompareProc=array.fCompareProc;
338     // uses OBJ operator=
339 gpertea 16 for (int i=0;i<this->fCount;i++) this->fArray[i]=array[i];
340 gpertea 2 }
341    
342 gpertea 16 template <class OBJ> const GVec<OBJ>& GVec<OBJ>::operator=(GVec<OBJ>& array) {
343 gpertea 2 if (&array==this) return *this;
344     Clear();
345     fCount=array.fCount;
346     fCapacity=array.fCapacity;
347     if (fCapacity>0) {
348 gpertea 104 //GMALLOC(fArray, fCapacity*sizeof(OBJ));
349     fArray=new OBJ[this->fCapacity];
350 gpertea 2 }
351     fCount=array.fCount;
352     // uses OBJ operator=
353     for (int i=0;i<fCount;i++) {
354     fArray[i]=array[i];
355     }
356     return *this;
357     }
358 gpertea 16
359     template <class OBJ> const GArray<OBJ>& GArray<OBJ>::operator=(GArray<OBJ>& array) {
360     if (&array==this) return *this;
361     GVec<OBJ>::Clear();
362     this->fCount=array.fCount;
363     this->fUnique=array.fUnique;
364     this->fCapacity=array.fCapacity;
365     if (this->fCapacity>0) {
366 gpertea 104 //GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
367     this->fArray=new OBJ[this->fCapacity];
368 gpertea 16 }
369     this->fCompareProc=array.fCompareProc;
370     this->fCount=array.fCount;
371     // uses OBJ operator=
372     for (int i=0;i<this->fCount;i++) {
373     this->fArray[i]=array[i];
374     }
375     return *this;
376     }
377    
378     template <class OBJ> GArray<OBJ>::GArray(CompareProc* cmpFunc):GVec<OBJ>(0) {
379 gpertea 2 fCompareProc = cmpFunc;
380     fUnique = false; //only affects sorted lists
381     }
382    
383 gpertea 16 template <class OBJ> GArray<OBJ>::GArray(bool sorted, bool unique):GVec<OBJ>(0) {
384 gpertea 2 fUnique=unique;
385     fCompareProc=sorted? &DefaultCompareProc : NULL;
386     }
387    
388     template <class OBJ> GArray<OBJ>::GArray(int init_capacity,
389 gpertea 16 bool sorted, bool unique):GVec<OBJ>(init_capacity) {
390 gpertea 2 fUnique=unique;
391     fCompareProc=sorted? &DefaultCompareProc : NULL;
392     }
393    
394 gpertea 16 template <class OBJ> GVec<OBJ>::~GVec() {
395     this->Clear();
396 gpertea 2 }
397    
398 gpertea 16
399     template <class OBJ> void GVec<OBJ>::setCapacity(int NewCapacity) {
400 gpertea 2 if (NewCapacity < fCount || NewCapacity > MAXLISTSIZE)
401     GError(SLISTCAPACITY_ERR, NewCapacity);
402     //error: capacity not within range
403     if (NewCapacity!=fCapacity) {
404     if (NewCapacity==0) {
405 gpertea 104 //GFREE(fArray);
406     delete[] fArray;
407 gpertea 2 }
408     else {
409 gpertea 104 //GREALLOC(fArray, NewCapacity*sizeof(OBJ));
410     OBJ* oldArray=fArray;
411     fArray=new OBJ[NewCapacity];
412     for (int i=0;i<this->fCount;i++) {
413     fArray[i] = oldArray[i];
414     }
415 gpertea 2 }
416     fCapacity=NewCapacity;
417     }
418     }
419    
420 gpertea 16 template <class OBJ> void GVec<OBJ>::Clear() {
421     setCount(0);
422     setCapacity(0); //so the array itself is deallocated too!
423     }
424    
425     /*
426 gpertea 2 template <class OBJ> void GArray<OBJ>::Clear() {
427     CompareProc* fcmp=fCompareProc;
428     fCompareProc=NULL;
429 gpertea 16 GVec<OBJ>::setCount(0);
430     GVec<OBJ>::setCapacity(0); //so the array itself is deallocated too!
431 gpertea 2 fCompareProc=fcmp;
432     }
433 gpertea 16 */
434 gpertea 2 template <class OBJ> void GArray<OBJ>::setSorted(CompareProc* cmpFunc) {
435     CompareProc* old_proc=fCompareProc;
436     fCompareProc=cmpFunc;
437     if (fCompareProc!=old_proc && fCompareProc!=NULL)
438     Sort(); //new compare method
439     }
440    
441 gpertea 16 template <class OBJ> void GVec<OBJ>::Grow() {
442 gpertea 2 int delta;
443     if (fCapacity > 64) delta = fCapacity/4;
444     else if (fCapacity > 8) delta = 16;
445     else delta = 4;
446     setCapacity(fCapacity + delta);
447     }
448    
449 gpertea 16 template <class OBJ> void GVec<OBJ>::Reverse() {
450 gpertea 2 int l=0;
451     int r=fCount-1;
452     OBJ c;
453     while (l<r) {
454     c=fArray[l];fArray[l]=fArray[r];
455     fArray[r]=c;
456     l++;r--;
457     }
458     }
459    
460 gpertea 16 template <class OBJ> void GVec<OBJ>::Grow(int idx, OBJ& item) {
461 gpertea 2 int delta;
462     if (fCapacity > 64) delta = fCapacity/4;
463     else if (fCapacity > 8) delta = 16;
464     else delta = 4;
465     int NewCapacity=fCapacity+delta;
466     if (NewCapacity <= fCount || NewCapacity >= MAXLISTSIZE)
467     GError(SLISTCAPACITY_ERR, NewCapacity);
468     //error: capacity not within range
469 gpertea 104
470 gpertea 2 if (NewCapacity!=fCapacity) {
471 gpertea 16 if (NewCapacity==0) {
472 gpertea 104 //GFREE(fArray);
473     delete[] fArray;
474     fArray=NULL;
475 gpertea 16 }
476 gpertea 2 else { //add the new item
477     if (idx==fCount) { //append item
478 gpertea 104 //GREALLOC(fArray, NewCapacity*sizeof(OBJ));
479     setCapacity(NewCapacity);
480 gpertea 2 fArray[idx]=item;
481     }
482     else { //insert item at idx
483     OBJ* newList;
484 gpertea 104 //GMALLOC(newList, NewCapacity*sizeof(OBJ));
485     newList=new OBJ[NewCapacity];
486 gpertea 2 //copy data before idx
487 gpertea 104 //memmove(&newList[0],&fArray[0], idx*sizeof(OBJ));
488     // operator= required!
489     for (int i=0;i<idx;i++) {
490     newList[i]=fArray[i];
491     }
492     newList[idx]=item;
493 gpertea 2 //copy data after idx
494 gpertea 104 //memmove(&newList[idx+1],&fArray[idx], (fCount-idx)*sizeof(OBJ));
495     for (int i=idx+1;i<fCount;i++) {
496     newList[i]=fArray[i-1];
497     }
498     //memset(&newList[fCount+1], 0, (NewCapacity-fCount-1)*sizeof(OBJ));
499 gpertea 2 //data copied:
500 gpertea 104 //GFREE(fArray);
501     delete[] fArray;
502 gpertea 2 fArray=newList;
503     }
504     fCount++;
505     }
506     fCapacity=NewCapacity;
507     }
508     }
509    
510     template <class OBJ> int GArray<OBJ>::IndexOf(OBJ& item) {
511     int result=0;
512     if (Found(item, result)) return result;
513     else return -1;
514     }
515    
516     template <class OBJ> bool GArray<OBJ>::Exists(OBJ& item) {
517     int result=0;
518     if (Found(item, result)) return true;
519     else return false;
520     }
521    
522    
523 gpertea 16 template <class OBJ> int GVec<OBJ>::Add(OBJ* item) {
524     if (item==NULL) return -1;
525     int result=fCount;
526     if (result==fCapacity) Grow();
527     fArray[result] = *item; //OBJ::operator= must copy OBJ properly!
528     fCount++;
529     return result;
530     }
531    
532 gpertea 2 template <class OBJ> int GArray<OBJ>::Add(OBJ* item) {
533     if (item==NULL) return -1;
534     int result;
535     if (SORTED) {
536     if (Found(*item, result))
537     if (fUnique) return -1; //cannot add a duplicate!
538     //Found sets result to the position where the item should be!
539 gpertea 16 this->idxInsert(result, *item);
540 gpertea 2 }
541     else {
542     if (fUnique && Found(*item,result)) return -1; //set behaviour
543 gpertea 16 result = this->fCount;
544     if (result==this->fCapacity) GVec<OBJ>::Grow();
545     this->fArray[result] = *item; //operator=, copies the item
546     this->fCount++;
547 gpertea 2 }
548     return result;
549     }
550    
551 gpertea 16
552     template <class OBJ> void GVec<OBJ>::Add(GVec<OBJ>& list) {
553     if (list.Count()==0) return;
554     //simply copy
555     setCapacity(fCapacity+list.fCount);
556     int s=fCount;
557     for (int i=0;i<list.fCount;i++)
558     fArray[s+i]=list.fArray[i];
559     fCount+=list.fCount;
560     }
561    
562    
563 gpertea 2 template <class OBJ> void GArray<OBJ>::Add(GArray<OBJ>& list) {
564     if (list.Count()==0) return;
565     if (SORTED) {
566     for (int i=0;i<list.fCount;i++) Add(&list[i]);
567     }
568     else { //simply copy
569 gpertea 16 this->setCapacity(this->fCapacity+list.fCount);
570     int s=this->fCount;
571 gpertea 2 for (int i=0;i<list.fCount;i++)
572 gpertea 16 this->fArray[s+i]=list.fArray[i];
573     this->fCount+=list.fCount;
574 gpertea 2 }
575     }
576    
577     template <class OBJ> bool GArray<OBJ>::Found(OBJ& item, int& idx) {
578     //search the list by using CompareProc (if defined)
579     //or == operator for a non-sortable list
580     //for sorted lists, even when the result is false, the idx is
581     //set to the closest matching object!
582     int i;
583     idx=-1;
584 gpertea 16 if (this->fCount==0) { idx=0;return false;}
585 gpertea 2 if (SORTED) { //binary search based on CompareProc
586     //do the simplest tests first:
587 gpertea 16 if ((*fCompareProc)(this->fArray[0],item)>0) {
588 gpertea 2 idx=0;
589     return false;
590     }
591 gpertea 16 if ((*fCompareProc)(item, this->fArray[this->fCount-1])>0) {
592     idx=this->fCount;
593 gpertea 2 return false;
594     }
595    
596     int l=0;
597 gpertea 16 int h = this->fCount - 1;
598 gpertea 2 int c;
599     while (l <= h) {
600     i = (l + h) >> 1;
601 gpertea 16 c = (*fCompareProc)(this->fArray[i], item);
602 gpertea 2 if (c < 0) l = i + 1;
603     else {
604     h = i - 1;
605     if (c == 0) { //found!
606     idx=i;
607     return true;
608     }
609     }
610     } //while
611     idx = l;
612     return false;
613     }
614     else {//not sorted: use linear search
615     // needs == operator to compare user defined objects !
616     i=0;
617 gpertea 16 while (i<this->fCount) {
618     if (this->fArray[i]==item) { //requires operator==
619 gpertea 2 idx=i;
620     return true;
621     }
622     i++;
623     }
624     return false;
625     }
626     }
627    
628 gpertea 16 template <class OBJ> void GVec<OBJ>::idxInsert(int idx, OBJ& item) {
629 gpertea 2 //idx must be the new position this new item must have
630     //so the allowed range is [0..fCount]
631     //the old idx item all the above will be shifted to idx+1
632     if (idx<0 || idx>fCount) GError(SLISTINDEX_ERR, idx);
633     if (fCount==fCapacity) { //need to resize
634     Grow(idx, item);
635     //expand and also copy/move data and insert the new item
636     return;
637     }
638     //move data around to make room for the new item
639     if (idx<fCount)
640     memmove(&fArray[idx+1], &fArray[idx], (fCount-idx)*sizeof(OBJ));
641     fArray[idx]=item;
642     fCount++;
643     }
644    
645 gpertea 16 template <class OBJ> void GVec<OBJ>::Insert(int idx, OBJ* item) {
646     //idx can be [0..fCount] so an item can be actually added
647     idxInsert(idx, item);
648     }
649    
650 gpertea 2 template <class OBJ> void GArray<OBJ>::Insert(int idx, OBJ* item) {
651     //idx can be [0..fCount] so an item can be actually added
652     BE_UNSORTED; //forbid this operation on sorted data
653     idxInsert(idx, item);
654     }
655    
656 gpertea 16
657     template <class OBJ> void GVec<OBJ>::Move(int curidx, int newidx) {
658 gpertea 2 if (curidx!=newidx || newidx>=fCount)
659     GError(SLISTINDEX_ERR, newidx);
660     OBJ tmp=fArray[curidx]; //copy constructor here
661     fArray[curidx]=fArray[newidx];
662     fArray[newidx]=tmp;
663     }
664    
665 gpertea 16
666     template <class OBJ> void GArray<OBJ>::Move(int curidx, int newidx) {
667     BE_UNSORTED; //cannot do this in a sorted list!
668     if (curidx!=newidx || newidx>=this->fCount)
669     GError(SLISTINDEX_ERR, newidx);
670    
671     OBJ tmp=this->fArray[curidx]; //copy constructor here
672     this->fArray[curidx]=this->fArray[newidx];
673     this->fArray[newidx]=tmp;
674     }
675    
676     template <class OBJ> void GVec<OBJ>::Replace(int idx, OBJ& item) {
677 gpertea 2 TEST_INDEX(idx);
678     fArray[idx]=item;
679     }
680    
681 gpertea 60 template <class OBJ> void GVec<OBJ>::Exchange(int idx1, int idx2) {
682     TEST_INDEX(idx1);
683     TEST_INDEX(idx2);
684     OBJ item=fArray[idx1];
685     fArray[idx1]=fArray[idx2];
686     fArray[idx2]=item;
687     }
688    
689    
690 gpertea 16 template <class OBJ> void GArray<OBJ>::Replace(int idx, OBJ& item) {
691     //TEST_INDEX(idx);
692     if (idx<0 || idx>=this->fCount) GError(SLISTINDEX_ERR, __FILE__,__LINE__, idx);
693     this->fArray[idx]=item;
694     if ( SORTED ) Sort(); //re-sort ! this could be very expensive, don't do it
695     }
696    
697    
698     template <class OBJ> void GVec<OBJ>::Delete(int index) {
699 gpertea 2 TEST_INDEX(index);
700     //fArray[index]=NULL;
701     fCount--;
702     if (index<fCount) //move higher elements if any
703     memmove(&fArray[index], &fArray[index+1], (fCount-index)*sizeof(OBJ));
704     }
705    
706 gpertea 16 template <class OBJ> void GVec<OBJ>::setCount(int NewCount) {
707 gpertea 2 if (NewCount<0 || NewCount > MAXLISTSIZE)
708     GError(SLISTCOUNT_ERR, NewCount);
709     if (NewCount > fCapacity) setCapacity(NewCount);
710 gpertea 104 //if (NewCount > fCount)
711     // memset(&fArray[fCount], 0, (NewCount - fCount) * sizeof(OBJ));
712 gpertea 2 fCount = NewCount;
713     }
714    
715     template <class OBJ> void GArray<OBJ>::qSort(int l, int r) {
716     int i, j;
717     OBJ p,t;
718     do {
719     i = l; j = r;
720 gpertea 16 p = this->fArray[(l + r) >> 1];
721 gpertea 2 do {
722 gpertea 16 while (fCompareProc(this->fArray[i], p) < 0) i++;
723     while (fCompareProc(this->fArray[j], p) > 0) j--;
724 gpertea 2 if (i <= j) {
725 gpertea 16 t = this->fArray[i];
726     this->fArray[i] = this->fArray[j];
727     this->fArray[j] = t;
728 gpertea 2 i++; j--;
729     }
730     } while (i <= j);
731     if (l < j) qSort(l, j);
732     l = i;
733     } while (i < r);
734     }
735    
736     template <class OBJ> void GArray<OBJ>::Sort() {
737 gpertea 16 if (this->fArray!=NULL && this->fCount>0 && fCompareProc!=NULL)
738     qSort(0, this->fCount-1);
739 gpertea 2 }
740    
741     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
742 gpertea 16 //*=> GPVec and GList implementation -- sortable array of pointers to OBJ
743 gpertea 2
744 gpertea 16 template <class OBJ> GPVec<OBJ>::GPVec(GPVec& list) { //copy constructor
745 gpertea 2 fCount=list.fCount;
746     fCapacity=list.fCapacity;
747     if (fCapacity>0) {
748     GMALLOC(fList, fCapacity*sizeof(OBJ*));
749     }
750     fFreeProc=list.fFreeProc;
751     fCount=list.fCount;
752     memcpy(fList, list.fList, fCount*sizeof(OBJ*));
753     //for (int i=0;i<list.Count();i++) Add(list[i]);
754     }
755    
756 gpertea 16 template <class OBJ> GPVec<OBJ>::GPVec(GPVec* plist) { //another copy constructor
757 gpertea 2 fCount=0;
758     fCapacity=plist->fCapacity;
759 gpertea 16 fList=NULL;
760 gpertea 2 if (fCapacity>0) {
761     GMALLOC(fList, fCapacity*sizeof(OBJ*));
762     }
763     fFreeProc=plist->fFreeProc;
764     fCount=plist->fCount;
765     memcpy(fList, plist->fList, fCount*sizeof(OBJ*));
766     //for (int i=0;i<list->fCount;i++) Add(plist->Get(i));
767     }
768    
769 gpertea 16 template <class OBJ> const GPVec<OBJ>& GPVec<OBJ>::operator=(GPVec& list) {
770     if (&list!=this) {
771     Clear();
772     fFreeProc=list.fFreeProc;
773     //Attention: the object *POINTERS* are copied,
774     // but the actual object content is NOT duplicated
775     for (int i=0;i<list.Count();i++) Add(list[i]);
776     }
777     return *this;
778     }
779    
780    
781     template <class OBJ> void GPVec<OBJ>::Add(GPVec<OBJ>& list) {
782     if (list.Count()==0) return;
783     //simply copy the pointers! -- the objects will be shared
784     setCapacity(fCapacity+list.fCount);
785     memcpy( & (fList[fCount]), list.fList, list.fCount*sizeof(OBJ*));
786     fCount+=list.fCount;
787     }
788    
789    
790     template <class OBJ> GList<OBJ>::GList(GList<OBJ>& list):GPVec<OBJ>(list) { //copy constructor
791     fUnique=list.fUnique;
792     fCompareProc=list.fCompareProc;
793     }
794    
795     template <class OBJ> GList<OBJ>::GList(GList<OBJ>* plist):GPVec<OBJ>(0) { //another copy constructor
796     this->fCapacity=plist->fCapacity;
797     this->fList=NULL;
798     if (this->fCapacity>0) {
799     GMALLOC(this->fList, this->fCapacity*sizeof(OBJ*));
800     }
801     fUnique=plist->fUnique;
802     fCompareProc=plist->fCompareProc;
803     this->fFreeProc=plist->fFreeProc;
804     this->fCount=plist->fCount;
805     memcpy(this->fList, plist->fList, this->fCount*sizeof(OBJ*));
806     //for (int i=0;i<list->fCount;i++) Add(plist->Get(i));
807     }
808    
809 gpertea 2 template <class OBJ> void GList<OBJ>::Add(GList<OBJ>& list) {
810     if (list.Count()==0) return;
811     if (SORTED) {
812     for (int i=0;i<list.Count();i++) Add(list[i]);
813     }
814     else { //simply copy
815 gpertea 16 this->setCapacity(this->fCapacity+list.fCount);
816     memcpy( & (this->fList[this->fCount]), list.fList, list.fCount*sizeof(OBJ*));
817     this->fCount+=list.fCount;
818 gpertea 2 }
819     }
820    
821    
822     template <class OBJ> GList<OBJ>::GList(GCompareProc* compareProc,
823     GFreeProc* freeProc, bool beUnique) {
824     fCompareProc = compareProc;
825 gpertea 16 this->fFreeProc = freeProc;
826 gpertea 2 fUnique = beUnique; //only affects sorted lists
827     }
828    
829     template <class OBJ> GList<OBJ>::GList(GCompareProc* compareProc) {
830     fCompareProc = compareProc;
831 gpertea 16 this->fFreeProc = GPVec<OBJ>::DefaultFreeProc;
832 gpertea 2 fUnique = false; //only affects sorted lists
833     }
834    
835 gpertea 16 template <class OBJ> void GPVec<OBJ>::Reverse() {
836 gpertea 2 int l=0;
837     int r=fCount-1;
838     OBJ* c;
839     while (l<r) {
840     c=fList[l];fList[l]=fList[r];
841     fList[r]=c;
842     l++;r--;
843     }
844     }
845    
846     template <class OBJ> GList<OBJ>::GList(bool sorted,
847     bool free_elements, bool beUnique) {
848     if (sorted) {
849     if (free_elements) {
850     fCompareProc=&DefaultCompareProc;
851 gpertea 16 this->fFreeProc = GPVec<OBJ>::DefaultFreeProc;
852 gpertea 2 fUnique=beUnique;
853     }
854     else {
855     fCompareProc=&DefaultCompareProc;
856 gpertea 16 this->fFreeProc=NULL;
857 gpertea 2 fUnique=beUnique;
858     }
859     }
860     else {
861     if (free_elements) {
862     fCompareProc=NULL;
863 gpertea 16 this->fFreeProc=GPVec<OBJ>::DefaultFreeProc;
864 gpertea 2 fUnique=beUnique;
865     }
866     else {
867     fCompareProc=NULL;
868 gpertea 16 this->fFreeProc=NULL;
869 gpertea 2 fUnique=beUnique;
870     }
871     }
872     }
873    
874 gpertea 16 template <class OBJ> GPVec<OBJ>::GPVec(int init_capacity, bool free_elements) {
875 gpertea 2 fCount=0;
876     fCapacity=0;
877     fList=NULL;
878 gpertea 16 fFreeProc=(free_elements) ? DefaultFreeProc : NULL;
879     setCapacity(init_capacity);
880     }
881    
882    
883     template <class OBJ> GList<OBJ>::GList(int init_capacity, bool sorted,
884     bool free_elements, bool beUnique):GPVec<OBJ>(init_capacity, free_elements) {
885 gpertea 2 if (sorted) {
886 gpertea 16 fCompareProc=&DefaultCompareProc;
887     fUnique=beUnique;
888     }
889 gpertea 2 else {
890 gpertea 16 fCompareProc=NULL;
891     fUnique=beUnique;
892     }
893 gpertea 2 }
894    
895 gpertea 16 template <class OBJ> GPVec<OBJ>::~GPVec() {
896     this->Clear();//this will free the items if fFreeProc is defined
897     }
898    
899     /*
900 gpertea 2 template <class OBJ> GList<OBJ>::~GList() {
901 gpertea 16 this->Clear();//this will free the items if fFreeProc is defined
902 gpertea 2 }
903 gpertea 16 */
904 gpertea 2
905 gpertea 16 template <class OBJ> void GPVec<OBJ>::setCapacity(int NewCapacity) {
906 gpertea 2 if (NewCapacity < fCount || NewCapacity > MAXLISTSIZE)
907     GError(SLISTCAPACITY_ERR, NewCapacity);
908     //error: capacity not within range
909     if (NewCapacity!=fCapacity) {
910 gpertea 16 if (NewCapacity==0) {
911 gpertea 2 GFREE(fList);
912 gpertea 16 }
913     else {
914 gpertea 2 GREALLOC(fList, NewCapacity*sizeof(OBJ*));
915 gpertea 16 }
916 gpertea 2 fCapacity=NewCapacity;
917     }
918     }
919    
920 gpertea 16 template <class OBJ> void GPVec<OBJ>::deallocate_item(OBJ* item) {
921     if (item==NULL) return;
922     if (FREEDATA) {
923     (*fFreeProc)(item);
924     }
925     else {
926     delete item;
927     }
928 gpertea 2 }
929    
930 gpertea 16 template <class OBJ> void GPVec<OBJ>::Clear() {
931 gpertea 2 if (FREEDATA) {
932     for (int i=0; i<fCount; i++) {
933     (*fFreeProc)(fList[i]);
934     }
935     }
936     setCount(0);
937     setCapacity(0); //so the array itself is deallocated too!
938     }
939    
940 gpertea 16 template <class OBJ> void GPVec<OBJ>::Exchange(int idx1, int idx2) {
941 gpertea 60 //Warning: this will BREAK sort order for sorted GList
942 gpertea 2 TEST_INDEX(idx1);
943     TEST_INDEX(idx2);
944     OBJ* item=fList[idx1];
945     fList[idx1]=fList[idx2];
946     fList[idx2]=item;
947     }
948    
949 gpertea 16 template <class OBJ> void GPVec<OBJ>::Expand() {
950 gpertea 2 if (fCount==fCapacity) Grow();
951     //return this;
952     }
953    
954 gpertea 16 template <class OBJ> OBJ* GPVec<OBJ>::Get(int idx) {
955 gpertea 2 TEST_INDEX(idx);
956     return fList[idx];
957     }
958    
959     template <class OBJ> const GList<OBJ>& GList<OBJ>::operator=(GList& list) {
960     if (&list!=this) {
961 gpertea 16 GPVec<OBJ>::Clear();
962 gpertea 2 fCompareProc=list.fCompareProc;
963 gpertea 16 this->fFreeProc=list.fFreeProc;
964 gpertea 2 //Attention: the object pointers are copied directly,
965     //but the actual objects are NOT duplicated
966     for (int i=0;i<list.Count();i++) Add(list[i]);
967     }
968     return *this;
969     }
970    
971     template <class OBJ> void GList<OBJ>::setSorted(GCompareProc* compareProc) {
972     GCompareProc* old_proc=fCompareProc;
973     fCompareProc=compareProc;
974     if (fCompareProc!=old_proc && fCompareProc!=NULL)
975     Sort(); //new compare method
976     }
977    
978 gpertea 16 template <class OBJ> void GPVec<OBJ>::Grow() {
979 gpertea 2 int delta;
980     if (fCapacity > 64) delta = fCapacity/4;
981     else if (fCapacity > 8) delta = 16;
982     else delta = 4;
983     setCapacity(fCapacity + delta);
984     }
985    
986 gpertea 16 template <class OBJ> void GPVec<OBJ>::Grow(int idx, OBJ* newitem) {
987 gpertea 2 int delta;
988     if (fCapacity > 64) delta = fCapacity/4;
989     else if (fCapacity > 8) delta = 16;
990     else delta = 4;
991     // setCapacity(fCapacity + delta);
992     int NewCapacity=fCapacity+delta;
993     if (NewCapacity <= fCount || NewCapacity > MAXLISTSIZE)
994     GError(SLISTCAPACITY_ERR, NewCapacity);
995     //error: capacity not within range
996     if (NewCapacity!=fCapacity) {
997 gpertea 16 if (NewCapacity==0) {
998 gpertea 2 GFREE(fList);
999 gpertea 16 }
1000 gpertea 2 else {//add the new item
1001     if (idx==fCount) {
1002     GREALLOC(fList, NewCapacity*sizeof(OBJ*));
1003     fList[idx]=newitem;
1004     }
1005     else {
1006     OBJ** newList;
1007     GMALLOC(newList, NewCapacity*sizeof(OBJ*));
1008     //copy data before idx
1009     memmove(&newList[0],&fList[0], idx*sizeof(OBJ*));
1010     newList[idx]=newitem;
1011     //copy data after idx
1012     memmove(&newList[idx+1],&fList[idx], (fCount-idx)*sizeof(OBJ*));
1013     memset(&newList[fCount+1], 0, (NewCapacity-fCount-1)*sizeof(OBJ*));
1014     //data copied:
1015     GFREE(fList);
1016     fList=newList;
1017     }
1018     fCount++;
1019     }
1020     fCapacity=NewCapacity;
1021     }
1022     }
1023    
1024 gpertea 16 template <class OBJ> int GPVec<OBJ>::IndexOf(pointer item) {
1025     int result=-1;
1026     for (int i=0;i<fCount;i++) {
1027     if (item==(pointer)fList[i]) return i;
1028     }
1029     return -1;
1030     }
1031 gpertea 2
1032     template <class OBJ> int GList<OBJ>::IndexOf(OBJ* item) {
1033     int result=0;
1034     if (Found(item, result)) return result;
1035     else return -1;
1036     }
1037    
1038     template <class OBJ> bool GList<OBJ>::Exists(OBJ& item) {
1039     int result=0;
1040     if (Found(&item, result)) return true;
1041     else return false;
1042     }
1043    
1044     template <class OBJ> bool GList<OBJ>::Exists(OBJ* item) {
1045     int result=0;
1046     if (Found(item, result)) return true;
1047     else return false;
1048     }
1049    
1050 gpertea 16 template <class OBJ> int GPVec<OBJ>::Add(OBJ* item) {
1051     int result;
1052     if (item==NULL) return -1;
1053     result = fCount;
1054     if (result==fCapacity) this->Grow();
1055     fList[result]=item;
1056     fCount++;
1057     return fCount-1;
1058     }
1059    
1060 gpertea 2 template <class OBJ> int GList<OBJ>::Add(OBJ* item) {
1061     int result;
1062     if (item==NULL) return -1;
1063     if (SORTED) {
1064     if (Found(item, result))
1065     if (fUnique) return -1; //duplicates forbidden
1066     //Found sets result to the position where the item should be!
1067     sortInsert(result, item);
1068     }
1069     else {
1070     if (fUnique && Found(item,result)) return -1; //set behaviour
1071 gpertea 16 result = this->fCount;
1072     if (result==this->fCapacity) GPVec<OBJ>::Grow();
1073     this->fList[result]=item;
1074     this->fCount++;
1075 gpertea 2 }
1076     return result;
1077     }
1078    
1079     //by default, it deletes the item if it has an equal in the list!
1080     //returns the existing equal (==) object if it's in the list already
1081     //or returns the item itself if it's unique (and adds it)
1082     template <class OBJ> OBJ* GList<OBJ>::AddIfNew(OBJ* item,
1083     bool deleteIfFound, int* fidx) {
1084     int r;
1085     if (Found(item, r)) {
1086 gpertea 16 if (deleteIfFound && (pointer)item != (pointer)(this->fList[r])) {
1087     this->deallocate_item(item);
1088     }
1089 gpertea 2 if (fidx!=NULL) *fidx=r;
1090 gpertea 16 return this->fList[r]; //found
1091 gpertea 2 }
1092     //not found:
1093     if (SORTED) {
1094     //Found() set result to the position where the item should be inserted:
1095     sortInsert(r, item);
1096     }
1097     else {
1098 gpertea 16 r = this->fCount;
1099     if (r==this->fCapacity) GPVec<OBJ>::Grow();
1100     this->fList[r]=item;
1101     this->fCount++;
1102 gpertea 2 }
1103     if (fidx!=NULL) *fidx=r;
1104     return item;
1105     }
1106    
1107 gpertea 16 //if item is found already in the list DELETE it and return -1
1108     //otherwise the item is added and its index is returned
1109     template <class OBJ> int GList<OBJ>::AddedIfNew(OBJ* item) {
1110     int r;
1111     if (Found(item, r)) {
1112     if ((pointer)item != (pointer)(this->fList[r])) {
1113     this->deallocate_item(item);
1114     }
1115     return -1;
1116     }
1117     //not found:
1118     if (SORTED) {
1119     //Found() set r to the position where the item should be inserted:
1120     sortInsert(r, item);
1121     }
1122     else {
1123     r = this->fCount;
1124     if (r==this->fCapacity) GPVec<OBJ>::Grow();
1125     this->fList[r]=item;
1126     this->fCount++;
1127     }
1128     return r;
1129     }
1130    
1131    
1132 gpertea 2 template <class OBJ> bool GList<OBJ>::Found(OBJ* item, int& idx) {
1133     //search the list by using CompareProc (if defined)
1134     //or == operator for a non-sortable list
1135     //for sorted lists, even when the result is false, the idx is
1136     //set to the closest matching object!
1137     int i;
1138     idx=-1;
1139 gpertea 16 if (this->fCount==0) { idx=0;return false;}
1140 gpertea 2 if (SORTED) { //binary search based on CompareProc
1141     //do the simple test first:
1142    
1143 gpertea 16 if ((*fCompareProc)(this->fList[0],item)>0) {
1144 gpertea 2 idx=0;
1145     return false;
1146     }
1147 gpertea 16 if ((*fCompareProc)(item, this->fList[this->fCount-1])>0) {
1148     idx=this->fCount;
1149 gpertea 2 return false;
1150     }
1151    
1152     int l, h, c;
1153     l = 0;
1154 gpertea 16 h = this->fCount - 1;
1155 gpertea 2 while (l <= h) {
1156     i = (l + h) >> 1;
1157 gpertea 16 c = (*fCompareProc)(this->fList[i], item);
1158 gpertea 2 if (c < 0) l = i + 1;
1159     else {
1160     h = i - 1;
1161     if (c == 0) {
1162     idx=i;
1163     return true;
1164     }
1165     }
1166     } //while
1167     idx = l;
1168     return false;
1169     }
1170     else {//not sorted: use linear search
1171     // needs == operator to compare user defined objects !
1172     i=0;
1173 gpertea 16 while (i<this->fCount) {
1174     if (*this->fList[i]==*item) {
1175 gpertea 2 idx=i;
1176     return true;
1177     }
1178     i++;
1179     }
1180     return false;
1181     }
1182     }
1183    
1184     template <class OBJ> void GList<OBJ>::sortInsert(int idx, OBJ* item) {
1185     //idx must be the new position this new item must have
1186     //so the allowed range is [0..fCount]
1187     //the old idx item all the above will be shifted to idx+1
1188 gpertea 16 if (idx<0 || idx>this->fCount) GError(SLISTINDEX_ERR, idx);
1189     if (this->fCount==this->fCapacity) {
1190     GPVec<OBJ>::Grow(idx, item);
1191 gpertea 2 //expand and also copy/move data and insert the new item
1192     return;
1193     }
1194     //room still left, just move data around and insert the new one
1195 gpertea 16 if (idx<this->fCount) //copy/move pointers only!
1196     memmove(&(this->fList[idx+1]), &(this->fList[idx]), (this->fCount-idx)*sizeof(OBJ*));
1197     this->fList[idx]=item;
1198     this->fCount++;
1199 gpertea 2 }
1200    
1201 gpertea 16 template <class OBJ> void GPVec<OBJ>::Insert(int idx, OBJ* item) {
1202 gpertea 2 //idx can be [0..fCount] so an item can be actually added
1203     if (idx<0 || idx>fCount) GError(SLISTINDEX_ERR, idx);
1204     if (fCount==fCapacity) {
1205     Grow(idx, item);
1206     return;
1207     }
1208     if (idx<fCount)
1209     memmove(&fList[idx+1], &fList[idx], (fCount-idx)*sizeof(OBJ*));
1210     fList[idx]=item;
1211     fCount++;
1212     }
1213    
1214 gpertea 16 template <class OBJ> void GList<OBJ>::Insert(int idx, OBJ* item) {
1215     //idx can be [0..fCount] so an item can be actually added
1216     BE_UNSORTED; //cannot do that with a sorted list!
1217     GPVec<OBJ>::Insert(idx,item);
1218     }
1219    
1220     template <class OBJ> void GPVec<OBJ>::Move(int curidx, int newidx) {
1221     //BE_UNSORTED; //cannot do that in a sorted list!
1222 gpertea 2 if (curidx!=newidx || newidx>=fCount)
1223     GError(SLISTINDEX_ERR, newidx);
1224     OBJ* p;
1225     p=Get(curidx);
1226     //this is a delete:
1227     fCount--;
1228     if (curidx<fCount)
1229     memmove(&fList[curidx], &fList[curidx+1], (fCount-curidx)*sizeof(OBJ*));
1230     //-this was instead of delete
1231     Insert(newidx, p);
1232     }
1233    
1234 gpertea 16 template <class OBJ> void GList<OBJ>::Move(int curidx, int newidx) {
1235     BE_UNSORTED; //cannot do this in a sorted list!
1236     GPVec<OBJ>::Move(curidx,newidx);
1237     }
1238 gpertea 2
1239 gpertea 16 template <class OBJ> void GPVec<OBJ>::Put(int idx, OBJ* item) {
1240     //WARNING: this will never free the replaced item!
1241 gpertea 2 TEST_INDEX(idx);
1242     fList[idx]=item;
1243 gpertea 16 }
1244    
1245     template <class OBJ> void GList<OBJ>::Put(int idx, OBJ* item, bool re_sort) {
1246     //WARNING: this will never free the replaced item!
1247 gpertea 60 // this may BREAK the sort order unless the "re_sort" parameter is given
1248 gpertea 16 if (idx<0 || idx>this->fCount) GError(SLISTINDEX_ERR, idx);
1249     this->fList[idx]=item;
1250 gpertea 2 if (SORTED && item!=NULL && re_sort) Sort(); //re-sort
1251     }
1252    
1253 gpertea 16
1254     template <class OBJ> void GPVec<OBJ>::Forget(int idx) {
1255 gpertea 2 TEST_INDEX(idx);
1256 gpertea 16 fList[idx]=NULL; //user should free that somewhere else
1257 gpertea 2 }
1258    
1259 gpertea 16 template <class OBJ> void GPVec<OBJ>::freeItem(int idx) {
1260     TEST_INDEX(idx);
1261     if (fFreeProc!=NULL) {
1262     (*fFreeProc)(fList[idx]);
1263     }
1264     else this->DefaultFreeProc(fList[idx]);
1265     fList[idx]=NULL;
1266     }
1267    
1268     template <class OBJ> void GPVec<OBJ>::Delete(int index) {
1269 gpertea 2 TEST_INDEX(index);
1270     if (fFreeProc!=NULL && fList[index]!=NULL) {
1271     (*fFreeProc)(fList[index]); //freeItem
1272     }
1273     fList[index]=NULL;
1274     fCount--;
1275     if (index<fCount) //move higher elements if any
1276     memmove(&fList[index], &fList[index+1], (fCount-index)*sizeof(OBJ*));
1277     }
1278    
1279     //Stack usage:
1280 gpertea 16 template <class OBJ> OBJ* GPVec<OBJ>::Pop() {
1281 gpertea 2 if (fCount<=0) return NULL;
1282     fCount--;
1283     OBJ* o=fList[fCount];
1284     fList[fCount]=NULL;
1285     return o;
1286     }
1287    
1288     //Queue usage:
1289 gpertea 16 template <class OBJ> OBJ* GPVec<OBJ>::Shift() {
1290 gpertea 2 if (fCount<=0) return NULL;
1291     fCount--;
1292     OBJ* o=fList[0];
1293     if (fCount>0)
1294     memmove(&fList[0], &fList[1], (fCount)*sizeof(OBJ*));
1295     fList[fCount]=NULL; //not that it matters..
1296     return o;
1297     }
1298    
1299     template <class OBJ> int GList<OBJ>::Remove(OBJ* item) {
1300     //removes an item if it's in our list
1301     int result=IndexOf(item);
1302 gpertea 16 if (result>=0) GPVec<OBJ>::Delete(result);
1303 gpertea 2 return result;
1304     }
1305    
1306 gpertea 16 //linear search for the pointer address
1307     template <class OBJ> int GPVec<OBJ>::RemovePtr(pointer item) {
1308 gpertea 2 if (item==NULL) return -1;
1309 gpertea 16 for (int i=0;i<fCount;i++)
1310     if ((pointer)fList[i] == item) {
1311     Delete(i);
1312     return i;
1313     }
1314     return -1; //not found
1315 gpertea 2 }
1316    
1317 gpertea 16 template <class OBJ> void GPVec<OBJ>::Pack() {//also frees items!
1318 gpertea 2 for (int i=fCount-1; i>=0; i--)
1319 gpertea 16 if (fList[i]==NULL) Delete(i); //shift rest of fList content accordingly
1320 gpertea 2 }
1321    
1322 gpertea 16 template <class OBJ> void GPVec<OBJ>::setCount(int NewCount) {
1323 gpertea 2 if (NewCount<0 || NewCount > MAXLISTSIZE)
1324     GError(SLISTCOUNT_ERR, NewCount);
1325     if (NewCount > fCapacity) setCapacity(NewCount);
1326     if (NewCount > fCount)
1327     memset(fList[fCount], 0, (NewCount - fCount) * sizeof(OBJ*));
1328     fCount = NewCount;
1329     }
1330    
1331     template <class OBJ> void GList<OBJ>::QuickSort(int L, int R) {
1332     int I, J;
1333     OBJ* P;
1334     OBJ* T;
1335     do {
1336     I = L;
1337     J = R;
1338 gpertea 16 P = this->fList[(L + R) >> 1];
1339 gpertea 2 do {
1340 gpertea 16 while (fCompareProc(this->fList[I], P) < 0) I++;
1341     while (fCompareProc(this->fList[J], P) > 0) J--;
1342 gpertea 2 if (I <= J) {
1343 gpertea 16 T = this->fList[I];
1344     this->fList[I] = this->fList[J];
1345     this->fList[J] = T;
1346 gpertea 2 I++;
1347     J--;
1348     }
1349     }
1350     while (I <= J);
1351     if (L < J) QuickSort(L, J);
1352     L = I;
1353     }
1354     while (I < R);
1355    
1356     }
1357    
1358     template <class OBJ> void GList<OBJ>::Sort() {
1359 gpertea 16 if (this->fList!=NULL && this->fCount>0 && fCompareProc!=NULL)
1360     QuickSort(0, this->fCount-1);
1361 gpertea 2 }
1362    
1363     //---------------------------------------------------------------------------
1364     #endif