Home | History | Annotate | Download | only in left_child_next_sibling_heap_
      1 // -*- C++ -*-
      2 
      3 // Copyright (C) 2005-2013 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 left_child_next_sibling_heap_/insert_fn_imps.hpp
     38  * Contains an implementation class for left_child_next_sibling_heap_.
     39  */
     40 
     41 PB_DS_CLASS_T_DEC
     42 inline typename PB_DS_CLASS_C_DEC::node_pointer
     43 PB_DS_CLASS_C_DEC::
     44 get_new_node_for_insert(const_reference r_val)
     45 {
     46   return get_new_node_for_insert(r_val, s_no_throw_copies_ind);
     47 }
     48 
     49 PB_DS_CLASS_T_DEC
     50 inline typename PB_DS_CLASS_C_DEC::node_pointer
     51 PB_DS_CLASS_C_DEC::
     52 get_new_node_for_insert(const_reference r_val, false_type)
     53 {
     54   node_pointer p_new_nd = s_node_allocator.allocate(1);
     55 
     56   cond_dealtor_t cond(p_new_nd);
     57 
     58   new (const_cast<void* >(
     59 			  static_cast<const void* >(&p_new_nd->m_value)))
     60     typename node::value_type(r_val);
     61 
     62   cond.set_no_action();
     63 
     64   ++m_size;
     65 
     66   return (p_new_nd);
     67 }
     68 
     69 PB_DS_CLASS_T_DEC
     70 inline typename PB_DS_CLASS_C_DEC::node_pointer
     71 PB_DS_CLASS_C_DEC::
     72 get_new_node_for_insert(const_reference r_val, true_type)
     73 {
     74   node_pointer p_new_nd = s_node_allocator.allocate(1);
     75 
     76   new (const_cast<void* >(
     77 			  static_cast<const void* >(&p_new_nd->m_value)))
     78     typename node::value_type(r_val);
     79 
     80   ++m_size;
     81 
     82   return (p_new_nd);
     83 }
     84 
     85 PB_DS_CLASS_T_DEC
     86 inline void
     87 PB_DS_CLASS_C_DEC::
     88 make_child_of(node_pointer p_nd, node_pointer p_new_parent)
     89 {
     90   _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
     91   _GLIBCXX_DEBUG_ASSERT(p_new_parent != 0);
     92 
     93   p_nd->m_p_next_sibling = p_new_parent->m_p_l_child;
     94 
     95   if (p_new_parent->m_p_l_child != 0)
     96     p_new_parent->m_p_l_child->m_p_prev_or_parent = p_nd;
     97 
     98   p_nd->m_p_prev_or_parent = p_new_parent;
     99 
    100   p_new_parent->m_p_l_child = p_nd;
    101 }
    102 
    103 PB_DS_CLASS_T_DEC
    104 inline typename PB_DS_CLASS_C_DEC::node_pointer
    105 PB_DS_CLASS_C_DEC::
    106 parent(node_pointer p_nd)
    107 {
    108   while (true)
    109     {
    110       node_pointer p_pot = p_nd->m_p_prev_or_parent;
    111 
    112       if (p_pot == 0 || p_pot->m_p_l_child == p_nd)
    113 	return p_pot;
    114 
    115       p_nd = p_pot;
    116     }
    117 }
    118 
    119 PB_DS_CLASS_T_DEC
    120 inline void
    121 PB_DS_CLASS_C_DEC::
    122 swap_with_parent(node_pointer p_nd, node_pointer p_parent)
    123 {
    124   if (p_parent == m_p_root)
    125     m_p_root = p_nd;
    126 
    127   _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
    128   _GLIBCXX_DEBUG_ASSERT(p_parent != 0);
    129   _GLIBCXX_DEBUG_ASSERT(parent(p_nd) == p_parent);
    130 
    131   const bool nd_direct_child = p_parent->m_p_l_child == p_nd;
    132   const bool parent_root = p_parent->m_p_prev_or_parent == 0;
    133   const bool parent_direct_child =
    134     !parent_root&&  p_parent->m_p_prev_or_parent->m_p_l_child == p_parent;
    135 
    136   std::swap(p_parent->m_p_prev_or_parent, p_nd->m_p_prev_or_parent);
    137   std::swap(p_parent->m_p_next_sibling, p_nd->m_p_next_sibling);
    138   std::swap(p_parent->m_p_l_child, p_nd->m_p_l_child);
    139   std::swap(p_parent->m_metadata, p_nd->m_metadata);
    140 
    141   _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child != 0);
    142   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent != 0);
    143 
    144   if (p_nd->m_p_next_sibling != 0)
    145     p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd;
    146 
    147   if (p_parent->m_p_next_sibling != 0)
    148     p_parent->m_p_next_sibling->m_p_prev_or_parent = p_parent;
    149 
    150   if (p_parent->m_p_l_child != 0)
    151     p_parent->m_p_l_child->m_p_prev_or_parent = p_parent;
    152 
    153   if (parent_direct_child)
    154     p_nd->m_p_prev_or_parent->m_p_l_child = p_nd;
    155   else if (!parent_root)
    156     p_nd->m_p_prev_or_parent->m_p_next_sibling = p_nd;
    157 
    158   if (!nd_direct_child)
    159     {
    160       p_nd->m_p_l_child->m_p_prev_or_parent = p_nd;
    161 
    162       p_parent->m_p_prev_or_parent->m_p_next_sibling = p_parent;
    163     }
    164   else
    165     {
    166       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child == p_nd);
    167       _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent == p_parent);
    168 
    169       p_nd->m_p_l_child = p_parent;
    170       p_parent->m_p_prev_or_parent = p_nd;
    171     }
    172 
    173   _GLIBCXX_DEBUG_ASSERT(parent(p_parent) == p_nd);
    174 }
    175 
    176