Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class 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 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         // add u <count> times to set; return idx of u if new, -1 if old
00059         typename PSet<ObjType>::SET_NODE  *sn;
00060         sn = PSet<ObjType>::internalAdd(u);
00061         if (sn==0) {
00062           // already in set, but need to increment count anyway
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; // grow may void sn
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         // remove u from set completely: returns 1 iff u was in set
00077         const int idx = ISet<ObjType>::internalRemove(u);
00078         if (idx==-1) return 0;                 // not a member
00079         countOfIndex[idx] = countOfIndex[this->currentSize-1];
00080         countOfIndex[this->currentSize-1] = 0;
00081         this->currentSize--;
00082         return 1;                              // was a member (not anymore)
00083       }
00084 
00085       int operator+=(const ObjType& u)  // add an elt to set: returns 1 if added. 
00086       { return add(u); }
00087   
00088       int operator-=(const ObjType& u)// remove elt from set: returns 1 if removed.
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       // alternate interface
00116   
00117     public: 
00118       void ClearData() { clear(); }
00119   
00120     protected:
00121       CountType *countOfIndex;
00122     };
00123   }
00124 }
00125 
00126 #endif
00127 

Generated on Tue Jun 15 11:02:53 2010 for Lemur by doxygen 1.3.4