From b453152f3eeff2425755073a9fb9803d73bffc1c Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Mon, 3 Jan 2022 23:44:51 -0500 Subject: [PATCH] Use template parameter for functions, enables auto # Conflicts: # gtsam/discrete/DecisionTree-inl.h # gtsam/discrete/DecisionTree.h --- gtsam/discrete/DecisionTree-inl.h | 35 ++++++++--------------- gtsam/discrete/DecisionTree.h | 11 ++++--- gtsam/discrete/tests/testDecisionTree.cpp | 8 ++---- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index a1ba0e4c1..a0df966a0 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -82,13 +82,7 @@ namespace gtsam { return compare(this->constant_, other->constant_); } - /** - * @brief Print method. - * - * @param s Prefix string. - * @param labelFormatter Functor to format the labels of type L. - * @param valueFormatter Functor to format the values of type Y. - */ + /** print */ void print(const std::string& s, const LabelFormatter& labelFormatter, const ValueFormatter& valueFormatter) const override { std::cout << s << " Leaf " << valueFormatter(constant_) << std::endl; @@ -465,23 +459,20 @@ namespace gtsam { /*********************************************************************************/ template - template + template DecisionTree::DecisionTree(const DecisionTree& other, - std::function Y_of_X) { + Func Y_of_X) { // Define functor for identity mapping of node label. - auto L_of_L = [](const L& label) { return label; }; + auto L_of_L = [](const L& label) { return label; }; root_ = convertFrom(other.root_, L_of_L, Y_of_X); } /*********************************************************************************/ template - template + template DecisionTree::DecisionTree(const DecisionTree& other, - const std::map& map, - std::function Y_of_X) { - std::function L_of_M = [&map](const M& label) -> L { - return map.at(label); - }; + const std::map& map, Func Y_of_X) { + auto L_of_M = [&map](const M& label) -> L { return map.at(label); }; root_ = convertFrom(other.root_, L_of_M, Y_of_X); } @@ -601,18 +592,16 @@ namespace gtsam { const typename DecisionTree::NodePtr& f, std::function L_of_M, std::function Y_of_X) const { - using MX = DecisionTree; - using MXLeaf = typename MX::Leaf; - using MXChoice = typename MX::Choice; - using MXNodePtr = typename MX::NodePtr; using LY = DecisionTree; // ugliness below because apparently we can't have templated virtual functions // If leaf, apply unary conversion "op" and create a unique leaf - auto leaf = boost::dynamic_pointer_cast(f); - if (leaf) return NodePtr(new Leaf(Y_of_X(leaf->constant()))); + using MXLeaf = typename DecisionTree::Leaf; + if (auto leaf = boost::dynamic_pointer_cast(f)) + return NodePtr(new Leaf(Y_of_X(leaf->constant()))); // Check if Choice + using MXChoice = typename DecisionTree::Choice; auto choice = boost::dynamic_pointer_cast(f); if (!choice) throw std::invalid_argument( "DecisionTree::Convert: Invalid NodePtr"); @@ -623,7 +612,7 @@ namespace gtsam { // put together via Shannon expansion otherwise not sorted. std::vector functions; - for(const MXNodePtr& branch: choice->branches()) { + for(auto && branch: choice->branches()) { LY converted(convertFrom(branch, L_of_M, Y_of_X)); functions += converted; } diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index fbcb59665..9692094e1 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -177,9 +177,8 @@ namespace gtsam { * @param other The DecisionTree to convert from. * @param Y_of_X Functor to convert from value type X to type Y. */ - template - DecisionTree(const DecisionTree& other, - std::function Y_of_X); + template + DecisionTree(const DecisionTree& other, Func Y_of_X); /** * @brief Convert from a different value type X to value type Y, also transate @@ -191,9 +190,9 @@ namespace gtsam { * @param L_of_M Map from label type M to type L. * @param Y_of_X Functor to convert from type X to type Y. */ - template - DecisionTree(const DecisionTree& other, const std::map& L_of_M, - std::function Y_of_X); + template + DecisionTree(const DecisionTree& other, const std::map& map, + Func Y_of_X); /// @} /// @name Testable diff --git a/gtsam/discrete/tests/testDecisionTree.cpp b/gtsam/discrete/tests/testDecisionTree.cpp index 84b9ca5fa..2e6ec59f7 100644 --- a/gtsam/discrete/tests/testDecisionTree.cpp +++ b/gtsam/discrete/tests/testDecisionTree.cpp @@ -230,9 +230,7 @@ TEST(DecisionTree, example) { /* ******************************************************************************** */ // test Conversion of values -std::function bool_of_int = [](const int& y) { - return y != 0; -}; +bool bool_of_int(const int& y) { return y != 0; }; typedef DecisionTree StringBoolTree; TEST(DecisionTree, ConvertValuesOnly) { @@ -269,7 +267,7 @@ TEST(DecisionTree, ConvertBoth) { map ordering; ordering[A] = X; ordering[B] = Y; - LabelBoolTree f2(f1, ordering, bool_of_int); + LabelBoolTree f2(f1, ordering, &bool_of_int); // Check some values Assignment