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
00027 template <class ObjType, class CountType>
00028 class CSet : public ISet<ObjType>
00029 {
00030 public:
00031 CSet() : ISet<ObjType>(), countOfIndex(0) {}
00032 CSet(const int maxSize_) : countOfIndex(0) { open(maxSize_); }
00033 ~CSet() { close(); }
00034
00035 void open(const int maxSize_) {
00036 ISet<ObjType>::open(maxSize_);
00037 countOfIndex = new CountType [maxSize_ + 1];
00038 memset(countOfIndex, 0, (1+maxSize_) * sizeof(CountType));
00039 }
00040
00041 void close() {
00042 ISet<ObjType>::close();
00043 delete [] countOfIndex;
00044 countOfIndex=0;
00045 }
00046
00047 void clear() {
00048 if (maxSize==0) return;
00049 close();
00050 open(maxSize);
00051 }
00052
00053 int add(const ObjType& u, const CountType &count=(CountType) 1.0) {
00054
00055 typename PSet<ObjType>::SET_NODE *sn = PSet<ObjType>::internalAdd(u);
00056 if (sn==0) {
00057
00058 int idx = operator[](u);
00059 countOfIndex[idx] += count;
00060 return -1;
00061 }
00062 index[sn->idx] = sn;
00063 countOfIndex[sn->idx] = count;
00064 if (++currentSize>maxSize)
00065 grow((int) (currentSize*GROW_FACTOR + 1));
00066 return sn->idx;
00067 }
00068
00069 int remove(const ObjType& u){
00070
00071 const int idx = ISet<ObjType>::internalRemove(u);
00072 if (idx==-1) return 0;
00073 countOfIndex[idx] = countOfIndex[currentSize-1];
00074 countOfIndex[currentSize-1] = 0;
00075 currentSize--;
00076 return 1;
00077 }
00078
00079 int operator+=(const ObjType& u)
00080 { return add(u); }
00081
00082 int operator-=(const ObjType& u)
00083 { return remove(u); }
00084
00085 const CountType count(const ObjType& u) const {
00086 int idx=ISet<ObjType>::operator[](u);
00087 if (idx==-1) return 0;
00088 return count(idx);
00089 }
00090
00091 const CountType count(const int idx) const { return countOfIndex[idx]; }
00092
00093 void setCount(const ObjType&u, const CountType count) {
00094 int idx=ISet<ObjType>::operator[](u);
00095 assert(idx!=-1);
00096 setCount(idx,count);
00097 }
00098
00099 void setCount(const int idx,const CountType count) {countOfIndex[idx]=count;}
00100
00101 void grow(const int newSize) {
00102 ISet<ObjType>::grow(newSize);
00103 CountType *newCountOfIndex = new CountType [maxSize+1];
00104 memcpy(newCountOfIndex, countOfIndex, currentSize*sizeof(CountType));
00105 delete [] countOfIndex;
00106 countOfIndex = newCountOfIndex;
00107 }
00108
00109
00110
00111 public:
00112 void ClearData() { clear(); }
00113
00114 protected:
00115 CountType *countOfIndex;
00116 };
00117
00118 #endif
00119