libstdc++
binary_heap_/split_join_fn_imps.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2018 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 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 {
47  PB_DS_ASSERT_VALID((*this))
48 
49  typedef
50  typename entry_pred<value_type, Pred, _Alloc, simple_value>::type
51  pred_t;
52 
53  const size_type left = partition(pred_t(pred));
54  _GLIBCXX_DEBUG_ASSERT(m_size >= left);
55 
56  const size_type ersd = m_size - left;
57  _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
58 
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);
61 
62  entry_pointer a_entries = 0;
63  entry_pointer a_other_entries = 0;
64 
65  __try
66  {
67  a_entries = s_entry_allocator.allocate(new_size);
68  a_other_entries = s_entry_allocator.allocate(other_actual_size);
69  }
70  __catch(...)
71  {
72  if (a_entries != 0)
73  s_entry_allocator.deallocate(a_entries, new_size);
74 
75  if (a_other_entries != 0)
76  s_entry_allocator.deallocate(a_other_entries, other_actual_size);
77 
78  __throw_exception_again;
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 
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);
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 
91  m_actual_size = new_size;
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 
100  make_heap();
101  other.make_heap();
102 
103  resize_policy::notify_arbitrary(m_actual_size);
104  other.notify_arbitrary(other.m_actual_size);
105 
106  PB_DS_ASSERT_VALID((*this))
107  PB_DS_ASSERT_VALID(other)
108 }
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 {
115  PB_DS_ASSERT_VALID((*this))
116  PB_DS_ASSERT_VALID(other)
117 
118  const size_type len = m_size + other.m_size;
119  const size_type new_size = resize_policy::get_new_size_for_arbitrary(len);
120 
121  entry_pointer a_entries = 0;
122  entry_pointer a_other_entries = 0;
123 
124  __try
125  {
126  a_entries = s_entry_allocator.allocate(new_size);
127  a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
128  }
129  __catch(...)
130  {
131  if (a_entries != 0)
132  s_entry_allocator.deallocate(a_entries, new_size);
133 
134  if (a_other_entries != 0)
135  s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
136 
137  __throw_exception_again;
138  }
139 
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,
142  a_entries + m_size);
143 
144  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
145  m_a_entries = a_entries;
146  m_size = len;
147  m_actual_size = new_size;
148  resize_policy::notify_arbitrary(new_size);
149  make_heap();
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;
155  other.notify_arbitrary(resize_policy::min_size);
156  other.make_heap();
157 
158  PB_DS_ASSERT_VALID((*this))
159  PB_DS_ASSERT_VALID(other)
160 }
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition: ios_base.h:1001