]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2005-2020 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 | ||
574dfb67 JW |
41 | #ifdef PB_DS_CLASS_C_DEC |
42 | ||
4569a895 AT |
43 | PB_DS_CLASS_T_DEC |
44 | template<typename Pred> | |
45 | void | |
46 | PB_DS_CLASS_C_DEC:: | |
47 | split(Pred pred, PB_DS_CLASS_C_DEC& other) | |
48 | { | |
cfca3f72 FD |
49 | PB_DS_ASSERT_VALID_COND((*this),true) |
50 | PB_DS_ASSERT_VALID_COND(other,true) | |
4569a895 | 51 | |
f5886803 | 52 | other.clear(); |
4569a895 AT |
53 | if (base_type::empty()) |
54 | { | |
cfca3f72 FD |
55 | PB_DS_ASSERT_VALID_COND((*this),true) |
56 | PB_DS_ASSERT_VALID_COND(other,true) | |
f5886803 | 57 | return; |
4569a895 AT |
58 | } |
59 | ||
60 | base_type::to_linked_list(); | |
4569a895 | 61 | node_pointer p_out = base_type::prune(pred); |
8fc81078 | 62 | while (p_out != 0) |
4569a895 | 63 | { |
47bea7b8 | 64 | _GLIBCXX_DEBUG_ASSERT(base_type::m_size > 0); |
4569a895 | 65 | --base_type::m_size; |
4569a895 AT |
66 | ++other.m_size; |
67 | ||
68 | node_pointer p_next = p_out->m_p_next_sibling; | |
8fc81078 | 69 | p_out->m_p_l_child = p_out->m_p_prev_or_parent = 0; |
4569a895 AT |
70 | p_out->m_metadata = 0; |
71 | ||
72 | p_out->m_p_next_sibling = other.m_p_root; | |
8fc81078 | 73 | if (other.m_p_root != 0) |
4569a895 AT |
74 | other.m_p_root->m_p_prev_or_parent = p_out; |
75 | ||
76 | other.m_p_root = p_out; | |
4569a895 | 77 | other.m_p_root = other.fix(other.m_p_root); |
4569a895 AT |
78 | p_out = p_next; |
79 | } | |
80 | ||
cfca3f72 | 81 | PB_DS_ASSERT_VALID_COND(other,true) |
f5886803 | 82 | node_pointer p_cur = base_type::m_p_root; |
8fc81078 | 83 | base_type::m_p_root = 0; |
4569a895 | 84 | |
8fc81078 | 85 | while (p_cur != 0) |
4569a895 AT |
86 | { |
87 | node_pointer p_next = p_cur->m_p_next_sibling; | |
8fc81078 | 88 | p_cur->m_p_l_child = p_cur->m_p_prev_or_parent = 0; |
4569a895 | 89 | p_cur->m_metadata = 0; |
4569a895 AT |
90 | p_cur->m_p_next_sibling = base_type::m_p_root; |
91 | ||
8fc81078 | 92 | if (base_type::m_p_root != 0) |
4569a895 AT |
93 | base_type::m_p_root->m_p_prev_or_parent = p_cur; |
94 | ||
95 | base_type::m_p_root = p_cur; | |
4569a895 | 96 | base_type::m_p_root = fix(base_type::m_p_root); |
4569a895 AT |
97 | p_cur = p_next; |
98 | } | |
99 | ||
8fc81078 | 100 | m_p_max = 0; |
cfca3f72 FD |
101 | PB_DS_ASSERT_VALID_COND((*this),true) |
102 | PB_DS_ASSERT_VALID_COND(other,true) | |
f5886803 | 103 | } |
4569a895 AT |
104 | |
105 | PB_DS_CLASS_T_DEC | |
106 | inline void | |
107 | PB_DS_CLASS_C_DEC:: | |
108 | join(PB_DS_CLASS_C_DEC& other) | |
109 | { | |
cfca3f72 FD |
110 | PB_DS_ASSERT_VALID_COND((*this),true) |
111 | PB_DS_ASSERT_VALID_COND(other,true) | |
4569a895 | 112 | |
f5886803 | 113 | node_pointer p_other = other.m_p_root; |
8fc81078 | 114 | if (p_other != 0) |
4569a895 AT |
115 | do |
116 | { | |
117 | node_pointer p_next = p_other->m_p_next_sibling; | |
4569a895 | 118 | std::swap(p_other->m_p_next_sibling, p_other->m_p_prev_or_parent); |
4569a895 AT |
119 | p_other = p_next; |
120 | } | |
8fc81078 | 121 | while (p_other != 0); |
4569a895 AT |
122 | |
123 | base_type::m_p_root = join(base_type::m_p_root, other.m_p_root); | |
124 | base_type::m_size += other.m_size; | |
8fc81078 | 125 | m_p_max = 0; |
4569a895 | 126 | |
8fc81078 | 127 | other.m_p_root = 0; |
4569a895 | 128 | other.m_size = 0; |
8fc81078 | 129 | other.m_p_max = 0; |
4569a895 | 130 | |
cfca3f72 FD |
131 | PB_DS_ASSERT_VALID_COND((*this),true) |
132 | PB_DS_ASSERT_VALID_COND(other,true) | |
f5886803 | 133 | } |
4569a895 AT |
134 | |
135 | PB_DS_CLASS_T_DEC | |
136 | inline typename PB_DS_CLASS_C_DEC::node_pointer | |
137 | PB_DS_CLASS_C_DEC:: | |
138 | join(node_pointer p_lhs, node_pointer p_rhs) const | |
139 | { | |
8fc81078 | 140 | node_pointer p_ret = 0; |
8fc81078 | 141 | node_pointer p_cur = 0; |
4569a895 | 142 | |
8fc81078 | 143 | while (p_lhs != 0 || p_rhs != 0) |
4569a895 | 144 | { |
8fc81078 | 145 | if (p_rhs == 0) |
a345e45d | 146 | { |
8fc81078 | 147 | if (p_cur == 0) |
4569a895 AT |
148 | p_ret = p_cur = p_lhs; |
149 | else | |
a345e45d | 150 | { |
4569a895 | 151 | p_cur->m_p_next_sibling = p_lhs; |
4569a895 | 152 | p_lhs->m_p_prev_or_parent = p_cur; |
a345e45d | 153 | } |
8fc81078 | 154 | p_cur = p_lhs = 0; |
a345e45d | 155 | } |
8fc81078 | 156 | else if (p_lhs == 0 || p_rhs->m_metadata < p_lhs->m_metadata) |
a345e45d | 157 | { |
8fc81078 | 158 | if (p_cur == 0) |
a345e45d | 159 | { |
4569a895 | 160 | p_ret = p_cur = p_rhs; |
4569a895 | 161 | p_rhs = p_rhs->m_p_prev_or_parent; |
a345e45d | 162 | } |
4569a895 | 163 | else |
a345e45d | 164 | { |
4569a895 | 165 | p_cur->m_p_next_sibling = p_rhs; |
4569a895 | 166 | p_rhs = p_rhs->m_p_prev_or_parent; |
4569a895 | 167 | p_cur->m_p_next_sibling->m_p_prev_or_parent = p_cur; |
4569a895 | 168 | p_cur = p_cur->m_p_next_sibling; |
a345e45d BK |
169 | } |
170 | } | |
4569a895 | 171 | else if (p_lhs->m_metadata < p_rhs->m_metadata) |
a345e45d | 172 | { |
8fc81078 | 173 | if (p_cur == 0) |
4569a895 AT |
174 | p_ret = p_cur = p_lhs; |
175 | else | |
a345e45d | 176 | { |
4569a895 | 177 | p_cur->m_p_next_sibling = p_lhs; |
4569a895 | 178 | p_lhs->m_p_prev_or_parent = p_cur; |
4569a895 | 179 | p_cur = p_cur->m_p_next_sibling; |
a345e45d | 180 | } |
4569a895 | 181 | p_lhs = p_cur->m_p_next_sibling; |
a345e45d | 182 | } |
4569a895 | 183 | else |
a345e45d | 184 | { |
4569a895 | 185 | node_pointer p_next_rhs = p_rhs->m_p_prev_or_parent; |
4569a895 | 186 | p_rhs->m_p_next_sibling = p_lhs; |
4569a895 | 187 | p_lhs = fix(p_rhs); |
4569a895 | 188 | p_rhs = p_next_rhs; |
a345e45d | 189 | } |
4569a895 AT |
190 | } |
191 | ||
8fc81078 PC |
192 | if (p_cur != 0) |
193 | p_cur->m_p_next_sibling = 0; | |
4569a895 | 194 | |
8fc81078 PC |
195 | if (p_ret != 0) |
196 | p_ret->m_p_prev_or_parent = 0; | |
4569a895 AT |
197 | |
198 | return p_ret; | |
199 | } | |
574dfb67 | 200 | #endif |