Home | History | Annotate | Download | only in binary_heap_
      1 // -*- C++ -*-
      2 
      3 // Copyright (C) 2005, 2006, 2009 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 insert_fn_imps.hpp
     38  * Contains an implementation class for a binary_heap.
     39  */
     40 
     41 PB_DS_CLASS_T_DEC
     42 inline typename PB_DS_CLASS_C_DEC::point_iterator
     43 PB_DS_CLASS_C_DEC::
     44 push(const_reference r_val)
     45 {
     46   _GLIBCXX_DEBUG_ONLY(assert_valid();)
     47   insert_value(r_val, s_no_throw_copies_ind);
     48   std::push_heap(m_a_entries, m_a_entries + m_size,
     49 		 static_cast<entry_cmp&>(*this));
     50   _GLIBCXX_DEBUG_ONLY(assert_valid();)
     51   return point_iterator(m_a_entries);
     52 }
     53 
     54 PB_DS_CLASS_T_DEC
     55 inline void
     56 PB_DS_CLASS_C_DEC::
     57 insert_value(value_type val, true_type)
     58 {
     59   resize_for_insert_if_needed();
     60 
     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();
     70   pointer p_new = s_value_allocator.allocate(1);
     71   cond_dealtor_t cond(p_new);
     72   new (p_new) value_type(r_val);
     73   cond.set_no_action();
     74   m_a_entries[m_size++] = p_new;
     75 }
     76 
     77 PB_DS_CLASS_T_DEC
     78 inline void
     79 PB_DS_CLASS_C_DEC::
     80 insert_entry(entry e)
     81 {
     82   resize_for_insert_if_needed();
     83   m_a_entries[m_size++] = e;
     84 }
     85 
     86 PB_DS_CLASS_T_DEC
     87 inline void
     88 PB_DS_CLASS_C_DEC::
     89 resize_for_insert_if_needed()
     90 {
     91   if (!resize_policy::resize_needed_for_grow(m_size))
     92     {
     93       _GLIBCXX_DEBUG_ASSERT(m_size < m_actual_size);
     94       return;
     95     }
     96 
     97   const size_type new_actual_size = resize_policy::get_new_size_for_grow();
     98   entry_pointer a_new_entries = s_entry_allocator.allocate(new_actual_size);
     99   resize_policy::notify_grow_resize();
    100   std::copy(m_a_entries, m_a_entries + m_size, a_new_entries);
    101   s_entry_allocator.deallocate(m_a_entries, m_actual_size);
    102   m_actual_size = new_actual_size;
    103   m_a_entries = a_new_entries;
    104 }
    105 
    106 PB_DS_CLASS_T_DEC
    107 void
    108 PB_DS_CLASS_C_DEC::
    109 modify(point_iterator it, const_reference r_new_val)
    110 {
    111   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    112   swap_value_imp(it.m_p_e, r_new_val, s_no_throw_copies_ind);
    113   fix(it.m_p_e);
    114   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    115 }
    116 
    117 PB_DS_CLASS_T_DEC
    118 void
    119 PB_DS_CLASS_C_DEC::
    120 fix(entry_pointer p_e)
    121 {
    122   size_type i = p_e - m_a_entries;
    123   if (i > 0 && entry_cmp::operator()(m_a_entries[parent(i)], m_a_entries[i]))
    124     {
    125       size_type parent_i = parent(i);
    126       while (i > 0
    127 	     && entry_cmp::operator()(m_a_entries[parent_i], m_a_entries[i]))
    128         {
    129 	  std::swap(m_a_entries[i], m_a_entries[parent_i]);
    130 	  i = parent_i;
    131 	  parent_i = parent(i);
    132         }
    133 
    134       _GLIBCXX_DEBUG_ONLY(assert_valid();)
    135       return;
    136     }
    137 
    138   while (i < m_size)
    139     {
    140       const size_type left_child_i = left_child(i);
    141       const size_type right_child_i = right_child(i);
    142       _GLIBCXX_DEBUG_ASSERT(right_child_i > left_child_i);
    143       const bool smaller_than_left_child = left_child_i < m_size &&
    144 	entry_cmp::operator()(m_a_entries[i], m_a_entries[left_child_i]);
    145 
    146       const bool smaller_than_right_child = right_child_i < m_size &&
    147 	entry_cmp::operator()(m_a_entries[i], m_a_entries[right_child_i]);
    148 
    149       const bool swap_with_r_child = smaller_than_right_child && (!smaller_than_left_child || entry_cmp::operator()(m_a_entries[left_child_i], m_a_entries[right_child_i]));
    150 
    151       const bool swap_with_l_child = !swap_with_r_child && smaller_than_left_child;
    152 
    153       if (swap_with_l_child)
    154         {
    155 	  std::swap(m_a_entries[i], m_a_entries[left_child_i]);
    156 	  i = left_child_i;
    157         }
    158       else if (swap_with_r_child)
    159         {
    160 	  std::swap(m_a_entries[i], m_a_entries[right_child_i]);
    161 	  i = right_child_i;
    162         }
    163       else
    164 	i = m_size;
    165     }
    166 }
    167 
    168 PB_DS_CLASS_T_DEC
    169 inline void
    170 PB_DS_CLASS_C_DEC::
    171 swap_value_imp(entry_pointer p_e, value_type new_val, true_type)
    172 {
    173   * p_e = new_val;
    174 }
    175 
    176 PB_DS_CLASS_T_DEC
    177 inline void
    178 PB_DS_CLASS_C_DEC::
    179 swap_value_imp(entry_pointer p_e, const_reference r_new_val, false_type)
    180 {
    181   value_type tmp(r_new_val);
    182   (*p_e)->swap(tmp);
    183 }
    184