![]() |
Home | Libraries | People | FAQ | More |
boost::intrusive::cached_circular_slist_algorithms
// In header: <boost/intrusive/cached_circular_slist_algorithms.hpp> template<typename NodeTraits> class cached_circular_slist_algorithms { public: // types typedef NodeTraits::node node; typedef NodeTraits::node_ptr node_ptr; typedef NodeTraits::const_node_ptr const_node_ptr; typedef NodeTraits node_traits; // public static functions static node_ptr get_previous_previous_node(node_ptr) noexcept; static node_ptr get_previous_previous_node(node_ptr, node_ptr) noexcept; static void init(node_ptr) noexcept; static bool unique(const_node_ptr) noexcept; static bool inited(const_node_ptr) noexcept; static node_ptr end_node(const_node_ptr) noexcept; static bool is_empty(const_node_ptr) noexcept; static node_ptr get_previous_node(node_ptr, node_ptr) noexcept; static node_ptr get_previous_node(node_ptr) noexcept; static std::size_t count(const_node_ptr) noexcept; static void init_header(node_ptr, node_ptr &) noexcept; static void link_after(node_ptr, node_ptr, node_ptr &) noexcept; static void unlink_after(node_ptr, node_ptr &) noexcept; static void unlink_after(node_ptr, node_ptr, node_ptr &) noexcept; static void incorporate_after(node_ptr, node_ptr, node_ptr, node_ptr &) noexcept; static void transfer_after(node_ptr, node_ptr, node_ptr, node_ptr &, node_ptr &) noexcept; static void transfer_after(node_ptr, node_ptr, node_ptr &, node_ptr &) noexcept; static void swap_trailing_nodes(node_ptr, node_ptr, node_ptr &, node_ptr &) noexcept; static void reverse(node_ptr, node_ptr &) noexcept; static void shift_backwards(node_ptr, std::size_t, node_ptr &) noexcept; static void shift_forward(node_ptr, std::size_t, node_ptr &) noexcept; template<typename NodePtrCompare> static void sort(node_ptr, NodePtrCompare, node_ptr &); template<typename NodePtrCompare> static node_ptr merge(node_ptr, node_ptr, NodePtrCompare, node_ptr &, node_ptr &); template<typename Disposer> static std::size_t detach_and_dispose(node_ptr, Disposer, node_ptr &) noexcept; };
cached_circular_slist_algorithms provides basic algorithms to manipulate nodes forming a circular singly linked list that has a header node and a cached pointer to its last node. The nodes are the same as the nodes used by circular_slist_algorithms, and the cached last node can be stored anywhere (it is not part of the nodes).
The cached last node of a list is the last node of the list, or the header node if the list is empty. Functions that change the links take a reference to the cached last node ("last") and update it. Functions that can not update the cached last node of circular_slist_algorithms are not available.
cached_circular_slist_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_next(const_node_ptr n);
static void set_next(node_ptr n, node_ptr next);
cached_circular_slist_algorithms public static functionsstatic node_ptr get_previous_previous_node(node_ptr this_node) noexcept;
Requires: this_node must be in a circular list or be an empty circular list.
Effects: Returns the previous node of the previous node of this_node in the circular list.
Complexity: Linear to the number of elements in the circular list.
Throws: Nothing.
static node_ptr get_previous_previous_node(node_ptr p, node_ptr this_node) noexcept;
Requires: this_node and p must be in the same circular list.
Effects: Returns the previous node of the previous node of this_node in the circular list starting the search from p. The first node checked for equality is NodeTraits::get_next(NodeTraits::get_next(p)).
Complexity: Linear to the number of elements in the circular list.
Throws: Nothing.
static void init(node_ptr this_node) noexcept;
Effects: Constructs a non-used list element, putting the next pointer to null: NodeTraits::get_next(this_node) == node_ptr()
Complexity: Constant
Throws: Nothing.
static bool unique(const_node_ptr this_node) noexcept;
Effects: Returns true if "this_node" is not followed by other nodes or if it is a non-used node.
Complexity: Constant
Throws: Nothing.
static bool inited(const_node_ptr this_node) noexcept;
Effects: Returns true if "this_node" has the same state as if it was inited using "init(node_ptr)"
Complexity: Constant
Throws: Nothing.
static node_ptr end_node(const_node_ptr header) noexcept;
Effects: Returns a pointer to a node that represents the "end" (one past end) node
Complexity: Constant time.
Throws: Nothing.
static bool is_empty(const_node_ptr header) noexcept;
Effects: Returns true if "header" is the header of an empty list.
Complexity: Constant
Throws: Nothing.
static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node) noexcept;
Requires: this_node and prev_init_node must be in the same circular list.
Effects: Returns the previous node of this_node in the circular list starting the search from prev_init_node. The first node checked for equality is NodeTraits::get_next(prev_init_node).
Complexity: Linear to the number of elements between prev_init_node and this_node.
Throws: Nothing.
static node_ptr get_previous_node(node_ptr this_node) noexcept;
Requires: this_node must be in a circular list or be an empty circular list.
Effects: Returns the previous node of this_node in the circular list.
Complexity: Linear to the number of elements in the circular list.
Throws: Nothing.
static std::size_t count(const_node_ptr this_node) noexcept;
Effects: Returns the number of nodes of the circular list that contains this_node (this_node included).
Complexity: Linear
Throws: Nothing.
static void init_header(node_ptr header, node_ptr & last) noexcept;
Effects: Constructs an empty list, with "header" as the header node.
Postcondition: last == header.
Complexity: Constant
Throws: Nothing.
static void link_after(node_ptr prev_node, node_ptr this_node, node_ptr & last) noexcept;
Requires: prev_node must be a node of a circular list (or its header) whose cached last node is "last".
Effects: Links this_node after prev_node in the circular list and updates "last".
Complexity: Constant
Throws: Nothing.
static void unlink_after(node_ptr prev_node, node_ptr & last) noexcept;
Requires: prev_node must be a node of a circular list (or its header) whose cached last node is "last", and it must not be the last node.
Effects: Unlinks the next node of prev_node from the circular list and updates "last".
Complexity: Constant
Throws: Nothing.
static void unlink_after(node_ptr prev_node, node_ptr e, node_ptr & last) noexcept;
Requires: (prev_node, e) must be a valid range of a circular list whose cached last node is "last". "e" can be the end node.
Effects: Unlinks the range (prev_node, e) from the circular list and updates "last".
Complexity: Constant
Throws: Nothing.
static void incorporate_after(node_ptr bp, node_ptr b, node_ptr be, node_ptr & last) noexcept;
Requires: bp must be a node of a circular list (or its header) whose cached last node is "last". [b, be] must be a chain of nodes that are not in a list.
Effects: Links the nodes [b, be] after bp and updates "last".
Complexity: Constant
Throws: Nothing.
static void transfer_after(node_ptr p, node_ptr b, node_ptr e, node_ptr & p_last, node_ptr & b_last) noexcept;
Requires: p must be a node of a circular list (or its header) whose cached last node is "p_last". (b, e] must be a range of a circular list whose cached last node is "b_last". If both are in the same list, p must not be in (b, e], and p_last and b_last can be the same object.
Effects: Removes the nodes (b, e] from their list and inserts them after p in p's list. Updates "p_last" and "b_last".
Complexity: Constant
Throws: Nothing.
static void transfer_after(node_ptr p, node_ptr other, node_ptr & p_last, node_ptr & other_last) noexcept;
Requires: p must be a node of a circular list (or its header) whose cached last node is "p_last". "other" must be the header of a different circular list whose cached last node is "other_last".
Effects: Transfers all nodes of other's list after p in p's list. Updates "p_last" and "other_last".
Complexity: Constant
Throws: Nothing.
static void swap_trailing_nodes(node_ptr this_node, node_ptr other_node, node_ptr & this_last, node_ptr & other_last) noexcept;
Requires: this_node and other_node must be the headers of two circular lists whose cached last nodes are "this_last" and "other_last".
Effects: Swaps the nodes of both lists and their cached last nodes.
Complexity: Constant
Throws: Nothing.
static void reverse(node_ptr header, node_ptr & last) noexcept;
Requires: header must be the header of a circular list whose cached last node is "last".
Effects: Reverses the order of the nodes of the list and updates "last".
Complexity: Linear to the number of nodes.
Throws: Nothing.
static void shift_backwards(node_ptr header, std::size_t n, node_ptr & last) noexcept;
Requires: header must be the header of a circular list whose cached last node is "last".
Effects: Moves backwards all the nodes, so that the first node becomes the second, the second becomes the third... the last node becomes the first one. Repeats this n times. Updates "last".
Complexity: Linear to the number of nodes plus the number of moved positions.
Throws: Nothing.
static void shift_forward(node_ptr header, std::size_t n, node_ptr & last) noexcept;
Requires: header must be the header of a circular list whose cached last node is "last".
Effects: Moves forward all the nodes, so that the second node becomes the first, the third becomes the second... the first node becomes the last one. Repeats this n times. Updates "last".
Complexity: Linear to the number of nodes plus the number of moved positions.
Throws: Nothing.
template<typename NodePtrCompare> static void sort(node_ptr header, NodePtrCompare comp, node_ptr & last);
Requires: header must be the header of a circular list whose cached last node is "last". "comp" must be a function object that induces a strict weak ordering on node_ptrs: comp(a, b) returns true if node a must be placed before node b.
Effects: Stable sorts, according to "comp", all the nodes of the list and updates "last".
Complexity: Approximately N log N comparisons, where N is the number of nodes. Uses a fixed amount of stack memory.
Throws: If "comp" throws. Basic guarantee: all nodes remain in the list in unspecified order and "last" is the last node of the list.
template<typename NodePtrCompare> static node_ptr merge(node_ptr p, node_ptr x, NodePtrCompare comp, node_ptr & p_last, node_ptr & x_last);
Requires: p and x must be the headers of two different circular lists whose cached last nodes are "p_last" and "x_last". The nodes of both lists must be sorted according to "comp", a function object that induces a strict weak ordering on node_ptrs: comp(a, b) returns true if node a must be placed before node b.
Effects: Removes all the nodes of x's list and inserts them in order in p's list. The merge is stable: if a node of p's list is equivalent to a node of x's list, the node of p's list goes first. Updates "p_last" and "x_last".
Returns: The last node that was transferred from x's list, or a null pointer if x's list was empty.
Complexity: Linear: it performs at most N + M - 1 comparisons, where N and M are the number of nodes of each list.
Throws: If "comp" throws. Basic guarantee: all the nodes of x's list are moved to p's list in unspecified order, and "p_last" and "x_last" are updated.
template<typename Disposer> static std::size_t detach_and_dispose(node_ptr header, Disposer disposer, node_ptr & last) noexcept;
Requires: header must be the header of a circular list whose cached last node is "last". "disposer" must be an object function taking a node_ptr parameter. Its call operator and its copy and move constructors shouldn't throw.
Effects: Unlinks all nodes of the list and calls void disposer::operator()(node_ptr) for every node. The list is left empty.
Returns: The number of disposed nodes
Complexity: Linear to the number of nodes.
Throws: Nothing.