1247738Sbapt// std::list utilities implementation -*- C++ -*- 2247738Sbapt 3247738Sbapt// Copyright (C) 2003, 2005 Free Software Foundation, Inc. 4247738Sbapt// 5247738Sbapt// This file is part of the GNU ISO C++ Library. This library is free 6247738Sbapt// software; you can redistribute it and/or modify it under the 7247738Sbapt// terms of the GNU General Public License as published by the 8247738Sbapt// Free Software Foundation; either version 2, or (at your option) 9247738Sbapt// any later version. 10247738Sbapt 11247738Sbapt// This library is distributed in the hope that it will be useful, 12247738Sbapt// but WITHOUT ANY WARRANTY; without even the implied warranty of 13247738Sbapt// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14247738Sbapt// GNU General Public License for more details. 15247738Sbapt 16247738Sbapt// You should have received a copy of the GNU General Public License along 17247738Sbapt// with this library; see the file COPYING. If not, write to the Free 18247738Sbapt// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19247738Sbapt// USA. 20247738Sbapt 21247738Sbapt// As a special exception, you may use this file as part of a free software 22247738Sbapt// library without restriction. Specifically, if other files instantiate 23247738Sbapt// templates or use macros or inline functions from this file, or you compile 24247738Sbapt// this file and link it with other files to produce an executable, this 25247738Sbapt// file does not by itself cause the resulting executable to be covered by 26247738Sbapt// the GNU General Public License. This exception does not however 27247738Sbapt// invalidate any other reasons why the executable file might be covered by 28247738Sbapt// the GNU General Public License. 29247738Sbapt 30247738Sbapt/* 31247738Sbapt * 32247738Sbapt * Copyright (c) 1994 33247738Sbapt * Hewlett-Packard Company 34247738Sbapt * 35247738Sbapt * Permission to use, copy, modify, distribute and sell this software 36247738Sbapt * and its documentation for any purpose is hereby granted without fee, 37247738Sbapt * provided that the above copyright notice appear in all copies and 38247738Sbapt * that both that copyright notice and this permission notice appear 39247738Sbapt * in supporting documentation. Hewlett-Packard Company makes no 40247738Sbapt * representations about the suitability of this software for any 41247738Sbapt * purpose. It is provided "as is" without express or implied warranty. 42247738Sbapt * 43247738Sbapt * 44247738Sbapt * Copyright (c) 1996,1997 45247738Sbapt * Silicon Graphics Computer Systems, Inc. 46247738Sbapt * 47247738Sbapt * Permission to use, copy, modify, distribute and sell this software 48247738Sbapt * and its documentation for any purpose is hereby granted without fee, 49247738Sbapt * provided that the above copyright notice appear in all copies and 50247738Sbapt * that both that copyright notice and this permission notice appear 51247738Sbapt * in supporting documentation. Silicon Graphics makes no 52247738Sbapt * representations about the suitability of this software for any 53247738Sbapt * purpose. It is provided "as is" without express or implied warranty. 54247738Sbapt */ 55247738Sbapt 56247738Sbapt#include <list> 57247738Sbapt 58247738Sbapt_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD) 59247738Sbapt 60247738Sbapt void 61247738Sbapt _List_node_base::swap(_List_node_base& __x, _List_node_base& __y) 62247738Sbapt { 63247738Sbapt if ( __x._M_next != &__x ) 64247738Sbapt { 65247738Sbapt if ( __y._M_next != &__y ) 66247738Sbapt { 67247738Sbapt // Both __x and __y are not empty. 68247738Sbapt std::swap(__x._M_next,__y._M_next); 69247738Sbapt std::swap(__x._M_prev,__y._M_prev); 70247738Sbapt __x._M_next->_M_prev = __x._M_prev->_M_next = &__x; 71247738Sbapt __y._M_next->_M_prev = __y._M_prev->_M_next = &__y; 72 } 73 else 74 { 75 // __x is not empty, __y is empty. 76 __y._M_next = __x._M_next; 77 __y._M_prev = __x._M_prev; 78 __y._M_next->_M_prev = __y._M_prev->_M_next = &__y; 79 __x._M_next = __x._M_prev = &__x; 80 } 81 } 82 else if ( __y._M_next != &__y ) 83 { 84 // __x is empty, __y is not empty. 85 __x._M_next = __y._M_next; 86 __x._M_prev = __y._M_prev; 87 __x._M_next->_M_prev = __x._M_prev->_M_next = &__x; 88 __y._M_next = __y._M_prev = &__y; 89 } 90 } 91 92 void 93 _List_node_base::transfer(_List_node_base * const __first, 94 _List_node_base * const __last) 95 { 96 if (this != __last) 97 { 98 // Remove [first, last) from its old position. 99 __last->_M_prev->_M_next = this; 100 __first->_M_prev->_M_next = __last; 101 this->_M_prev->_M_next = __first; 102 103 // Splice [first, last) into its new position. 104 _List_node_base* const __tmp = this->_M_prev; 105 this->_M_prev = __last->_M_prev; 106 __last->_M_prev = __first->_M_prev; 107 __first->_M_prev = __tmp; 108 } 109 } 110 111 void 112 _List_node_base::reverse() 113 { 114 _List_node_base* __tmp = this; 115 do 116 { 117 std::swap(__tmp->_M_next, __tmp->_M_prev); 118 __tmp = __tmp->_M_prev; // Old next node is now prev. 119 } 120 while (__tmp != this); 121 } 122 123 void 124 _List_node_base::hook(_List_node_base* const __position) 125 { 126 this->_M_next = __position; 127 this->_M_prev = __position->_M_prev; 128 __position->_M_prev->_M_next = this; 129 __position->_M_prev = this; 130 } 131 132 void 133 _List_node_base::unhook() 134 { 135 _List_node_base* const __next_node = this->_M_next; 136 _List_node_base* const __prev_node = this->_M_prev; 137 __prev_node->_M_next = __next_node; 138 __next_node->_M_prev = __prev_node; 139 } 140 141_GLIBCXX_END_NESTED_NAMESPACE 142