3 // Copyright (C) 2005-2018 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
37 * @file binary_heap_/insert_fn_imps.hpp
38 * Contains an implementation class for a binary_heap.
42 inline typename
PB_DS_CLASS_C_DEC::point_iterator
44 push(const_reference r_val
)
46 PB_DS_ASSERT_VALID((*this))
47 insert_value(r_val
, s_no_throw_copies_ind
);
49 PB_DS_ASSERT_VALID((*this))
50 return point_iterator(m_a_entries
);
56 insert_value(value_type val
, true_type
)
58 resize_for_insert_if_needed();
59 m_a_entries
[m_size
++] = val
;
65 insert_value(const_reference r_val
, false_type
)
67 resize_for_insert_if_needed();
68 pointer p_new
= s_value_allocator
.allocate(1);
69 cond_dealtor_t
cond(p_new
);
70 new (p_new
) value_type(r_val
);
72 m_a_entries
[m_size
++] = p_new
;
78 resize_for_insert_if_needed()
80 if (!resize_policy::resize_needed_for_grow(m_size
))
82 _GLIBCXX_DEBUG_ASSERT(m_size
< m_actual_size
);
86 const size_type new_size
= resize_policy::get_new_size_for_grow();
87 entry_pointer new_entries
= s_entry_allocator
.allocate(new_size
);
88 resize_policy::notify_grow_resize();
90 std::copy(m_a_entries
, m_a_entries
+ m_size
, new_entries
);
91 s_entry_allocator
.deallocate(m_a_entries
, m_actual_size
);
92 m_actual_size
= new_size
;
93 m_a_entries
= new_entries
;
100 modify(point_iterator it
, const_reference r_new_val
)
102 PB_DS_ASSERT_VALID((*this))
103 swap_value_imp(it
.m_p_e
, r_new_val
, s_no_throw_copies_ind
);
105 PB_DS_ASSERT_VALID((*this))
111 fix(entry_pointer p_e
)
113 size_type i
= p_e
- m_a_entries
;
114 if (i
> 0 && entry_cmp::operator()(m_a_entries
[parent(i
)], m_a_entries
[i
]))
116 size_type parent_i
= parent(i
);
118 && entry_cmp::operator()(m_a_entries
[parent_i
], m_a_entries
[i
]))
120 std::swap(m_a_entries
[i
], m_a_entries
[parent_i
]);
122 parent_i
= parent(i
);
125 PB_DS_ASSERT_VALID((*this))
131 const size_type lchild_i
= left_child(i
);
132 const size_type rchild_i
= right_child(i
);
133 _GLIBCXX_DEBUG_ASSERT(rchild_i
> lchild_i
);
135 const bool smaller_than_lchild
= lchild_i
< m_size
&&
136 entry_cmp::operator()(m_a_entries
[i
], m_a_entries
[lchild_i
]);
138 const bool smaller_than_rchild
= rchild_i
< m_size
&&
139 entry_cmp::operator()(m_a_entries
[i
], m_a_entries
[rchild_i
]);
141 const bool swap_with_rchild
= smaller_than_rchild
&& (!smaller_than_lchild
|| entry_cmp::operator()(m_a_entries
[lchild_i
], m_a_entries
[rchild_i
]));
143 const bool swap_with_lchild
= !swap_with_rchild
&& smaller_than_lchild
;
145 if (swap_with_lchild
)
147 std::swap(m_a_entries
[i
], m_a_entries
[lchild_i
]);
150 else if (swap_with_rchild
)
152 std::swap(m_a_entries
[i
], m_a_entries
[rchild_i
]);
163 swap_value_imp(entry_pointer p_e
, value_type new_val
, true_type
)
169 swap_value_imp(entry_pointer p_e
, const_reference r_new_val
, false_type
)
171 value_type
tmp(r_new_val
);