libstdc++
binary_heap_/split_join_fn_imps.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2022 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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 
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.
19 
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/>.
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 /**
37  * @file binary_heap_/split_join_fn_imps.hpp
38  * Contains an implementation class for a binary_heap.
39  */
40 
41 #ifdef PB_DS_CLASS_C_DEC
42 
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 {
49  PB_DS_ASSERT_VALID((*this))
50 
51  typedef
52  typename entry_pred<value_type, Pred, _Alloc, simple_value>::type
53  pred_t;
54 
55  const size_type left = partition(pred_t(pred));
56  _GLIBCXX_DEBUG_ASSERT(m_size >= left);
57 
58  const size_type ersd = m_size - left;
59  _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
60 
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);
63 
64  entry_pointer a_entries = 0;
65  entry_pointer a_other_entries = 0;
66 
67  __try
68  {
69  a_entries = s_entry_allocator.allocate(new_size);
70  a_other_entries = s_entry_allocator.allocate(other_actual_size);
71  }
72  __catch(...)
73  {
74  if (a_entries != 0)
75  s_entry_allocator.deallocate(a_entries, new_size);
76 
77  if (a_other_entries != 0)
78  s_entry_allocator.deallocate(a_other_entries, other_actual_size);
79 
80  __throw_exception_again;
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 
86  _GLIBCXX_DEBUG_ASSERT(new_size >= left);
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 
93  m_actual_size = new_size;
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 
102  make_heap();
103  other.make_heap();
104 
105  resize_policy::notify_arbitrary(m_actual_size);
106  other.notify_arbitrary(other.m_actual_size);
107 
108  PB_DS_ASSERT_VALID((*this))
109  PB_DS_ASSERT_VALID(other)
110 }
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 {
117  PB_DS_ASSERT_VALID((*this))
118  PB_DS_ASSERT_VALID(other)
119 
120  const size_type len = m_size + other.m_size;
121  const size_type new_size = resize_policy::get_new_size_for_arbitrary(len);
122 
123  entry_pointer a_entries = 0;
124  entry_pointer a_other_entries = 0;
125 
126  __try
127  {
128  a_entries = s_entry_allocator.allocate(new_size);
129  a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
130  }
131  __catch(...)
132  {
133  if (a_entries != 0)
134  s_entry_allocator.deallocate(a_entries, new_size);
135 
136  if (a_other_entries != 0)
137  s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
138 
139  __throw_exception_again;
140  }
141 
142  std::copy(m_a_entries, m_a_entries + m_size, a_entries);
143  std::copy(other.m_a_entries, other.m_a_entries + other.m_size,
144  a_entries + m_size);
145 
146  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
147  m_a_entries = a_entries;
148  m_size = len;
149  m_actual_size = new_size;
150  resize_policy::notify_arbitrary(new_size);
151  make_heap();
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;
157  other.notify_arbitrary(resize_policy::min_size);
158  other.make_heap();
159 
160  PB_DS_ASSERT_VALID((*this))
161  PB_DS_ASSERT_VALID(other)
162 }
163 #endif
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition: ios_base.h:1038