gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <boost/foreach.hpp>
22 #include <iostream>
23 #include <vector>
24 #include <map>
25 
26 
27 namespace gtsam {
28 
34  template<class L>
35  class Assignment: public std::map<L, size_t> {
36  public:
37  void print(const std::string& s = "Assignment: ") const {
38  std::cout << s << ": ";
39  BOOST_FOREACH(const typename Assignment::value_type& keyValue, *this)
40  std::cout << "(" << keyValue.first << ", " << keyValue.second << ")";
41  std::cout << std::endl;
42  }
43 
44  bool equals(const Assignment& other, double tol = 1e-9) const {
45  return (*this == other);
46  }
47  }; //Assignment
48 
49 
62  template<typename L>
63  std::vector<Assignment<L> > cartesianProduct(
64  const std::vector<std::pair<L, size_t> >& keys) {
65  std::vector<Assignment<L> > allPossValues;
66  Assignment<L> values;
67  typedef std::pair<L, size_t> DiscreteKey;
68  BOOST_FOREACH(const DiscreteKey& key, keys)
69  values[key.first] = 0; //Initialize from 0
70  while (1) {
71  allPossValues.push_back(values);
72  size_t j = 0;
73  for (j = 0; j < keys.size(); j++) {
74  L idx = keys[j].first;
75  values[idx]++;
76  if (values[idx] < keys[j].second)
77  break;
78  //Wrap condition
79  values[idx] = 0;
80  }
81  if (j == keys.size())
82  break;
83  }
84  return allPossValues;
85  }
86 
87 } // namespace gtsam
An assignment from labels to value index (size_t).
Definition: Assignment.h:35
Template to create a binary predicate.
Definition: Testable.h:102
std::pair< Key, size_t > DiscreteKey
Key type for discrete conditionals Includes name and cardinality.
Definition: DiscreteKey.h:33
std::vector< Assignment< L > > cartesianProduct(const std::vector< std::pair< L, size_t > > &keys)
Get Cartesian product consisting all possible configurations.
Definition: Assignment.h:63