00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #ifndef _CSETH_
00021 #define _CSETH_
00022 
00023 
00024 
00025 #include <ISet.hpp>
00026 namespace lemur 
00027 {
00028   namespace utility
00029   {
00030     
00031     template <class ObjType, class CountType>
00032     class CSet : public ISet<ObjType>
00033     {
00034     public:
00035       CSet() :  ISet<ObjType>(), countOfIndex(0) {}
00036       CSet(const int maxSize_) : countOfIndex(0) { open(maxSize_); }
00037       ~CSet() { close(); }
00038 
00039       void open(const int maxSize_) {
00040         ISet<ObjType>::open(maxSize_); 
00041         countOfIndex = new CountType [maxSize_ + 1];
00042         memset(countOfIndex, 0, (1+maxSize_) * sizeof(CountType));
00043       }
00044   
00045       void close() {
00046         ISet<ObjType>::close();
00047         delete [] countOfIndex;
00048         countOfIndex=0;
00049       }
00050 
00051       void clear() {
00052         if (this->maxSize==0) return;
00053         close();
00054         open(this->maxSize);
00055       }
00056 
00057       int add(const ObjType& u, const CountType &count=(CountType) 1.0) {
00058         
00059         typename PSet<ObjType>::SET_NODE  *sn;
00060         sn = PSet<ObjType>::internalAdd(u);
00061         if (sn==0) {
00062           
00063           int idx = operator[](u);
00064           countOfIndex[idx] += count;
00065           return -1;
00066         }
00067         this->index[sn->idx] = sn;
00068         countOfIndex[sn->idx] = count;
00069         int retval = sn->idx; 
00070         if (++this->currentSize>this->maxSize) 
00071           grow((int) (this->currentSize*GROW_FACTOR + 1));
00072         return retval;
00073       }
00074   
00075       int remove(const ObjType& u){
00076         
00077         const int idx = ISet<ObjType>::internalRemove(u);
00078         if (idx==-1) return 0;                 
00079         countOfIndex[idx] = countOfIndex[this->currentSize-1];
00080         countOfIndex[this->currentSize-1] = 0;
00081         this->currentSize--;
00082         return 1;                              
00083       }
00084 
00085       int operator+=(const ObjType& u)  
00086       { return add(u); }
00087   
00088       int operator-=(const ObjType& u)
00089       { return remove(u); }
00090 
00091       const CountType count(const ObjType& u) const {
00092         int idx=ISet<ObjType>::operator[](u); 
00093         if (idx==-1) return 0;
00094         return count(idx);
00095       }
00096   
00097       const CountType count(const int idx) const { return countOfIndex[idx]; }
00098 
00099       void setCount(const ObjType&u, const CountType count) {
00100         int idx=ISet<ObjType>::operator[](u); 
00101         assert(idx!=-1);
00102         setCount(idx,count);
00103       }
00104   
00105       void setCount(const int idx,const CountType count) {countOfIndex[idx]=count;}
00106   
00107       void grow(const int newSize) {
00108         ISet<ObjType>::grow(newSize);
00109         CountType *newCountOfIndex = new CountType [this->maxSize+1];
00110         memcpy(newCountOfIndex, countOfIndex, this->currentSize*sizeof(CountType));
00111         delete  [] countOfIndex;
00112         countOfIndex = newCountOfIndex;
00113       }
00114   
00115       
00116   
00117     public: 
00118       void ClearData() { clear(); }
00119   
00120     protected:
00121       CountType *countOfIndex;
00122     };
00123   }
00124 }
00125 
00126 #endif
00127