]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
8d9254fc | 3 | // Copyright (C) 2005-2020 Free Software Foundation, Inc. |
4569a895 AT |
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 | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
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 | ||
748086b7 JJ |
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. | |
4569a895 | 19 | |
748086b7 JJ |
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/>. | |
4569a895 AT |
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 | /** | |
a345e45d | 37 | * @file binary_heap_/insert_fn_imps.hpp |
4569a895 AT |
38 | * Contains an implementation class for a binary_heap. |
39 | */ | |
40 | ||
574dfb67 JW |
41 | #ifdef PB_DS_CLASS_C_DEC |
42 | ||
4569a895 AT |
43 | PB_DS_CLASS_T_DEC |
44 | inline typename PB_DS_CLASS_C_DEC::point_iterator | |
45 | PB_DS_CLASS_C_DEC:: | |
46 | push(const_reference r_val) | |
47 | { | |
f5886803 | 48 | PB_DS_ASSERT_VALID((*this)) |
2e3f9c21 | 49 | insert_value(r_val, s_no_throw_copies_ind); |
a345e45d | 50 | push_heap(); |
f5886803 | 51 | PB_DS_ASSERT_VALID((*this)) |
2e3f9c21 | 52 | return point_iterator(m_a_entries); |
4569a895 AT |
53 | } |
54 | ||
55 | PB_DS_CLASS_T_DEC | |
56 | inline void | |
57 | PB_DS_CLASS_C_DEC:: | |
58 | insert_value(value_type val, true_type) | |
59 | { | |
60 | resize_for_insert_if_needed(); | |
4569a895 AT |
61 | m_a_entries[m_size++] = val; |
62 | } | |
63 | ||
64 | PB_DS_CLASS_T_DEC | |
65 | inline void | |
66 | PB_DS_CLASS_C_DEC:: | |
67 | insert_value(const_reference r_val, false_type) | |
68 | { | |
69 | resize_for_insert_if_needed(); | |
4569a895 | 70 | pointer p_new = s_value_allocator.allocate(1); |
4569a895 | 71 | cond_dealtor_t cond(p_new); |
4569a895 | 72 | new (p_new) value_type(r_val); |
4569a895 | 73 | cond.set_no_action(); |
4569a895 AT |
74 | m_a_entries[m_size++] = p_new; |
75 | } | |
76 | ||
4569a895 AT |
77 | PB_DS_CLASS_T_DEC |
78 | inline void | |
79 | PB_DS_CLASS_C_DEC:: | |
80 | resize_for_insert_if_needed() | |
81 | { | |
82 | if (!resize_policy::resize_needed_for_grow(m_size)) | |
83 | { | |
47bea7b8 | 84 | _GLIBCXX_DEBUG_ASSERT(m_size < m_actual_size); |
4569a895 AT |
85 | return; |
86 | } | |
87 | ||
a345e45d BK |
88 | const size_type new_size = resize_policy::get_new_size_for_grow(); |
89 | entry_pointer new_entries = s_entry_allocator.allocate(new_size); | |
4569a895 | 90 | resize_policy::notify_grow_resize(); |
a345e45d BK |
91 | |
92 | std::copy(m_a_entries, m_a_entries + m_size, new_entries); | |
4569a895 | 93 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); |
a345e45d BK |
94 | m_actual_size = new_size; |
95 | m_a_entries = new_entries; | |
96 | make_heap(); | |
4569a895 AT |
97 | } |
98 | ||
99 | PB_DS_CLASS_T_DEC | |
100 | void | |
101 | PB_DS_CLASS_C_DEC:: | |
102 | modify(point_iterator it, const_reference r_new_val) | |
103 | { | |
f5886803 | 104 | PB_DS_ASSERT_VALID((*this)) |
2e3f9c21 | 105 | swap_value_imp(it.m_p_e, r_new_val, s_no_throw_copies_ind); |
4569a895 | 106 | fix(it.m_p_e); |
f5886803 | 107 | PB_DS_ASSERT_VALID((*this)) |
2e3f9c21 | 108 | } |
4569a895 AT |
109 | |
110 | PB_DS_CLASS_T_DEC | |
111 | void | |
112 | PB_DS_CLASS_C_DEC:: | |
113 | fix(entry_pointer p_e) | |
114 | { | |
115 | size_type i = p_e - m_a_entries; | |
2e3f9c21 | 116 | if (i > 0 && entry_cmp::operator()(m_a_entries[parent(i)], m_a_entries[i])) |
4569a895 AT |
117 | { |
118 | size_type parent_i = parent(i); | |
a345e45d | 119 | while (i > 0 |
2e3f9c21 | 120 | && entry_cmp::operator()(m_a_entries[parent_i], m_a_entries[i])) |
a345e45d | 121 | { |
4569a895 | 122 | std::swap(m_a_entries[i], m_a_entries[parent_i]); |
4569a895 | 123 | i = parent_i; |
4569a895 | 124 | parent_i = parent(i); |
a345e45d | 125 | } |
4569a895 | 126 | |
f5886803 | 127 | PB_DS_ASSERT_VALID((*this)) |
2e3f9c21 | 128 | return; |
4569a895 AT |
129 | } |
130 | ||
131 | while (i < m_size) | |
132 | { | |
a345e45d BK |
133 | const size_type lchild_i = left_child(i); |
134 | const size_type rchild_i = right_child(i); | |
135 | _GLIBCXX_DEBUG_ASSERT(rchild_i > lchild_i); | |
136 | ||
137 | const bool smaller_than_lchild = lchild_i < m_size && | |
138 | entry_cmp::operator()(m_a_entries[i], m_a_entries[lchild_i]); | |
139 | ||
140 | const bool smaller_than_rchild = rchild_i < m_size && | |
141 | entry_cmp::operator()(m_a_entries[i], m_a_entries[rchild_i]); | |
142 | ||
143 | const bool swap_with_rchild = smaller_than_rchild && (!smaller_than_lchild || entry_cmp::operator()(m_a_entries[lchild_i], m_a_entries[rchild_i])); | |
144 | ||
145 | const bool swap_with_lchild = !swap_with_rchild && smaller_than_lchild; | |
146 | ||
147 | if (swap_with_lchild) | |
148 | { | |
149 | std::swap(m_a_entries[i], m_a_entries[lchild_i]); | |
150 | i = lchild_i; | |
151 | } | |
152 | else if (swap_with_rchild) | |
153 | { | |
154 | std::swap(m_a_entries[i], m_a_entries[rchild_i]); | |
155 | i = rchild_i; | |
156 | } | |
4569a895 AT |
157 | else |
158 | i = m_size; | |
159 | } | |
160 | } | |
161 | ||
162 | PB_DS_CLASS_T_DEC | |
163 | inline void | |
164 | PB_DS_CLASS_C_DEC:: | |
165 | swap_value_imp(entry_pointer p_e, value_type new_val, true_type) | |
a345e45d | 166 | { *p_e = new_val; } |
4569a895 AT |
167 | |
168 | PB_DS_CLASS_T_DEC | |
169 | inline void | |
170 | PB_DS_CLASS_C_DEC:: | |
171 | swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type) | |
172 | { | |
173 | value_type tmp(r_new_val); | |
174 | (*p_e)->swap(tmp); | |
175 | } | |
574dfb67 | 176 | #endif |