libstdc++
pairing_heap_/split_join_fn_imps.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file pairing_heap_/split_join_fn_imps.hpp
00038  * Contains an implementation class for a pairing heap.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 template<typename Pred>
00043 void
00044 PB_DS_CLASS_C_DEC::
00045 split(Pred pred, PB_DS_CLASS_C_DEC& other)
00046 {
00047   PB_DS_ASSERT_VALID((*this))
00048   PB_DS_ASSERT_VALID(other)
00049 
00050   other.clear();
00051 
00052   if (base_type::empty())
00053     {
00054       PB_DS_ASSERT_VALID((*this))
00055       PB_DS_ASSERT_VALID(other)
00056       return;
00057     }
00058 
00059   base_type::to_linked_list();
00060   node_pointer p_out = base_type::prune(pred);
00061   while (p_out != 0)
00062     {
00063       _GLIBCXX_DEBUG_ASSERT(base_type::m_size > 0);
00064       --base_type::m_size;
00065       ++other.m_size;
00066       node_pointer p_next = p_out->m_p_next_sibling;
00067       p_out->m_p_l_child = p_out->m_p_next_sibling = p_out->m_p_prev_or_parent = 0;
00068 
00069       other.push_imp(p_out);
00070       p_out = p_next;
00071     }
00072 
00073   PB_DS_ASSERT_VALID(other)
00074   node_pointer p_cur = base_type::m_p_root;
00075   base_type::m_p_root = 0;
00076   while (p_cur != 0)
00077     {
00078       node_pointer p_next = p_cur->m_p_next_sibling;
00079       p_cur->m_p_l_child = p_cur->m_p_next_sibling = p_cur->m_p_prev_or_parent = 0;
00080 
00081       push_imp(p_cur);
00082       p_cur = p_next;
00083     }
00084 
00085   PB_DS_ASSERT_VALID((*this))
00086   PB_DS_ASSERT_VALID(other)
00087 }
00088 
00089 PB_DS_CLASS_T_DEC
00090 inline void
00091 PB_DS_CLASS_C_DEC::
00092 join(PB_DS_CLASS_C_DEC& other)
00093 {
00094   PB_DS_ASSERT_VALID((*this))
00095   PB_DS_ASSERT_VALID(other)
00096 
00097   if (other.m_p_root == 0)
00098     {
00099       PB_DS_ASSERT_VALID((*this))
00100       PB_DS_ASSERT_VALID(other)
00101       return;
00102     }
00103 
00104   if (base_type::m_p_root == 0)
00105     base_type::m_p_root = other.m_p_root;
00106   else if (Cmp_Fn::operator()(base_type::m_p_root->m_value, other.m_p_root->m_value))
00107     {
00108       base_type::make_child_of(base_type::m_p_root, other.m_p_root);
00109       PB_DS_ASSERT_NODE_CONSISTENT(other.m_p_root, false)
00110       base_type::m_p_root = other.m_p_root;
00111     }
00112   else
00113     {
00114       base_type::make_child_of(other.m_p_root, base_type::m_p_root);
00115       PB_DS_ASSERT_NODE_CONSISTENT(base_type::m_p_root, false)
00116     }
00117 
00118   base_type::m_size += other.m_size;
00119   other.m_p_root = 0;
00120   other.m_size = 0;
00121   PB_DS_ASSERT_VALID((*this))
00122   PB_DS_ASSERT_VALID(other)
00123 }