insert_fn_imps.hpp revision 169691
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 bin_search_tree_. 45169691Skan */ 46169691Skan 47169691SkanPB_DS_CLASS_T_DEC 48169691Skaninline std::pair<typename PB_DS_CLASS_C_DEC::point_iterator, bool> 49169691SkanPB_DS_CLASS_C_DEC:: 50169691Skaninsert_leaf(const_reference r_value) 51169691Skan{ 52169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 53169691Skan 54169691Skan if (m_size == 0) 55169691Skan return (std::make_pair( 56169691Skan insert_imp_empty(r_value), 57169691Skan true)); 58169691Skan 59169691Skan node_pointer p_nd = m_p_head->m_p_parent; 60169691Skan node_pointer p_pot = m_p_head; 61169691Skan 62169691Skan while (p_nd != NULL) 63169691Skan if (!Cmp_Fn::operator()( 64169691Skan PB_DS_V2F(p_nd->m_value), 65169691Skan PB_DS_V2F(r_value))) 66169691Skan { 67169691Skan p_pot = p_nd; 68169691Skan 69169691Skan p_nd = p_nd->m_p_left; 70169691Skan } 71169691Skan else 72169691Skan p_nd = p_nd->m_p_right; 73169691Skan 74169691Skan if (p_pot == m_p_head) 75169691Skan return (std::make_pair( 76169691Skan insert_leaf_new(r_value, m_p_head->m_p_right, false), 77169691Skan true)); 78169691Skan 79169691Skan if (!Cmp_Fn::operator()( 80169691Skan PB_DS_V2F(r_value), 81169691Skan PB_DS_V2F(p_pot->m_value))) 82169691Skan { 83169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 84169691Skan 85169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists( 86169691Skan PB_DS_V2F(r_value))); 87169691Skan 88169691Skan return (std::make_pair(p_pot, false)); 89169691Skan } 90169691Skan 91169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist( 92169691Skan PB_DS_V2F(r_value))); 93169691Skan 94169691Skan p_nd = p_pot->m_p_left; 95169691Skan if (p_nd == NULL) 96169691Skan return (std::make_pair( 97169691Skan insert_leaf_new(r_value, p_pot, true), 98169691Skan true)); 99169691Skan 100169691Skan while (p_nd->m_p_right != NULL) 101169691Skan p_nd = p_nd->m_p_right; 102169691Skan 103169691Skan return (std::make_pair( 104169691Skan insert_leaf_new(r_value, p_nd, false), 105169691Skan true)); 106169691Skan} 107169691Skan 108169691SkanPB_DS_CLASS_T_DEC 109169691Skaninline typename PB_DS_CLASS_C_DEC::iterator 110169691SkanPB_DS_CLASS_C_DEC:: 111169691Skaninsert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd) 112169691Skan{ 113169691Skan node_pointer p_new_nd = 114169691Skan get_new_node_for_leaf_insert( r_value, traits_base::m_no_throw_copies_indicator); 115169691Skan 116169691Skan if (left_nd) 117169691Skan { 118169691Skan _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == NULL); 119169691Skan _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()( 120169691Skan PB_DS_V2F(r_value), 121169691Skan PB_DS_V2F(p_nd->m_value))); 122169691Skan 123169691Skan p_nd->m_p_left = p_new_nd; 124169691Skan 125169691Skan if (m_p_head->m_p_left == p_nd) 126169691Skan m_p_head->m_p_left = p_new_nd; 127169691Skan } 128169691Skan else 129169691Skan { 130169691Skan _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_right == NULL); 131169691Skan _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()( 132169691Skan PB_DS_V2F(p_nd->m_value), 133169691Skan PB_DS_V2F(r_value))); 134169691Skan 135169691Skan p_nd->m_p_right = p_new_nd; 136169691Skan 137169691Skan if (m_p_head->m_p_right == p_nd) 138169691Skan m_p_head->m_p_right = p_new_nd; 139169691Skan } 140169691Skan 141169691Skan p_new_nd->m_p_parent = p_nd; 142169691Skan 143169691Skan p_new_nd->m_p_left = p_new_nd->m_p_right = NULL; 144169691Skan 145169691Skan _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_nd)); 146169691Skan 147169691Skan update_to_top(p_new_nd, (node_update* )this); 148169691Skan 149169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new( 150169691Skan PB_DS_V2F(r_value))); 151169691Skan 152169691Skan return (iterator(p_new_nd)); 153169691Skan} 154169691Skan 155169691SkanPB_DS_CLASS_T_DEC 156169691Skaninline typename PB_DS_CLASS_C_DEC::iterator 157169691SkanPB_DS_CLASS_C_DEC:: 158169691Skaninsert_imp_empty(const_reference r_value) 159169691Skan{ 160169691Skan node_pointer p_new_node = 161169691Skan get_new_node_for_leaf_insert( r_value, traits_base::m_no_throw_copies_indicator); 162169691Skan 163169691Skan m_p_head->m_p_left = m_p_head->m_p_right = 164169691Skan m_p_head->m_p_parent = p_new_node; 165169691Skan 166169691Skan p_new_node->m_p_parent = m_p_head; 167169691Skan 168169691Skan p_new_node->m_p_left = p_new_node->m_p_right = NULL; 169169691Skan 170169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new( 171169691Skan PB_DS_V2F(r_value))); 172169691Skan 173169691Skan update_to_top(m_p_head->m_p_parent, (node_update* )this); 174169691Skan 175169691Skan return (iterator(p_new_node)); 176169691Skan} 177169691Skan 178169691SkanPB_DS_CLASS_T_DEC 179169691Skaninline typename PB_DS_CLASS_C_DEC::node_pointer 180169691SkanPB_DS_CLASS_C_DEC:: 181169691Skanget_new_node_for_leaf_insert(const_reference r_val, false_type) 182169691Skan{ 183169691Skan node_pointer p_new_nd = s_node_allocator.allocate(1); 184169691Skan 185169691Skan cond_dealtor_t cond(p_new_nd); 186169691Skan 187169691Skan new (const_cast<void* >( 188169691Skan static_cast<const void* >(&p_new_nd->m_value))) 189169691Skan typename node::value_type(r_val); 190169691Skan 191169691Skan cond.set_no_action(); 192169691Skan 193169691Skan p_new_nd->m_p_left = p_new_nd->m_p_right = NULL; 194169691Skan 195169691Skan ++m_size; 196169691Skan 197169691Skan return (p_new_nd); 198169691Skan} 199169691Skan 200169691SkanPB_DS_CLASS_T_DEC 201169691Skaninline typename PB_DS_CLASS_C_DEC::node_pointer 202169691SkanPB_DS_CLASS_C_DEC:: 203169691Skanget_new_node_for_leaf_insert(const_reference r_val, true_type) 204169691Skan{ 205169691Skan node_pointer p_new_nd = s_node_allocator.allocate(1); 206169691Skan 207169691Skan new (const_cast<void* >( 208169691Skan static_cast<const void* >(&p_new_nd->m_value))) 209169691Skan typename node::value_type(r_val); 210169691Skan 211169691Skan p_new_nd->m_p_left = p_new_nd->m_p_right = NULL; 212169691Skan 213169691Skan ++m_size; 214169691Skan 215169691Skan return (p_new_nd); 216169691Skan} 217169691Skan 218