]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/binary_heap_/split_join_fn_imps.hpp
re PR fortran/18918 (Eventually support Fortran 2008's coarrays [co-arrays])
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / binary_heap_ / split_join_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
d652f226
JJ
3// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4// Free Software Foundation, Inc.
4569a895
AT
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
748086b7 9// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
748086b7
JJ
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.
4569a895 20
748086b7
JJ
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/>.
4569a895
AT
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 split_join_fn_imps.hpp
39 * Contains an implementation class for a binary_heap.
40 */
41
42PB_DS_CLASS_T_DEC
43template<typename Pred>
44void
45PB_DS_CLASS_C_DEC::
46split(Pred pred, PB_DS_CLASS_C_DEC& other)
47{
47bea7b8 48 _GLIBCXX_DEBUG_ONLY(assert_valid();)
4569a895
AT
49
50 typedef
51 typename entry_pred<
52 value_type,
53 Pred,
54 simple_value,
55 Allocator>::type
56 pred_t;
57
58 const size_type left = partition(pred_t(pred));
59
47bea7b8 60 _GLIBCXX_DEBUG_ASSERT(m_size >= left);
4569a895
AT
61
62 const size_type ersd = m_size - left;
63
47bea7b8 64 _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
4569a895
AT
65
66 const size_type actual_size =
67 resize_policy::get_new_size_for_arbitrary(left);
68
69 const size_type other_actual_size =
70 other.get_new_size_for_arbitrary(ersd);
71
8fc81078
PC
72 entry_pointer a_entries = 0;
73 entry_pointer a_other_entries = 0;
4569a895 74
bc2631e0 75 __try
4569a895
AT
76 {
77 a_entries = s_entry_allocator.allocate(actual_size);
78
79 a_other_entries = s_entry_allocator.allocate(other_actual_size);
80 }
bc2631e0 81 __catch(...)
4569a895 82 {
8fc81078 83 if (a_entries != 0)
4569a895
AT
84 s_entry_allocator.deallocate(a_entries, actual_size);
85
8fc81078 86 if (a_other_entries != 0)
4569a895
AT
87 s_entry_allocator.deallocate(a_other_entries, other_actual_size);
88
8fafc2d3 89 __throw_exception_again;
4569a895
AT
90 };
91
92 for (size_type i = 0; i < other.m_size; ++i)
93 erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
94
47bea7b8 95 _GLIBCXX_DEBUG_ASSERT(actual_size >= left);
4569a895
AT
96 std::copy(m_a_entries, m_a_entries + left, a_entries);
97 std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries);
98
99 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
100 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
101
102 m_actual_size = actual_size;
103 other.m_actual_size = other_actual_size;
104
105 m_size = left;
106 other.m_size = ersd;
107
108 m_a_entries = a_entries;
109 other.m_a_entries = a_other_entries;
110
111 std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
112 std::make_heap(other.m_a_entries, other.m_a_entries + other.m_size, static_cast<entry_cmp& >(other));
113
114 resize_policy::notify_arbitrary(m_actual_size);
115 other.notify_arbitrary(other.m_actual_size);
116
47bea7b8
BK
117 _GLIBCXX_DEBUG_ONLY(assert_valid();)
118 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
4569a895
AT
119 }
120
121PB_DS_CLASS_T_DEC
122inline void
123PB_DS_CLASS_C_DEC::
124join(PB_DS_CLASS_C_DEC& other)
125{
47bea7b8 126 _GLIBCXX_DEBUG_ONLY(assert_valid();)
8fafc2d3 127 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
4569a895 128
1ab79481
BK
129 const size_type len = m_size + other.m_size;
130 const size_type actual_size = resize_policy::get_new_size_for_arbitrary(len);
4569a895 131
8fc81078
PC
132 entry_pointer a_entries = 0;
133 entry_pointer a_other_entries = 0;
4569a895 134
bc2631e0 135 __try
4569a895
AT
136 {
137 a_entries = s_entry_allocator.allocate(actual_size);
4569a895
AT
138 a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
139 }
bc2631e0 140 __catch(...)
4569a895 141 {
8fc81078 142 if (a_entries != 0)
4569a895
AT
143 s_entry_allocator.deallocate(a_entries, actual_size);
144
8fc81078 145 if (a_other_entries != 0)
4569a895
AT
146 s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
147
8fafc2d3 148 __throw_exception_again;
4569a895
AT
149 }
150
151 std::copy(m_a_entries, m_a_entries + m_size, a_entries);
152 std::copy(other.m_a_entries, other.m_a_entries + other.m_size, a_entries + m_size);
153
154 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
155 m_a_entries = a_entries;
1ab79481 156 m_size = len;
4569a895
AT
157 m_actual_size = actual_size;
158
159 resize_policy::notify_arbitrary(actual_size);
160
161 std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
162
163 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
164 other.m_a_entries = a_other_entries;
165 other.m_size = 0;
166 other.m_actual_size = resize_policy::min_size;
167
168 other.notify_arbitrary(resize_policy::min_size);
169
47bea7b8 170 _GLIBCXX_DEBUG_ONLY(assert_valid();)
8fafc2d3
BK
171 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
172}
4569a895 173