Add labelformatter to Assignment for convenience
parent
c4d388990d
commit
aef1669a50
|
|
@ -11,15 +11,17 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file Assignment.h
|
* @file Assignment.h
|
||||||
* @brief An assignment from labels to a discrete value index (size_t)
|
* @brief An assignment from labels to a discrete value index (size_t)
|
||||||
* @author Frank Dellaert
|
* @author Frank Dellaert
|
||||||
* @date Feb 5, 2012
|
* @date Feb 5, 2012
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <sstream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
@ -32,13 +34,30 @@ namespace gtsam {
|
||||||
*/
|
*/
|
||||||
template <class L>
|
template <class L>
|
||||||
class Assignment : public std::map<L, size_t> {
|
class Assignment : public std::map<L, size_t> {
|
||||||
|
/**
|
||||||
|
* @brief Default method used by `labelFormatter` or `valueFormatter` when
|
||||||
|
* printing.
|
||||||
|
*
|
||||||
|
* @param x The value passed to format.
|
||||||
|
* @return std::string
|
||||||
|
*/
|
||||||
|
static std::string DefaultFormatter(const L& x) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << x;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using std::map<L, size_t>::operator=;
|
using std::map<L, size_t>::operator=;
|
||||||
|
|
||||||
void print(const std::string& s = "Assignment: ") const {
|
void print(const std::string& s = "Assignment: ",
|
||||||
|
const std::function<std::string(L)>& labelFormatter =
|
||||||
|
&DefaultFormatter) const {
|
||||||
std::cout << s << ": ";
|
std::cout << s << ": ";
|
||||||
for (const typename Assignment::value_type& keyValue : *this)
|
for (const typename Assignment::value_type& keyValue : *this) {
|
||||||
std::cout << "(" << keyValue.first << ", " << keyValue.second << ")";
|
std::cout << "(" << labelFormatter(keyValue.first) << ", "
|
||||||
|
<< keyValue.second << ")";
|
||||||
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue