Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

CSet.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2001 Carnegie Mellon University.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 
00014 // ------------------------------------------------------------------
00015 // CSet.H (Set with counts)
00016 // ALB
00017 // derived from ISet.H (set with index)
00018 // ---------------------------------------------------------------
00019 
00020 #ifndef _CSETH_
00021 #define _CSETH_
00022 
00023 // #pragma interface
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      // add u <count> times to set; return idx of u if new, -1 if old
00055     typename PSet<ObjType>::SET_NODE  *sn = PSet<ObjType>::internalAdd(u);
00056     if (sn==0) {
00057       // already in set, but need to increment count anyway
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     // remove u from set completely: returns 1 iff u was in set
00071     const int idx = ISet<ObjType>::internalRemove(u);
00072     if (idx==-1) return 0;                 // not a member
00073     countOfIndex[idx] = countOfIndex[currentSize-1];
00074     countOfIndex[currentSize-1] = 0;
00075     currentSize--;
00076     return 1;                              // was a member (not anymore)
00077   }
00078 
00079   int operator+=(const ObjType& u)  // add an elt to set: returns 1 if added. 
00080   { return add(u); }
00081   
00082   int operator-=(const ObjType& u)// remove elt from set: returns 1 if removed.
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   // alternate interface
00110   
00111 public: 
00112   void ClearData() { clear(); }
00113   
00114 protected:
00115   CountType *countOfIndex;
00116 };
00117 
00118 #endif
00119 

Generated on Wed Nov 3 12:58:53 2004 for Lemur Toolkit by doxygen1.2.18