]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/binomial_heap_base_/split_join_fn_imps.hpp
re PR libstdc++/37144 (A bug in include/ext/pb_ds/detail/pat_trie_/constructors_destr...
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / binomial_heap_base_ / split_join_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
f5886803 3// Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
4569a895
AT
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the terms
7// of the GNU General Public License as published by the Free Software
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
9// version.
10
11// This library is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
4569a895 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
4569a895
AT
24
25// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
27// Permission to use, copy, modify, sell, and distribute this software
28// is hereby granted without fee, provided that the above copyright
29// notice appears in all copies, and that both that copyright notice
30// and this permission notice appear in supporting documentation. None
31// of the above authors, nor IBM Haifa Research Laboratories, make any
32// representation about the suitability of this software for any
33// purpose. It is provided "as is" without express or implied
34// warranty.
35
36/**
a345e45d 37 * @file binomial_heap_base_/split_join_fn_imps.hpp
4569a895
AT
38 * Contains an implementation class for a base of binomial heaps.
39 */
40
41PB_DS_CLASS_T_DEC
42template<typename Pred>
43void
44PB_DS_CLASS_C_DEC::
45split(Pred pred, PB_DS_CLASS_C_DEC& other)
46{
cfca3f72
FD
47 PB_DS_ASSERT_VALID_COND((*this),true)
48 PB_DS_ASSERT_VALID_COND(other,true)
4569a895 49
f5886803 50 other.clear();
4569a895
AT
51 if (base_type::empty())
52 {
cfca3f72
FD
53 PB_DS_ASSERT_VALID_COND((*this),true)
54 PB_DS_ASSERT_VALID_COND(other,true)
f5886803 55 return;
4569a895
AT
56 }
57
58 base_type::to_linked_list();
4569a895 59 node_pointer p_out = base_type::prune(pred);
8fc81078 60 while (p_out != 0)
4569a895 61 {
47bea7b8 62 _GLIBCXX_DEBUG_ASSERT(base_type::m_size > 0);
4569a895 63 --base_type::m_size;
4569a895
AT
64 ++other.m_size;
65
66 node_pointer p_next = p_out->m_p_next_sibling;
8fc81078 67 p_out->m_p_l_child = p_out->m_p_prev_or_parent = 0;
4569a895
AT
68 p_out->m_metadata = 0;
69
70 p_out->m_p_next_sibling = other.m_p_root;
8fc81078 71 if (other.m_p_root != 0)
4569a895
AT
72 other.m_p_root->m_p_prev_or_parent = p_out;
73
74 other.m_p_root = p_out;
4569a895 75 other.m_p_root = other.fix(other.m_p_root);
4569a895
AT
76 p_out = p_next;
77 }
78
cfca3f72 79 PB_DS_ASSERT_VALID_COND(other,true)
f5886803 80 node_pointer p_cur = base_type::m_p_root;
8fc81078 81 base_type::m_p_root = 0;
4569a895 82
8fc81078 83 while (p_cur != 0)
4569a895
AT
84 {
85 node_pointer p_next = p_cur->m_p_next_sibling;
8fc81078 86 p_cur->m_p_l_child = p_cur->m_p_prev_or_parent = 0;
4569a895 87 p_cur->m_metadata = 0;
4569a895
AT
88 p_cur->m_p_next_sibling = base_type::m_p_root;
89
8fc81078 90 if (base_type::m_p_root != 0)
4569a895
AT
91 base_type::m_p_root->m_p_prev_or_parent = p_cur;
92
93 base_type::m_p_root = p_cur;
4569a895 94 base_type::m_p_root = fix(base_type::m_p_root);
4569a895
AT
95 p_cur = p_next;
96 }
97
8fc81078 98 m_p_max = 0;
cfca3f72
FD
99 PB_DS_ASSERT_VALID_COND((*this),true)
100 PB_DS_ASSERT_VALID_COND(other,true)
f5886803 101}
4569a895
AT
102
103PB_DS_CLASS_T_DEC
104inline void
105PB_DS_CLASS_C_DEC::
106join(PB_DS_CLASS_C_DEC& other)
107{
cfca3f72
FD
108 PB_DS_ASSERT_VALID_COND((*this),true)
109 PB_DS_ASSERT_VALID_COND(other,true)
4569a895 110
f5886803 111 node_pointer p_other = other.m_p_root;
8fc81078 112 if (p_other != 0)
4569a895
AT
113 do
114 {
115 node_pointer p_next = p_other->m_p_next_sibling;
4569a895 116 std::swap(p_other->m_p_next_sibling, p_other->m_p_prev_or_parent);
4569a895
AT
117 p_other = p_next;
118 }
8fc81078 119 while (p_other != 0);
4569a895
AT
120
121 base_type::m_p_root = join(base_type::m_p_root, other.m_p_root);
122 base_type::m_size += other.m_size;
8fc81078 123 m_p_max = 0;
4569a895 124
8fc81078 125 other.m_p_root = 0;
4569a895 126 other.m_size = 0;
8fc81078 127 other.m_p_max = 0;
4569a895 128
cfca3f72
FD
129 PB_DS_ASSERT_VALID_COND((*this),true)
130 PB_DS_ASSERT_VALID_COND(other,true)
f5886803 131}
4569a895
AT
132
133PB_DS_CLASS_T_DEC
134inline typename PB_DS_CLASS_C_DEC::node_pointer
135PB_DS_CLASS_C_DEC::
136join(node_pointer p_lhs, node_pointer p_rhs) const
137{
8fc81078 138 node_pointer p_ret = 0;
8fc81078 139 node_pointer p_cur = 0;
4569a895 140
8fc81078 141 while (p_lhs != 0 || p_rhs != 0)
4569a895 142 {
8fc81078 143 if (p_rhs == 0)
a345e45d 144 {
8fc81078 145 if (p_cur == 0)
4569a895
AT
146 p_ret = p_cur = p_lhs;
147 else
a345e45d 148 {
4569a895 149 p_cur->m_p_next_sibling = p_lhs;
4569a895 150 p_lhs->m_p_prev_or_parent = p_cur;
a345e45d 151 }
8fc81078 152 p_cur = p_lhs = 0;
a345e45d 153 }
8fc81078 154 else if (p_lhs == 0 || p_rhs->m_metadata < p_lhs->m_metadata)
a345e45d 155 {
8fc81078 156 if (p_cur == 0)
a345e45d 157 {
4569a895 158 p_ret = p_cur = p_rhs;
4569a895 159 p_rhs = p_rhs->m_p_prev_or_parent;
a345e45d 160 }
4569a895 161 else
a345e45d 162 {
4569a895 163 p_cur->m_p_next_sibling = p_rhs;
4569a895 164 p_rhs = p_rhs->m_p_prev_or_parent;
4569a895 165 p_cur->m_p_next_sibling->m_p_prev_or_parent = p_cur;
4569a895 166 p_cur = p_cur->m_p_next_sibling;
a345e45d
BK
167 }
168 }
4569a895 169 else if (p_lhs->m_metadata < p_rhs->m_metadata)
a345e45d 170 {
8fc81078 171 if (p_cur == 0)
4569a895
AT
172 p_ret = p_cur = p_lhs;
173 else
a345e45d 174 {
4569a895 175 p_cur->m_p_next_sibling = p_lhs;
4569a895 176 p_lhs->m_p_prev_or_parent = p_cur;
4569a895 177 p_cur = p_cur->m_p_next_sibling;
a345e45d 178 }
4569a895 179 p_lhs = p_cur->m_p_next_sibling;
a345e45d 180 }
4569a895 181 else
a345e45d 182 {
4569a895 183 node_pointer p_next_rhs = p_rhs->m_p_prev_or_parent;
4569a895 184 p_rhs->m_p_next_sibling = p_lhs;
4569a895 185 p_lhs = fix(p_rhs);
4569a895 186 p_rhs = p_next_rhs;
a345e45d 187 }
4569a895
AT
188 }
189
8fc81078
PC
190 if (p_cur != 0)
191 p_cur->m_p_next_sibling = 0;
4569a895 192
8fc81078
PC
193 if (p_ret != 0)
194 p_ret->m_p_prev_or_parent = 0;
4569a895
AT
195
196 return p_ret;
197}