Add labelformatter to Assignment for convenience
parent
c4d388990d
commit
aef1669a50
|
|
@ -18,8 +18,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
|
@ -32,13 +34,30 @@ namespace gtsam {
|
|||
*/
|
||||
template <class L>
|
||||
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:
|
||||
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 << ": ";
|
||||
for (const typename Assignment::value_type& keyValue : *this)
|
||||
std::cout << "(" << keyValue.first << ", " << keyValue.second << ")";
|
||||
for (const typename Assignment::value_type& keyValue : *this) {
|
||||
std::cout << "(" << labelFormatter(keyValue.first) << ", "
|
||||
<< keyValue.second << ")";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue