3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file insert_fn_imps.hpp
44 * Contains an implementation class for a binary_heap.
48 inline typename PB_DS_CLASS_C_DEC::point_iterator
50 push(const_reference r_val)
52 _GLIBCXX_DEBUG_ONLY(assert_valid();)
54 insert_value(r_val, s_no_throw_copies_ind);
56 std::push_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
58 _GLIBCXX_DEBUG_ONLY(assert_valid();)
60 return point_iterator(m_a_entries);
66 insert_value(value_type val, true_type)
68 resize_for_insert_if_needed();
70 m_a_entries[m_size++] = val;
76 insert_value(const_reference r_val, false_type)
78 resize_for_insert_if_needed();
80 pointer p_new = s_value_allocator.allocate(1);
82 cond_dealtor_t cond(p_new);
84 new (p_new) value_type(r_val);
88 m_a_entries[m_size++] = p_new;
96 resize_for_insert_if_needed();
98 m_a_entries[m_size++] = e;
104 resize_for_insert_if_needed()
106 if (!resize_policy::resize_needed_for_grow(m_size))
108 _GLIBCXX_DEBUG_ASSERT(m_size < m_actual_size);
113 const size_type new_actual_size =
114 resize_policy::get_new_size_for_grow();
116 entry_pointer a_new_entries = s_entry_allocator.allocate(new_actual_size);
118 resize_policy::notify_grow_resize();
120 std::copy(m_a_entries, m_a_entries + m_size, a_new_entries);
122 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
124 m_actual_size = new_actual_size;
126 m_a_entries = a_new_entries;
132 modify(point_iterator it, const_reference r_new_val)
134 _GLIBCXX_DEBUG_ONLY(assert_valid();)
136 swap_value_imp(it.m_p_e, r_new_val, s_no_throw_copies_ind);
140 _GLIBCXX_DEBUG_ONLY(assert_valid();)
146 fix(entry_pointer p_e)
148 size_type i = p_e - m_a_entries;
150 if (i > 0&& entry_cmp::operator()(m_a_entries[parent(i)], m_a_entries[i]))
152 size_type parent_i = parent(i);
154 while (i > 0&& entry_cmp::operator()(m_a_entries[parent_i], m_a_entries[i]))
156 std::swap(m_a_entries[i], m_a_entries[parent_i]);
160 parent_i = parent(i);
163 _GLIBCXX_DEBUG_ONLY(assert_valid();)
170 const size_type left_child_i = left_child(i);
171 const size_type right_child_i = right_child(i);
173 _GLIBCXX_DEBUG_ASSERT(right_child_i > left_child_i);
175 const bool smaller_than_left_child =
176 left_child_i < m_size&&
177 entry_cmp::operator()(m_a_entries[i], m_a_entries[left_child_i]);
179 const bool smaller_than_right_child =
180 right_child_i < m_size&&
181 entry_cmp::operator()(m_a_entries[i], m_a_entries[right_child_i]);
183 const bool swap_with_r_child = smaller_than_right_child&& (!smaller_than_left_child ||
184 entry_cmp::operator()(m_a_entries[left_child_i], m_a_entries[right_child_i]));
186 const bool swap_with_l_child = !swap_with_r_child&& smaller_than_left_child;
188 if (swap_with_l_child)
190 std::swap(m_a_entries[i], m_a_entries[left_child_i]);
194 else if (swap_with_r_child)
196 std::swap(m_a_entries[i], m_a_entries[right_child_i]);
208 swap_value_imp(entry_pointer p_e, value_type new_val, true_type)
216 swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type)
218 value_type tmp(r_new_val);