Assignment.h
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 #pragma once
20 
21 #include <functional>
22 #include <iostream>
23 #include <map>
24 #include <sstream>
25 #include <utility>
26 #include <vector>
27 
28 namespace gtsam {
29 
36 template <class L>
37 class Assignment : public std::map<L, size_t> {
45  static std::string DefaultFormatter(const L& x) {
46  std::stringstream ss;
47  ss << x;
48  return ss.str();
49  }
50 
51  public:
52  using std::map<L, size_t>::operator=;
53 
54  // Define the implicit default constructor.
55  Assignment() = default;
56 
57  // Construct from initializer list.
58  Assignment(std::initializer_list<std::pair<const L, size_t>> init)
59  : std::map<L, size_t>{init} {}
60 
61  void print(const std::string& s = "Assignment: ",
62  const std::function<std::string(L)>& labelFormatter =
63  &DefaultFormatter) const {
64  std::cout << s << ": ";
65  for (const typename Assignment::value_type& keyValue : *this) {
66  std::cout << "(" << labelFormatter(keyValue.first) << ", "
67  << keyValue.second << ")";
68  }
69  std::cout << std::endl;
70  }
71 
72  bool equals(const Assignment& other, double tol = 1e-9) const {
73  return (*this == other);
74  }
75 
88  template <typename Derived = Assignment<L>>
89  static std::vector<Derived> CartesianProduct(
90  const std::vector<std::pair<L, size_t>>& keys) {
91  std::vector<Derived> allPossValues;
92  Derived values;
93  typedef std::pair<L, size_t> DiscreteKey;
94  for (const DiscreteKey& key : keys)
95  values[key.first] = 0; // Initialize from 0
96  while (1) {
97  allPossValues.push_back(values);
98  size_t j = 0;
99  for (j = 0; j < keys.size(); j++) {
100  L idx = keys[j].first;
101  values[idx]++;
102  if (values[idx] < keys[j].second) break;
103  // Wrap condition
104  values[idx] = 0;
105  }
106  if (j == keys.size()) break;
107  }
108  return allPossValues;
109  }
110 }; // Assignment
111 
112 } // namespace gtsam
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
const gtsam::Symbol key('X', 0)
Assignment()=default
leaf::MyValues values
MatrixXd L
Definition: LLT_example.cpp:6
Definition: BFloat16.h:88
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
Assignment(std::initializer_list< std::pair< const L, size_t >> init)
Definition: Assignment.h:58
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
const G double tol
Definition: Group.h:86
const KeyVector keys
static std::string DefaultFormatter(const L &x)
Default method used by labelFormatter or valueFormatter when printing.
Definition: Assignment.h:45
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::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:56