Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Counter.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 #ifndef _COUNTER_HPP
00014 #define _COUNTER_HPP
00015 
00016 namespace lemur 
00017 {
00018   namespace utility
00019   {
00020     
00022 
00029     class Counter {
00030     public:
00031 
00032       virtual ~Counter() {}
00033       // access functions
00034   
00036       virtual double count(int eventIndex) const= 0;
00038       virtual double sum() const= 0;
00039   
00041       virtual void startIteration() const= 0;
00042       virtual bool hasMore() const= 0;
00043       virtual void nextCount(int &eventIndex, double &count) const= 0;
00044     };
00045 
00047     class ModifiableCounter : public Counter {
00048     public: 
00049       // manipulation functions
00050       virtual ~ModifiableCounter() {}
00051       virtual void incCount(int eventIndex, double count) = 0;
00052       virtual void setCount(int eventIndex, double count)  = 0;
00053     };
00054 
00056 
00057     template<class T>
00058     class ArrayCounter : public ModifiableCounter {
00059     public:
00060   
00061       ArrayCounter(int size) : sz(size), ct(new T[size]), total(0) {
00062         for (int i=0; i<size; i++) ct[i]=0;
00063       }
00064       virtual ~ArrayCounter() { delete [] ct;}
00065 
00067       virtual double count(int eventIndex) const{
00068         return ct[eventIndex];
00069       }
00070 
00072       virtual double sum() const{
00073         return total;
00074       }
00075 
00076       virtual void incCount(int eventIndex, double count) {
00077         ct[eventIndex] += (T)count;
00078         total += (T)count;
00079       }
00080 
00081       virtual void setCount(int eventIndex, double count) {
00082         total = total - ct[eventIndex]+ (T)count;
00083         ct[eventIndex] = (T)count;
00084       }
00085 
00086 
00087       virtual void startIteration() const{
00088         pos=0;
00089       }
00090 
00091       virtual bool hasMore() const{
00092         while ((pos < sz) && (ct[pos] == 0))
00093           pos++;
00094         return (pos<sz); 
00095       }
00096 
00097       virtual void nextCount(int &eventIndex, double &count) const{
00098         eventIndex = pos;
00099         count = ct[pos];
00100         pos++;
00101       }
00102 
00103     protected:
00104       T *ct;
00105       int sz;
00106       T total;
00107       mutable int pos;
00108     };
00109   }
00110 }
00111 
00112 #endif /* _COUNTER_HPP */
00113 

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