diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index f3b0dbf3a..ec6222fd7 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -108,7 +108,7 @@ namespace gtsam { /** print */ void print(const std::string& s, - const std::function formatter) const override { + const std::function& formatter) const override { bool showZero = true; if (showZero || constant_) std::cout << s << " Leaf " << constant_ << std::endl; } @@ -261,7 +261,8 @@ namespace gtsam { } /** print (as a tree) */ - void print(const std::string& s, const std::function formatter) const override { + void print(const std::string& s, + const std::function& formatter) const override { std::cout << s << " Choice("; std::cout << formatter(label_) << ") " << std::endl; for (size_t i = 0; i < branches_.size(); i++) @@ -675,7 +676,7 @@ namespace gtsam { template void DecisionTree::print( const std::string& s, - const std::function formatter) const { + const std::function& formatter) const { root_->print(s, formatter); } diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index 3b91def63..498fce5aa 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -39,6 +39,13 @@ namespace gtsam { template 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: /** Handy typedefs for unary and binary function types */ @@ -79,13 +86,9 @@ namespace gtsam { const void* id() const { return this; } // everything else is virtual, no documentation here as internal - virtual void print( - const std::string& s = "", - const std::function formatter = [](const L& x) { - std::stringstream ss; - ss << x; - return ss.str(); - }) const = 0; + virtual void print(const std::string& s = "", + const std::function& formatter = + &DefaultFormatter) const = 0; virtual void dot(std::ostream& os, bool showZero) const = 0; virtual bool sameLeaf(const Leaf& q) const = 0; virtual bool sameLeaf(const Node& q) const = 0; @@ -170,13 +173,9 @@ namespace gtsam { /// @{ /** GTSAM-style print */ - void print( - const std::string& s = "DecisionTree", - const std::function formatter = [](const L& x) { - std::stringstream ss; - ss << x; - return ss.str(); - }) const; + void print(const std::string& s = "DecisionTree", + const std::function& formatter = + &DefaultFormatter) const; // Testable bool equals(const DecisionTree& other, double tol = 1e-9) const; @@ -241,20 +240,19 @@ namespace gtsam { /** free versions of apply */ - //TODO(Varun) where are these templates Y, L and not L, Y? - template + template DecisionTree apply(const DecisionTree& f, const typename DecisionTree::Unary& op) { return f.apply(op); } - template + template DecisionTree apply(const DecisionTree& f, const std::function& op) { return f.apply(op); } - template + template DecisionTree apply(const DecisionTree& f, const DecisionTree& g, const typename DecisionTree::Binary& op) {