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 AssignmentType = Assignment<L>>
89  static std::vector<AssignmentType> CartesianProduct(
90  const std::vector<std::pair<L, size_t>>& keys) {
91  std::vector<AssignmentType> allPossValues;
92  AssignmentType assignment;
93  for (const auto& [idx, _] : keys) assignment[idx] = 0; // Initialize from 0
94 
95  const size_t nrKeys = keys.size();
96  while (true) {
97  allPossValues.push_back(assignment);
98 
99  // Increment the assignment. This generalizes incrementing a binary number
100  size_t j = 0;
101  for (j = 0; j < nrKeys; j++) {
102  auto [idx, cardinality] = keys[j];
103  // Most of the time, we just increment the value for the first key, j=0:
104  assignment[idx]++;
105  // But if this key is done, we increment next key.
106  const bool carry = (assignment[idx] == cardinality);
107  if (!carry) break;
108  assignment[idx] = 0; // wrap on carry, and continue to next variable
109  }
110 
111  // If we propagated carry past the last key, exit:
112  if (j == nrKeys) break;
113  }
114  return allPossValues;
115  }
116 }; // Assignment
117 
118 } // 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
nrKeys
static const size_t nrKeys
Definition: testRegularJacobianFactor.cpp:31
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
gtsam::Assignment::CartesianProduct
static std::vector< AssignmentType > CartesianProduct(const std::vector< std::pair< L, size_t >> &keys)
Get Cartesian product consisting all possible configurations.
Definition: Assignment.h:89
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
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:509
gtsam
traits
Definition: SFMdata.h:40
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
_
constexpr descr< N - 1 > _(char const (&text)[N])
Definition: descr.h:109
init
Definition: TutorialInplaceLU.cpp:2
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:16