static void swap_nodes(const node_ptr &this_node, const node_ptr &other_node) { if (other_node == this_node) return; bool this_inited = inited(this_node); bool other_inited = inited(other_node); if(this_inited){ init_header(this_node); } if(other_inited){ init_header(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); if(this_inited){ init(other_node); } if(other_inited){ init(this_node); } }
//! <b>Requires</b>: this_node and other_node must be nodes inserted //! in circular lists or be empty circular lists. //! //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in //! other_nodes position in the second circular list and the other_node is inserted //! in this_node's position in the first circular list. //! //! <b>Complexity</b>: Linear to number of elements of both lists //! //! <b>Throws</b>: Nothing. static void swap_nodes(node_ptr this_node, node_ptr other_node) { if (other_node == this_node) return; bool this_inited = base_t::inited(this_node); bool other_inited = base_t::inited(other_node); if(this_inited){ base_t::init_header(this_node); } if(other_inited){ base_t::init_header(other_node); } bool empty1 = base_t::unique(this_node); bool empty2 = base_t::unique(other_node); node_ptr prev_this (get_previous_node(this_node)); node_ptr prev_other(get_previous_node(other_node)); node_ptr this_next (NodeTraits::get_next(this_node)); node_ptr other_next(NodeTraits::get_next(other_node)); NodeTraits::set_next(this_node, other_next); NodeTraits::set_next(other_node, this_next); NodeTraits::set_next(empty1 ? other_node : prev_this, other_node); NodeTraits::set_next(empty2 ? this_node : prev_other, this_node); if(this_inited){ base_t::init(other_node); } if(other_inited){ base_t::init(this_node); } }