<commit_before>/** @file
    @brief Header

    @date 2014

    @author
    <NAME>
    <<EMAIL>>
    <http://sensics.com>
*/

// Copyright 2014 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

#ifndef INCLUDED_TreeNode_h_GUID_EEC6C5AF_332A_4780_55C2_D794B3BF5106
#define INCLUDED_TreeNode_h_GUID_EEC6C5AF_332A_4780_55C2_D794B3BF5106

// Internal Includes
#include <osvr/Util/TreeNode_fwd.h>

// Library/third-party includes
#include <boost/noncopyable.hpp>
#include <boost/assert.hpp>

// Standard includes
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>

namespace osvr {
namespace util {
    /// @brief A node in a generic tree, which can contain an object by value.
    /// @tparam ValueType The contained value type: must be
    /// default-constructible.
    ///
    /// Key features:
    ///
    /// - All non-root nodes are named, and children may only be created
    /// attached to a parent.
    /// - All children of a given node are uniquely named.
    /// - Node names are immutable.
    /// - Contained values are mutable.
    /// - Traversal is provided for by templated visit methods that accept a
    /// functor.
    /// - A "get or create" method is provided that guarantees the return a
    /// child of the given name (default-constructing one if it doesn't exist)
    ///
    /// @todo methods to remove a child (by pointer and by name)
    /// @todo We have parent pointers - use them or remove them
    template <typename ValueType>
    class TreeNode : public enable_shared_from_this<TreeNode<ValueType> >,
                     boost::noncopyable {
      public:
        /// @brief This template instantiation's type
        typedef TreeNode<ValueType> type;

        /// @brief The pointer for holding this template instantiation - used
        /// primarily/only in holding the root node
        typedef typename TreeNodePointer<ValueType>::type ptr_type;

        /// @brief The contained value type
        typedef ValueType value_type;

        /// @brief Create a path node
        /// @throws std::logic_error if a child of that name already exists or
        /// if the name provided is empty.
        static type &create(TreeNode &parent, std::string const &name);

        /// @brief Create a root.
        static ptr_type createRoot();

        /// @brief Get the named child, creating it if it doesn't exist.
        type &getOrCreateChildByName(std::string const &name);

        /// @brief Gets the name of the current node. This will be empty if and
        /// only if this is the root.
        std::string const &getName() const;

        /// @brief Is the current node a root node?
        bool isRoot() const;

        /// @brief Does the node have any children?
        ///
        /// May be faster than testing numChildren() != 0
        bool hasChildren() const;

        /// @brief How many children does the node have?
        size_t numChildren() const;

        /// @brief Reference accessor for contained value.
        value_type &value() { return m_value; }

        /// @brief Const reference accessor for contained value.
        value_type const &value() const { return m_value; }

        /// @brief Generic visitation method that calls a functor on each of the
        /// children in an undefined order.
        template <typename F> void visitChildren(F &visitor) {
            std::for_each(begin(m_children), end(m_children), visitor);
        }

        /// @brief Generic constant visitation method that calls a functor on
        /// each of the children (as const) in an undefined order.
        template <typename F> void visitConstChildren(F &visitor) const {
            std::for_each(begin(m_children), end(m_children), visitor);
        }

        /// @brief Generic constant visitation method that calls a functor on
        /// each of the children in an undefined order.
        /// @todo does this overload clutter the interface and reduce clarity
        /// between const and non-const visitors?
        template <typename F> void visitChildren(F &visitor) const {
            visitConstChildren(visitor);
        }

      private:
        /// @brief Private constructor for a non-root node
        TreeNode(type &parent, std::string const &name);

        /// @brief Private constructor for a root node
        TreeNode();

        /// @brief Internal helper to get child by name, or a null pointer if no
        /// such child.
        ptr_type m_getChildByName(std::string const &name);

        /// @brief Internal helper to add a named child. Assumes no such child
        /// already exists!
        void m_addChild(ptr_type const &child);

        /// @brief Contained value.<commit_msg>Visitors are given a reference or const reference, not a pointer.<commit_after>/** @file
    @brief Header

    @date 2014

    @author
    <NAME>
    <<EMAIL>>
    <http://sensics.com>
*/

// Copyright 2014 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

#ifndef INCLUDED_TreeNode_h_GUID_EEC6C5AF_332A_4780_55C2_D794B3BF5106
#define INCLUDED_TreeNode_h_GUID_EEC6C5AF_332A_4780_55C2_D794B3BF5106

// Internal Includes
#include <osvr/Util/TreeNode_fwd.h>

// Library/third-party includes
#include <boost/noncopyable.hpp>
#include <boost/assert.hpp>

// Standard includes
#include <string>
#include <vector>
#include <algorithm>
#include <stdexcept>

namespace osvr {
namespace util {
    /// @brief A node in a generic tree, which can contain an object by value.
    /// @tparam ValueType The contained value type: must be
    /// default-constructible.
    ///
    /// Key features:
    ///
    /// - All non-root nodes are named, and children may only be created
    /// attached to a parent.
    /// - All children of a given node are uniquely named.
    /// - Node names are immutable.
    /// - Contained values are mutable.
    /// - Traversal is provided for by templated visit methods that accept a
    /// functor.
    /// - A "get or create" method is provided that guarantees the return a
    /// child of the given name (default-constructing one if it doesn't exist)
    ///
    /// @todo methods to remove a child (by pointer and by name)
    /// @todo We have parent pointers - use them or remove them
    template <typename ValueType>
    class TreeNode : public enable_shared_from_this<TreeNode<ValueType> >,
                     boost::noncopyable {
      public:
        /// @brief This template instantiation's type
        typedef TreeNode<ValueType> type;

        /// @brief The pointer for holding this template instantiation - used
        /// primarily/only in holding the root node
        typedef typename TreeNodePointer<ValueType>::type ptr_type;

        /// @brief The contained value type
        typedef ValueType value_type;

        /// @brief Create a path node
        /// @throws std::logic_error if a child of that name already exists or
        /// if the name provided is empty.
        static type &create(TreeNode &parent, std::string const &name);

        /// @brief Create a root.
        static ptr_type createRoot();

        /// @brief Get the named child, creating it if it doesn't exist.
        type &getOrCreateChildByName(std::string const &name);

        /// @brief Gets the name of the current node. This will be empty if and
        /// only if this is the root.
        std::string const &getName() const;

        /// @brief Is the current node a root node?
        bool isRoot() const;

        /// @brief Does the node have any children?
        ///
        /// May be faster than testing numChildren() != 0
        bool hasChildren() const;

        /// @brief How many children does the node have?
        size_t numChildren() const;

        /// @brief Reference accessor for contained value.
        value_type &value() { return m_value; }

        /// @brief Const reference accessor for contained value.
        value_type const &value() const { return m_value; }

        /// @brief Generic visitation method that calls a functor on each of the
        /// children in an undefined order.
        template <typename F> void visitChildren(F &visitor) {
            std::for_each(begin(m_children), end(m_children),
                          [&](ptr_type &node) { visitor(*node); });
        }

        /// @brief Generic constant visitation method that calls a functor on
        /// each of the children (as const) in an undefined order.
        template <typename F> void visitConstChildren(F &visitor) const {
            std::for_each(begin(m_children), end(m_children),
                          [&](ptr_type const &node) {
                visitor(const_cast<type const &>(*node));
            });
        }

        /// @brief Generic constant visitation method that calls a functor on
        /// each of the children in an undefined order.
        /// @todo does this overload clutter the interface and reduce clarity
        /// between const and non-const visitors?
        template <typename F> void visitChildren(F &visitor) const {
            visitConstChildren(visitor);
        }

      private:
        /// @brief Private constructor for a non-root node
        TreeNode(type &parent, std::string const &name);

        /// @brief Private constructor for a root node
        TreeNode();

        /// @brief Internal helper to get child by name, or a null pointer if no
        /// such child.
        ptr_type m_getChildByName(std::string const &name);

        /// @brief Internal helper to add a named child. Assumes no such child
        /// already exists!
        void m_addChild(ptr_type const &child);

        /// @brief Contained value.
{'fraction_non_alphanumeric': 0.09691584967320262, 'fraction_numerical': 0.010416666666666666, 'mean_word_length': 2.2480928689883912, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1517623', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2570, 'n_words': 1036}