]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2005-2024 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 binary_heap_/split_join_fn_imps.hpp |
4569a895 AT |
38 | * Contains an implementation class for a binary_heap. |
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 | { | |
f5886803 | 49 | PB_DS_ASSERT_VALID((*this)) |
4569a895 | 50 | |
f5886803 | 51 | typedef |
a345e45d | 52 | typename entry_pred<value_type, Pred, _Alloc, simple_value>::type |
4569a895 AT |
53 | pred_t; |
54 | ||
55 | const size_type left = partition(pred_t(pred)); | |
47bea7b8 | 56 | _GLIBCXX_DEBUG_ASSERT(m_size >= left); |
4569a895 AT |
57 | |
58 | const size_type ersd = m_size - left; | |
47bea7b8 | 59 | _GLIBCXX_DEBUG_ASSERT(m_size >= ersd); |
4569a895 | 60 | |
a345e45d BK |
61 | const size_type new_size = resize_policy::get_new_size_for_arbitrary(left); |
62 | const size_type other_actual_size = other.get_new_size_for_arbitrary(ersd); | |
4569a895 | 63 | |
8fc81078 PC |
64 | entry_pointer a_entries = 0; |
65 | entry_pointer a_other_entries = 0; | |
4569a895 | 66 | |
bc2631e0 | 67 | __try |
4569a895 | 68 | { |
a345e45d | 69 | a_entries = s_entry_allocator.allocate(new_size); |
4569a895 AT |
70 | a_other_entries = s_entry_allocator.allocate(other_actual_size); |
71 | } | |
bc2631e0 | 72 | __catch(...) |
4569a895 | 73 | { |
8fc81078 | 74 | if (a_entries != 0) |
a345e45d | 75 | s_entry_allocator.deallocate(a_entries, new_size); |
4569a895 | 76 | |
8fc81078 | 77 | if (a_other_entries != 0) |
4569a895 AT |
78 | s_entry_allocator.deallocate(a_other_entries, other_actual_size); |
79 | ||
8fafc2d3 | 80 | __throw_exception_again; |
4569a895 AT |
81 | }; |
82 | ||
83 | for (size_type i = 0; i < other.m_size; ++i) | |
84 | erase_at(other.m_a_entries, i, s_no_throw_copies_ind); | |
85 | ||
a345e45d | 86 | _GLIBCXX_DEBUG_ASSERT(new_size >= left); |
4569a895 AT |
87 | std::copy(m_a_entries, m_a_entries + left, a_entries); |
88 | std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries); | |
89 | ||
90 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); | |
91 | s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size); | |
92 | ||
a345e45d | 93 | m_actual_size = new_size; |
4569a895 AT |
94 | other.m_actual_size = other_actual_size; |
95 | ||
96 | m_size = left; | |
97 | other.m_size = ersd; | |
98 | ||
99 | m_a_entries = a_entries; | |
100 | other.m_a_entries = a_other_entries; | |
101 | ||
a345e45d BK |
102 | make_heap(); |
103 | other.make_heap(); | |
4569a895 AT |
104 | |
105 | resize_policy::notify_arbitrary(m_actual_size); | |
106 | other.notify_arbitrary(other.m_actual_size); | |
107 | ||
f5886803 FD |
108 | PB_DS_ASSERT_VALID((*this)) |
109 | PB_DS_ASSERT_VALID(other) | |
110 | } | |
4569a895 AT |
111 | |
112 | PB_DS_CLASS_T_DEC | |
113 | inline void | |
114 | PB_DS_CLASS_C_DEC:: | |
115 | join(PB_DS_CLASS_C_DEC& other) | |
116 | { | |
f5886803 FD |
117 | PB_DS_ASSERT_VALID((*this)) |
118 | PB_DS_ASSERT_VALID(other) | |
4569a895 | 119 | |
1ab79481 | 120 | const size_type len = m_size + other.m_size; |
a345e45d | 121 | const size_type new_size = resize_policy::get_new_size_for_arbitrary(len); |
4569a895 | 122 | |
8fc81078 PC |
123 | entry_pointer a_entries = 0; |
124 | entry_pointer a_other_entries = 0; | |
4569a895 | 125 | |
bc2631e0 | 126 | __try |
4569a895 | 127 | { |
a345e45d | 128 | a_entries = s_entry_allocator.allocate(new_size); |
4569a895 AT |
129 | a_other_entries = s_entry_allocator.allocate(resize_policy::min_size); |
130 | } | |
bc2631e0 | 131 | __catch(...) |
4569a895 | 132 | { |
8fc81078 | 133 | if (a_entries != 0) |
a345e45d | 134 | s_entry_allocator.deallocate(a_entries, new_size); |
4569a895 | 135 | |
8fc81078 | 136 | if (a_other_entries != 0) |
4569a895 AT |
137 | s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size); |
138 | ||
8fafc2d3 | 139 | __throw_exception_again; |
4569a895 AT |
140 | } |
141 | ||
142 | std::copy(m_a_entries, m_a_entries + m_size, a_entries); | |
a345e45d BK |
143 | std::copy(other.m_a_entries, other.m_a_entries + other.m_size, |
144 | a_entries + m_size); | |
4569a895 AT |
145 | |
146 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); | |
147 | m_a_entries = a_entries; | |
1ab79481 | 148 | m_size = len; |
a345e45d BK |
149 | m_actual_size = new_size; |
150 | resize_policy::notify_arbitrary(new_size); | |
151 | make_heap(); | |
4569a895 AT |
152 | |
153 | s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size); | |
154 | other.m_a_entries = a_other_entries; | |
155 | other.m_size = 0; | |
156 | other.m_actual_size = resize_policy::min_size; | |
4569a895 | 157 | other.notify_arbitrary(resize_policy::min_size); |
a345e45d BK |
158 | other.make_heap(); |
159 | ||
f5886803 FD |
160 | PB_DS_ASSERT_VALID((*this)) |
161 | PB_DS_ASSERT_VALID(other) | |
8fafc2d3 | 162 | } |
574dfb67 | 163 | #endif |