<commit_before>#include "treenode.h"

TreeNode::TreeNode(int type, TreeNode *parent) :
    _name(),
    _type(type),
    _flags(TreeFlags::NoNodeFlags),
    _parent(parent),
    _children()
{
    if (_parent != NULL)
        if (!_parent->addChild(this))
            _parent = NULL;
}

TreeNode::TreeNode(const std::string &name, int type, TreeNode *parent) :
    _name(name),
    _type(type),
    _flags(TreeFlags::NoNodeFlags),
    _parent(parent),
    _children()
{
    if (_parent != NULL)
        if (!_parent->addChild(this))
            _parent = NULL;
}

TreeNode::~TreeNode()
{
    if (_parent != NULL)
        _parent->_childDestroyed(this);

    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
        delete *itr;
}

TreeFlags TreeNode::flags() const
{
    return _flags;
}

void TreeNode::setFlags(TreeFlags flags)
{
    _flags = flags;

    if (_flags & TreeFlags::NodeNeverHasChildren)
        _children.clear();
}

bool TreeNode::addChild(TreeNode *child)
{
    if (_flags & TreeFlags::NodeNeverHasChildren)
        return false;
    else if (child == NULL || child->_parent != NULL)
        return false;

    child->_parent = this;
    _children.insert(child);
    return true;
}

bool TreeNode::moveChild(TreeNode *child)
{
    if (_flags & TreeFlags::NodeNeverHasChildren)
        return false;
    else if (child == NULL)
        return false;

    if (child->_parent != NULL)
        child->_parent->removeChild(child);
    child->_parent = this;
    _children.insert(child);
    return true;
}

bool TreeNode::removeChild(TreeNode *child)
{
    return _children.erase(child);
}

const std::set<TreeNode*>& TreeNode::children() const
{
    return _children;
}

const TreeNode* TreeNode::findChild(const std::string &name) const
{
    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        const TreeNode *child = *itr;
        if (child->_name == name)
            return child;
    }

    return NULL;
}

const TreeNode* TreeNode::findChildRecursively(const std::string &name, int maxDepth) const
{
    if (maxDepth == 0)
        return NULL;

    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        const TreeNode *child = *itr;
        if (child->_name == name)
            return child;

        const TreeNode *res = child->findChildRecursively(name, maxDepth - 1);
        if (res != NULL)
            return res;
    }

    return NULL;
}

TreeNode* TreeNode::findChild(const std::string &name)
{
    std::set<TreeNode*>::iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        TreeNode *child = *itr;
        if (child->_name == name)
            return child;
    }

    return NULL;
}

TreeNode* TreeNode::findChildRecursively(const std::string &name, int maxDepth)
{
    if (maxDepth == 0)
        return NULL;

    std::set<TreeNode*>::iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        TreeNode *child = *itr;
        if (child->_name == name)
            return child;

        TreeNode *res = child->findChildRecursively(name, maxDepth - 1);
        if (res != NULL)
            return res;
    }

    return NULL;
}

bool TreeNode::isLeaf() const
{
    return _children.empty();
}

bool TreeNode::isRoot() const
{
    return _parent == NULL;
}

int TreeNode::depth() const
{
    if (_children.empty())
        return 0;

    int max = 0;
    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
        max = std::max(max, (*itr)->depth());

    return max + 1;
}

void TreeNode::_childDestroyed(TreeNode *child)
{
    removeChild(child);
}
<commit_msg>Bug correction in TreeNode constructors<commit_after>#include "treenode.h"

TreeNode::TreeNode(int type, TreeNode *parent) :
    _name(),
    _type(type),
    _flags(TreeFlags::NoNodeFlags),
    _parent(NULL),
    _children()
{
    if (parent != NULL)
        _parent = parent->addChild(this) ? parent : NULL;
}

TreeNode::TreeNode(const std::string &name, int type, TreeNode *parent) :
    _name(name),
    _type(type),
    _flags(TreeFlags::NoNodeFlags),
    _parent(NULL),
    _children()
{
    if (parent != NULL)
        _parent = parent->addChild(this) ? parent : NULL;
}

TreeNode::~TreeNode()
{
    if (_parent != NULL)
        _parent->_childDestroyed(this);

    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
        delete *itr;
}

TreeFlags TreeNode::flags() const
{
    return _flags;
}

void TreeNode::setFlags(TreeFlags flags)
{
    _flags = flags;

    if (_flags & TreeFlags::NodeNeverHasChildren)
        _children.clear();
}

bool TreeNode::addChild(TreeNode *child)
{
    if (_flags & TreeFlags::NodeNeverHasChildren)
        return false;
    else if (child == NULL || child->_parent != NULL)
        return false;

    child->_parent = this;
    _children.insert(child);
    return true;
}

bool TreeNode::moveChild(TreeNode *child)
{
    if (_flags & TreeFlags::NodeNeverHasChildren)
        return false;
    else if (child == NULL)
        return false;

    if (child->_parent != NULL)
        child->_parent->removeChild(child);
    child->_parent = this;
    _children.insert(child);
    return true;
}

bool TreeNode::removeChild(TreeNode *child)
{
    return _children.erase(child);
}

const std::set<TreeNode*>& TreeNode::children() const
{
    return _children;
}

const TreeNode* TreeNode::findChild(const std::string &name) const
{
    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        const TreeNode *child = *itr;
        if (child->_name == name)
            return child;
    }

    return NULL;
}

const TreeNode* TreeNode::findChildRecursively(const std::string &name, int maxDepth) const
{
    if (maxDepth == 0)
        return NULL;

    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        const TreeNode *child = *itr;
        if (child->_name == name)
            return child;

        const TreeNode *res = child->findChildRecursively(name, maxDepth - 1);
        if (res != NULL)
            return res;
    }

    return NULL;
}

TreeNode* TreeNode::findChild(const std::string &name)
{
    std::set<TreeNode*>::iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        TreeNode *child = *itr;
        if (child->_name == name)
            return child;
    }

    return NULL;
}

TreeNode* TreeNode::findChildRecursively(const std::string &name, int maxDepth)
{
    if (maxDepth == 0)
        return NULL;

    std::set<TreeNode*>::iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
    {
        TreeNode *child = *itr;
        if (child->_name == name)
            return child;

        TreeNode *res = child->findChildRecursively(name, maxDepth - 1);
        if (res != NULL)
            return res;
    }

    return NULL;
}

bool TreeNode::isLeaf() const
{
    return _children.empty();
}

bool TreeNode::isRoot() const
{
    return _parent == NULL;
}

int TreeNode::depth() const
{
    if (_children.empty())
        return 0;

    int max = 0;
    std::set<TreeNode*>::const_iterator itr;
    for (itr = _children.begin(); itr != _children.end(); ++itr)
        max = std::max(max, (*itr)->depth());

    return max + 1;
}

void TreeNode::_childDestroyed(TreeNode *child)
{
    removeChild(child);
}

{'fraction_non_alphanumeric': 0.14622641509433962, 'fraction_numerical': 0.0018343815513626835, 'mean_word_length': 2.85699848408287, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, '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': '563639', 'n_tokens_mistral': 2498, 'n_tokens_neox': 2496, 'n_words': 582}