libstdc++
|
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 binomial_heap_base_/split_join_fn_imps.hpp 00038 * Contains an implementation class for a base of binomial heaps. 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_COND((*this),true) 00048 PB_DS_ASSERT_VALID_COND(other,true) 00049 00050 other.clear(); 00051 if (base_type::empty()) 00052 { 00053 PB_DS_ASSERT_VALID_COND((*this),true) 00054 PB_DS_ASSERT_VALID_COND(other,true) 00055 return; 00056 } 00057 00058 base_type::to_linked_list(); 00059 node_pointer p_out = base_type::prune(pred); 00060 while (p_out != 0) 00061 { 00062 _GLIBCXX_DEBUG_ASSERT(base_type::m_size > 0); 00063 --base_type::m_size; 00064 ++other.m_size; 00065 00066 node_pointer p_next = p_out->m_p_next_sibling; 00067 p_out->m_p_l_child = p_out->m_p_prev_or_parent = 0; 00068 p_out->m_metadata = 0; 00069 00070 p_out->m_p_next_sibling = other.m_p_root; 00071 if (other.m_p_root != 0) 00072 other.m_p_root->m_p_prev_or_parent = p_out; 00073 00074 other.m_p_root = p_out; 00075 other.m_p_root = other.fix(other.m_p_root); 00076 p_out = p_next; 00077 } 00078 00079 PB_DS_ASSERT_VALID_COND(other,true) 00080 node_pointer p_cur = base_type::m_p_root; 00081 base_type::m_p_root = 0; 00082 00083 while (p_cur != 0) 00084 { 00085 node_pointer p_next = p_cur->m_p_next_sibling; 00086 p_cur->m_p_l_child = p_cur->m_p_prev_or_parent = 0; 00087 p_cur->m_metadata = 0; 00088 p_cur->m_p_next_sibling = base_type::m_p_root; 00089 00090 if (base_type::m_p_root != 0) 00091 base_type::m_p_root->m_p_prev_or_parent = p_cur; 00092 00093 base_type::m_p_root = p_cur; 00094 base_type::m_p_root = fix(base_type::m_p_root); 00095 p_cur = p_next; 00096 } 00097 00098 m_p_max = 0; 00099 PB_DS_ASSERT_VALID_COND((*this),true) 00100 PB_DS_ASSERT_VALID_COND(other,true) 00101 } 00102 00103 PB_DS_CLASS_T_DEC 00104 inline void 00105 PB_DS_CLASS_C_DEC:: 00106 join(PB_DS_CLASS_C_DEC& other) 00107 { 00108 PB_DS_ASSERT_VALID_COND((*this),true) 00109 PB_DS_ASSERT_VALID_COND(other,true) 00110 00111 node_pointer p_other = other.m_p_root; 00112 if (p_other != 0) 00113 do 00114 { 00115 node_pointer p_next = p_other->m_p_next_sibling; 00116 std::swap(p_other->m_p_next_sibling, p_other->m_p_prev_or_parent); 00117 p_other = p_next; 00118 } 00119 while (p_other != 0); 00120 00121 base_type::m_p_root = join(base_type::m_p_root, other.m_p_root); 00122 base_type::m_size += other.m_size; 00123 m_p_max = 0; 00124 00125 other.m_p_root = 0; 00126 other.m_size = 0; 00127 other.m_p_max = 0; 00128 00129 PB_DS_ASSERT_VALID_COND((*this),true) 00130 PB_DS_ASSERT_VALID_COND(other,true) 00131 } 00132 00133 PB_DS_CLASS_T_DEC 00134 inline typename PB_DS_CLASS_C_DEC::node_pointer 00135 PB_DS_CLASS_C_DEC:: 00136 join(node_pointer p_lhs, node_pointer p_rhs) const 00137 { 00138 node_pointer p_ret = 0; 00139 node_pointer p_cur = 0; 00140 00141 while (p_lhs != 0 || p_rhs != 0) 00142 { 00143 if (p_rhs == 0) 00144 { 00145 if (p_cur == 0) 00146 p_ret = p_cur = p_lhs; 00147 else 00148 { 00149 p_cur->m_p_next_sibling = p_lhs; 00150 p_lhs->m_p_prev_or_parent = p_cur; 00151 } 00152 p_cur = p_lhs = 0; 00153 } 00154 else if (p_lhs == 0 || p_rhs->m_metadata < p_lhs->m_metadata) 00155 { 00156 if (p_cur == 0) 00157 { 00158 p_ret = p_cur = p_rhs; 00159 p_rhs = p_rhs->m_p_prev_or_parent; 00160 } 00161 else 00162 { 00163 p_cur->m_p_next_sibling = p_rhs; 00164 p_rhs = p_rhs->m_p_prev_or_parent; 00165 p_cur->m_p_next_sibling->m_p_prev_or_parent = p_cur; 00166 p_cur = p_cur->m_p_next_sibling; 00167 } 00168 } 00169 else if (p_lhs->m_metadata < p_rhs->m_metadata) 00170 { 00171 if (p_cur == 0) 00172 p_ret = p_cur = p_lhs; 00173 else 00174 { 00175 p_cur->m_p_next_sibling = p_lhs; 00176 p_lhs->m_p_prev_or_parent = p_cur; 00177 p_cur = p_cur->m_p_next_sibling; 00178 } 00179 p_lhs = p_cur->m_p_next_sibling; 00180 } 00181 else 00182 { 00183 node_pointer p_next_rhs = p_rhs->m_p_prev_or_parent; 00184 p_rhs->m_p_next_sibling = p_lhs; 00185 p_lhs = fix(p_rhs); 00186 p_rhs = p_next_rhs; 00187 } 00188 } 00189 00190 if (p_cur != 0) 00191 p_cur->m_p_next_sibling = 0; 00192 00193 if (p_ret != 0) 00194 p_ret->m_p_prev_or_parent = 0; 00195 00196 return p_ret; 00197 }