1 #ifndef TABLES_H
2 #define TABLES_H
3 
4 #include <cmath>
5 #include <iostream>
6 #include <set>
7 #include <stdlib.h>
8 
9 #include "MathUtil.h"
10 #include "StrIdMap.h"
11 
12 /** One dimensional table for storing first degree probabilities or
13     frequency counts (in log scale).  */
14 class HmmMap : public std::map<unsigned long, double> {
15 	double _smoothedZeroCount;
16 public:
17 	HmmMap();
smoothedZeroCount()18 	double smoothedZeroCount() { return _smoothedZeroCount;}
19 	double get(unsigned long event);
20 	void add(unsigned long event, double count);
21 	void load(std::istream& file, StrIdMap& strIdMap);
22 	void save(std::ostream& file, StrIdMap& strIdMap);
23 
24 	/** Randomly generate the next value according to the probabilities
25       in this table. Return true if the next value is filled. */
26 	bool rand(unsigned long& next);
~HmmMap()27 	~HmmMap() {}
28 };
29 
30 typedef std::set<unsigned long> ULSet; // set of unsigned long integers
31 
32 /** Two-dimensional table for storing second degree probabilities or
33     frequency counts (in log scale).  */
34 class HmmKeyedMaps : public std::map<unsigned long, HmmMap*> {
35 	std::map<unsigned long, ULSet*> possibleContexts;
36 	HmmMap _backoff;
37 public:
38 	/** Retrieve the value associated with event and context. */
39 	double get(unsigned long event, unsigned long context);
40 
41 	/** Add the count to the  */
42 	void add(unsigned long event, unsigned long context, double count);
43 
44 	/** Return the set of contexts such that (event, context) has an
45       associated values. */
46 	ULSet* getCntx(unsigned long event);
47 
48 	/** fill the table with data in the file. */
49 	void load(std::istream& file, StrIdMap& strIdMap);
50 	void load(std::string& name1, std::string& name2, const double value, StrIdMap& strIdMap);
51 
52 	/** save the contents in the table in the file. */
53 	void save(std::ostream& file, StrIdMap& strIdMap);
54 
55 	/** clear the contents of this table */
56 	void clear();
57 
58 	/** Randomly generate the next value according to the probabilities
59       in this table. Return true if the next value is filled. */
60 	bool rand(unsigned long curr, unsigned long& next);
61 
62 	~HmmKeyedMaps();
63 };
64 
65 #endif
66