Home | History | Annotate | Download | only in thin_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 erase_fn_imps.hpp
     38  * Contains an implementation for thin_heap_.
     39  */
     40 
     41 PB_DS_CLASS_T_DEC
     42 void
     43 PB_DS_CLASS_C_DEC::
     44 pop()
     45 {
     46   _GLIBCXX_DEBUG_ONLY(assert_valid();)
     47     _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
     48 
     49   _GLIBCXX_DEBUG_ASSERT(m_p_max != NULL);
     50 
     51   node_pointer p_nd = m_p_max;
     52 
     53   remove_max_node();
     54 
     55   base_type::actual_erase_node(p_nd);
     56 
     57   _GLIBCXX_DEBUG_ONLY(assert_valid();)
     58     }
     59 
     60 PB_DS_CLASS_T_DEC
     61 inline void
     62 PB_DS_CLASS_C_DEC::
     63 remove_max_node()
     64 {
     65   to_aux_except_max();
     66 
     67   make_from_aux();
     68 }
     69 
     70 PB_DS_CLASS_T_DEC
     71 void
     72 PB_DS_CLASS_C_DEC::
     73 to_aux_except_max()
     74 {
     75   node_pointer p_add = base_type::m_p_root;
     76 
     77   while (p_add != m_p_max)
     78     {
     79       node_pointer p_next_add = p_add->m_p_next_sibling;
     80 
     81       add_to_aux(p_add);
     82 
     83       p_add = p_next_add;
     84     }
     85 
     86   p_add = m_p_max->m_p_l_child;
     87 
     88   while (p_add != NULL)
     89     {
     90       node_pointer p_next_add = p_add->m_p_next_sibling;
     91 
     92       p_add->m_metadata = p_add->m_p_l_child == NULL?
     93 	0 :
     94 	p_add->m_p_l_child->m_metadata + 1;
     95 
     96       add_to_aux(p_add);
     97 
     98       p_add = p_next_add;
     99     }
    100 
    101   p_add = m_p_max->m_p_next_sibling;
    102 
    103   while (p_add != NULL)
    104     {
    105       node_pointer p_next_add = p_add->m_p_next_sibling;
    106 
    107       add_to_aux(p_add);
    108 
    109       p_add = p_next_add;
    110     }
    111 }
    112 
    113 PB_DS_CLASS_T_DEC
    114 inline void
    115 PB_DS_CLASS_C_DEC::
    116 add_to_aux(node_pointer p_nd)
    117 {
    118   size_type r = p_nd->m_metadata;
    119 
    120   while (m_a_aux[r] != NULL)
    121     {
    122       _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata < rank_bound());
    123 
    124       if (Cmp_Fn::operator()(m_a_aux[r]->m_value, p_nd->m_value))
    125 	make_child_of(m_a_aux[r], p_nd);
    126       else
    127         {
    128 	  make_child_of(p_nd, m_a_aux[r]);
    129 
    130 	  p_nd = m_a_aux[r];
    131         }
    132 
    133       m_a_aux[r] = NULL;
    134 
    135       ++r;
    136     }
    137 
    138   _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata < rank_bound());
    139 
    140   m_a_aux[r] = p_nd;
    141 }
    142 
    143 PB_DS_CLASS_T_DEC
    144 inline void
    145 PB_DS_CLASS_C_DEC::
    146 make_child_of(node_pointer p_nd, node_pointer p_new_parent)
    147 {
    148   _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata == p_new_parent->m_metadata);
    149   _GLIBCXX_DEBUG_ASSERT(m_a_aux[p_nd->m_metadata] == p_nd ||
    150 		   m_a_aux[p_nd->m_metadata] == p_new_parent);
    151 
    152   ++p_new_parent->m_metadata;
    153 
    154   base_type::make_child_of(p_nd, p_new_parent);
    155 }
    156 
    157 PB_DS_CLASS_T_DEC
    158 inline void
    159 PB_DS_CLASS_C_DEC::
    160 make_from_aux()
    161 {
    162   base_type::m_p_root = m_p_max = NULL;
    163 
    164   const size_type rnk_bnd = rank_bound();
    165 
    166   size_type i = 0;
    167 
    168   while (i < rnk_bnd)
    169     {
    170       if (m_a_aux[i] != NULL)
    171         {
    172 	  make_root_and_link(m_a_aux[i]);
    173 
    174 	  m_a_aux[i] = NULL;
    175         }
    176 
    177       ++i;
    178     }
    179 
    180   _GLIBCXX_DEBUG_ONLY(assert_aux_null();)
    181     }
    182 
    183 PB_DS_CLASS_T_DEC
    184 inline void
    185 PB_DS_CLASS_C_DEC::
    186 remove_node(node_pointer p_nd)
    187 {
    188   node_pointer p_parent = p_nd;
    189   while (base_type::parent(p_parent) != NULL)
    190     p_parent = base_type::parent(p_parent);
    191 
    192   base_type::bubble_to_top(p_nd);
    193 
    194   m_p_max = p_nd;
    195 
    196   node_pointer p_fix = base_type::m_p_root;
    197   while (p_fix != NULL&&  p_fix->m_p_next_sibling != p_parent)
    198     p_fix = p_fix->m_p_next_sibling;
    199 
    200   if (p_fix != NULL)
    201     p_fix->m_p_next_sibling = p_nd;
    202 
    203   remove_max_node();
    204 }
    205 
    206 PB_DS_CLASS_T_DEC
    207 inline void
    208 PB_DS_CLASS_C_DEC::
    209 clear()
    210 {
    211   base_type::clear();
    212 
    213   m_p_max = NULL;
    214 }
    215 
    216 PB_DS_CLASS_T_DEC
    217 void
    218 PB_DS_CLASS_C_DEC::
    219 erase(point_iterator it)
    220 {
    221   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    222     _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
    223 
    224   node_pointer p_nd = it.m_p_nd;
    225 
    226   remove_node(p_nd);
    227 
    228   base_type::actual_erase_node(p_nd);
    229 
    230   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    231     }
    232 
    233 PB_DS_CLASS_T_DEC
    234 template<typename Pred>
    235 typename PB_DS_CLASS_C_DEC::size_type
    236 PB_DS_CLASS_C_DEC::
    237 erase_if(Pred pred)
    238 {
    239   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    240 
    241     if (base_type::empty())
    242       {
    243         _GLIBCXX_DEBUG_ONLY(assert_valid();)
    244 
    245 	  return 0;
    246       }
    247 
    248   base_type::to_linked_list();
    249 
    250   node_pointer p_out = base_type::prune(pred);
    251 
    252   size_type ersd = 0;
    253 
    254   while (p_out != NULL)
    255     {
    256       ++ersd;
    257 
    258       node_pointer p_next = p_out->m_p_next_sibling;
    259 
    260       base_type::actual_erase_node(p_out);
    261 
    262       p_out = p_next;
    263     }
    264 
    265   node_pointer p_cur = base_type::m_p_root;
    266 
    267   m_p_max = base_type::m_p_root = NULL;
    268 
    269   while (p_cur != NULL)
    270     {
    271       node_pointer p_next = p_cur->m_p_next_sibling;
    272 
    273       make_root_and_link(p_cur);
    274 
    275       p_cur = p_next;
    276     }
    277 
    278   _GLIBCXX_DEBUG_ONLY(assert_valid();)
    279 
    280     return ersd;
    281 }
    282 
    283 PB_DS_CLASS_T_DEC
    284 inline typename PB_DS_CLASS_C_DEC::size_type
    285 PB_DS_CLASS_C_DEC::
    286 rank_bound()
    287 {
    288   const std::size_t* const p_upper =
    289     std::upper_bound(            g_a_rank_bounds, g_a_rank_bounds + num_distinct_rank_bounds, base_type::m_size);
    290 
    291   if (p_upper == g_a_rank_bounds + num_distinct_rank_bounds)
    292     return max_rank;
    293 
    294   return (p_upper - g_a_rank_bounds);
    295 }
    296 
    297