[intrusive] list_algorithms::swap_nodes

AMDG Surprisingly, this implementation of swap nodes for a doubly linked list works regardless of aliasing. private: static void swap_prev(node_ptr this_node, node_ptr other_node) { node_ptr temp(NodeTraits::get_previous(this_node)); NodeTraits::set_previous(this_node, NodeTraits::get_previous(other_node)); NodeTraits::set_previous(other_node, temp); } static void swap_next(node_ptr this_node, node_ptr other_node) { node_ptr temp(NodeTraits::get_next(this_node)); NodeTraits::set_next(this_node, NodeTraits::get_next(other_node)); NodeTraits::set_next(other_node, temp); } public: static void swap_nodes(node_ptr this_node, node_ptr other_node) { node_ptr next_this(NodeTraits::get_next(this_node)); node_ptr prev_this(NodeTraits::get_previous(this_node)); node_ptr next_other(NodeTraits::get_next(other_node)); node_ptr prev_other(NodeTraits::get_previous(other_node)); //these first two swaps must happen before the other two swap_prev(next_this, next_other); swap_next(prev_this, prev_other); swap_next(this_node, other_node); swap_prev(this_node, other_node); } In Christ, Steven Watanabe
participants (2)
-
Ion Gaztañaga
-
Steven Watanabe