libstdc++
thin_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 thin_heap_/split_join_fn_imps.hpp
00038  * Contains an implementation for thin_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   if (base_type::empty())
00052     {
00053       PB_DS_ASSERT_VALID((*this))
00054       PB_DS_ASSERT_VALID(other)
00055       return;
00056     }
00057 
00058   base_type::to_linked_list();
00059   node_pointer p_out = base_type::prune(pred);
00060 
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       other.make_root_and_link(p_out);
00068       p_out = p_next;
00069     }
00070 
00071   PB_DS_ASSERT_VALID(other)
00072   node_pointer p_cur = base_type::m_p_root;
00073   m_p_max = 0;
00074   base_type::m_p_root = 0;
00075   while (p_cur != 0)
00076     {
00077       node_pointer p_next = p_cur->m_p_next_sibling;
00078       make_root_and_link(p_cur);
00079       p_cur = p_next;
00080     }
00081 
00082   PB_DS_ASSERT_VALID((*this))
00083   PB_DS_ASSERT_VALID(other)
00084 }
00085 
00086 PB_DS_CLASS_T_DEC
00087 inline void
00088 PB_DS_CLASS_C_DEC::
00089 join(PB_DS_CLASS_C_DEC& other)
00090 {
00091   PB_DS_ASSERT_VALID((*this))
00092   PB_DS_ASSERT_VALID(other)
00093 
00094   node_pointer p_other = other.m_p_root;
00095   while (p_other != 0)
00096     {
00097       node_pointer p_next = p_other->m_p_next_sibling;
00098       make_root_and_link(p_other);
00099       p_other = p_next;
00100     }
00101   base_type::m_size += other.m_size;
00102   other.m_p_root = 0;
00103   other.m_size = 0;
00104   other.m_p_max = 0;
00105 
00106   PB_DS_ASSERT_VALID((*this))
00107   PB_DS_ASSERT_VALID(other)
00108 }