1 // -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the terms 8 // of the GNU General Public License as published by the Free Software 9 // Foundation; either version 3, or (at your option) any later 10 // version. 11 12 // This library is distributed in the hope that it will be useful, but 13 // WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 // General Public License for more details. 16 17 // Under Section 7 of GPL version 3, you are granted additional 18 // permissions described in the GCC Runtime Library Exception, version 19 // 3.1, as published by the Free Software Foundation. 20 21 // You should have received a copy of the GNU General Public License and 22 // a copy of the GCC Runtime Library Exception along with this program; 23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 // <http://www.gnu.org/licenses/>. 25 26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 27 28 // Permission to use, copy, modify, sell, and distribute this software 29 // is hereby granted without fee, provided that the above copyright 30 // notice appears in all copies, and that both that copyright notice 31 // and this permission notice appear in supporting documentation. None 32 // of the above authors, nor IBM Haifa Research Laboratories, make any 33 // representation about the suitability of this software for any 34 // purpose. It is provided "as is" without express or implied 35 // warranty. 36 37 /** 38 * @file binary_heap_/constructors_destructor_fn_imps.hpp 39 * Contains an implementation class for binary_heap_. 40 */ 41 42 PB_DS_CLASS_T_DEC 43 typename PB_DS_CLASS_C_DEC::entry_allocator 44 PB_DS_CLASS_C_DEC::s_entry_allocator; 45 46 PB_DS_CLASS_T_DEC 47 typename PB_DS_CLASS_C_DEC::value_allocator 48 PB_DS_CLASS_C_DEC::s_value_allocator; 49 50 PB_DS_CLASS_T_DEC 51 typename PB_DS_CLASS_C_DEC::no_throw_copies_t 52 PB_DS_CLASS_C_DEC::s_no_throw_copies_ind; 53 54 PB_DS_CLASS_T_DEC 55 template<typename It> 56 void 57 PB_DS_CLASS_C_DEC:: 58 copy_from_range(It first_it, It last_it) 59 { 60 while (first_it != last_it) 61 { 62 insert_value(*first_it, s_no_throw_copies_ind); 63 ++first_it; 64 } 65 make_heap(); 66 PB_DS_ASSERT_VALID((*this)) 67 } 68 69 PB_DS_CLASS_T_DEC 70 PB_DS_CLASS_C_DEC:: 71 binary_heap() 72 : m_size(0), m_actual_size(resize_policy::min_size), 73 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 74 { PB_DS_ASSERT_VALID((*this)) } 75 76 PB_DS_CLASS_T_DEC 77 PB_DS_CLASS_C_DEC:: 78 binary_heap(const Cmp_Fn& r_cmp_fn) 79 : entry_cmp(r_cmp_fn), m_size(0), m_actual_size(resize_policy::min_size), 80 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 81 { PB_DS_ASSERT_VALID((*this)) } 82 83 PB_DS_CLASS_T_DEC 84 PB_DS_CLASS_C_DEC:: 85 binary_heap(const PB_DS_CLASS_C_DEC& other) 86 : entry_cmp(other), resize_policy(other), m_size(0), 87 m_actual_size(other.m_actual_size), 88 m_a_entries(s_entry_allocator.allocate(m_actual_size)) 89 { 90 PB_DS_ASSERT_VALID(other) 91 _GLIBCXX_DEBUG_ASSERT(m_a_entries != other.m_a_entries); 92 93 __try 94 { 95 copy_from_range(other.begin(), other.end()); 96 } 97 __catch(...) 98 { 99 for (size_type i = 0; i < m_size; ++i) 100 erase_at(m_a_entries, i, s_no_throw_copies_ind); 101 102 s_entry_allocator.deallocate(m_a_entries, m_actual_size); 103 __throw_exception_again; 104 } 105 PB_DS_ASSERT_VALID((*this)) 106 } 107 108 PB_DS_CLASS_T_DEC 109 void 110 PB_DS_CLASS_C_DEC:: 111 swap(PB_DS_CLASS_C_DEC& other) 112 { 113 PB_DS_ASSERT_VALID((*this)) 114 PB_DS_ASSERT_VALID(other) 115 _GLIBCXX_DEBUG_ASSERT(m_a_entries != other.m_a_entries); 116 value_swap(other); 117 std::swap((entry_cmp&)(*this), (entry_cmp&)other); 118 PB_DS_ASSERT_VALID((*this)) 119 PB_DS_ASSERT_VALID(other) 120 } 121 122 PB_DS_CLASS_T_DEC 123 void 124 PB_DS_CLASS_C_DEC:: 125 value_swap(PB_DS_CLASS_C_DEC& other) 126 { 127 std::swap(m_a_entries, other.m_a_entries); 128 std::swap(m_size, other.m_size); 129 std::swap(m_actual_size, other.m_actual_size); 130 static_cast<resize_policy*>(this)->swap(other); 131 } 132 133 PB_DS_CLASS_T_DEC 134 PB_DS_CLASS_C_DEC:: 135 ~binary_heap() 136 { 137 for (size_type i = 0; i < m_size; ++i) 138 erase_at(m_a_entries, i, s_no_throw_copies_ind); 139 s_entry_allocator.deallocate(m_a_entries, m_actual_size); 140 } 141