1169691Skan// -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the terms
7169691Skan// of the GNU General Public License as published by the Free Software
8169691Skan// Foundation; either version 2, or (at your option) any later
9169691Skan// version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful, but
12169691Skan// WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14169691Skan// General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License
17169691Skan// along with this library; see the file COPYING.  If not, write to
18169691Skan// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19169691Skan// MA 02111-1307, USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free
22169691Skan// software library without restriction.  Specifically, if other files
23169691Skan// instantiate templates or use macros or inline functions from this
24169691Skan// file, or you compile this file and link it with other files to
25169691Skan// produce an executable, this file does not by itself cause the
26169691Skan// resulting executable to be covered by the GNU General Public
27169691Skan// License.  This exception does not however invalidate any other
28169691Skan// reasons why the executable file might be covered by the GNU General
29169691Skan// Public License.
30169691Skan
31169691Skan// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32169691Skan
33169691Skan// Permission to use, copy, modify, sell, and distribute this software
34169691Skan// is hereby granted without fee, provided that the above copyright
35169691Skan// notice appears in all copies, and that both that copyright notice
36169691Skan// and this permission notice appear in supporting documentation. None
37169691Skan// of the above authors, nor IBM Haifa Research Laboratories, make any
38169691Skan// representation about the suitability of this software for any
39169691Skan// purpose. It is provided "as is" without express or implied
40169691Skan// warranty.
41169691Skan
42169691Skan/**
43169691Skan * @file insert_fn_imps.hpp
44169691Skan * Contains an implementation class for a binary_heap.
45169691Skan */
46169691Skan
47169691SkanPB_DS_CLASS_T_DEC
48169691Skaninline typename PB_DS_CLASS_C_DEC::point_iterator
49169691SkanPB_DS_CLASS_C_DEC::
50169691Skanpush(const_reference r_val)
51169691Skan{
52169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
53169691Skan
54169691Skan    insert_value(r_val, s_no_throw_copies_ind);
55169691Skan
56169691Skan  std::push_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
57169691Skan
58169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
59169691Skan
60169691Skan    return point_iterator(m_a_entries);
61169691Skan}
62169691Skan
63169691SkanPB_DS_CLASS_T_DEC
64169691Skaninline void
65169691SkanPB_DS_CLASS_C_DEC::
66169691Skaninsert_value(value_type val, true_type)
67169691Skan{
68169691Skan  resize_for_insert_if_needed();
69169691Skan
70169691Skan  m_a_entries[m_size++] = val;
71169691Skan}
72169691Skan
73169691SkanPB_DS_CLASS_T_DEC
74169691Skaninline void
75169691SkanPB_DS_CLASS_C_DEC::
76169691Skaninsert_value(const_reference r_val, false_type)
77169691Skan{
78169691Skan  resize_for_insert_if_needed();
79169691Skan
80169691Skan  pointer p_new = s_value_allocator.allocate(1);
81169691Skan
82169691Skan  cond_dealtor_t cond(p_new);
83169691Skan
84169691Skan  new (p_new) value_type(r_val);
85169691Skan
86169691Skan  cond.set_no_action();
87169691Skan
88169691Skan  m_a_entries[m_size++] = p_new;
89169691Skan}
90169691Skan
91169691SkanPB_DS_CLASS_T_DEC
92169691Skaninline void
93169691SkanPB_DS_CLASS_C_DEC::
94169691Skaninsert_entry(entry e)
95169691Skan{
96169691Skan  resize_for_insert_if_needed();
97169691Skan
98169691Skan  m_a_entries[m_size++] = e;
99169691Skan}
100169691Skan
101169691SkanPB_DS_CLASS_T_DEC
102169691Skaninline void
103169691SkanPB_DS_CLASS_C_DEC::
104169691Skanresize_for_insert_if_needed()
105169691Skan{
106169691Skan  if (!resize_policy::resize_needed_for_grow(m_size))
107169691Skan    {
108169691Skan      _GLIBCXX_DEBUG_ASSERT(m_size < m_actual_size);
109169691Skan
110169691Skan      return;
111169691Skan    }
112169691Skan
113169691Skan  const size_type new_actual_size =
114169691Skan    resize_policy::get_new_size_for_grow();
115169691Skan
116169691Skan  entry_pointer a_new_entries = s_entry_allocator.allocate(new_actual_size);
117169691Skan
118169691Skan  resize_policy::notify_grow_resize();
119169691Skan
120169691Skan  std::copy(m_a_entries, m_a_entries + m_size, a_new_entries);
121169691Skan
122169691Skan  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
123169691Skan
124169691Skan  m_actual_size = new_actual_size;
125169691Skan
126169691Skan  m_a_entries = a_new_entries;
127169691Skan}
128169691Skan
129169691SkanPB_DS_CLASS_T_DEC
130169691Skanvoid
131169691SkanPB_DS_CLASS_C_DEC::
132169691Skanmodify(point_iterator it, const_reference r_new_val)
133169691Skan{
134169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
135169691Skan
136169691Skan    swap_value_imp(it.m_p_e, r_new_val, s_no_throw_copies_ind);
137169691Skan
138169691Skan  fix(it.m_p_e);
139169691Skan
140169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
141169691Skan    }
142169691Skan
143169691SkanPB_DS_CLASS_T_DEC
144169691Skanvoid
145169691SkanPB_DS_CLASS_C_DEC::
146169691Skanfix(entry_pointer p_e)
147169691Skan{
148169691Skan  size_type i = p_e - m_a_entries;
149169691Skan
150169691Skan  if (i > 0&&  entry_cmp::operator()(m_a_entries[parent(i)], m_a_entries[i]))
151169691Skan    {
152169691Skan      size_type parent_i = parent(i);
153169691Skan
154169691Skan      while (i > 0&&  entry_cmp::operator()(m_a_entries[parent_i], m_a_entries[i]))
155169691Skan        {
156169691Skan	  std::swap(m_a_entries[i], m_a_entries[parent_i]);
157169691Skan
158169691Skan	  i = parent_i;
159169691Skan
160169691Skan	  parent_i = parent(i);
161169691Skan        }
162169691Skan
163169691Skan      _GLIBCXX_DEBUG_ONLY(assert_valid();)
164169691Skan
165169691Skan        return;
166169691Skan    }
167169691Skan
168169691Skan  while (i < m_size)
169169691Skan    {
170169691Skan      const size_type left_child_i = left_child(i);
171169691Skan      const size_type right_child_i = right_child(i);
172169691Skan
173169691Skan      _GLIBCXX_DEBUG_ASSERT(right_child_i > left_child_i);
174169691Skan
175169691Skan      const bool smaller_than_left_child =
176169691Skan	left_child_i < m_size&&
177169691Skan	entry_cmp::operator()(m_a_entries[i], m_a_entries[left_child_i]);
178169691Skan
179169691Skan      const bool smaller_than_right_child =
180169691Skan	right_child_i < m_size&&
181169691Skan	entry_cmp::operator()(m_a_entries[i], m_a_entries[right_child_i]);
182169691Skan
183169691Skan      const bool swap_with_r_child = smaller_than_right_child&& (!smaller_than_left_child ||
184169691Skan								 entry_cmp::operator()(m_a_entries[left_child_i], m_a_entries[right_child_i]));
185169691Skan
186169691Skan      const bool swap_with_l_child = !swap_with_r_child&&  smaller_than_left_child;
187169691Skan
188169691Skan      if (swap_with_l_child)
189169691Skan        {
190169691Skan	  std::swap(m_a_entries[i], m_a_entries[left_child_i]);
191169691Skan
192169691Skan	  i = left_child_i;
193169691Skan        }
194169691Skan      else if (swap_with_r_child)
195169691Skan        {
196169691Skan	  std::swap(m_a_entries[i], m_a_entries[right_child_i]);
197169691Skan
198169691Skan	  i = right_child_i;
199169691Skan        }
200169691Skan      else
201169691Skan	i = m_size;
202169691Skan    }
203169691Skan}
204169691Skan
205169691SkanPB_DS_CLASS_T_DEC
206169691Skaninline void
207169691SkanPB_DS_CLASS_C_DEC::
208169691Skanswap_value_imp(entry_pointer p_e, value_type new_val, true_type)
209169691Skan{
210169691Skan  * p_e = new_val;
211169691Skan}
212169691Skan
213169691SkanPB_DS_CLASS_T_DEC
214169691Skaninline void
215169691SkanPB_DS_CLASS_C_DEC::
216169691Skanswap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type)
217169691Skan{
218169691Skan  value_type tmp(r_new_val);
219169691Skan  (*p_e)->swap(tmp);
220169691Skan}
221