formatter passed as reference and added a default formatter method

release/4.3a0
Varun Agrawal 2021-12-29 23:47:20 -05:00 committed by Frank Dellaert
parent 573d0d1773
commit ed839083e2
2 changed files with 20 additions and 21 deletions

View File

@ -108,7 +108,7 @@ namespace gtsam {
/** print */ /** print */
void print(const std::string& s, void print(const std::string& s,
const std::function<std::string(L)> formatter) const override { const std::function<std::string(L)>& formatter) const override {
bool showZero = true; bool showZero = true;
if (showZero || constant_) std::cout << s << " Leaf " << constant_ << std::endl; if (showZero || constant_) std::cout << s << " Leaf " << constant_ << std::endl;
} }
@ -261,7 +261,8 @@ namespace gtsam {
} }
/** print (as a tree) */ /** print (as a tree) */
void print(const std::string& s, const std::function<std::string(L)> formatter) const override { void print(const std::string& s,
const std::function<std::string(L)>& formatter) const override {
std::cout << s << " Choice("; std::cout << s << " Choice(";
std::cout << formatter(label_) << ") " << std::endl; std::cout << formatter(label_) << ") " << std::endl;
for (size_t i = 0; i < branches_.size(); i++) for (size_t i = 0; i < branches_.size(); i++)
@ -675,7 +676,7 @@ namespace gtsam {
template <typename L, typename Y> template <typename L, typename Y>
void DecisionTree<L, Y>::print( void DecisionTree<L, Y>::print(
const std::string& s, const std::string& s,
const std::function<std::string(L)> formatter) const { const std::function<std::string(L)>& formatter) const {
root_->print(s, formatter); root_->print(s, formatter);
} }

View File

@ -39,6 +39,13 @@ namespace gtsam {
template<typename L, typename Y> template<typename L, typename Y>
class GTSAM_EXPORT DecisionTree { class GTSAM_EXPORT DecisionTree {
/// default method used by `formatter` when printing.
static std::string DefaultFormatter(const L& x) {
std::stringstream ss;
ss << x;
return ss.str();
}
public: public:
/** Handy typedefs for unary and binary function types */ /** Handy typedefs for unary and binary function types */
@ -79,13 +86,9 @@ namespace gtsam {
const void* id() const { return this; } const void* id() const { return this; }
// everything else is virtual, no documentation here as internal // everything else is virtual, no documentation here as internal
virtual void print( virtual void print(const std::string& s = "",
const std::string& s = "", const std::function<std::string(L)>& formatter =
const std::function<std::string(L)> formatter = [](const L& x) { &DefaultFormatter) const = 0;
std::stringstream ss;
ss << x;
return ss.str();
}) const = 0;
virtual void dot(std::ostream& os, bool showZero) const = 0; virtual void dot(std::ostream& os, bool showZero) const = 0;
virtual bool sameLeaf(const Leaf& q) const = 0; virtual bool sameLeaf(const Leaf& q) const = 0;
virtual bool sameLeaf(const Node& q) const = 0; virtual bool sameLeaf(const Node& q) const = 0;
@ -170,13 +173,9 @@ namespace gtsam {
/// @{ /// @{
/** GTSAM-style print */ /** GTSAM-style print */
void print( void print(const std::string& s = "DecisionTree",
const std::string& s = "DecisionTree", const std::function<std::string(L)>& formatter =
const std::function<std::string(L)> formatter = [](const L& x) { &DefaultFormatter) const;
std::stringstream ss;
ss << x;
return ss.str();
}) const;
// Testable // Testable
bool equals(const DecisionTree& other, double tol = 1e-9) const; bool equals(const DecisionTree& other, double tol = 1e-9) const;
@ -241,20 +240,19 @@ namespace gtsam {
/** free versions of apply */ /** free versions of apply */
//TODO(Varun) where are these templates Y, L and not L, Y? template<typename L, typename Y>
template<typename Y, typename L>
DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f, DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
const typename DecisionTree<L, Y>::Unary& op) { const typename DecisionTree<L, Y>::Unary& op) {
return f.apply(op); return f.apply(op);
} }
template<typename Y, typename L, typename X> template<typename L, typename Y, typename X>
DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f, DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
const std::function<Y(const X&)>& op) { const std::function<Y(const X&)>& op) {
return f.apply(op); return f.apply(op);
} }
template<typename Y, typename L> template<typename L, typename Y>
DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f, DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
const DecisionTree<L, Y>& g, const DecisionTree<L, Y>& g,
const typename DecisionTree<L, Y>::Binary& op) { const typename DecisionTree<L, Y>::Binary& op) {