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
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::Assignment::print
void print(const std::string &s="Assignment: ", const std::function< std::string(L)> &labelFormatter=&DefaultFormatter) const
Definition: Assignment.h:61
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
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::Assignment::equals
bool equals(const Assignment &other, double tol=1e-9) const
Definition: Assignment.h:72
gtsam::Assignment::DefaultFormatter
static std::string DefaultFormatter(const L &x)
Default method used by labelFormatter or valueFormatter when printing.
Definition: Assignment.h:45
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::Assignment::Assignment
Assignment()=default
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Assignment::CartesianProduct
static std::vector< Derived > CartesianProduct(const std::vector< std::pair< L, size_t >> &keys)
Get Cartesian product consisting all possible configurations.
Definition: Assignment.h:89
L
MatrixXd L
Definition: LLT_example.cpp:6
gtsam::Assignment
Definition: Assignment.h:37
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:476
key
const gtsam::Symbol key('X', 0)
gtsam
traits
Definition: chartTesting.h:28
leaf::values
leaf::MyValues values
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::Assignment::Assignment
Assignment(std::initializer_list< std::pair< const L, size_t >> init)
Definition: Assignment.h:58
init
Definition: TutorialInplaceLU.cpp:2
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:00:29