From cfb601156002c0514aea473a91c9dcf55753864d Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Mon, 3 Jan 2022 00:04:06 -0500 Subject: [PATCH] replace typedef with using and improve docstrings --- gtsam/discrete/AlgebraicDecisionTree.h | 1 + gtsam/discrete/DecisionTree-inl.h | 23 +++++++++++------ gtsam/discrete/DecisionTree.h | 34 ++++++++++++++++++++------ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/gtsam/discrete/AlgebraicDecisionTree.h b/gtsam/discrete/AlgebraicDecisionTree.h index 0b13f408e..7e046199a 100644 --- a/gtsam/discrete/AlgebraicDecisionTree.h +++ b/gtsam/discrete/AlgebraicDecisionTree.h @@ -113,6 +113,7 @@ namespace gtsam { template AlgebraicDecisionTree(const AlgebraicDecisionTree& other, const std::map& map) { + // Functor for label conversion so we can use `convertFrom`. std::function L_of_M = [&map](const M& label) -> L { return map.at(label); }; diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index 259489f06..0c016b6c5 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -82,13 +82,19 @@ namespace gtsam { return compare(this->constant_, other->constant_); } - /** print */ + /** + * @brief Print method. + * + * @param s Prefix string. + * @param labelFormatter Functor to format the node label. + * @param valueFormatter Functor to format the node value. + */ void print(const std::string& s, const LabelFormatter& labelFormatter, const ValueFormatter& valueFormatter) const override { std::cout << s << " Leaf " << valueFormatter(constant_) << std::endl; } - /** to graphviz file */ + /** Write graphviz format to stream `os`. */ void dot(std::ostream& os, const LabelFormatter& labelFormatter, const ValueFormatter& valueFormatter, bool showZero) const override { @@ -154,7 +160,7 @@ namespace gtsam { /** incremental allSame */ size_t allSame_; - typedef boost::shared_ptr ChoicePtr; + using ChoicePtr = boost::shared_ptr; public: @@ -462,6 +468,7 @@ namespace gtsam { template DecisionTree::DecisionTree(const DecisionTree& other, std::function Y_of_X) { + // Define functor for identity mapping of node label. auto L_of_L = [](const L& label) { return label; }; root_ = convertFrom(Y_of_X, L_of_L); } @@ -594,11 +601,11 @@ namespace gtsam { const typename DecisionTree::NodePtr& f, std::function L_of_M, std::function Y_of_X) const { - typedef DecisionTree MX; - typedef typename MX::Leaf MXLeaf; - typedef typename MX::Choice MXChoice; - typedef typename MX::NodePtr MXNodePtr; - typedef DecisionTree LY; + 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 diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index b02c2b302..0717405dd 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -64,11 +64,11 @@ namespace gtsam { using CompareFunc = std::function; /** Handy typedefs for unary and binary function types */ - typedef std::function Unary; - typedef std::function Binary; + using Unary = std::function; + using Binary = std::function; /** A label annotated with cardinality */ - typedef std::pair LabelC; + using LabelC = std::pair; /** DTs consist of Leaf and Choice nodes, both subclasses of Node */ class Leaf; @@ -77,7 +77,7 @@ namespace gtsam { /** ------------------------ Node base class --------------------------- */ class Node { public: - typedef boost::shared_ptr Ptr; + using Ptr = boost::shared_ptr; #ifdef DT_DEBUG_MEMORY static int nrNodes; @@ -128,7 +128,7 @@ namespace gtsam { /** A function is a shared pointer to the root of a DT */ using NodePtr = typename Node::Ptr; - /// a DecisionTree just contains the root. TODO(dellaert): make protected. + /// A DecisionTree just contains the root. TODO(dellaert): make protected. NodePtr root_; protected: @@ -137,7 +137,16 @@ namespace gtsam { template NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const; - /// Convert from a DecisionTree. + /** + * @brief Convert from a DecisionTree to DecisionTree. + * + * @tparam M The previous label type. + * @tparam X The previous node type. + * @param f The node pointer to the root of the previous DecisionTree. + * @param L_of_M Functor to convert from label type M to type L. + * @param Y_of_X Functor to convert from node type X to type Y. + * @return NodePtr + */ template NodePtr convertFrom(const typename DecisionTree::NodePtr& f, std::function L_of_M, @@ -174,7 +183,13 @@ namespace gtsam { DecisionTree(const L& label, // const DecisionTree& f0, const DecisionTree& f1); - /** Convert from a different type. */ + /** + * @brief Convert from a different node type. + * + * @tparam X The previous node type. + * @param other The DecisionTree to convert from. + * @param Y_of_X Functor to convert from node type X to type Y. + */ template DecisionTree(const DecisionTree& other, std::function Y_of_X); @@ -220,7 +235,7 @@ namespace gtsam { virtual ~DecisionTree() { } - /** empty tree? */ + /// Check if tree is empty. bool empty() const { return !root_; } /** equality */ @@ -283,18 +298,21 @@ namespace gtsam { /** free versions of apply */ + /// Apply unary operator `op` to DecisionTree `f`. template DecisionTree apply(const DecisionTree& f, const typename DecisionTree::Unary& op) { return f.apply(op); } + /// Apply unary operator `op` to DecisionTree `f` but with node type. template DecisionTree apply(const DecisionTree& f, const std::function& op) { return f.apply(op); } + /// Apply binary operator `op` to DecisionTree `f`. template DecisionTree apply(const DecisionTree& f, const DecisionTree& g,