Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-03-19 16:51:04


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk