ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GList.hh
(Generate patch)
# Line 79 | Line 79
79      void Move(int curidx, int newidx);
80   };
81  
82 < // GArray is the fully sortable collection, but requires the comparison operators to be defined
82 > // GArray is the sortable collection, but requires the comparison operators to be defined
83   template <class OBJ> class GArray:public GVec<OBJ> {
84    protected:
85      bool fUnique;
# Line 163 | Line 163
163      void deallocate_item(OBJ* item); //forcefully call fFreeProc or delete on item
164      void Clear();
165      void Exchange(int idx1, int idx2);
166 +    void Swap(int idx1, int idx2)  { Exchange(idx1, idx2); }
167      OBJ* First() { return (fCount>0)?fList[0]:NULL; }
168      OBJ* Last()  { return (fCount>0)?fList[fCount-1]:NULL;}
169      bool isEmpty() { return fCount==0; }
# Line 315 | Line 316
316   this->fCapacity=array.fCapacity;
317   this->fArray=NULL;
318   if (this->fCapacity>0) {
319 <    GMALLOC(fArray, fCapacity*sizeof(OBJ));
319 >    //GMALLOC(fArray, fCapacity*sizeof(OBJ));
320 >    fArray=new OBJ[this->fCapacity];
321      }
322   this->fCount=array.fCount;
323   // uses OBJ operator=
# Line 327 | Line 329
329   this->fCapacity=array.fCapacity;
330   this->fArray=NULL;
331   if (this->fCapacity>0) {
332 <    GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
332 >    //GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
333 >    this->fArray=new OBJ[this->fCapacity];
334      }
335   this->fCount=array.fCount;
336   fUnique=array.fUnique;
# Line 342 | Line 345
345   fCount=array.fCount;
346   fCapacity=array.fCapacity;
347   if (fCapacity>0) {
348 <    GMALLOC(fArray, fCapacity*sizeof(OBJ));
348 >    //GMALLOC(fArray, fCapacity*sizeof(OBJ));
349 >    fArray=new OBJ[this->fCapacity];
350      }
351   fCount=array.fCount;
352   // uses OBJ operator=
# Line 359 | Line 363
363   this->fUnique=array.fUnique;
364   this->fCapacity=array.fCapacity;
365   if (this->fCapacity>0) {
366 <    GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
366 >    //GMALLOC(this->fArray, this->fCapacity*sizeof(OBJ));
367 >    this->fArray=new OBJ[this->fCapacity];
368      }
369   this->fCompareProc=array.fCompareProc;
370   this->fCount=array.fCount;
# Line 397 | Line 402
402      //error: capacity not within range
403    if (NewCapacity!=fCapacity) {
404     if (NewCapacity==0) {
405 <      GFREE(fArray);
405 >      //GFREE(fArray);
406 >      delete[] fArray;
407        }
408      else {
409 <      GREALLOC(fArray, NewCapacity*sizeof(OBJ));
409 >      //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        }
416     fCapacity=NewCapacity;
417     }
# Line 455 | Line 466
466    if (NewCapacity <= fCount || NewCapacity >= MAXLISTSIZE)
467      GError(SLISTCAPACITY_ERR, NewCapacity);
468      //error: capacity not within range
469 +
470    if (NewCapacity!=fCapacity) {
471      if (NewCapacity==0) {
472 <      GFREE(fArray);
472 >      //GFREE(fArray);
473 >      delete[] fArray;
474 >      fArray=NULL;
475        }
476      else { //add the new item
477        if (idx==fCount) { //append item
478 <         GREALLOC(fArray, NewCapacity*sizeof(OBJ));
478 >         //GREALLOC(fArray, NewCapacity*sizeof(OBJ));
479 >         setCapacity(NewCapacity);
480           fArray[idx]=item;
481           }
482         else { //insert item at idx
483          OBJ* newList;
484 <        GMALLOC(newList, NewCapacity*sizeof(OBJ));
484 >        //GMALLOC(newList, NewCapacity*sizeof(OBJ));
485 >        newList=new OBJ[NewCapacity];
486          //copy data before idx
487 <        memmove(&newList[0],&fArray[0], idx*sizeof(OBJ));
488 <        newList[idx]=item; // operator=
487 >        //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          //copy data after idx
494 <        memmove(&newList[idx+1],&fArray[idx], (fCount-idx)*sizeof(OBJ));
495 <        memset(&newList[fCount+1], 0, (NewCapacity-fCount-1)*sizeof(OBJ));
494 >        //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          //data copied:
500 <        GFREE(fArray);
500 >        //GFREE(fArray);
501 >        delete[] fArray;
502          fArray=newList;
503          }
504        fCount++;
# Line 606 | Line 630
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);
611 <    //expand and also copy/move data and insert the new item
633 > if (fCount==fCapacity) { //need to resize the array
634 >    Grow(idx, item); //expand and also copy/move data and insert the new item
635      return;
636      }
637   //move data around to make room for the new item
638 < if (idx<fCount)
639 <      memmove(&fArray[idx+1], &fArray[idx], (fCount-idx)*sizeof(OBJ));
638 > if (idx<fCount) {
639 >      //copy after-idx items (shift up)
640 >      //memmove(&newList[idx+1],&fArray[idx], (fCount-idx)*sizeof(OBJ));
641 >      for (int i=fCount; i>idx; i--) {
642 >          fArray[i]=fArray[i-1];
643 >          }
644 >      }
645   fArray[idx]=item;
646   fCount++;
647   }
# Line 654 | Line 682
682   fArray[idx]=item;
683   }
684  
685 + template <class OBJ> void GVec<OBJ>::Exchange(int idx1, int idx2) {
686 + TEST_INDEX(idx1);
687 + TEST_INDEX(idx2);
688 + OBJ item=fArray[idx1];
689 + fArray[idx1]=fArray[idx2];
690 + fArray[idx2]=item;
691 + }
692 +
693 +
694   template <class OBJ> void GArray<OBJ>::Replace(int idx, OBJ& item) {
695   //TEST_INDEX(idx);
696   if (idx<0 || idx>=this->fCount) GError(SLISTINDEX_ERR, __FILE__,__LINE__, idx);
# Line 664 | Line 701
701  
702   template <class OBJ> void GVec<OBJ>::Delete(int index) {
703   TEST_INDEX(index);
667 //fArray[index]=NULL;
704   fCount--;
705 < if (index<fCount) //move higher elements if any
706 <   memmove(&fArray[index], &fArray[index+1], (fCount-index)*sizeof(OBJ));
705 > while (index<fCount) {
706 >    //move higher elements if any (shift down)
707 >    //memmove(&fArray[index], &fArray[index+1], (fCount-index)*sizeof(OBJ));
708 >    fArray[index]=fArray[index+1];
709 >    index++;
710 >    }
711   }
712  
713   template <class OBJ> void GVec<OBJ>::setCount(int NewCount) {
714    if (NewCount<0 || NewCount > MAXLISTSIZE)
715       GError(SLISTCOUNT_ERR, NewCount);
716    if (NewCount > fCapacity) setCapacity(NewCount);
717 <  if (NewCount > fCount)
718 <    memset(&fArray[fCount], 0, (NewCount - fCount) * sizeof(OBJ));
717 >  //if (NewCount > fCount)
718 >  //  memset(&fArray[fCount], 0, (NewCount - fCount) * sizeof(OBJ));
719    fCount = NewCount;
720   }
721  
# Line 905 | Line 945
945   }
946  
947   template <class OBJ> void GPVec<OBJ>::Exchange(int idx1, int idx2) {
948 < //BE_UNSORTED; //cannot do that in a sorted list!
948 > //Warning: this will BREAK sort order for sorted GList
949   TEST_INDEX(idx1);
950   TEST_INDEX(idx2);
951   OBJ* item=fList[idx1];
# Line 1211 | Line 1251
1251  
1252   template <class OBJ> void GList<OBJ>::Put(int idx, OBJ* item, bool re_sort) {
1253   //WARNING: this will never free the replaced item!
1254 + // this may BREAK the sort order unless the "re_sort" parameter is given
1255   if (idx<0 || idx>this->fCount) GError(SLISTINDEX_ERR, idx);
1256   this->fList[idx]=item;
1257   if (SORTED && item!=NULL && re_sort) Sort(); //re-sort

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines