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 00018 00025 class Counter { 00026 public: 00027 00028 virtual ~Counter() {} 00029 // access functions 00030 00032 virtual double count(int eventIndex) const= 0; 00034 virtual double sum() const= 0; 00035 00037 virtual void startIteration() const= 0; 00038 virtual bool hasMore() const= 0; 00039 virtual void nextCount(int &eventIndex, double &count) const= 0; 00040 }; 00041 00043 class ModifiableCounter : public Counter { 00044 public: 00045 // manipulation functions 00046 virtual ~ModifiableCounter() {} 00047 virtual void incCount(int eventIndex, double count) = 0; 00048 virtual void setCount(int eventIndex, double count) = 0; 00049 }; 00050 00052 00053 template<class T> 00054 class ArrayCounter : public ModifiableCounter { 00055 public: 00056 00057 ArrayCounter(int size) : sz(size), ct(new T[size]), total(0) { 00058 for (int i=0; i<size; i++) ct[i]=0; 00059 } 00060 virtual ~ArrayCounter() { delete [] ct;} 00061 00063 virtual double count(int eventIndex) const{ 00064 return ct[eventIndex]; 00065 } 00066 00068 virtual double sum() const{ 00069 return total; 00070 } 00071 00072 virtual void incCount(int eventIndex, double count) { 00073 ct[eventIndex] += (T)count; 00074 total += (T)count; 00075 } 00076 00077 virtual void setCount(int eventIndex, double count) { 00078 total = total - ct[eventIndex]+ (T)count; 00079 ct[eventIndex] = (T)count; 00080 } 00081 00082 00083 virtual void startIteration() const{ 00084 pos=0; 00085 } 00086 00087 virtual bool hasMore() const{ 00088 while ((pos < sz) && (ct[pos] == 0)) 00089 pos++; 00090 return (pos<sz); 00091 } 00092 00093 virtual void nextCount(int &eventIndex, double &count) const{ 00094 eventIndex = pos; 00095 count = ct[pos]; 00096 pos++; 00097 } 00098 00099 protected: 00100 T *ct; 00101 int sz; 00102 T total; 00103 mutable int pos; 00104 }; 00105 00106 #endif /* _COUNTER_HPP */ 00107