libstdc++
|
00001 // -*- C++ -*- 00002 00003 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the terms 00008 // of the GNU General Public License as published by the Free Software 00009 // Foundation; either version 3, or (at your option) any later 00010 // version. 00011 00012 // This library is distributed in the hope that it will be useful, but 00013 // WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 // General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 00027 00028 // Permission to use, copy, modify, sell, and distribute this software 00029 // is hereby granted without fee, provided that the above copyright 00030 // notice appears in all copies, and that both that copyright notice 00031 // and this permission notice appear in supporting documentation. None 00032 // of the above authors, nor IBM Haifa Research Laboratories, make any 00033 // representation about the suitability of this software for any 00034 // purpose. It is provided "as is" without express or implied 00035 // warranty. 00036 00037 /** 00038 * @file binary_heap_/constructors_destructor_fn_imps.hpp 00039 * Contains an implementation class for binary_heap_. 00040 */ 00041 00042 PB_DS_CLASS_T_DEC 00043 typename PB_DS_CLASS_C_DEC::entry_allocator 00044 PB_DS_CLASS_C_DEC::s_entry_allocator; 00045 00046 PB_DS_CLASS_T_DEC 00047 typename PB_DS_CLASS_C_DEC::value_allocator 00048 PB_DS_CLASS_C_DEC::s_value_allocator; 00049 00050 PB_DS_CLASS_T_DEC 00051 typename PB_DS_CLASS_C_DEC::no_throw_copies_t 00052 PB_DS_CLASS_C_DEC::s_no_throw_copies_ind; 00053 00054 PB_DS_CLASS_T_DEC 00055 template<typename It> 00056 void 00057 PB_DS_CLASS_C_DEC:: 00058 copy_from_range(It first_it, It last_it) 00059 { 00060 while (first_it != last_it) 00061 { 00062 insert_value(*first_it, s_no_throw_copies_ind); 00063 ++first_it; 00064 } 00065 make_heap(); 00066 PB_DS_ASSERT_VALID((*this)) 00067 } 00068 00069 PB_DS_CLASS_T_DEC 00070 PB_DS_CLASS_C_DEC:: 00071 binary_heap() 00072 : m_size(0), m_actual_size(resize_policy::min_size), 00073 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 00074 { PB_DS_ASSERT_VALID((*this)) } 00075 00076 PB_DS_CLASS_T_DEC 00077 PB_DS_CLASS_C_DEC:: 00078 binary_heap(const Cmp_Fn& r_cmp_fn) 00079 : entry_cmp(r_cmp_fn), m_size(0), m_actual_size(resize_policy::min_size), 00080 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 00081 { PB_DS_ASSERT_VALID((*this)) } 00082 00083 PB_DS_CLASS_T_DEC 00084 PB_DS_CLASS_C_DEC:: 00085 binary_heap(const PB_DS_CLASS_C_DEC& other) 00086 : entry_cmp(other), resize_policy(other), m_size(0), 00087 m_actual_size(other.m_actual_size), 00088 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 00089 { 00090 PB_DS_ASSERT_VALID(other) 00091 _GLIBCXX_DEBUG_ASSERT(m_a_entries != other.m_a_entries); 00092 00093 __try 00094 { 00095 copy_from_range(other.begin(), other.end()); 00096 } 00097 __catch(...) 00098 { 00099 for (size_type i = 0; i < m_size; ++i) 00100 erase_at(m_a_entries, i, s_no_throw_copies_ind); 00101 00102 s_entry_allocator.deallocate(m_a_entries, m_actual_size); 00103 __throw_exception_again; 00104 } 00105 PB_DS_ASSERT_VALID((*this)) 00106 } 00107 00108 PB_DS_CLASS_T_DEC 00109 void 00110 PB_DS_CLASS_C_DEC:: 00111 swap(PB_DS_CLASS_C_DEC& other) 00112 { 00113 PB_DS_ASSERT_VALID((*this)) 00114 PB_DS_ASSERT_VALID(other) 00115 _GLIBCXX_DEBUG_ASSERT(m_a_entries != other.m_a_entries); 00116 value_swap(other); 00117 std::swap((entry_cmp&)(*this), (entry_cmp&)other); 00118 PB_DS_ASSERT_VALID((*this)) 00119 PB_DS_ASSERT_VALID(other) 00120 } 00121 00122 PB_DS_CLASS_T_DEC 00123 void 00124 PB_DS_CLASS_C_DEC:: 00125 value_swap(PB_DS_CLASS_C_DEC& other) 00126 { 00127 std::swap(m_a_entries, other.m_a_entries); 00128 std::swap(m_size, other.m_size); 00129 std::swap(m_actual_size, other.m_actual_size); 00130 static_cast<resize_policy*>(this)->swap(other); 00131 } 00132 00133 PB_DS_CLASS_T_DEC 00134 PB_DS_CLASS_C_DEC:: 00135 ~binary_heap() 00136 { 00137 for (size_type i = 0; i < m_size; ++i) 00138 erase_at(m_a_entries, i, s_no_throw_copies_ind); 00139 s_entry_allocator.deallocate(m_a_entries, m_actual_size); 00140 }