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
const gtsam::Symbol key('X', 0)
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a markdown table.
leaf::MyValues values
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a html table.
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
DiscreteValues & update(const DiscreteValues &values)
RealScalar s
bool equals(const DiscreteValues &x, double tol=1e-9) const
equals required by Testable for unit testing.
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
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
const G double tol
Definition: Group.h:86
std::pair< iterator, bool > insert(const value_type &value)
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
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10