DiscreteValues.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 
20 #include <sstream>
21 
22 using std::cout;
23 using std::endl;
24 using std::string;
25 using std::stringstream;
26 
27 namespace gtsam {
28 
29 /* ************************************************************************ */
30 void DiscreteValues::print(const string& s,
31  const KeyFormatter& keyFormatter) const {
32  cout << s << ": ";
33  for (auto&& kv : *this)
34  cout << "(" << keyFormatter(kv.first) << ", " << kv.second << ")";
35  cout << endl;
36 }
37 
38 /* ************************************************************************ */
39 bool DiscreteValues::equals(const DiscreteValues& x, double tol) const {
40  if (this->size() != x.size()) return false;
41  auto it1 = x.begin();
42  auto it2 = this->begin();
43  for (; it1 != x.end(); ++it1, ++it2) {
44  if (it1->first != it2->first || it1->second != it2->second) return false;
45  }
46  return true;
47 }
48 
49 /* ************************************************************************ */
51  for (const auto& kv : values) {
52  if (count(kv.first)) {
53  throw std::out_of_range(
54  "Requested to insert a DiscreteValues into another DiscreteValues "
55  "that already contains one or more of its keys.");
56  } else {
57  this->emplace(kv);
58  }
59  }
60  return *this;
61 }
62 
63 /* ************************************************************************ */
65  for (const auto& kv : values) {
66  if (!count(kv.first)) {
67  throw std::out_of_range(
68  "Requested to update a DiscreteValues with another DiscreteValues "
69  "that contains keys not present in the first.");
70  } else {
71  (*this)[kv.first] = kv.second;
72  }
73  }
74  return *this;
75 }
76 
77 /* ************************************************************************ */
78 string DiscreteValues::Translate(const Names& names, Key key, size_t index) {
79  if (names.empty()) {
80  stringstream ss;
81  ss << index;
82  return ss.str();
83  } else {
84  return names.at(key)[index];
85  }
86 }
87 
88 string DiscreteValues::markdown(const KeyFormatter& keyFormatter,
89  const Names& names) const {
90  stringstream ss;
91 
92  // Print out header and separator with alignment hints.
93  ss << "|Variable|value|\n|:-:|:-:|\n";
94 
95  // Print out all rows.
96  for (const auto& kv : *this) {
97  ss << "|" << keyFormatter(kv.first) << "|"
98  << Translate(names, kv.first, kv.second) << "|\n";
99  }
100 
101  return ss.str();
102 }
103 
104 /* ************************************************************************ */
105 string DiscreteValues::html(const KeyFormatter& keyFormatter,
106  const Names& names) const {
107  stringstream ss;
108 
109  // Print out preamble.
110  ss << "<div>\n<table class='DiscreteValues'>\n <thead>\n";
111 
112  // Print out header row.
113  ss << " <tr><th>Variable</th><th>value</th></tr>\n";
114 
115  // Finish header and start body.
116  ss << " </thead>\n <tbody>\n";
117 
118  // Print out all rows.
119  for (const auto& kv : *this) {
120  ss << " <tr>";
121  ss << "<th>" << keyFormatter(kv.first) << "</th><td>"
122  << Translate(names, kv.first, kv.second) << "</td>";
123  ss << "</tr>\n";
124  }
125  ss << " </tbody>\n</table>\n</div>";
126  return ss.str();
127 }
128 
129 /* ************************************************************************ */
130 string markdown(const DiscreteValues& values, const KeyFormatter& keyFormatter,
131  const DiscreteValues::Names& names) {
132  return values.markdown(keyFormatter, names);
133 }
134 
135 string html(const DiscreteValues& values, const KeyFormatter& keyFormatter,
136  const DiscreteValues::Names& names) {
137  return values.html(keyFormatter, names);
138 }
139 
140 } // namespace gtsam
gtsam::DiscreteValues::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print required by Testable.
Definition: DiscreteValues.cpp:30
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:130
s
RealScalar s
Definition: level1_cplx_impl.h:126
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::DiscreteValues::markdown
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a markdown table.
Definition: DiscreteValues.cpp:88
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::DiscreteValues::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a html table.
Definition: DiscreteValues.cpp:105
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::DiscreteValues::update
DiscreteValues & update(const DiscreteValues &values)
Definition: DiscreteValues.cpp:64
key
const gtsam::Symbol key('X', 0)
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam::DiscreteValues::equals
bool equals(const DiscreteValues &x, double tol=1e-9) const
equals required by Testable for unit testing.
Definition: DiscreteValues.cpp:39
gtsam
traits
Definition: SFMdata.h:40
DiscreteValues.h
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteValues::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: DiscreteValues.h:68
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::html
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition: DiscreteValues.cpp:135
gtsam::DiscreteValues::Names
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition: DiscreteValues.h:95
gtsam::DiscreteValues::Translate
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
Definition: DiscreteValues.cpp:78
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::HybridValues::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Output as a html table.
Definition: HybridValues.cpp:163


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:13