<commit_before>//
//  poNodeContainer.cpp
//  BasicScene
//
//  Created by <NAME> on 3/22/14.
//
//

#include "poNodeContainer.h"

namespace po {
    NodeContainerRef NodeContainer::create(std::string name)
    {
        return std::shared_ptr<NodeContainer>(new NodeContainer(name));
    }

    NodeContainer::NodeContainer(std::string name)
    : Node(name)
    {
    }
    
    #pragma mark Set and Remove Scene
    void NodeContainer::setScene(SceneRef scene)
    {
        Node::setScene(scene);
        for(NodeRef &childNode : mChildren) {
            childNode->setScene(scene);
        }
    }
    
    void NodeContainer::removeScene()
    {
        Node::removeScene();
        for(NodeRef &childNode : mChildren) {
            childNode->removeScene();
        }
    }

    void NodeContainer::addChild(NodeRef node)
    {
        setParentAndScene(node);
        mChildren.push_back(node);
    }
    
    void NodeContainer::addChildAt(int index, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin()+index, node);
    }
    
    void NodeContainer::addChildBefore(NodeRef before, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin() + getChildIndex(before), node);
    }
    
    void NodeContainer::addChildAfter(NodeRef after, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin() + getChildIndex(after)+1, node);
    }
    
    int NodeContainer::getChildIndex(const NodeRef& child)
    {
        std::vector<NodeRef>::iterator iter = std::find(mChildren.begin(), mChildren.end(), child);
        if(iter != mChildren.end())
            return (int)std::distance(mChildren.begin(), iter);
        return INVALID_INDEX;
    }
    
    NodeRef NodeContainer::getChildByIndex(int index)
    {
        if(index < 0 || index >= mChildren.size())
            return NodeRef();
        return *(mChildren.begin() + index);
    }
    
    NodeRef NodeContainer::getChildByUID(uint uid)
    {
        //Go through our tree to find any node with UID
        for(NodeRef& node : mChildren) {
            NodeContainerRef container = std::dynamic_pointer_cast<NodeContainer>(node);
            if(container) {
                NodeRef foundNode = container->getChildByUID(uid);
                if(foundNode) return foundNode;
            }
            
            else {
                if(node->getUID() == uid) return node;
            }
        }
        
        //See if it is us
        if(mUid == uid) return shared_from_this();
            
        //Not found
        return NodeRef();
    }
    
    NodeRef NodeContainer::getChildByName(const std::string &name)
    {
        for(NodeRef& node : mChildren) {
            if(node->getName() == name) return node;
        }
        
        return NodeRef();
    }
    
    NodeRef NodeContainer::getFirstChild()
    {
        if(mChildren.empty())
            return NodeRef();
        
        return mChildren.front();
    }
    
    NodeRef NodeContainer::getLastChild()
    {
        if(mChildren.empty())
            return NodeRef();
        
        return mChildren.back();
    }
    
    std::vector<NodeRef> NodeContainer::getChildren()
    {
        return mChildren;
    };
    
    std::vector<NodeRef>& NodeContainer::getChildrenByReference()
    {
        return mChildren;
    }
    
    bool NodeContainer::removeChild(NodeRef node)
    {
        std::vector<NodeRef>::iterator iter = std::find(mChildren.begin(), mChildren.end(), node);
        if(iter != mChildren.end()) {
            (*iter)->removeParent();
            (*iter)->removeScene();
            
            #pragma message "This is not safe in recursion..."
            mChildren.erase(iter);
            
            return true;
        }
        
        return false;
    }
    
    bool NodeContainer::removeChildAt(int index)
    {
        if(index <= 0 || index >= mChildren.size())
            return false;
        
        mChildren[index]->removeParent();
        mChildren[index]->removeScene();
        
        mChildren.erase(mChildren.begin() + index);
        
        return true;
    }
    
    void removeAllChildren();
    
    void moveChildToFront(NodeRef& node);
    void moveChildForward(NodeRef& node);
    void moveChildToBack(NodeRef& node);
    void moveChildBackward(NodeRef& node);

    bool NodeContainer::removeChild(NodeRef node)
    {
        std::vector<NodeRef>::iterator iter = std::find(mChildren.begin(), mChildren.end(), node);
        
        if(iter != mChildren.end()) {
            //Remove reference to this node in child
            node->removeParent();
            node->removeScene();
            
            //Erase node
            mChildren.erase(iter);
            
            return true;
        }
        
        return false;
    }
    
    void NodeContainer::setParentAndScene(NodeRef node)
    {
        //See if the node is already a child of another node.
        if(node->getParent())
            node->getParent()->removeChild(node);
        
        //Assign ourselves as the parent
        node->setParent(std::dynamic_pointer_cast<NodeContainer>(shared_from_this()));
        node->setScene(mScene.lock());
    }

    void NodeContainer::updateTree()
    {
        update();
        for(NodeRef &childNode : mChildren)
            childNode->updateTree();
    }

    void NodeContainer::drawTree()
    {
        #pragma message "Need to implement matrix order"
        ci::gl::pushMatrices();
        setTransformation();<commit_msg>Finished adding util functions to po::Container<commit_after>//
//  poNodeContainer.cpp
//  BasicScene
//
//  Created by <NAME> on 3/22/14.
//
//

#include "poNodeContainer.h"

namespace po {
    NodeContainerRef NodeContainer::create(std::string name)
    {
        return std::shared_ptr<NodeContainer>(new NodeContainer(name));
    }

    NodeContainer::NodeContainer(std::string name)
    : Node(name)
    {
    }
    
    #pragma mark Set and Remove Scene
    void NodeContainer::setScene(SceneRef scene)
    {
        Node::setScene(scene);
        for(NodeRef &childNode : mChildren) {
            childNode->setScene(scene);
        }
    }
    
    void NodeContainer::removeScene()
    {
        Node::removeScene();
        for(NodeRef &childNode : mChildren) {
            childNode->removeScene();
        }
    }

    void NodeContainer::addChild(NodeRef node)
    {
        setParentAndScene(node);
        mChildren.push_back(node);
    }
    
    void NodeContainer::addChildAt(int index, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin()+index, node);
    }
    
    void NodeContainer::addChildBefore(NodeRef before, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin() + getChildIndex(before), node);
    }
    
    void NodeContainer::addChildAfter(NodeRef after, NodeRef node)
    {
        setParentAndScene(node);
        mChildren.insert(mChildren.begin() + getChildIndex(after)+1, node);
    }
    
    int NodeContainer::getChildIndex(const NodeRef& child)
    {
        std::vector<NodeRef>::iterator iter = std::find(mChildren.begin(), mChildren.end(), child);
        if(iter != mChildren.end())
            return (int)std::distance(mChildren.begin(), iter);
        return INVALID_INDEX;
    }
    
    NodeRef NodeContainer::getChildByIndex(int index)
    {
        if(index < 0 || index >= mChildren.size())
            return NodeRef();
        return *(mChildren.begin() + index);
    }
    
    NodeRef NodeContainer::getChildByUID(uint uid)
    {
        //Go through our tree to find any node with UID
        for(NodeRef& node : mChildren) {
            NodeContainerRef container = std::dynamic_pointer_cast<NodeContainer>(node);
            if(container) {
                NodeRef foundNode = container->getChildByUID(uid);
                if(foundNode) return foundNode;
            }
            
            else {
                if(node->getUID() == uid) return node;
            }
        }
        
        //See if it is us
        if(mUid == uid) return shared_from_this();
            
        //Not found
        return NodeRef();
    }
    
    NodeRef NodeContainer::getChildByName(const std::string &name)
    {
        for(NodeRef& node : mChildren) {
            if(node->getName() == name) return node;
        }
        
        return NodeRef();
    }
    
    NodeRef NodeContainer::getFirstChild()
    {
        if(mChildren.empty())
            return NodeRef();
        
        return mChildren.front();
    }
    
    NodeRef NodeContainer::getLastChild()
    {
        if(mChildren.empty())
            return NodeRef();
        
        return mChildren.back();
    }
    
    std::vector<NodeRef> NodeContainer::getChildren()
    {
        return mChildren;
    };
    
    std::vector<NodeRef>& NodeContainer::getChildrenByReference()
    {
        return mChildren;
    }
    
    bool NodeContainer::removeChild(NodeRef node)
    {
        std::vector<NodeRef>::iterator iter = std::find(mChildren.begin(), mChildren.end(), node);
        if(iter != mChildren.end()) {
            (*iter)->removeParent();
            (*iter)->removeScene();
            
            #pragma message "This is not safe in recursion..."
            mChildren.erase(iter);
            
            return true;
        }
        
        return false;
    }
    
    bool NodeContainer::removeChildAt(int index)
    {
        if(index <= 0 || index >= mChildren.size())
            return false;
        
        mChildren[index]->removeParent();
        mChildren[index]->removeScene();
        
        mChildren.erase(mChildren.begin() + index);
        
        return true;
    }
    
    void NodeContainer::removeAllChildren()
    {
        for(NodeRef& node : mChildren) {
            node->removeParent();
            node->removeScene();
        }
        
        mChildren.clear();
    }
    
    void NodeContainer::moveChildToFront(NodeRef& node)
    {
        if(removeChild(node))
            addChild(node);
    }
    
    void NodeContainer::moveChildForward(NodeRef& node)
    {
        int idx = getChildIndex(node);
        if(removeChild(node))
            #pragma message "Does this work with a vector, or would this be out of bounds?"
            addChildAt(std::min(idx+1, getNumChildren()), node);
    }
    
    void NodeContainer::moveChildToBack(NodeRef& node)
    {
        if(removeChild(node))
            addChildAt(0, node);
    }
    
    void NodeContainer::moveChildBackward(NodeRef& node)
    {
        int idx = getChildIndex(node);
        if(removeChild(node))
            addChildAt(std::max(idx-1, 0), node);
    }
    
    void NodeContainer::setParentAndScene(NodeRef node)
    {
        //See if the node is already a child of another node.
        if(node->getParent())
            node->getParent()->removeChild(node);
        
        //Assign ourselves as the parent
        node->setParent(std::dynamic_pointer_cast<NodeContainer>(shared_from_this()));
        node->setScene(mScene.lock());
    }

    void NodeContainer::updateTree()
    {
        update();
        for(NodeRef &childNode : mChildren)
            childNode->updateTree();
    }

    void NodeContainer::drawTree()
    {
        #pragma message "Need to implement matrix order"
        ci::gl::pushMatrices();
        setTransformation();
{'fraction_non_alphanumeric': 0.11794425087108014, 'fraction_numerical': 0.0017421602787456446, 'mean_word_length': 1.999216300940439, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 58, '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': '4646319', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3208, 'n_words': 620}