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