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 constructors_destructor_fn_imps.hpp 44169691Skan * Contains an implementation class for bin_search_tree_. 45169691Skan */ 46169691Skan 47169691SkanPB_DS_CLASS_T_DEC 48169691Skantypename PB_DS_CLASS_C_DEC::node_allocator 49169691SkanPB_DS_CLASS_C_DEC::s_node_allocator; 50169691Skan 51169691SkanPB_DS_CLASS_T_DEC 52169691SkanPB_DS_CLASS_C_DEC:: 53169691SkanPB_DS_CLASS_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0) 54169691Skan{ 55169691Skan initialize(); 56169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 57169691Skan} 58169691Skan 59169691SkanPB_DS_CLASS_T_DEC 60169691SkanPB_DS_CLASS_C_DEC:: 61169691SkanPB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn) : 62169691Skan Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0) 63169691Skan{ 64169691Skan initialize(); 65169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 66169691Skan} 67169691Skan 68169691SkanPB_DS_CLASS_T_DEC 69169691SkanPB_DS_CLASS_C_DEC:: 70169691SkanPB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) : 71169691Skan Cmp_Fn(r_cmp_fn), 72169691Skan node_update(r_node_update), 73169691Skan m_p_head(s_node_allocator.allocate(1)), 74169691Skan m_size(0) 75169691Skan{ 76169691Skan initialize(); 77169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 78169691Skan} 79169691Skan 80169691SkanPB_DS_CLASS_T_DEC 81169691SkanPB_DS_CLASS_C_DEC:: 82169691SkanPB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) : 83169691Skan#ifdef _GLIBCXX_DEBUG 84169691Skan map_debug_base(other), 85169691Skan#endif 86169691Skan#ifdef PB_DS_TREE_TRACE 87169691Skan PB_DS_TREE_TRACE_BASE_C_DEC(other), 88169691Skan#endif 89169691Skan Cmp_Fn(other), 90169691Skan node_update(other), 91169691Skan m_p_head(s_node_allocator.allocate(1)), 92169691Skan m_size(0) 93169691Skan{ 94169691Skan initialize(); 95169691Skan m_size = other.m_size; 96169691Skan _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();) 97169691Skan 98169691Skan try 99169691Skan { 100169691Skan m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent); 101169691Skan if (m_p_head->m_p_parent != NULL) 102169691Skan m_p_head->m_p_parent->m_p_parent = m_p_head; 103169691Skan m_size = other.m_size; 104169691Skan initialize_min_max(); 105169691Skan } 106169691Skan catch(...) 107169691Skan { 108169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::clear();) 109169691Skan s_node_allocator.deallocate(m_p_head, 1); 110169691Skan __throw_exception_again; 111169691Skan } 112169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 113169691Skan} 114169691Skan 115169691SkanPB_DS_CLASS_T_DEC 116169691Skanvoid 117169691SkanPB_DS_CLASS_C_DEC:: 118169691Skanswap(PB_DS_CLASS_C_DEC& other) 119169691Skan{ 120169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 121169691Skan _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();) 122169691Skan value_swap(other); 123169691Skan std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other); 124169691Skan _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();) 125169691Skan _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();) 126169691Skan} 127169691Skan 128169691SkanPB_DS_CLASS_T_DEC 129169691Skanvoid 130169691SkanPB_DS_CLASS_C_DEC:: 131169691Skanvalue_swap(PB_DS_CLASS_C_DEC& other) 132169691Skan{ 133169691Skan _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other);) 134169691Skan std::swap(m_p_head, other.m_p_head); 135169691Skan std::swap(m_size, other.m_size); 136169691Skan} 137169691Skan 138169691SkanPB_DS_CLASS_T_DEC 139169691SkanPB_DS_CLASS_C_DEC:: 140169691Skan~PB_DS_CLASS_NAME() 141169691Skan{ 142169691Skan clear(); 143169691Skan s_node_allocator.deallocate(m_p_head, 1); 144169691Skan} 145169691Skan 146169691SkanPB_DS_CLASS_T_DEC 147169691Skanvoid 148169691SkanPB_DS_CLASS_C_DEC:: 149169691Skaninitialize() 150169691Skan{ 151169691Skan m_p_head->m_p_parent = NULL; 152169691Skan m_p_head->m_p_left = m_p_head; 153169691Skan m_p_head->m_p_right = m_p_head; 154169691Skan m_size = 0; 155169691Skan} 156169691Skan 157169691SkanPB_DS_CLASS_T_DEC 158169691Skantypename PB_DS_CLASS_C_DEC::node_pointer 159169691SkanPB_DS_CLASS_C_DEC:: 160169691Skanrecursive_copy_node(const node_pointer p_nd) 161169691Skan{ 162169691Skan if (p_nd == NULL) 163169691Skan return (NULL); 164169691Skan 165169691Skan node_pointer p_ret = s_node_allocator.allocate(1); 166169691Skan try 167169691Skan { 168169691Skan new (p_ret) node(*p_nd); 169169691Skan } 170169691Skan catch(...) 171169691Skan { 172169691Skan s_node_allocator.deallocate(p_ret, 1); 173169691Skan __throw_exception_again; 174169691Skan } 175169691Skan 176169691Skan p_ret->m_p_left = p_ret->m_p_right = NULL; 177169691Skan 178169691Skan try 179169691Skan { 180169691Skan p_ret->m_p_left = recursive_copy_node(p_nd->m_p_left); 181169691Skan p_ret->m_p_right = recursive_copy_node(p_nd->m_p_right); 182169691Skan } 183169691Skan catch(...) 184169691Skan { 185169691Skan clear_imp(p_ret); 186169691Skan __throw_exception_again; 187169691Skan } 188169691Skan 189169691Skan if (p_ret->m_p_left != NULL) 190169691Skan p_ret->m_p_left->m_p_parent = p_ret; 191169691Skan 192169691Skan if (p_ret->m_p_right != NULL) 193169691Skan p_ret->m_p_right->m_p_parent = p_ret; 194169691Skan 195169691Skan _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret);) 196169691Skan return p_ret; 197169691Skan} 198169691Skan 199169691SkanPB_DS_CLASS_T_DEC 200169691Skanvoid 201169691SkanPB_DS_CLASS_C_DEC:: 202169691Skaninitialize_min_max() 203169691Skan{ 204169691Skan if (m_p_head->m_p_parent == NULL) 205169691Skan { 206169691Skan m_p_head->m_p_left = m_p_head->m_p_right = m_p_head; 207169691Skan return; 208169691Skan } 209169691Skan 210169691Skan { 211169691Skan node_pointer p_min = m_p_head->m_p_parent; 212169691Skan while (p_min->m_p_left != NULL) 213169691Skan p_min = p_min->m_p_left; 214169691Skan m_p_head->m_p_left = p_min; 215169691Skan } 216169691Skan 217169691Skan { 218169691Skan node_pointer p_max = m_p_head->m_p_parent; 219169691Skan while (p_max->m_p_right != NULL) 220169691Skan p_max = p_max->m_p_right; 221169691Skan m_p_head->m_p_right = p_max; 222169691Skan } 223169691Skan} 224169691Skan 225