]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/pb_ds/detail/binary_heap_/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 / binary_heap_ / split_join_fn_imps.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
10 // version.
11
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
35 // warranty.
36
37 /**
38 * @file binary_heap_/split_join_fn_imps.hpp
39 * Contains an implementation class for a binary_heap.
40 */
41
42 PB_DS_CLASS_T_DEC
43 template<typename Pred>
44 void
45 PB_DS_CLASS_C_DEC::
46 split(Pred pred, PB_DS_CLASS_C_DEC& other)
47 {
48 PB_DS_ASSERT_VALID((*this))
49
50 typedef
51 typename entry_pred<value_type, Pred, _Alloc, simple_value>::type
52 pred_t;
53
54 const size_type left = partition(pred_t(pred));
55 _GLIBCXX_DEBUG_ASSERT(m_size >= left);
56
57 const size_type ersd = m_size - left;
58 _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
59
60 const size_type new_size = resize_policy::get_new_size_for_arbitrary(left);
61 const size_type other_actual_size = other.get_new_size_for_arbitrary(ersd);
62
63 entry_pointer a_entries = 0;
64 entry_pointer a_other_entries = 0;
65
66 __try
67 {
68 a_entries = s_entry_allocator.allocate(new_size);
69 a_other_entries = s_entry_allocator.allocate(other_actual_size);
70 }
71 __catch(...)
72 {
73 if (a_entries != 0)
74 s_entry_allocator.deallocate(a_entries, new_size);
75
76 if (a_other_entries != 0)
77 s_entry_allocator.deallocate(a_other_entries, other_actual_size);
78
79 __throw_exception_again;
80 };
81
82 for (size_type i = 0; i < other.m_size; ++i)
83 erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
84
85 _GLIBCXX_DEBUG_ASSERT(new_size >= left);
86 std::copy(m_a_entries, m_a_entries + left, a_entries);
87 std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries);
88
89 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
90 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
91
92 m_actual_size = new_size;
93 other.m_actual_size = other_actual_size;
94
95 m_size = left;
96 other.m_size = ersd;
97
98 m_a_entries = a_entries;
99 other.m_a_entries = a_other_entries;
100
101 make_heap();
102 other.make_heap();
103
104 resize_policy::notify_arbitrary(m_actual_size);
105 other.notify_arbitrary(other.m_actual_size);
106
107 PB_DS_ASSERT_VALID((*this))
108 PB_DS_ASSERT_VALID(other)
109 }
110
111 PB_DS_CLASS_T_DEC
112 inline void
113 PB_DS_CLASS_C_DEC::
114 join(PB_DS_CLASS_C_DEC& other)
115 {
116 PB_DS_ASSERT_VALID((*this))
117 PB_DS_ASSERT_VALID(other)
118
119 const size_type len = m_size + other.m_size;
120 const size_type new_size = resize_policy::get_new_size_for_arbitrary(len);
121
122 entry_pointer a_entries = 0;
123 entry_pointer a_other_entries = 0;
124
125 __try
126 {
127 a_entries = s_entry_allocator.allocate(new_size);
128 a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
129 }
130 __catch(...)
131 {
132 if (a_entries != 0)
133 s_entry_allocator.deallocate(a_entries, new_size);
134
135 if (a_other_entries != 0)
136 s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
137
138 __throw_exception_again;
139 }
140
141 std::copy(m_a_entries, m_a_entries + m_size, a_entries);
142 std::copy(other.m_a_entries, other.m_a_entries + other.m_size,
143 a_entries + m_size);
144
145 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
146 m_a_entries = a_entries;
147 m_size = len;
148 m_actual_size = new_size;
149 resize_policy::notify_arbitrary(new_size);
150 make_heap();
151
152 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
153 other.m_a_entries = a_other_entries;
154 other.m_size = 0;
155 other.m_actual_size = resize_policy::min_size;
156 other.notify_arbitrary(resize_policy::min_size);
157 other.make_heap();
158
159 PB_DS_ASSERT_VALID((*this))
160 PB_DS_ASSERT_VALID(other)
161 }