Advertisement
Guest User

TreeContainer.h

a guest
Feb 24th, 2016
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.90 KB | None | 0 0
  1. #ifndef TREECONTAINER_H
  2. #define TREECONTAINER_H
  3.  
  4. /* API & Description:
  5.  *
  6.  * This Container provides the abillity to store values of any type.
  7.  * The structure of the Container is a tree. Each node of the tree stores a value of type T and can have N children.
  8.  * It provides different kinds of iterators to navigate through the tree. Access to the stored value of a node is only possible with an iterator.
  9.  * The tree has always a root-node with two sentinels: Start and End of the children.
  10.  *
  11.  * Class Description:
  12.  *
  13.  * treeContainer:
  14.  * Description:
  15.  * The main class. It provides the functions to modify the tree like creating new nodes, removing them or move them to another position.
  16.  * It also provides the function to search in the tree.
  17.  *
  18.  * Public methods:
  19.  *  template<typename T> node_iterator find(const T& value);                            Find the first node with the value in the tree
  20.  *  template<typename T> node_iterator find(const tree_iterator& it, const T& value);   Find the first node with the value in the children of the node
  21.  *
  22.  *  void clear();                           Remove all nodes from the tree
  23.  *  void clear(const tree_iterator& it);    Remove all nodes from the children of the node
  24.  *
  25.  *  template<typename T> node_iterator createNode(const tree_iterator& parent, const T& value); Create a new Node with value and type of value T and
  26.  *                                                                                              append it to the parent
  27.  *
  28.  *  void move(const tree_iterator& parent, const tree_iterator& node);          Move an existing node to the end of the children of a node
  29.  *  void insertAfter(const tree_iterator& pos, const tree_iterator& node);      Append an existing node to the parent of a node at position after node
  30.  *  void insertPrevious(const tree_iterator& pos, const tree_iterator& node);   Append an existing node to the parent of a node at position previous to node
  31.  *  void swap(const tree_iterator& it_1, const tree_iterator& it_2);            Swap two Nodes
  32.  *
  33.  *  node_iterator remove(const tree_iterator& node);        Remove the node from tree
  34.  *  node_iterator removeToEnd(const tree_iterator& pos);    Remove all childnodes of the parent of the node from this node to the end of the children, including this node
  35.  *  node_iterator removeToStart(const tree_iterator& pos);  Remove all childnodes of the parent of the node from this node to the Start of the children, including this node
  36.  *
  37.  *  int getDepth(const tree_iterator& node);    Returns the depth of the node. The Childnodes of Root have depth = 0;
  38.  *
  39.  *  begin() & end()     Different begin()- and end()-functions for the different iterators. See the description of the functions for more information
  40.  *
  41.  * Classes:
  42.  *  Node
  43.  *  SentinelNode
  44.  *
  45.  * Members:
  46.  *  std::shared_ptr myRoot
  47.  *
  48.  * Iterators:
  49.  *
  50.  * Description:
  51.  * Different Iterators to navigate through the tree and get access to the values of the node or alter the values.
  52.  * Each Iterator has a reverse equivalent, which iterates reverse and is provided as const and non const iterator.
  53.  *
  54.  * node_iterator:
  55.  * Description:
  56.  * The standard iterator of the tree. It iterates through all node until it reaches the end of the tree.
  57.  *
  58.  * child_iterator:
  59.  * Description:
  60.  * This iterator iterates through the children of a node and ignores the children of the children.
  61.  *
  62.  * deep_child_iterator:
  63.  * Description:
  64.  * Same as the child_iterator beside it doesn't ignore the children of the children.
  65.  *
  66.  */
  67.  
  68. /* ---------------------------------------------------------------------------------------------------------
  69.  *
  70.  * CAUTION: The following class descriptions contain the descriptions of internal classes or methods.
  71.  *          The descriptions provide more information about how the tree works in detail.
  72.  *
  73.  * Node:
  74.  * Description:
  75.  * The nodes of the tree, can be nodes and leafs.
  76.  * Providing the storage for values of any kind.
  77.  * Each node have to have a stored value.
  78.  * Users should not be able to access this class to ensure correct linking between the nodes in the tree.
  79.  *
  80.  * Linking between the Nodes:
  81.  * CAUTION: The parent of Root is Root
  82.  *
  83.  *
  84.  * Parent                            (Node)
  85.  *                                     |
  86.  *                                   Parent
  87.  *                                     |
  88.  * Siblings        (Node)---Prev---(Node)---Next---(Node)
  89.  *                            ______/ /  \ \_______
  90.  *                           /       /    \        \
  91.  *                         Start   First  Last     End
  92.  *                         /       /        \        \
  93.  * Children:  (Sentinal-Start) (Node) ... (Node)   (Sentinel-End)
  94.  *
  95.  *
  96.  * Public methods:
  97.  *  Node* getParent();      Returns the parent node
  98.  *  Node* getPrev();        Returns the previous siblings node
  99.  *  Node* getNext();        Returns the next sibling node
  100.  *  Node* getFirst();       Returns the first child of this node
  101.  *  Node* getLast();        Returns the last child of this node
  102.  *  Node* getStart();       Returns the sentinelnode for start of children of this node
  103.  *  Node* getEnd();         Returns the sentinelnode for end of children of this node
  104.  *
  105.  *  void setParent(Node* parent);   Set the parent of this node
  106.  *  void setPrev(Node* prev);       Set the previous siblings node
  107.  *  void setNext(Node* next);       Set the next siblings node
  108.  *  void setFirst(Node* first);     Set the first child of this node
  109.  *  void setLast(Node* last);       Set the last child of this node
  110.  *
  111.  *  void extract();                         Extract the Node. Deletes the links to relatives, ensure the siblings are correctly linked and
  112.  *                                          ensure the first/last-link of the parent is correct if necessary.
  113.  *                                          Does NOT delete the node from the children of the parent
  114.  *
  115.  *  void erase(Node* node);                 Erase the node from the childred of the parent. Make sure to call extract() after calling erase() to ensure correct linking
  116.  *  void erase(std::shared_ptr<Node> node);
  117.  *
  118.  *  std::shared_ptr<Node> getPtr();         Returns a shared_ptr of this
  119.  *
  120.  *  node_iterator appendNode(std::shared_ptr<Node> node);       Internal function to append a node to children of this
  121.  *  node_iterator insertAfter(std::shared_ptr<Node> node);      Internal function to append node to parent of this at position after this
  122.  *  node_iterator insertPrevious(std::shared_ptr<Node> node);   Internal function to append node to parent of this at position previous to this
  123.  *  void remove(int param);                                     Internal function to remove this node. Also call remove for the next or previous node
  124.  *                                                              to end/start if param is set. The parameters are:
  125.  *                                                              0 = remove only this node,
  126.  *                                                              1 = remove all nodes to the end of children of parent of this, including this node
  127.  *                                                              2 = remove all nodes to the start of children of parent of this, including this node
  128.  *
  129.  *  template<typename T> void setValue(T value);                Set the stored value of type T
  130.  *  template<typename T> T getValue();                          Get the stored value of type T
  131.  *  std::type_index getValueType();                             Get the value type of the stored value
  132.  *
  133.  * Members:
  134.  *  std::shared_ptr<placeholder> myValue = nullptr;             Pointer to the stored value of this node
  135.  *  std::shared_ptr<Node> mySentinelStart = nullptr;            Pointer to the sentinel node at start of the children
  136.  *  std::shared_ptr<Node> mySentinelEnd = nullptr;              Pointer to the sentinel node at end of the children
  137.  
  138.  *  Node* myParentNode = nullptr;                               Pointer to the parent node. If node is Root, it points to itself
  139.  *  Node* myPrevNode = nullptr;                                 Pointer to the previous siblings node
  140.  *  Node* myNextNode = nullptr;                                 Pointer to the next siblings node
  141.  *  Node* myFirstNode = nullptr;                                Pointer to the first child node. Is NULL if no children exist.
  142.  *  Node* myLastNode = nullptr;                                 Pointer to the last child node. Is NULL if no children exist.
  143.  
  144.  *  std::vector<std::shared_ptr<Node>> myChildren;              Vector of Pointers of the owned children
  145.  *
  146.  * classes:
  147.  * - placeholder        Abstract class to provide an abstract type for storing values of any type
  148.  * - holder             Template class, provides the storage for values of any type
  149.  *
  150.  *
  151.  * Iterators:
  152.  *
  153.  * tree_iterator:
  154.  * Description:
  155.  * An abstract class each iterator inheritates from to get an abstract type for iterators,
  156.  *
  157.  * Public Methods:
  158.  *  void operator =(tree::Node* node)           =Operator to set the iterator on a node
  159.  *  void operator =(const tree_iterator& rhs)   =Operstor to set the iterator to the same node as an other iterator
  160.  *  bool operator ==(tree_iterator rhs)         ==Operator to compare if the position of this iterator is the same as the position of an other itertor
  161.  *  bool operator !=(tree_iterator rhs)         !=Operator to compare if the position of this iterator is the not same as the position of an other itertor
  162.  *
  163.  *  void Next()     Set the iterator to the next siblings node. If the end the children of the parent is reached it will be set on the parent
  164.  *  void Prev()     Set the iterator to the previous siblings node. If the start the children of the parent is reached it will be set on the parent
  165.  *  void First()    Set the iterator to the first child of the node. If node has no child, the iterator is not set on an other node
  166.  *  void Last()     Set the iterator to the last child of the node. If node has no child, the iterator is not set on an other node
  167.  *  void Parent()   Set the iterator to the parent of this node.
  168.  *
  169.  *  template<typename T> void setValue(T value)  Store a new value of type T in the node
  170.  *  template<typename T> T getValue()            Get the stored value of the node
  171.  *  std::type_index getValueType()               Get the type of the stored value
  172.  *
  173.  *  bool hasChildren()      Returns true, if the node has one or more children
  174.  *
  175.  * Member:
  176.  *  Node* myNode    A raw pointer to the Node the iterator points to
  177.  *
  178.  * Other Iterators:
  179.  * Description: see above
  180.  *
  181.  * Public Methods:
  182.  *  node_iterator& operator ++()            increment the iterator depending the type of the iterator before returning
  183.  *  const node_iterator operator ++(int)    increment the iterator depending the type of the iterator after returning
  184.  *  node_iterator& operator --()            decrement the iterator depending the type of the iterator before returning
  185.  *  const node_iterator operator --(int)    decrement the iterator depending the type of the iterator after returning
  186.  *
  187.  * Members:
  188.  *  inherated myNode
  189.  *  deep_child_iterator: myStart, myEnd     save the positions where the iterator has to stop
  190.  *
  191.  * ---------------------------------------------------------------------------------------------------------
  192. */
  193. #include <map>
  194. #include <vector>
  195. #include <typeinfo>
  196. #include <typeindex>
  197. #include <memory>
  198.  
  199. namespace treeContainer {
  200. class tree_iterator;
  201. class node_iterator;
  202. class reverse_node_iterator;
  203. class child_iterator;
  204. class reverse_child_iterator;
  205. class deep_child_iterator;
  206. class reverse_deep_child_iterator;
  207. typedef const node_iterator const_node_iterator;
  208. typedef const reverse_node_iterator const_reverse_node_iterator;
  209. typedef const child_iterator const_child_iterator;
  210. typedef const reverse_child_iterator const_reverse_child_iterator;
  211. typedef const deep_child_iterator const_deep_child_iterator;
  212. typedef const reverse_deep_child_iterator const_reverse_deep_child_iterator;
  213.  
  214. class tree{
  215. private:    
  216.     friend class tree_iterator;
  217.     friend class node_iterator;
  218.     friend class reverse_node_iterator;
  219.     friend class child_iterator;
  220.     friend class reverse_child_iterator;
  221.     friend class deep_child_iterator;
  222.     friend class reverse_deep_child_iterator;
  223.  
  224.     class Node;
  225.  
  226. //structors
  227. public:
  228.     tree();
  229.     ~tree();
  230.  
  231. //functions
  232. public:
  233.     /* Description: find first node with value in tree
  234.      * Parameters:  "value" of type T
  235.      * Return:      node_iterator to first node with value of type T  */
  236.     template<typename T> node_iterator find(const T& value);
  237.  
  238.     /* Description: find first node with value in childnodes of node
  239.      * Parameters:  tree_iterator "it", "value" of type T
  240.      * Return:      node_iterator to first node with value of type T   */
  241.     template<typename T> node_iterator find(const tree_iterator& it, const T& value);
  242.  
  243.     /* Description: Remove all nodes from tree
  244.      * Parameters:  None
  245.      * Return:      None    */
  246.     void clear();
  247.  
  248.     /* Description: Remove all childnodes from node
  249.      * Parameters:  tree_iterator "it"
  250.      * Return:      None    */
  251.     void clear(const tree_iterator& it);
  252.  
  253.     /* Description: Create new node with value of type T and append it to childnodes of parent
  254.      * CAUTION:     Use the end()-method to append the node to Root
  255.      * Parameters:  tree_iterator "it", "value" of type T
  256.      * Return:      node_iterator to created node    */
  257.     template<typename T> node_iterator createNode(const tree_iterator& parent, const T& value);
  258.  
  259.     /* Description: Append existing node to childnodes of the new parent
  260.      * Parameters:  new parent: tree_iterator "parent", moved node: tree_iterator "node"
  261.      * Return:      None    */
  262.     void move(const tree_iterator& parent, const tree_iterator& node);
  263.  
  264.     /* Description: Append existing node to childnodes of parent of node at position after node
  265.      * Parameters:  tree_iterator "pos", tree_iterator "node"
  266.      * Return:      None    */
  267.     void insertAfter(const tree_iterator& pos, const tree_iterator& node);
  268.  
  269.     /* Description: Append existing node to childnodes of parent of node at position previous to node
  270.      * Parameters:  tree_iterator "pos", tree_iterator "node"
  271.      * Return:      None    */
  272.     void insertPrevious(const tree_iterator& pos, const tree_iterator& node);
  273.  
  274.     /* Description: swap nodes of "it_1" and "it_2"
  275.      * Parameters:  tree_iterator "it_1", tree_iterator "it_2"
  276.      * Return:      None    */
  277.     void swap(const tree_iterator& it_1, const tree_iterator& it_2);
  278.  
  279.     /* Description: delete node
  280.      * Parameters:  tree_iterator "node"
  281.      * Return:      node_iterator to previous or in case node is the first children to the parent node   */
  282.     node_iterator remove(const tree_iterator& node);
  283.  
  284.     /* Description: delete all nodes from node at position to end of the childnodes of parent of node including node at position.
  285.      * Parameters:  tree_iterator "pos"
  286.      * Return:      node_iterator to previous or in case node is the first children to the parent node.   */
  287.  
  288.     node_iterator removeToEnd(const tree_iterator& pos);
  289.  
  290.     /* Description: delete all nodes from node at position to start of the childnodes of parent of node including node at position.
  291.      * Parameters:  tree_iterator "pos"
  292.      * Return:      node_iterator to next or in case node is the last children to the parent node.   */
  293.     node_iterator removeToStart(const tree_iterator& pos);
  294.  
  295.     /* Description: get depth of node in tree, children of Root has Deep = 0
  296.      * Parameters:  tree_iterator "node"
  297.      * Return:      depth of type int    */
  298.     int getDepth(const tree_iterator& node);
  299.  
  300. private:
  301.     /* Description: Internal recursive function to search in the tree
  302.      * Parameters:  T value: value to find, Node* node: raw pointer to the actual Node
  303.      * Return:      raw pointer to the first node with the value, nullptr if the value was not found    */
  304.     template<typename T> Node* find(const T& value, Node* node);
  305.  
  306. //Functions iterating
  307. public:
  308.     /* Description: returns iterator to beginning of tree/childnodes
  309.      * CAUTION:     use the function which belongs to the used iterator to ensure correctly iterating
  310.      * Parameters:  node_iterators: None, child_iterators: tree_iterator "child"
  311.      * Return:      iterator to the beginning of tree/childnodes    */
  312.  
  313.     /* Description: returns (const_)node_iterator to beginning of tree for const an non-const node_iterators
  314.      *              In case the tree is empty it returns an node_iterator to root, als to the first child   */
  315.     node_iterator begin();
  316.     const_node_iterator cbegin() const;
  317.  
  318.     /* Description: returns (const_)reverse_node_iterator to reverse beginning of tree for const an non-const reverse_node_iterators */
  319.     reverse_node_iterator rbegin();
  320.     const_reverse_node_iterator crbegin() const;
  321.  
  322.     /* Description: returns (const_)child_iterator to beginning of childnodes of node for const an non-const child_node_iterators */
  323.     child_iterator begin(const tree_iterator& parent);
  324.     const_child_iterator cbegin(const tree_iterator& parent) const;
  325.  
  326.     /* Description: returns (const_)reverse_child_iterator to reverse beginning of childnodes of node for const an non-const reverse_child_node_iterators */
  327.     reverse_child_iterator rbegin(const tree_iterator& parent);
  328.     const_reverse_child_iterator crbegin(const tree_iterator& parent) const;
  329.  
  330.     /* Description: returns (const_)deep_child_iterator to beginning of childnodes of node for const an non-const deep_child_node_iterators */
  331.     deep_child_iterator dbegin(const tree_iterator& parent);
  332.     const_deep_child_iterator cdbegin(const tree_iterator& parent) const;
  333.  
  334.     /* Description: returns (const_)reverse_deep_child_iterator to reverse beginning of childnodes of node for const an non-const reverse_deep_child_node_iterators */
  335.     reverse_deep_child_iterator rdbegin(const tree_iterator& parent);
  336.     const_reverse_deep_child_iterator crdbegin(const tree_iterator& parent) const;
  337.  
  338.     /* Description: returns iterator to end of tree/childnodes
  339.      * CAUTION:     use the function which belongs to the used iterator to ensure correctly iterating
  340.      * Parameters:  node_iterators: None, child_iterators: tree_iterator "child"
  341.      * Return:      iterator to the end of tree/childnodes    */
  342.  
  343.     /* Description: returns (const_)node_iterator to end of tree for const an non-const node_iterators */
  344.     node_iterator end();
  345.     const_node_iterator cend() const;
  346.  
  347.     /* Description: returns (const_)reverse_node_iterator to reverse end of tree for const an non-const reverse_node_iterators */
  348.     reverse_node_iterator rend();
  349.     const_reverse_node_iterator crend() const;
  350.  
  351.     /* Description: returns (const_)child_iterator to end of childnodes of node for const an non-const child_node_iterators */
  352.     child_iterator end(const tree_iterator& parent);
  353.     const_child_iterator cend(const tree_iterator& parent) const;
  354.  
  355.     /* Description: returns (const_)reverse_child_iterator to reverse end of childnodes of node for const an non-const reverse_child_node_iterators */
  356.     reverse_child_iterator rend(const tree_iterator& parent);
  357.     const_reverse_child_iterator crend(const tree_iterator& parent) const;
  358.  
  359.     /* Description: returns (const_)deep_child_iterator to end of childnodes of node for const an non-const deep_child_node_iterators */
  360.     deep_child_iterator dend(const tree_iterator& parent);
  361.     const_deep_child_iterator cdend(const tree_iterator& parent) const;
  362.  
  363.     /* Description: returns (const_)reverse_deep_child_iterator to reverse end of childnodes of node for const an non-const reverse_deep_child_node_iterators */
  364.     reverse_deep_child_iterator rdend(const tree_iterator& parent);
  365.     const_reverse_deep_child_iterator crdend(const tree_iterator& parent) const;
  366.  
  367. //Classes
  368. private:
  369.     class Node{
  370.         /* Description:
  371.          * This class provides the Nodes and the methods of the Nodes. */
  372.         friend class tree;
  373.     private:
  374.  
  375.             class placeholder{
  376.                 /* Description:
  377.                  * Abstract class for providing an abstract type for any type of value  */
  378.             public:
  379.                 virtual ~placeholder() {}
  380.  
  381.                 //Description: virtual function to get access to type of stored value
  382.                 virtual std::type_index getType() = 0;
  383.             };
  384.  
  385.             template<typename T>
  386.             class holder : public placeholder{
  387.                 /* Description:
  388.                  * Template class to provide the abillity to store values of any type, inherits from placeholder*/
  389.             public:
  390.                 holder(T value) : myValue(value) {}
  391.  
  392.                 /* Description: Returns the type_index of the value which is stored
  393.                  * Parameters:  None
  394.                  * Return:      std::type_index   */
  395.                 std::type_index getType();
  396.  
  397.                 T myValue;
  398.             };
  399.     private:
  400.         //Members
  401.         std::shared_ptr<placeholder> myValue = nullptr;
  402.         std::shared_ptr<Node> mySentinelStart = nullptr;
  403.         std::shared_ptr<Node> mySentinelEnd = nullptr;
  404.  
  405.         Node* myParentNode = nullptr;
  406.         Node* myPrevNode = nullptr;
  407.         Node* myNextNode = nullptr;
  408.         Node* myFirstNode = nullptr;
  409.         Node* myLastNode = nullptr;
  410.  
  411.         std::vector<std::shared_ptr<Node>> myChildren;
  412.  
  413.     public:
  414.         //structors
  415.         Node();
  416.         template<typename T> Node(T value);
  417.         ~Node();
  418.  
  419.     public:
  420.         //Public Methods
  421.  
  422.         /* Description: Get a raw pointer to the related node
  423.          * Parameters:  None
  424.          * Return:      Node*    */
  425.         Node* getParent();
  426.         Node* getPrev();
  427.         Node* getNext();
  428.         Node* getFirst();
  429.         Node* getLast();
  430.         Node* getStart();
  431.         Node* getEnd();
  432.  
  433.         /* Description: Set a raw pointer to the related node
  434.          * Parameters:  Pointer Node* to relative
  435.          * Return:      None    */
  436.         void setParent(Node* parent);
  437.         void setPrev(Node* prev);
  438.         void setNext(Node* next);
  439.         void setFirst(Node* first);
  440.         void setLast(Node* last);
  441.  
  442.         /* Description: Extract this node. See class description for details
  443.          * Parameters:  None
  444.          * Return:      None    */
  445.         void extract();
  446.  
  447.         /* Description: Erase node from children of this. If node is not a sentinel call extract() after erase() to ensure correct linking,
  448.          * Parameters:  None
  449.          * Return:      None    */
  450.         void erase(Node* node);
  451.  
  452.         /* Description: Get the shared_ptr of this. The shared_ptr is get from the parent node.
  453.          * Parameters:  None
  454.          * Return:      shared_ptr of this    */
  455.         std::shared_ptr<Node> getPtr();
  456.  
  457.         /* Description: Append a node to the children of this
  458.          * Parameters:  shared_ptr to appending node
  459.          * Return:      node_iterator to appended node    */
  460.         node_iterator appendNode(std::shared_ptr<Node> node);
  461.  
  462.         /* Description: Append a node to the children of parent of this at position after this
  463.          * Parameters:  shared_ptr to appending node
  464.          * Return:      node_iterator to appended node    */
  465.         node_iterator insertAfter(std::shared_ptr<Node> node);
  466.  
  467.         /* Description: Append a node to the children of parent of this at position previous to this
  468.          * Parameters:  shared_ptr to appending node
  469.          * Return:      node_iterator to appended node    */
  470.         node_iterator insertPrevious(std::shared_ptr<Node> node);
  471.  
  472.         /* Description: Remove this node and if param is set all nodes to start or end of children of parent of this. See the class description for parameterlist
  473.          * Parameters:  type of removing as int
  474.          * Return:      None    */
  475.         void remove(int param);
  476.  
  477.         /* Description: Template function to set the stored value of this node
  478.          * Parameters:  value with type T
  479.          * Return:      None    */
  480.         template<typename T> void setValue(T value);
  481.  
  482.         /* Description: Get the stored value of this node of type T
  483.          * Parameters:  None
  484.          * Return:      value of type T   */
  485.         template<typename T> T getValue();
  486.  
  487.         /* Description: Get the type of the stored value
  488.          * Parameters:  None
  489.          * Return:      type of the stored value as type_index    */
  490.         std::type_index getValueType();
  491.  
  492.         /* Description: Operator to set the stored value of this node
  493.          * Parameters:  value of type T
  494.          * Return:      None    */
  495.         template<typename T> void operator =(T value) { setValue(value); }
  496.  
  497.     private:
  498.         //Helper functions
  499.  
  500.         /* Description: push new node to the list of children of this
  501.          * Parameters:  shared_ptr to the new child
  502.          * Return:      shared_ptr of the new child    */
  503.         std::shared_ptr<Node> pushNewNode(std::shared_ptr<Node> node);
  504.  
  505.         /* Description: Check, if the sentinel nodes and the first/last pointer of this are correctly linked
  506.          * Parameters:  None
  507.          * Return:      None    */
  508.         void checkSentinels();
  509.  
  510.         /* Description: Link to nodes as siblings. First is linked as the previous node of second and second is linked as the next of first.
  511.          *              Pointer can be NULL
  512.          * Parameters:  first: raw pointer to previous node, second: raw pointer to next node
  513.          * Return:      None    */
  514.         void linkNodes(Node* first, Node* second);
  515.     };
  516.  
  517.     class SentinelNode : public Node{
  518.         /* Description: Simpel inhertated class from Node to get an another type of node to provide an own type for sentinel nodes.
  519.          *              Stored values are always int.
  520.          *              List of values and meanings:
  521.          *              0: Root
  522.          *              1: Start
  523.          *              2: End
  524.          *              Different values to the list have no effect on the correct function of the tree. The different values are just for debugging
  525.          *              and because the nodes have to have a value.
  526.          */
  527.         friend class tree;
  528.     public:
  529.         SentinelNode(int value);
  530.         ~SentinelNode();
  531.     };
  532.  
  533. //Storage
  534. private:
  535.     std::shared_ptr<Node> myRoot = nullptr;
  536. };
  537.  
  538.  
  539. //Iterators
  540. class tree_iterator
  541. {
  542.     /* Description:
  543.      * Abstract class to provide an abstract type for all iterators. Provides the methods to iterate through the tree */
  544. private:
  545.     friend class tree;
  546.     friend class node_iterator;
  547.     friend class reverse_node_iterator;
  548.     friend class child_iterator;
  549.     friend class reverse_child_iterator;
  550.     friend class deep_child_iterator;
  551.     friend class reverse_deep_child_iterator;
  552. public:
  553.     virtual ~tree_iterator() {}
  554.  
  555.     /*Description: Set the iterator to the node. Only used internaly */
  556.     void operator =(tree::Node* node)
  557.     {
  558.         myNode = node;
  559.     }
  560.  
  561.     /*Description: Set the iterator to the node the passed iterator is pointing to */
  562.     void operator =(const tree_iterator& rhs)
  563.     {
  564.         myNode = rhs.myNode;
  565.     }
  566.  
  567.     /*Description: Compare if the nodes the passed iterator and this are pointing to are the same */
  568.     bool operator ==(const tree_iterator& rhs)
  569.     {
  570.         return myNode == rhs.myNode;
  571.     }
  572.  
  573.     /*Description: Compare if the nodes the passed iterator and this are pointing to are not the same */
  574.     bool operator !=(const tree_iterator& rhs)
  575.     {
  576.         return !(myNode == rhs.myNode);
  577.     }
  578.  
  579.     /*Description: Set the iterator to the next sibling.
  580.      * If the next sibling is the end of the children of the parent, the iterator is set to the parent */
  581.     void Next()
  582.     {
  583.         if(myNode->getNext() != myNode->getParent()->getEnd()) myNode = myNode->getNext();
  584.         else Parent();
  585.     }
  586.  
  587.     /*Description: Set the iterator to the previous sibling.
  588.      * If the previous sibling is the start of the children of the parent, the iterator is set to the parent */
  589.     void Prev()
  590.     {
  591.         if(myNode->getPrev() != myNode->getParent()->getStart()) myNode = myNode->getPrev();
  592.         else Parent();
  593.     }
  594.  
  595.     /*Description: Set the iterator to the first child of the node.
  596.      * If the node has no children, the iterator is not set to an other node */
  597.     void First()
  598.     {
  599.         if(myNode->getFirst()) myNode = myNode->getFirst();
  600.     }
  601.  
  602.     /*Description: Set the iterator to the last child of the node.
  603.      * If the node has no children, the iterator is not set to an other node */
  604.     void Last()
  605.     {
  606.         if(myNode->getLast()) myNode = myNode->getLast();
  607.     }
  608.  
  609.     /*Description: Set the iterator to the parent of the node */
  610.     void Parent()
  611.     {
  612.         myNode = myNode->getParent();
  613.     }
  614.  
  615.     /*Description: Assigns a new value to the node */
  616.     template<typename T> void setValue(T value)
  617.     {
  618.         myNode->setValue(value);
  619.     }
  620.  
  621.     /*Description: Get the value of the node */
  622.     template<typename T> T getValue()
  623.     {
  624.         return myNode->getValue<T>();
  625.     }
  626.  
  627.     /*Description:  Get the type of the value of the node. Returns as type_index*/
  628.     std::type_index getValueType()
  629.     {
  630.         return myNode->getValueType();
  631.     }
  632.  
  633.     /*Description: Returns true, if the node has one or more children, else false */
  634.     bool hasChildren()
  635.     {
  636.         if(myNode->getFirst()) return true;
  637.         else return false;
  638.     }
  639.  
  640. protected:
  641.     /*Description:  Internal method. Set the iterator to the next node. If the actual node has children the iterator is set to the first child
  642.                     At the end of the children getNext() is called to get the next node which is not a sentinel Node*/
  643.     void increment()
  644.     {
  645.         if(myNode->getFirst()) myNode = myNode->getFirst();
  646.         else if(myNode->getNext())
  647.         {
  648.             myNode = myNode->getNext();
  649.             if(!myNode->getNext()) getNext();
  650.         }
  651.         else getNext();
  652.     }
  653.  
  654.     /*Description:  Internal method. Set the iterator to the previus node. If the actual node has children the iterator is set to the last child
  655.                     At the Start of the children getPrev() is called to get the previus node which is not a sentinel node*/
  656.     void decrement()
  657.     {
  658.         if(myNode->getLast()) myNode = myNode->getLast();
  659.         else if(myNode->getPrev())
  660.         {
  661.             myNode = myNode->getPrev();
  662.             if(!myNode->getPrev()) getPrev();
  663.         }
  664.         else getPrev();
  665.     }
  666.  
  667.     /*Description:  Internal method. Searches recursive for the next node which is not a sentinel an set the iterator to this*/
  668.     void getNext()
  669.     {
  670.         if(myNode->getParent() == myNode) myNode = myNode->getEnd(); //is root?
  671.         else
  672.         {
  673.             if(myNode->getParent()->getNext()) myNode = myNode->getParent()->getNext();
  674.             else
  675.             {
  676.                 myNode = myNode->getParent();
  677.                 getNext();
  678.                 return;
  679.             }
  680.             if(!myNode->getNext()) getNext();
  681.         }
  682.     }
  683.  
  684.     /*Description:  Internal method. Searches recursive for the previous node which is not a sentinel an set the iterator to this*/
  685.     void getPrev()
  686.     {
  687.         if(myNode->getParent() == myNode) myNode = myNode->getStart(); //is root?
  688.         else
  689.         {
  690.             if(myNode->getParent()->getPrev()) myNode = myNode->getParent()->getPrev();
  691.             else
  692.             {
  693.                 myNode = myNode->getParent();
  694.                 getPrev();
  695.                 return;
  696.             }
  697.             if(!myNode->getPrev()) getPrev();
  698.         }
  699.     }
  700.  
  701. protected:
  702.     tree::Node* myNode;
  703. };
  704.  
  705. class node_iterator : public tree_iterator
  706. {
  707. private:
  708.     friend class tree;
  709.     node_iterator(tree::Node* node) { myNode = node; }
  710.  
  711. public:
  712.     node_iterator() {}
  713.     node_iterator(const tree_iterator& it) {myNode = it.myNode; }
  714.  
  715.     ~node_iterator() {}
  716.  
  717.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  718.     node_iterator& operator ++()
  719.     {
  720.         increment();
  721.         return *this;
  722.     }
  723.     const node_iterator operator ++(int)
  724.     {
  725.         node_iterator ret(myNode);
  726.         ++(*this);
  727.         return ret;
  728.     }
  729.     node_iterator& operator --()
  730.     {
  731.         decrement();
  732.         return *this;
  733.     }
  734.     const node_iterator operator --(int)
  735.     {
  736.         node_iterator ret(myNode);
  737.         --(*this);
  738.         return ret;
  739.     }
  740. };
  741. class reverse_node_iterator : public tree_iterator
  742. {
  743. private:
  744.     friend class tree;
  745.     reverse_node_iterator(tree::Node* node) { myNode = node; }
  746. public:
  747.     reverse_node_iterator() {}
  748.     reverse_node_iterator(const tree_iterator& it) {myNode = it.myNode; }
  749.  
  750.     ~reverse_node_iterator() {}
  751.  
  752.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  753.     reverse_node_iterator& operator ++()
  754.     {
  755.         decrement();
  756.         return *this;
  757.     }
  758.     const reverse_node_iterator operator ++(int)
  759.     {
  760.         reverse_node_iterator ret(myNode);
  761.         ++(*this);
  762.         return ret;
  763.     }
  764.     reverse_node_iterator& operator --()
  765.     {
  766.         increment();
  767.         return *this;
  768.     }
  769.     const reverse_node_iterator operator --(int)
  770.     {
  771.         reverse_node_iterator ret(myNode);
  772.         --(*this);
  773.         return ret;
  774.     }
  775. };
  776. class child_iterator : public tree_iterator
  777. {
  778. private:
  779.     friend class tree;
  780.     child_iterator(tree::Node* node) { myNode = node; }
  781. public:
  782.     child_iterator() {}
  783.     child_iterator(const tree_iterator& it) {myNode = it.myNode->getFirst(); }
  784.  
  785.     ~child_iterator() {}
  786.  
  787.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  788.     child_iterator& operator ++()
  789.     {
  790.         myNode = myNode->getNext();
  791.         return *this;
  792.     }
  793.     const child_iterator operator ++(int)
  794.     {
  795.         child_iterator ret(*this);
  796.         ++(*this);
  797.         return ret;
  798.     }
  799.     child_iterator& operator --()
  800.     {
  801.         myNode = myNode->getPrev();
  802.         return *this;
  803.     }
  804.     const child_iterator operator --(int)
  805.     {
  806.         child_iterator ret(*this);
  807.         --(*this);
  808.         return ret;
  809.     }
  810. };
  811. class reverse_child_iterator : public tree_iterator
  812. {
  813. private:
  814.     friend class tree;
  815.     reverse_child_iterator(tree::Node* node) { myNode = node; }
  816. public:
  817.     reverse_child_iterator() {}
  818.     reverse_child_iterator(const tree_iterator& it) { myNode = it.myNode->getLast(); }
  819.  
  820.     ~reverse_child_iterator() {}
  821.  
  822.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  823.     reverse_child_iterator& operator ++()
  824.     {
  825.         myNode = myNode->getPrev();
  826.         return *this;
  827.     }
  828.     const reverse_child_iterator operator ++(int)
  829.     {
  830.         reverse_child_iterator ret(*this);
  831.         ++(*this);
  832.         return ret;
  833.     }
  834.     reverse_child_iterator& operator --()
  835.     {
  836.         myNode = myNode->getNext();
  837.         return *this;
  838.     }
  839.     const reverse_child_iterator operator --(int)
  840.     {
  841.         reverse_child_iterator ret(*this);
  842.         --(*this);
  843.         return ret;
  844.     }
  845. };
  846. class deep_child_iterator : public tree_iterator
  847. {
  848. private:
  849.     friend class tree;
  850.     deep_child_iterator(tree::Node* node)
  851.     {
  852.         myNode = node;
  853.         myEndNode = node->getEnd();
  854.         myStartNode = node->getStart();
  855.     }
  856. public:
  857.     deep_child_iterator() {}
  858.     deep_child_iterator(const tree_iterator& it)
  859.     {
  860.         myNode = it.myNode->getFirst();
  861.         myEndNode = it.myNode->getEnd();
  862.         myStartNode = it.myNode->getStart();
  863.     }
  864.  
  865.     ~deep_child_iterator() {}
  866.  
  867.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  868.     deep_child_iterator& operator ++()
  869.     {
  870.         if(myNode->getFirst()) myNode = myNode->getFirst();
  871.         else if(myNode->getNext() == myNode->getParent()->getEnd() && myNode->getNext() != myEndNode) myNode = myNode->getParent()->getNext();
  872.         else myNode = myNode->getNext();
  873.  
  874.         return *this;
  875.     }
  876.     const deep_child_iterator operator ++(int)
  877.     {
  878.         deep_child_iterator ret(*this);
  879.         ++(*this);
  880.         return ret;
  881.     }
  882.     deep_child_iterator& operator --()
  883.     {
  884.         if(myNode->getLast()) myNode = myNode->getLast();
  885.         else if(myNode->getPrev() == myNode->getParent()->getStart() && myNode->getPrev() != myStartNode) myNode = myNode->getParent()->getPrev();
  886.         else myNode = myNode->getPrev();
  887.         return *this;
  888.     }
  889.     const deep_child_iterator operator --(int)
  890.     {
  891.         deep_child_iterator ret(*this);
  892.         --(*this);
  893.         return ret;
  894.     }
  895. private:
  896.     tree::Node* myEndNode;
  897.     tree::Node* myStartNode;
  898. };
  899. class reverse_deep_child_iterator : public tree_iterator
  900. {
  901. private:
  902.     friend class tree;
  903.     reverse_deep_child_iterator(tree::Node* node)
  904.     {
  905.         myNode = node;
  906.         myEndNode = node->getEnd();
  907.         myStartNode = node->getStart();
  908.     }
  909. public:
  910.     reverse_deep_child_iterator() {}
  911.     reverse_deep_child_iterator(const tree_iterator& it)
  912.     {
  913.         myNode = it.myNode->getFirst();
  914.         myEndNode = it.myNode->getEnd();
  915.         myStartNode = it.myNode->getStart();
  916.     }
  917.  
  918.     ~reverse_deep_child_iterator() {}
  919.  
  920.     /*Description:  Increment an decrement operators. Behaviour depends on type of the iterator */
  921.     reverse_deep_child_iterator& operator ++()
  922.     {
  923.         if(myNode->getLast()) myNode = myNode->getLast();
  924.         else if(myNode->getPrev() == myNode->getParent()->getStart() && myNode->getPrev() != myStartNode) myNode = myNode->getParent()->getPrev();
  925.         else myNode = myNode->getPrev();
  926.         return *this;
  927.     }
  928.     const reverse_deep_child_iterator operator ++(int)
  929.     {
  930.         reverse_child_iterator ret(*this);
  931.         ++(*this);
  932.         return ret;
  933.     }
  934.     reverse_deep_child_iterator& operator --()
  935.     {
  936.         if(myNode->getLast()) myNode = myNode->getLast();
  937.         else if(myNode->getPrev() == myNode->getParent()->getStart() && myNode->getPrev() != myStartNode) myNode = myNode->getParent()->getNext();
  938.         else myNode = myNode->getPrev();
  939.         return *this;
  940.     }
  941.     const reverse_deep_child_iterator operator --(int)
  942.     {
  943.         reverse_deep_child_iterator ret(*this);
  944.         --(*this);
  945.         return ret;
  946.     }
  947. private:
  948.     tree::Node* myEndNode;
  949.     tree::Node* myStartNode;
  950. };
  951. }
  952.  
  953.  
  954. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  955.  
  956. //Functions
  957.  
  958. //Find value in tree
  959. template<typename T> treeContainer::node_iterator treeContainer::tree::find(const T& value)
  960. {
  961.     Node* ret = find(value, myRoot->getFirst());
  962.     if(!ret) ret = myRoot->getEnd();
  963.  
  964.     return node_iterator(ret);
  965. }
  966.  
  967. //Find values in children of passed node
  968. template<typename T> treeContainer::node_iterator treeContainer::tree::find(const tree_iterator& it, const T& value)
  969. {
  970.     Node* ret = find(value, it.myNode);
  971.     if(!ret) ret = myRoot->getEnd();
  972.  
  973.     return node_iterator(ret);
  974. }
  975.  
  976. //Internal recursive method to search in the tree
  977. template<typename T> treeContainer::tree::Node* treeContainer::tree::find(const T& value, Node* node)
  978. {
  979.     Node* ret = nullptr;
  980.     if(node->getValueType() == typeid(T) && node->getValue<T>() == value) ret = node;
  981.     else if(node->getFirst()) ret = find(value, node->getFirst());
  982.     if(!ret && node->getNext() && node->getNext() != node->getParent()->getEnd()) ret = find(value, node->getNext());
  983.  
  984.     return ret;
  985. }
  986.  
  987. //create a new node with value of type T and append it to the children of passed node
  988. template<typename T> treeContainer::node_iterator treeContainer::tree::createNode(const tree_iterator& parent, const T& value)
  989. {
  990.     node_iterator ret;
  991.  
  992.     if(parent.myNode == myRoot->getEnd()) ret = myRoot->appendNode(std::make_shared<Node>(value));
  993.     else ret = parent.myNode->appendNode(std::make_shared<Node>(value));
  994.  
  995.     return ret;
  996. }
  997.  
  998. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  999.  
  1000. //class TreeNode
  1001. //structors
  1002. template<typename T> treeContainer::tree::Node::Node(T value)
  1003. {
  1004.     setValue(value);
  1005.  
  1006.     //Add SentinalNodes
  1007.     mySentinelStart = pushNewNode(std::make_shared<SentinelNode>(1));
  1008.     mySentinelEnd = pushNewNode(std::make_shared<SentinelNode>(2));
  1009.  
  1010.     linkNodes(mySentinelStart.get(), mySentinelEnd.get());
  1011. }
  1012.  
  1013. //functions
  1014.  
  1015. //Store passed value of type T an store it in newly created holder
  1016. template<typename T> void treeContainer::tree::Node::setValue(T value)
  1017. {
  1018.     myValue = std::shared_ptr<placeholder>(new holder<T>(value));
  1019. }
  1020.  
  1021. //Returns the stored value of type T
  1022. template<typename T> T treeContainer::tree::Node::getValue()
  1023. {
  1024.     return std::static_pointer_cast<holder<T>>(myValue)->myValue;
  1025. }
  1026.  
  1027. //Returns the type of stored value as type_index
  1028. template<typename T>
  1029. std::type_index treeContainer::tree::Node::holder<T>::getType()
  1030. {
  1031.     return std::type_index(typeid(myValue));
  1032. }
  1033.  
  1034. #endif // TREECONTAINER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement