split_join_fn_imps.hpp revision 285830
1234353Sdim// -*- C++ -*- 2210006Srdivacky 3210006Srdivacky// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4210006Srdivacky// 5210006Srdivacky// This file is part of the GNU ISO C++ Library. This library is free 6210006Srdivacky// software; you can redistribute it and/or modify it under the terms 7210006Srdivacky// of the GNU General Public License as published by the Free Software 8210006Srdivacky// Foundation; either version 2, or (at your option) any later 9210006Srdivacky// version. 10218893Sdim 11218893Sdim// This library is distributed in the hope that it will be useful, but 12210006Srdivacky// WITHOUT ANY WARRANTY; without even the implied warranty of 13210006Srdivacky// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14210006Srdivacky// General Public License for more details. 15210006Srdivacky 16210006Srdivacky// You should have received a copy of the GNU General Public License 17210006Srdivacky// along with this library; see the file COPYING. If not, write to 18218893Sdim// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 19280031Sdim// MA 02111-1307, USA. 20276479Sdim 21218893Sdim// As a special exception, you may use this file as part of a free 22210006Srdivacky// software library without restriction. Specifically, if other files 23280031Sdim// instantiate templates or use macros or inline functions from this 24249423Sdim// file, or you compile this file and link it with other files to 25280031Sdim// produce an executable, this file does not by itself cause the 26249423Sdim// resulting executable to be covered by the GNU General Public 27249423Sdim// License. This exception does not however invalidate any other 28249423Sdim// reasons why the executable file might be covered by the GNU General 29249423Sdim// Public License. 30249423Sdim 31226633Sdim// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 32249423Sdim 33249423Sdim// Permission to use, copy, modify, sell, and distribute this software 34249423Sdim// is hereby granted without fee, provided that the above copyright 35249423Sdim// notice appears in all copies, and that both that copyright notice 36249423Sdim// and this permission notice appear in supporting documentation. None 37249423Sdim// of the above authors, nor IBM Haifa Research Laboratories, make any 38249423Sdim// representation about the suitability of this software for any 39234353Sdim// purpose. It is provided "as is" without express or implied 40249423Sdim// warranty. 41249423Sdim 42249423Sdim/** 43249423Sdim * @file split_join_fn_imps.hpp 44249423Sdim * Contains an implementation class for bin_search_tree_. 45249423Sdim */ 46234353Sdim 47249423SdimPB_DS_CLASS_T_DEC 48249423Sdimbool 49210006SrdivackyPB_DS_CLASS_C_DEC:: 50249423Sdimjoin_prep(PB_DS_CLASS_C_DEC& other) 51249423Sdim{ 52249423Sdim _GLIBCXX_DEBUG_ONLY(assert_valid();) 53249423Sdim _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 54249423Sdim if (other.m_size == 0) 55210006Srdivacky return false; 56249423Sdim 57249423Sdim if (m_size == 0) 58210006Srdivacky { 59249423Sdim value_swap(other); 60249423Sdim return false; 61210006Srdivacky } 62249423Sdim 63249423Sdim const bool greater = Cmp_Fn::operator()(PB_DS_V2F(m_p_head->m_p_right->m_value), PB_DS_V2F(other.m_p_head->m_p_left->m_value)); 64210006Srdivacky 65249423Sdim const bool lesser = Cmp_Fn::operator()(PB_DS_V2F(other.m_p_head->m_p_right->m_value), PB_DS_V2F(m_p_head->m_p_left->m_value)); 66249423Sdim 67234353Sdim if (!greater && !lesser) 68249423Sdim __throw_join_error(); 69249423Sdim 70210006Srdivacky if (lesser) 71249423Sdim value_swap(other); 72249423Sdim 73249423Sdim m_size += other.m_size; 74249423Sdim _GLIBCXX_DEBUG_ONLY(map_debug_base::join(other);) 75249423Sdim return true; 76226633Sdim} 77249423Sdim 78249423SdimPB_DS_CLASS_T_DEC 79249423Sdimvoid 80249423SdimPB_DS_CLASS_C_DEC:: 81210006Srdivackyjoin_finish(PB_DS_CLASS_C_DEC& other) 82249423Sdim{ 83249423Sdim initialize_min_max(); 84210006Srdivacky other.initialize(); 85249423Sdim} 86249423Sdim 87249423SdimPB_DS_CLASS_T_DEC 88249423Sdimbool 89210006SrdivackyPB_DS_CLASS_C_DEC:: 90249423Sdimsplit_prep(const_key_reference r_key, PB_DS_CLASS_C_DEC& other) 91280031Sdim{ 92280031Sdim _GLIBCXX_DEBUG_ONLY(assert_valid();) 93280031Sdim _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 94280031Sdim other.clear(); 95280031Sdim 96280031Sdim if (m_size == 0) 97280031Sdim { 98280031Sdim _GLIBCXX_DEBUG_ONLY(assert_valid();) 99280031Sdim _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 100280031Sdim return false; 101280031Sdim } 102280031Sdim 103249423Sdim if (Cmp_Fn::operator()(r_key, PB_DS_V2F(m_p_head->m_p_left->m_value))) 104210006Srdivacky { 105210006Srdivacky value_swap(other); 106210006Srdivacky _GLIBCXX_DEBUG_ONLY(assert_valid();) 107210006Srdivacky _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 108 return false; 109 } 110 111 if (!Cmp_Fn::operator()(r_key, PB_DS_V2F(m_p_head->m_p_right->m_value))) 112 { 113 _GLIBCXX_DEBUG_ONLY(assert_valid();) 114 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 115 return false; 116 } 117 118 if (m_size == 1) 119 { 120 value_swap(other); 121 _GLIBCXX_DEBUG_ONLY(assert_valid();) 122 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 123 return false; 124 } 125 126 _GLIBCXX_DEBUG_ONLY(map_debug_base::split(r_key,(Cmp_Fn& )(*this), other);) 127 return true; 128} 129 130PB_DS_CLASS_T_DEC 131void 132PB_DS_CLASS_C_DEC:: 133split_finish(PB_DS_CLASS_C_DEC& other) 134{ 135 other.initialize_min_max(); 136 other.m_size = std::distance(other.begin(), other.end()); 137 m_size -= other.m_size; 138 initialize_min_max(); 139 _GLIBCXX_DEBUG_ONLY(assert_valid();) 140 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 141} 142 143PB_DS_CLASS_T_DEC 144typename PB_DS_CLASS_C_DEC::size_type 145PB_DS_CLASS_C_DEC:: 146recursive_count(node_pointer p) const 147{ 148 if (p == NULL) 149 return 0; 150 return 1 + recursive_count(p->m_p_left) + recursive_count(p->m_p_right); 151} 152 153