3 // Copyright (C) 2005-2018 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
37 * @file binary_heap_/split_join_fn_imps.hpp
38 * Contains an implementation class for a binary_heap.
42 template<typename Pred>
45 split(Pred pred, PB_DS_CLASS_C_DEC& other)
47 PB_DS_ASSERT_VALID((*this))
50 typename entry_pred<value_type, Pred, _Alloc, simple_value>::type
53 const size_type left = partition(pred_t(pred));
54 _GLIBCXX_DEBUG_ASSERT(m_size >= left);
56 const size_type ersd = m_size - left;
57 _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
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);
62 entry_pointer a_entries = 0;
63 entry_pointer a_other_entries = 0;
67 a_entries = s_entry_allocator.allocate(new_size);
68 a_other_entries = s_entry_allocator.allocate(other_actual_size);
73 s_entry_allocator.deallocate(a_entries, new_size);
75 if (a_other_entries != 0)
76 s_entry_allocator.deallocate(a_other_entries, other_actual_size);
78 __throw_exception_again;
81 for (size_type i = 0; i < other.m_size; ++i)
82 erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
84 _GLIBCXX_DEBUG_ASSERT(new_size >= left);
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);
88 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
89 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
91 m_actual_size = new_size;
92 other.m_actual_size = other_actual_size;
97 m_a_entries = a_entries;
98 other.m_a_entries = a_other_entries;
103 resize_policy::notify_arbitrary(m_actual_size);
104 other.notify_arbitrary(other.m_actual_size);
106 PB_DS_ASSERT_VALID((*this))
107 PB_DS_ASSERT_VALID(other)
113 join(PB_DS_CLASS_C_DEC& other)
115 PB_DS_ASSERT_VALID((*this))
116 PB_DS_ASSERT_VALID(other)
118 const size_type len = m_size + other.m_size;
119 const size_type new_size = resize_policy::get_new_size_for_arbitrary(len);
121 entry_pointer a_entries = 0;
122 entry_pointer a_other_entries = 0;
126 a_entries = s_entry_allocator.allocate(new_size);
127 a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
132 s_entry_allocator.deallocate(a_entries, new_size);
134 if (a_other_entries != 0)
135 s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
137 __throw_exception_again;
140 std::copy(m_a_entries, m_a_entries + m_size, a_entries);
141 std::copy(other.m_a_entries, other.m_a_entries + other.m_size,
144 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
145 m_a_entries = a_entries;
147 m_actual_size = new_size;
148 resize_policy::notify_arbitrary(new_size);
151 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
152 other.m_a_entries = a_other_entries;
154 other.m_actual_size = resize_policy::min_size;
155 other.notify_arbitrary(resize_policy::min_size);
158 PB_DS_ASSERT_VALID((*this))
159 PB_DS_ASSERT_VALID(other)