Advertisement
Guest User

TreeContainer.h

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