ProbabilityTable.cpp
Go to the documentation of this file.
1 
19 
21 
23  {
24  }
25 
26  ProbabilityTable::ProbabilityTable(unsigned int pRows, unsigned int pColums)
27  {
28  // Reserve space.
29  for(unsigned int y = 0; y < pRows; y++) {
30  mEntries.push_back(std::vector<double>());
31  for(unsigned int x = 0; x < pColums; x++)
32  mEntries[y].push_back(0);
33  }
34  }
35 
36  ProbabilityTable::ProbabilityTable(boost::property_tree::ptree& pPt)
37  {
38  load(pPt);
39  }
40 
42  {
43 
44  }
45 
46  void ProbabilityTable::load(boost::property_tree::ptree& pPt)
47  {
48  // Load the probability table.
49  BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pPt.get_child("table"))
50  {
51  // Only access the 'map' child nodes.
52  if(!std::strcmp(v.first.c_str(), "entry"))
53  {
54  // Load the string containing the values.
55  std::string valuesAsString = v.second.get<std::string>("<xmlattr>.values");
56 
57  // Reconstruct a vector of doubles from the string.
58  std::vector<double> line;
59  SerializationHelper::convertStringToVector(valuesAsString, line);
60 
61  // Add line to table.
62  mEntries.push_back(line);
63  }
64  }
65  }
66 
67  void ProbabilityTable::save(boost::property_tree::ptree& pPt)
68  {
69  // Create a subtree.
70  boost::property_tree::ptree subtree;
71 
72  // Export every line to XML.
73  for(unsigned int i = 0; i < mEntries.size(); i++)
74  {
75  // Convert entries into a string.
76  std::string lineAsString;
78 
79  // Add value attribute to a new subtree for the entry.
80  boost::property_tree::ptree subtreeEntry;
81  subtreeEntry.add("<xmlattr>.values", lineAsString);
82  subtree.add_child("entry", subtreeEntry);
83  }
84 
85  // Add subtree to main tree.
86  pPt.add_child("table", subtree);
87  }
88 
89  double ProbabilityTable::getProbability(unsigned int pRow, unsigned int pColumn)
90  {
91  // Check, for row boundaries.
92  if(!(pRow < mEntries.size()))
93  throw std::invalid_argument("Error in probability table. Row out of boundaries.");
94 
95  // Check, for column boundaries.
96  if(!(pColumn < mEntries[pRow].size()))
97  throw std::invalid_argument("Error in probability table. Column out of boundaries.");
98 
99  // Return the probability
100  return mEntries[pRow][pColumn];
101  }
102 
104  {
105  return mEntries[0].size();
106  }
107 
109  {
110  return mEntries.size();
111  }
112 
113  void ProbabilityTable::add(unsigned int pRow, unsigned int pColumn, unsigned int pCounts)
114  {
115  // Check, for row boundaries.
116  if(!(pRow < mEntries.size()))
117  throw std::invalid_argument("Error in probability table. Row out of boundaries.");
118 
119  // Check, for column boundaries.
120  if(!(pColumn < mEntries[pRow].size()))
121  throw std::invalid_argument("Error in probability table. Column out of boundaries.");
122 
123  // Inrement counter.
124  mEntries[pRow][pColumn] = mEntries[pRow][pColumn] + pCounts;
125  }
126 
128  {
129  // Iterate over all rows.
130  for(unsigned int i = 0; i < mEntries.size(); i++)
131  {
132  // Sum all colums.
133  double sum = 0;
134  BOOST_FOREACH(double d, mEntries[i])
135  sum += d;
136 
137  // Use sum from above for normalization.
138  for(unsigned int j = 0; j < mEntries[i].size(); j++)
139  mEntries[i][j] /= sum;
140  }
141  }
142 
143 }
d
static void convertStringToVector(std::string &str, std::vector< double > &vec)
void load(boost::property_tree::ptree &pPt)
static void convertVectorToString(std::vector< double > &vec, std::string &str)
std::vector< std::vector< double > > mEntries
TFSIMD_FORCE_INLINE const tfScalar & y() const
void save(boost::property_tree::ptree &pPt)
double getProbability(unsigned int pRow, unsigned int pColumn)
TFSIMD_FORCE_INLINE const tfScalar & x() const
void add(unsigned int pRow, unsigned int pColumn, unsigned int pCounts)


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 03:57:54