| Home | Libraries | People | FAQ | More

## Class template splaytree_algorithms

boost::intrusive::splaytree_algorithms

## Synopsis

    // In header: <boost/intrusive/splaytree_algorithms.hpp>

    template<typename NodeTraits>
    class splaytree_algorithms {
    public:
      // types
      typedef NodeTraits::node                    node;
      typedef NodeTraits                          node_traits;
      typedef NodeTraits::node_ptr                node_ptr;
      typedef NodeTraits::const_node_ptr          const_node_ptr;
      typedef tree_algorithms::insert_commit_data insert_commit_data;

      // public static functions
      static node_ptr begin_node(const const_node_ptr &);
      static node_ptr end_node(const const_node_ptr &);
      static bool unique(const const_node_ptr &);
      static void unlink(const node_ptr &);
      static void swap_nodes(const node_ptr &, const node_ptr &);
      static void swap_nodes(const node_ptr &, const node_ptr &, const node_ptr &,
                             const node_ptr &);
      static void replace_node(const node_ptr &, const node_ptr &);
      static void replace_node(const node_ptr &, const node_ptr &,
                               const node_ptr &);
      static node_ptr next_node(const node_ptr &);
      static node_ptr prev_node(const node_ptr &);
      static void init(const node_ptr &);
      static void init_header(const node_ptr &);
      template<typename Disposer>
        static void clear_and_dispose(const node_ptr &, Disposer);
      static std::size_t count(const const_node_ptr &);
      static std::size_t size(const const_node_ptr &);
      static void swap_tree(const node_ptr &, const node_ptr &);
      static void insert_unique_commit(const node_ptr &, const node_ptr &,
                                       const insert_commit_data &);
      template<typename KeyType, typename KeyNodePtrCompare>
        static std::pair< node_ptr, bool >
        insert_unique_check(const node_ptr &, const KeyType &, KeyNodePtrCompare,
                            insert_commit_data &);
      template<typename KeyType, typename KeyNodePtrCompare>
        static std::pair< node_ptr, bool >
        insert_unique_check(const node_ptr &, const node_ptr &, const KeyType &,
                            KeyNodePtrCompare, insert_commit_data &);
      static bool is_header(const const_node_ptr &);
      template<typename KeyType, typename KeyNodePtrCompare>
        static node_ptr
        find(const const_node_ptr &, const KeyType &, KeyNodePtrCompare,
             bool = true);
      template<typename KeyType, typename KeyNodePtrCompare>
        static std::pair< node_ptr, node_ptr >
        equal_range(const const_node_ptr &, const KeyType &, KeyNodePtrCompare,
                    bool = true);
      template<typename KeyType, typename KeyNodePtrCompare>
        static node_ptr
        lower_bound(const const_node_ptr &, const KeyType &, KeyNodePtrCompare,
                    bool = true);
      template<typename KeyType, typename KeyNodePtrCompare>
        static node_ptr
        upper_bound(const const_node_ptr &, const KeyType &, KeyNodePtrCompare,
                    bool = true);
      template<typename NodePtrCompare>
        static node_ptr
        insert_equal(const node_ptr &, const node_ptr &, const node_ptr &,
                     NodePtrCompare);
      static node_ptr
      insert_before(const node_ptr &, const node_ptr &, const node_ptr &);
      static void push_back(const node_ptr &, const node_ptr &);
      static void push_front(const node_ptr &, const node_ptr &);
      template<typename NodePtrCompare>
        static node_ptr
        insert_equal_upper_bound(const node_ptr &, const node_ptr &,
                                 NodePtrCompare);
      template<typename NodePtrCompare>
        static node_ptr
        insert_equal_lower_bound(const node_ptr &, const node_ptr &,
                                 NodePtrCompare);
      template<typename Cloner, typename Disposer>
        static void clone(const const_node_ptr &, const node_ptr &, Cloner,
                          Disposer);
      static void erase(const node_ptr &, const node_ptr &, bool = true);
      static void splay_up(const node_ptr &, const node_ptr &);
      template<typename KeyType, typename KeyNodePtrCompare>
        static node_ptr
        splay_down(const node_ptr &, const KeyType &, KeyNodePtrCompare);
      static void rebalance(const node_ptr &);
      static node_ptr rebalance_subtree(const node_ptr &);
      static node_ptr get_header(const node_ptr &);
    };

## Description

A splay tree is an implementation of a binary search tree. The tree is self balancing using the splay algorithm as described in

"Self-Adjusting Binary Search Trees by Daniel Dominic Sleator and Robert Endre Tarjan AT&T Bell Laboratories, Murray Hill, NJ Journal of the ACM, Vol 32, no 3, July 1985, pp 652-686 splaytree_algorithms is configured with a NodeTraits class, which encapsulates the information about the node to be manipulated. NodeTraits must support the following interface:

Typedefs:

`node`: The type of the node that forms the circular list

`node_ptr`: A pointer to a node

`const_node_ptr`: A pointer to a const node

Static functions:

`static node_ptr get_parent(const_node_ptr n);`

`static void set_parent(node_ptr n, node_ptr parent);`

`static node_ptr get_left(const_node_ptr n);`

`static void set_left(node_ptr n, node_ptr left);`

`static node_ptr get_right(const_node_ptr n);`

`static void set_right(node_ptr n, node_ptr right);`

### `splaytree_algorithms` public types

1. typedef tree_algorithms::insert_commit_data insert_commit_data;

   This type is the information that will be filled by insert_unique_check

### `splaytree_algorithms` public static functions

1.     static node_ptr begin_node(const const_node_ptr & header);

2.     static node_ptr end_node(const const_node_ptr & header);

3.     static bool unique(const const_node_ptr & node);

   Requires: node is a node of the tree or an node initialized by init(...).

   Effects: Returns true if the node is initialized by init().

   Complexity: Constant time.

   Throws: Nothing.

4.     static void unlink(const node_ptr & node);

5.     static void swap_nodes(const node_ptr & node1, const node_ptr & node2);

   Requires: node1 and node2 can't be header nodes of two trees.

   Effects: Swaps two nodes. After the function node1 will be inserted in the position node2 before the function. node2 will be inserted in the position node1 had before the function.

   Complexity: Logarithmic.

   Throws: Nothing.

   Note: This function will break container ordering invariants if node1 and node2 are not equivalent according to the ordering rules.

   Experimental function

6.     static void swap_nodes(const node_ptr & node1, const node_ptr & header1,
                              const node_ptr & node2, const node_ptr & header2);

   Requires: node1 and node2 can't be header nodes of two trees with header header1 and header2.

   Effects: Swaps two nodes. After the function node1 will be inserted in the position node2 before the function. node2 will be inserted in the position node1 had before the function.

   Complexity: Constant.

   Throws: Nothing.

   Note: This function will break container ordering invariants if node1 and node2 are not equivalent according to the ordering rules.

   Experimental function

7.     static void replace_node(const node_ptr & node_to_be_replaced,
                                const node_ptr & new_node);

   Requires: node_to_be_replaced must be inserted in a tree and new_node must not be inserted in a tree.

   Effects: Replaces node_to_be_replaced in its position in the tree with new_node. The tree does not need to be rebalanced

   Complexity: Logarithmic.

   Throws: Nothing.

   Note: This function will break container ordering invariants if new_node is not equivalent to node_to_be_replaced according to the ordering rules. This function is faster than erasing and inserting the node, since no rebalancing and comparison is needed.

   Experimental function

8.     static void replace_node(const node_ptr & node_to_be_replaced,
                                const node_ptr & header, const node_ptr & new_node);

   Requires: node_to_be_replaced must be inserted in a tree with header "header" and new_node must not be inserted in a tree.

   Effects: Replaces node_to_be_replaced in its position in the tree with new_node. The tree does not need to be rebalanced

   Complexity: Constant.

   Throws: Nothing.

   Note: This function will break container ordering invariants if new_node is not equivalent to node_to_be_replaced according to the ordering rules. This function is faster than erasing and inserting the node, since no rebalancing or comparison is needed.

   Experimental function

9.     static node_ptr next_node(const node_ptr & p);

   Requires: p is a node from the tree except the header.

   Effects: Returns the next node of the tree.

   Complexity: Average constant time.

   Throws: Nothing.

10.     static node_ptr prev_node(const node_ptr & p);

    Requires: p is a node from the tree except the leftmost node.

    Effects: Returns the previous node of the tree.

    Complexity: Average constant time.

    Throws: Nothing.

11.     static void init(const node_ptr & node);

    Requires: node must not be part of any tree.

    Effects: After the function unique(node) == true.

    Complexity: Constant.

    Throws: Nothing.

    Nodes: If node is inserted in a tree, this function corrupts the tree.

12.     static void init_header(const node_ptr & header);

    Requires: node must not be part of any tree.

    Effects: Initializes the header to represent an empty tree. unique(header) == true.

    Complexity: Constant.

    Throws: Nothing.

    Nodes: If node is inserted in a tree, this function corrupts the tree.

13.     template<typename Disposer>
          static void clear_and_dispose(const node_ptr & header, Disposer disposer);

    Requires: "disposer" must be an object function taking a node_ptr parameter and shouldn't throw.

    Effects: Empties the target tree calling `void disposer::operator()(const node_ptr &)` for every node of the tree except the header.

    Complexity: Linear to the number of element of the source tree plus the. number of elements of tree target tree when calling this function.

    Throws: If cloner functor throws. If this happens target nodes are disposed.

14.     static std::size_t count(const const_node_ptr & node);

    Requires: node is a node of the tree but it's not the header.

    Effects: Returns the number of nodes of the subtree.

    Complexity: Linear time.

    Throws: Nothing.

15.     static std::size_t size(const const_node_ptr & header);

    Requires: header is the header node of the tree.

    Effects: Returns the number of nodes above the header.

    Complexity: Linear time.

    Throws: Nothing.

16.     static void swap_tree(const node_ptr & header1, const node_ptr & header2);

    Requires: header1 and header2 must be the header nodes of two trees.

    Effects: Swaps two trees. After the function header1 will contain links to the second tree and header2 will have links to the first tree.

    Complexity: Constant.

    Throws: Nothing.

17.     static void insert_unique_commit(const node_ptr & header,
                                         const node_ptr & new_value,
                                         const insert_commit_data & commit_data);

    Requires: "header" must be the header node of a tree. "commit_data" must have been obtained from a previous call to "insert_unique_check". No objects should have been inserted or erased from the set between the "insert_unique_check" that filled "commit_data" and the call to "insert_commit".

    Effects: Inserts new_node in the set using the information obtained from the "commit_data" that a previous "insert_check" filled.

    Complexity: Constant time.

    Throws: Nothing.

    Notes: This function has only sense if a "insert_unique_check" has been previously executed to fill "commit_data". No value should be inserted or erased between the "insert_check" and "insert_commit" calls.

18.     template<typename KeyType, typename KeyNodePtrCompare>
          static std::pair< node_ptr, bool >
          insert_unique_check(const node_ptr & header, const KeyType & key,
                              KeyNodePtrCompare comp,
                              insert_commit_data & commit_data);

    Requires: "header" must be the header node of a tree. KeyNodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. NodePtrCompare compares KeyType with a node_ptr.

    Effects: Checks if there is an equivalent node to "key" in the tree according to "comp" and obtains the needed information to realize a constant-time node insertion if there is no equivalent node.

    Returns: If there is an equivalent value returns a pair containing a node_ptr to the already present node and false. If there is not equivalent key can be inserted returns true in the returned pair's boolean and fills "commit_data" that is meant to be used with the "insert_commit" function to achieve a constant-time insertion function.

    Complexity: Average complexity is at most logarithmic.

    Throws: If "comp" throws.

    Notes: This function is used to improve performance when constructing a node is expensive and the user does not want to have two equivalent nodes in the tree: if there is an equivalent value the constructed object must be discarded. Many times, the part of the node that is used to impose the order is much cheaper to construct than the node and this function offers the possibility to use that part to check if the insertion will be successful.

    If the check is successful, the user can construct the node and use "insert_commit" to insert the node in constant-time. This gives a total logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).

    "commit_data" remains valid for a subsequent "insert_unique_commit" only if no more objects are inserted or erased from the set.

19.     template<typename KeyType, typename KeyNodePtrCompare>
          static std::pair< node_ptr, bool >
          insert_unique_check(const node_ptr & header, const node_ptr & hint,
                              const KeyType & key, KeyNodePtrCompare comp,
                              insert_commit_data & commit_data);

20.     static bool is_header(const const_node_ptr & p);

21.     template<typename KeyType, typename KeyNodePtrCompare>
          static node_ptr
          find(const const_node_ptr & header, const KeyType & key,
               KeyNodePtrCompare comp, bool splay = true);

    Requires: "header" must be the header node of a tree. KeyNodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.

    Effects: Returns an node_ptr to the element that is equivalent to "key" according to "comp" or "header" if that element does not exist.

    Complexity: Logarithmic.

    Throws: If "comp" throws.

22.     template<typename KeyType, typename KeyNodePtrCompare>
          static std::pair< node_ptr, node_ptr >
          equal_range(const const_node_ptr & header, const KeyType & key,
                      KeyNodePtrCompare comp, bool splay = true);

    Requires: "header" must be the header node of a tree. KeyNodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.

    Effects: Returns an a pair of node_ptr delimiting a range containing all elements that are equivalent to "key" according to "comp" or an empty range that indicates the position where those elements would be if they there are no equivalent elements.

    Complexity: Logarithmic.

    Throws: If "comp" throws.

23.     template<typename KeyType, typename KeyNodePtrCompare>
          static node_ptr
          lower_bound(const const_node_ptr & header, const KeyType & key,
                      KeyNodePtrCompare comp, bool splay = true);

    Requires: "header" must be the header node of a tree. KeyNodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.

    Effects: Returns an node_ptr to the first element that is not less than "key" according to "comp" or "header" if that element does not exist.

    Complexity: Logarithmic.

    Throws: If "comp" throws.

24.     template<typename KeyType, typename KeyNodePtrCompare>
          static node_ptr
          upper_bound(const const_node_ptr & header, const KeyType & key,
                      KeyNodePtrCompare comp, bool splay = true);

    Requires: "header" must be the header node of a tree. KeyNodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.

    Effects: Returns an node_ptr to the first element that is greater than "key" according to "comp" or "header" if that element does not exist.

    Complexity: Logarithmic.

    Throws: If "comp" throws.

25.     template<typename NodePtrCompare>
          static node_ptr
          insert_equal(const node_ptr & header, const node_ptr & hint,
                       const node_ptr & new_node, NodePtrCompare comp);

    Requires: "header" must be the header node of a tree. NodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. NodePtrCompare compares two node_ptrs. "hint" is node from the "header"'s tree.

    Effects: Inserts new_node into the tree, using "hint" as a hint to where it will be inserted. If "hint" is the upper_bound the insertion takes constant time (two comparisons in the worst case).

    Complexity: Logarithmic in general, but it is amortized constant time if new_node is inserted immediately before "hint".

    Throws: If "comp" throws.

26.     static node_ptr
        insert_before(const node_ptr & header, const node_ptr & pos,
                      const node_ptr & new_node);

    Requires: "header" must be the header node of a tree. "pos" must be a valid iterator or header (end) node. "pos" must be an iterator pointing to the successor to "new_node" once inserted according to the order of already inserted nodes. This function does not check "pos" and this precondition must be guaranteed by the caller.

    Effects: Inserts new_node into the tree before "pos".

    Complexity: Constant-time.

    Throws: Nothing.

    Note: If "pos" is not the successor of the newly inserted "new_node" tree invariants might be broken.

27.     static void push_back(const node_ptr & header, const node_ptr & new_node);

    Requires: "header" must be the header node of a tree. "new_node" must be, according to the used ordering no less than the greatest inserted key.

    Effects: Inserts new_node into the tree before "pos".

    Complexity: Constant-time.

    Throws: Nothing.

    Note: If "new_node" is less than the greatest inserted key tree invariants are broken. This function is slightly faster than using "insert_before".

28.     static void push_front(const node_ptr & header, const node_ptr & new_node);

    Requires: "header" must be the header node of a tree. "new_node" must be, according to the used ordering, no greater than the lowest inserted key.

    Effects: Inserts new_node into the tree before "pos".

    Complexity: Constant-time.

    Throws: Nothing.

    Note: If "new_node" is greater than the lowest inserted key tree invariants are broken. This function is slightly faster than using "insert_before".

29.     template<typename NodePtrCompare>
          static node_ptr
          insert_equal_upper_bound(const node_ptr & header, const node_ptr & new_node,
                                   NodePtrCompare comp);

    Requires: "header" must be the header node of a tree. NodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. NodePtrCompare compares two node_ptrs.

    Effects: Inserts new_node into the tree before the upper bound according to "comp".

    Complexity: Average complexity for insert element is at most logarithmic.

    Throws: If "comp" throws.

30.     template<typename NodePtrCompare>
          static node_ptr
          insert_equal_lower_bound(const node_ptr & header, const node_ptr & new_node,
                                   NodePtrCompare comp);

    Requires: "header" must be the header node of a tree. NodePtrCompare is a function object that induces a strict weak ordering compatible with the strict weak ordering used to create the the tree. NodePtrCompare compares two node_ptrs.

    Effects: Inserts new_node into the tree before the lower bound according to "comp".

    Complexity: Average complexity for insert element is at most logarithmic.

    Throws: If "comp" throws.

31.     template<typename Cloner, typename Disposer>
          static void clone(const const_node_ptr & source_header,
                            const node_ptr & target_header, Cloner cloner,
                            Disposer disposer);

    Requires: "cloner" must be a function object taking a node_ptr and returning a new cloned node of it. "disposer" must take a node_ptr and shouldn't throw.

    Effects: First empties target tree calling `void disposer::operator()(const node_ptr &)` for every node of the tree except the header.

    Then, duplicates the entire tree pointed by "source_header" cloning each source node with `node_ptr Cloner::operator()(const node_ptr &)` to obtain the nodes of the target tree. If "cloner" throws, the cloned target nodes are disposed using `void disposer(const node_ptr &)`.

    Complexity: Linear to the number of element of the source tree plus the. number of elements of tree target tree when calling this function.

    Throws: If cloner functor throws. If this happens target nodes are disposed.

32.     static void erase(const node_ptr & header, const node_ptr & z,
                          bool splay = true);

33.     static void splay_up(const node_ptr & node, const node_ptr & header);

34.     template<typename KeyType, typename KeyNodePtrCompare>
          static node_ptr
          splay_down(const node_ptr & header, const KeyType & key,
                     KeyNodePtrCompare comp);

35.     static void rebalance(const node_ptr & header);

    Requires: header must be the header of a tree.

    Effects: Rebalances the tree.

    Throws: Nothing.

    Complexity: Linear.

36.     static node_ptr rebalance_subtree(const node_ptr & old_root);

    Requires: old_root is a node of a tree.

    Effects: Rebalances the subtree rooted at old_root.

    Returns: The new root of the subtree.

    Throws: Nothing.

    Complexity: Linear.

37.     static node_ptr get_header(const node_ptr & n);

    Requires: "n" must be a node inserted in a tree.

    Effects: Returns a pointer to the header node of the tree.

    Complexity: Logarithmic.

    Throws: Nothing.