ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GList.hh
(Generate patch)
# Line 37 | Line 37
37      //~GArray();
38      //assignment operator
39      void setSorted(GCompareProc* cmpFunc);
40 +    void setSorted(bool sorted) {
41 +     if (sorted) {
42 +         if (fCompareProc!=&DefaultCompareProc) {
43 +             fCompareProc=&DefaultCompareProc;
44 +             Sort();
45 +             }
46 +          }
47 +      else fCompareProc=NULL;
48 +      }
49      //sort the array if cmpFunc not NULL or changes
50      int Add(OBJ* item); // specific implementation if sorted
51      int Add(OBJ& item) { return Add(&item); } //both will CREATE a new OBJ and COPY to it
52                         // using OBJ new operator=
53 +    int cAdd(OBJ item) { return Add(&item); }
54 +    int cPush(OBJ item) { return Add(&item); }
55 +    int Push(OBJ& item) { return Add(&item); }
56 +
57      void Add(GArray<OBJ>& list); //add copies of all items from another list
58      //this will reject identical items in sorted lists only!
59      void setUnique(bool beUnique) { fUnique = beUnique; };
# Line 57 | Line 70
70      //unsorted only, place item at position idx:
71      void Move(int curidx, int newidx);
72      void Insert(int idx, OBJ* item);
73 <    void Insert(int idx, OBJ& item) { Insert(idx,&item); }
73 >    void Insert(int idx, OBJ item) { Insert(idx,&item); }
74   };
75  
76   //GList is a sortable collection of pointers to objects; requires operator< to be defined, or a custom compare function
# Line 65 | Line 78
78    protected:
79      bool fUnique;
80      GCompareProc* fCompareProc; //a pointer to a Compare function
81 +    
82      static int DefaultCompareProc(const pointer item1, const pointer item2) {
83        //operator< MUST be defined for OBJ class!
84        if (*((OBJ*)item2) < *((OBJ*)item1)) return 1;
# Line 177 | Line 191
191  
192   template <class OBJ> GArray<OBJ>::GArray(bool sorted, bool unique):GVec<OBJ>(0) {
193    fUnique=unique;
194 <  fCompareProc=sorted? &DefaultCompareProc : NULL;
194 >  fCompareProc = sorted ? DefaultCompareProc : NULL;
195   }
196  
197   template <class OBJ> GArray<OBJ>::GArray(int init_capacity,
198                          bool sorted, bool unique):GVec<OBJ>(init_capacity) {
199    fUnique=unique;
200 <  fCompareProc=sorted? &DefaultCompareProc : NULL;
200 >  fCompareProc=sorted ? DefaultCompareProc : NULL;
201   }
202  
203   template <class OBJ> void GArray<OBJ>::setSorted(GCompareProc* cmpFunc) {
# Line 213 | Line 227
227     if (Found(*item, result))
228        if (fUnique) return -1; //cannot add a duplicate!
229     //Found sets result to the position where the item should be!
230 <   this->idxInsert(result, *item);
230 >   GVec<OBJ>::Insert(result, *item);
231     }
232    else {
233     if (fUnique && Found(*item,result)) return -1; //set behaviour
# Line 294 | Line 308
308   template <class OBJ> void GArray<OBJ>::Insert(int idx, OBJ* item) {
309   //idx can be [0..fCount] so an item can be actually added
310   BE_UNSORTED; //forbid this operation on sorted data
311 < idxInsert(idx, item);
311 > GVec<OBJ>::Insert(idx, item);
312   }
313  
314  
# Line 316 | Line 330
330   }
331  
332   template <class OBJ> void GArray<OBJ>::Sort() {
333 < if (this->fArray!=NULL && this->fCount>0 && fCompareProc!=NULL)
334 <     qSort(0, this->fCount-1, fCompareProc);
333 > if (fCompareProc==NULL) { fCompareProc=DefaultCompareProc; }
334 > if (this->fArray!=NULL && this->fCount>0)
335 >     this->qSort(0, this->fCount-1, fCompareProc);
336   }
337  
338   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Line 614 | Line 629
629   }
630  
631   template <class OBJ> void GList<OBJ>::Sort() {
632 < if (this->fList!=NULL && this->fCount>0 && fCompareProc!=NULL)
632 > if (fCompareProc==NULL) fCompareProc = DefaultCompareProc;
633 > if (this->fList!=NULL && this->fCount>0)
634       this->qSort(0, this->fCount-1, fCompareProc);
635   }
636  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines