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 static void stream(std::ostream& os, const DiscreteValues& x,
31  const KeyFormatter& keyFormatter) {
32  for (const auto& kv : x)
33  os << "(" << keyFormatter(kv.first) << ", " << kv.second << ")";
34 }
35 
36 /* ************************************************************************ */
37 std::ostream& operator<<(std::ostream& os, const DiscreteValues& x) {
39  return os;
40 }
41 
42 /* ************************************************************************ */
43 void DiscreteValues::print(const string& s,
44  const KeyFormatter& keyFormatter) const {
45  cout << s << ": ";
46  stream(cout, *this, keyFormatter);
47  cout << endl;
48 }
49 
50 /* ************************************************************************ */
51 bool DiscreteValues::equals(const DiscreteValues& x, double tol) const {
52  if (this->size() != x.size()) return false;
53  auto it1 = x.begin();
54  auto it2 = this->begin();
55  for (; it1 != x.end(); ++it1, ++it2) {
56  if (it1->first != it2->first || it1->second != it2->second) return false;
57  }
58  return true;
59 }
60 
61 /* ************************************************************************ */
63  const std::pair<Key, size_t>& assignment) {
64  if (count(assignment.first)) {
65  throw std::out_of_range(
66  "Requested to insert a DiscreteValues into another DiscreteValues "
67  "that already contains one or more of its keys.");
68  } else {
69  this->emplace(assignment);
70  }
71  return *this;
72 }
73 /* ************************************************************************ */
75  for (const auto& kv : values) {
76  this->insert(kv);
77  }
78  return *this;
79 }
80 
81 /* ************************************************************************ */
83  for (const auto& kv : values) {
84  if (!count(kv.first)) {
85  throw std::out_of_range(
86  "Requested to update a DiscreteValues with another DiscreteValues "
87  "that contains keys not present in the first.");
88  } else {
89  (*this)[kv.first] = kv.second;
90  }
91  }
92  return *this;
93 }
94 
95 /* ************************************************************************ */
96 string DiscreteValues::Translate(const Names& names, Key key, size_t index) {
97  if (names.empty()) {
98  stringstream ss;
99  ss << index;
100  return ss.str();
101  } else {
102  return names.at(key)[index];
103  }
104 }
105 
106 string DiscreteValues::markdown(const KeyFormatter& keyFormatter,
107  const Names& names) const {
108  stringstream ss;
109 
110  // Print out header and separator with alignment hints.
111  ss << "|Variable|value|\n|:-:|:-:|\n";
112 
113  // Print out all rows.
114  for (const auto& kv : *this) {
115  ss << "|" << keyFormatter(kv.first) << "|"
116  << Translate(names, kv.first, kv.second) << "|\n";
117  }
118 
119  return ss.str();
120 }
121 
122 /* ************************************************************************ */
123 string DiscreteValues::html(const KeyFormatter& keyFormatter,
124  const Names& names) const {
125  stringstream ss;
126 
127  // Print out preamble.
128  ss << "<div>\n<table class='DiscreteValues'>\n <thead>\n";
129 
130  // Print out header row.
131  ss << " <tr><th>Variable</th><th>value</th></tr>\n";
132 
133  // Finish header and start body.
134  ss << " </thead>\n <tbody>\n";
135 
136  // Print out all rows.
137  for (const auto& kv : *this) {
138  ss << " <tr>";
139  ss << "<th>" << keyFormatter(kv.first) << "</th><td>"
140  << Translate(names, kv.first, kv.second) << "</td>";
141  ss << "</tr>\n";
142  }
143  ss << " </tbody>\n</table>\n</div>";
144  return ss.str();
145 }
146 
147 /* ************************************************************************ */
148 string markdown(const DiscreteValues& values, const KeyFormatter& keyFormatter,
149  const DiscreteValues::Names& names) {
150  return values.markdown(keyFormatter, names);
151 }
152 
153 string html(const DiscreteValues& values, const KeyFormatter& keyFormatter,
154  const DiscreteValues::Names& names) {
155  return values.html(keyFormatter, names);
156 }
157 
158 } // namespace gtsam
gtsam::stream
static void stream(std::ostream &os, const DiscreteValues &x, const KeyFormatter &keyFormatter)
Definition: DiscreteValues.cpp:30
gtsam::DiscreteValues::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print required by Testable.
Definition: DiscreteValues.cpp:43
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:148
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::operator<<
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
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:106
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
os
ofstream os("timeSchurFactors.csv")
gtsam::DiscreteValues::html
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const
Output as a html table.
Definition: DiscreteValues.cpp:123
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
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)
Update values with corresponding keys from another DiscreteValues object.
Definition: DiscreteValues.cpp:82
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:51
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:71
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:153
gtsam::DiscreteValues::Names
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition: DiscreteValues.h:158
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:96
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 Sun Feb 16 2025 04:01:18