constructor_destructor_fn_imps.hpp revision 169691
1204076Spjd// -*- C++ -*- 2204076Spjd 3204076Spjd// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4219351Spjd// 5204076Spjd// This file is part of the GNU ISO C++ Library. This library is free 6204076Spjd// software; you can redistribute it and/or modify it under the terms 7204076Spjd// of the GNU General Public License as published by the Free Software 8204076Spjd// Foundation; either version 2, or (at your option) any later 9204076Spjd// version. 10204076Spjd 11204076Spjd// This library is distributed in the hope that it will be useful, but 12204076Spjd// WITHOUT ANY WARRANTY; without even the implied warranty of 13204076Spjd// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14204076Spjd// General Public License for more details. 15204076Spjd 16204076Spjd// You should have received a copy of the GNU General Public License 17204076Spjd// along with this library; see the file COPYING. If not, write to 18204076Spjd// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 19204076Spjd// MA 02111-1307, USA. 20204076Spjd 21204076Spjd// As a special exception, you may use this file as part of a free 22204076Spjd// software library without restriction. Specifically, if other files 23204076Spjd// instantiate templates or use macros or inline functions from this 24204076Spjd// file, or you compile this file and link it with other files to 25204076Spjd// produce an executable, this file does not by itself cause the 26204076Spjd// resulting executable to be covered by the GNU General Public 27204076Spjd// License. This exception does not however invalidate any other 28204076Spjd// reasons why the executable file might be covered by the GNU General 29204076Spjd// Public License. 30204076Spjd 31204076Spjd// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 32204076Spjd 33204076Spjd// Permission to use, copy, modify, sell, and distribute this software 34204076Spjd// is hereby granted without fee, provided that the above copyright 35204076Spjd// notice appears in all copies, and that both that copyright notice 36204076Spjd// and this permission notice appear in supporting documentation. None 37204076Spjd// of the above authors, nor IBM Haifa Research Laboratories, make any 38204076Spjd// representation about the suitability of this software for any 39204076Spjd// purpose. It is provided "as is" without express or implied 40204076Spjd// warranty. 41204076Spjd 42204076Spjd/** 43204076Spjd * @file constructor_destructor_fn_imps.hpp 44204076Spjd * Contains implementations of gp_ht_map_'s constructors, destructor, 45204076Spjd * and related functions. 46204076Spjd */ 47228696Spjd 48228696SpjdPB_DS_CLASS_T_DEC 49228696Spjdtypename PB_DS_CLASS_C_DEC::entry_allocator 50204076SpjdPB_DS_CLASS_C_DEC::s_entry_allocator; 51204076Spjd 52226463SpjdPB_DS_CLASS_T_DEC 53204076Spjdtemplate<typename It> 54204076Spjdvoid 55204076SpjdPB_DS_CLASS_C_DEC:: 56219351Spjdcopy_from_range(It first_it, It last_it) 57219354Spjd{ 58207371Spjd while (first_it != last_it) 59211886Spjd insert(*(first_it++)); 60225830Spjd} 61204076Spjd 62204076SpjdPB_DS_CLASS_T_DEC 63204076SpjdPB_DS_CLASS_C_DEC:: 64204076SpjdPB_DS_CLASS_NAME() 65219818Spjd: ranged_probe_fn_base(resize_base::get_nearest_larger_size(1)), 66204076Spjd m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 67225830Spjd m_entries(s_entry_allocator.allocate(m_num_e)) 68204076Spjd{ 69204076Spjd initialize(); 70204076Spjd _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 71219351Spjd} 72219351Spjd 73219351SpjdPB_DS_CLASS_T_DEC 74219354SpjdPB_DS_CLASS_C_DEC:: 75219354SpjdPB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn) 76204076Spjd: ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn), 77222117Spjd m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 78204076Spjd m_entries(s_entry_allocator.allocate(m_num_e)) 79204076Spjd{ 80204076Spjd initialize(); 81204076Spjd _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 82204076Spjd} 83204076Spjd 84PB_DS_CLASS_T_DEC 85PB_DS_CLASS_C_DEC:: 86PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn) 87: hash_eq_fn_base(r_eq_fn), 88 ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn), 89 m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 90 m_entries(s_entry_allocator.allocate(m_num_e)) 91{ 92 initialize(); 93 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 94} 95 96PB_DS_CLASS_T_DEC 97PB_DS_CLASS_C_DEC:: 98PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 99 const Comb_Probe_Fn& r_comb_hash_fn) 100: hash_eq_fn_base(r_eq_fn), 101 ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), 102 r_hash_fn, r_comb_hash_fn), 103 m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 104 m_entries(s_entry_allocator.allocate(m_num_e)) 105{ 106 initialize(); 107 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 108} 109 110PB_DS_CLASS_T_DEC 111PB_DS_CLASS_C_DEC:: 112PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 113 const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober) 114: hash_eq_fn_base(r_eq_fn), 115 ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), 116 r_hash_fn, comb_hash_fn, prober), 117 m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 118 m_entries(s_entry_allocator.allocate(m_num_e)) 119{ 120 initialize(); 121 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 122} 123 124PB_DS_CLASS_T_DEC 125PB_DS_CLASS_C_DEC:: 126PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 127 const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober, 128 const Resize_Policy& r_resize_policy) 129: hash_eq_fn_base(r_eq_fn), resize_base(r_resize_policy), 130 ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), 131 r_hash_fn, comb_hash_fn, prober), 132 m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0), 133 m_entries(s_entry_allocator.allocate(m_num_e)) 134{ 135 initialize(); 136 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 137} 138 139PB_DS_CLASS_T_DEC 140PB_DS_CLASS_C_DEC:: 141PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) : 142#ifdef _GLIBCXX_DEBUG 143 map_debug_base(other), 144#endif 145 hash_eq_fn_base(other), 146 resize_base(other), 147 ranged_probe_fn_base(other), 148 m_num_e(other.m_num_e), 149 m_num_used_e(other.m_num_used_e), 150 m_entries(s_entry_allocator.allocate(m_num_e)) 151{ 152 for (size_type i = 0; i < m_num_e; ++i) 153 m_entries[i].m_stat = (entry_status)empty_entry_status; 154 155 try 156 { 157 for (size_type i = 0; i < m_num_e; ++i) 158 { 159 m_entries[i].m_stat = other.m_entries[i].m_stat; 160 if (m_entries[i].m_stat == valid_entry_status) 161 new (m_entries + i) entry(other.m_entries[i]); 162 } 163 } 164 catch(...) 165 { 166 deallocate_all(); 167 __throw_exception_again; 168 } 169 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) 170} 171 172PB_DS_CLASS_T_DEC 173PB_DS_CLASS_C_DEC:: 174~PB_DS_CLASS_NAME() 175{ deallocate_all(); } 176 177PB_DS_CLASS_T_DEC 178void 179PB_DS_CLASS_C_DEC:: 180swap(PB_DS_CLASS_C_DEC& other) 181{ 182 _GLIBCXX_DEBUG_ONLY(assert_valid()); 183 _GLIBCXX_DEBUG_ONLY(other.assert_valid()); 184 std::swap(m_num_e, other.m_num_e); 185 std::swap(m_num_used_e, other.m_num_used_e); 186 std::swap(m_entries, other.m_entries); 187 ranged_probe_fn_base::swap(other); 188 hash_eq_fn_base::swap(other); 189 resize_base::swap(other); 190 _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other)); 191 _GLIBCXX_DEBUG_ONLY(assert_valid()); 192 _GLIBCXX_DEBUG_ONLY(other.assert_valid()); 193} 194 195PB_DS_CLASS_T_DEC 196void 197PB_DS_CLASS_C_DEC:: 198deallocate_all() 199{ 200 clear(); 201 erase_all_valid_entries(m_entries, m_num_e); 202 s_entry_allocator.deallocate(m_entries, m_num_e); 203} 204 205PB_DS_CLASS_T_DEC 206void 207PB_DS_CLASS_C_DEC:: 208erase_all_valid_entries(entry_array a_entries_resized, size_type len) 209{ 210 for (size_type pos = 0; pos < len; ++pos) 211 { 212 entry_pointer p_e = &a_entries_resized[pos]; 213 if (p_e->m_stat == valid_entry_status) 214 p_e->m_value.~value_type(); 215 } 216} 217 218PB_DS_CLASS_T_DEC 219void 220PB_DS_CLASS_C_DEC:: 221initialize() 222{ 223 Resize_Policy::notify_resized(m_num_e); 224 Resize_Policy::notify_cleared(); 225 ranged_probe_fn_base::notify_resized(m_num_e); 226 for (size_type i = 0; i < m_num_e; ++i) 227 m_entries[i].m_stat = empty_entry_status; 228} 229 230