Move boost/intrusive to senf/boost_intrusive
[senf.git] / senf / boost_intrusive / slist_algorithms.hpp
diff --git a/senf/boost_intrusive/slist_algorithms.hpp b/senf/boost_intrusive/slist_algorithms.hpp
new file mode 100644 (file)
index 0000000..19158ce
--- /dev/null
@@ -0,0 +1,246 @@
+/////////////////////////////////////////////////////////////////////////////\r
+//\r
+// (C) Copyright Olaf Krzikalla 2004-2006.\r
+// (C) Copyright Ion GaztaƱaga  2006-2007\r
+//\r
+// Distributed under the Boost Software License, Version 1.0.\r
+//    (See accompanying file LICENSE_1_0.txt or copy at\r
+//          http://www.boost.org/LICENSE_1_0.txt)\r
+//\r
+// See http://www.boost.org/libs/intrusive for documentation.\r
+//\r
+/////////////////////////////////////////////////////////////////////////////\r
+\r
+#ifndef BOOST_INTRUSIVE_SLIST_ALGORITHMS_HPP\r
+#define BOOST_INTRUSIVE_SLIST_ALGORITHMS_HPP\r
+\r
+#include "detail/config_begin.hpp"\r
+#include <iterator>\r
+#include <boost/assert.hpp>\r
+#include "detail/pointer_type.hpp"\r
+#include "detail/pointer_to_other.hpp"\r
+#include <boost/get_pointer.hpp>\r
+#include <cstddef>\r
+\r
+namespace boost {\r
+namespace intrusive {\r
+\r
+//! slist_algorithms provides basic algorithms to manipulate nodes\r
+//! forming a circular singly linked list. An empty circular list is formed by a node\r
+//! whose pointer to the next node points to itself.\r
+//!\r
+//! slist_algorithms is configured with a NodeTraits class, which capsulates the\r
+//! information about the node to be manipulated. NodeTraits must support the\r
+//! following interface:\r
+//!\r
+//! <b>Typedefs</b>:\r
+//!\r
+//! <tt>node</tt>: The type of the node that forms the circular list\r
+//!\r
+//! <tt>node_ptr</tt>: A pointer to a node\r
+//!\r
+//! <tt>const_node_ptr</tt>: A pointer to a const node\r
+//!\r
+//! <b>Static functions</b>:\r
+//!\r
+//! <tt>static node_ptr get_next(const_node_ptr n);</tt>\r
+//! \r
+//! <tt>static void set_next(node_ptr n, node_ptr next);</tt>\r
+template<class NodeTraits>\r
+class slist_algorithms\r
+{\r
+   public:\r
+   typedef typename NodeTraits::node_ptr        node_ptr;\r
+   typedef typename NodeTraits::const_node_ptr  const_node_ptr;\r
+\r
+   //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.\r
+   //! \r
+   //! <b>Effects</b>: Returns the previous node of this_node in the circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Linear to the number of elements in the circular list.\r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static node_ptr get_previous_node(node_ptr this_node)\r
+   {\r
+      node_ptr p = this_node;\r
+      while (this_node != NodeTraits::get_next(p))\r
+         p = NodeTraits::get_next(p);\r
+      return p;\r
+   }\r
+\r
+   //! <b>Requires</b>: this_node and prev_init_node must be in the same circular list.\r
+   //! \r
+   //! <b>Effects</b>: Returns the previous node of this_node in the circular list starting.\r
+   //!   the search from prev_init_node. The first node checked for equality\r
+   //!   is NodeTraits::get_next(prev_init_node).\r
+   //! \r
+   //! <b>Complexity</b>: Linear to the number of elements between prev_init_node and this_node.\r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static node_ptr get_previous_node(node_ptr prev_init_node, node_ptr this_node)\r
+   {\r
+      node_ptr p = prev_init_node;\r
+      while (this_node != NodeTraits::get_next(p))\r
+         p = NodeTraits::get_next(p);\r
+      return p;\r
+   }\r
+\r
+   //! <b>Effects</b>: Constructs an empty list, making this_node the only\r
+   //!   node of the circular list:\r
+   //!  <tt>NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node)\r
+   //!  == this_node</tt>.\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void init(node_ptr this_node)  \r
+   {  NodeTraits::set_next(this_node, this_node);  }  \r
+\r
+   //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.\r
+   //! \r
+   //! <b>Effects</b>: Returns true is "this_node" is the only node of a circular list:\r
+   //!  <tt>return NodeTraits::get_next(this_node) == this_node</tt>\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static bool unique(const_node_ptr this_node)  \r
+   {  return NodeTraits::get_next(this_node) == this_node; }\r
+\r
+   //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.\r
+   //! \r
+   //! <b>Effects</b>: Returns the number of nodes in a circular list. If the circular list\r
+   //!  is empty, returns 1.\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static std::size_t count(const_node_ptr this_node) \r
+   {\r
+      std::size_t result = 0;\r
+      const_node_ptr p = this_node;\r
+      do{\r
+         p = NodeTraits::get_next(p);\r
+         ++result;\r
+      } while (p != this_node);\r
+      return result;\r
+   }\r
+\r
+   //! <b>Requires</b>: prev_node must be in a circular list or be an empty circular list.\r
+   //! \r
+   //! <b>Effects</b>: Unlinks the next node of prev_node from the circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void unlink_after(node_ptr prev_node)\r
+   {\r
+      node_ptr this_node(NodeTraits::get_next(prev_node));\r
+      NodeTraits::set_next(prev_node, NodeTraits::get_next(this_node));\r
+      NodeTraits::set_next(this_node, this_node);\r
+   }\r
+\r
+   //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.\r
+   //! \r
+   //! <b>Effects</b>: Unlinks the node from the circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Linear to the number of elements in the circular list \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void unlink(node_ptr this_node)\r
+   {  unlink_after(get_previous_node(this_node)); }\r
+\r
+   //! <b>Requires</b>: prev_node must be a node of a circular list.\r
+   //! \r
+   //! <b>Effects</b>: Links this_node after prev_node in the circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void link_after(node_ptr prev_node, node_ptr this_node)\r
+   {\r
+      NodeTraits::set_next(this_node, NodeTraits::get_next(prev_node));\r
+      NodeTraits::set_next(prev_node, this_node);\r
+   }\r
+\r
+   //! <b>Requires</b>: nxt_node must be a node of a circular list.\r
+   //! \r
+   //! <b>Effects</b>: Links this_node before nxt_node in the circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Linear to the number of elements in the circular list. \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void link_before (node_ptr nxt_node, node_ptr this_node)\r
+   {  link_after(get_previous_node(nxt_node), this_node);   }\r
+\r
+   //! <b>Requires</b>: this_node and other_node must be nodes inserted\r
+   //!  in circular lists or be empty circular lists.\r
+   //! \r
+   //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in\r
+   //!   other_nodes position in the second circular list and the other_node is inserted\r
+   //!   in this_node's position in the first circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Linear to number of elements of both lists \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void swap_nodes(node_ptr this_node, node_ptr other_node)\r
+   {\r
+      if (other_node == this_node)\r
+         return;\r
+      bool empty1 = unique(this_node);\r
+      bool empty2 = unique(other_node);\r
+      node_ptr prev_this (get_previous_node(this_node));\r
+      node_ptr prev_other(get_previous_node(other_node));\r
+\r
+      node_ptr this_next (NodeTraits::get_next(this_node));\r
+      node_ptr other_next(NodeTraits::get_next(other_node));\r
+      NodeTraits::set_next(this_node, other_next);\r
+      NodeTraits::set_next(other_node, this_next);\r
+      NodeTraits::set_next(empty1 ? other_node : prev_this, other_node);\r
+      NodeTraits::set_next(empty2 ? this_node  : prev_other, this_node);\r
+   }\r
+\r
+   //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.\r
+   //!   and p must be a node of a different circular list.\r
+   //! \r
+   //! <b>Effects</b>: Removes the nodes from [b, e) range from their circular list and inserts\r
+   //!   them after p in p's circular list.\r
+   //! \r
+   //! <b>Complexity</b>: Constant \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   static void transfer_after(node_ptr p, node_ptr b, node_ptr e)\r
+   {\r
+      if (p != b && p != e) {\r
+         node_ptr next_b = NodeTraits::get_next(b);\r
+         node_ptr next_e = NodeTraits::get_next(e);\r
+         node_ptr next_p = NodeTraits::get_next(p);\r
+         NodeTraits::set_next(b, next_e);\r
+         NodeTraits::set_next(e, next_p);\r
+         NodeTraits::set_next(p, next_b);\r
+      }\r
+   }\r
+\r
+   //! <b>Effects</b>: Reverses the order of elements in the list. \r
+   //! \r
+   //! <b>Throws</b>: Nothing.\r
+   //! \r
+   //! <b>Complexity</b>: This function is linear to the contained elements.\r
+   static void reverse(node_ptr p)\r
+   {\r
+      node_ptr i = NodeTraits::get_next(p), e(p); \r
+      for (;;) {\r
+         node_ptr nxt(NodeTraits::get_next(i));\r
+         if (nxt == e)\r
+            break;\r
+         transfer_after(e, i, nxt);\r
+      }\r
+   }\r
+};\r
+\r
+} //namespace intrusive \r
+} //namespace boost \r
+\r
+#include "detail/config_end.hpp"\r
+\r
+#endif //BOOST_INTRUSIVE_SLIST_ALGORITHMS_HPP\r