libstdc++
gp_hash_table_map_/constructor_destructor_fn_imps.hpp
Go to the documentation of this file.
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 gp_hash_table_map_/constructor_destructor_fn_imps.hpp
00039  * Contains implementations of gp_ht_map_'s constructors, destructor,
00040  * and related functions.
00041  */
00042 
00043 PB_DS_CLASS_T_DEC
00044 typename PB_DS_CLASS_C_DEC::entry_allocator
00045 PB_DS_CLASS_C_DEC::s_entry_allocator;
00046 
00047 PB_DS_CLASS_T_DEC
00048 template<typename It>
00049 void
00050 PB_DS_CLASS_C_DEC::
00051 copy_from_range(It first_it, It last_it)
00052 {
00053   while (first_it != last_it)
00054     insert(*(first_it++));
00055 }
00056 
00057 PB_DS_CLASS_T_DEC
00058 PB_DS_CLASS_C_DEC::
00059 PB_DS_GP_HASH_NAME() 
00060 : ranged_probe_fn_base(resize_base::get_nearest_larger_size(1)),
00061   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00062   m_entries(s_entry_allocator.allocate(m_num_e))
00063 {
00064   initialize();
00065   PB_DS_ASSERT_VALID((*this))
00066 }
00067 
00068 PB_DS_CLASS_T_DEC
00069 PB_DS_CLASS_C_DEC::
00070 PB_DS_GP_HASH_NAME(const Hash_Fn& r_hash_fn)    
00071 : ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
00072   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00073   m_entries(s_entry_allocator.allocate(m_num_e))
00074 {
00075   initialize();
00076   PB_DS_ASSERT_VALID((*this))
00077 }
00078 
00079 PB_DS_CLASS_T_DEC
00080 PB_DS_CLASS_C_DEC::
00081 PB_DS_GP_HASH_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn) 
00082 : hash_eq_fn_base(r_eq_fn),
00083   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
00084   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00085   m_entries(s_entry_allocator.allocate(m_num_e))
00086 {
00087   initialize();
00088   PB_DS_ASSERT_VALID((*this))
00089 }
00090 
00091 PB_DS_CLASS_T_DEC
00092 PB_DS_CLASS_C_DEC::
00093 PB_DS_GP_HASH_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 
00094          const Comb_Probe_Fn& r_comb_hash_fn) 
00095 : hash_eq_fn_base(r_eq_fn),
00096   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
00097                r_hash_fn, r_comb_hash_fn),
00098   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00099   m_entries(s_entry_allocator.allocate(m_num_e))
00100 {
00101   initialize();
00102   PB_DS_ASSERT_VALID((*this))
00103 }
00104 
00105 PB_DS_CLASS_T_DEC
00106 PB_DS_CLASS_C_DEC::
00107 PB_DS_GP_HASH_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 
00108          const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober) 
00109 : hash_eq_fn_base(r_eq_fn),
00110   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
00111                r_hash_fn, comb_hash_fn, prober),
00112   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00113   m_entries(s_entry_allocator.allocate(m_num_e))
00114 {
00115   initialize();
00116   PB_DS_ASSERT_VALID((*this))
00117 }
00118 
00119 PB_DS_CLASS_T_DEC
00120 PB_DS_CLASS_C_DEC::
00121 PB_DS_GP_HASH_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, 
00122          const Comb_Probe_Fn& comb_hash_fn, const Probe_Fn& prober, 
00123          const Resize_Policy& r_resize_policy) 
00124 : hash_eq_fn_base(r_eq_fn), resize_base(r_resize_policy),
00125   ranged_probe_fn_base(resize_base::get_nearest_larger_size(1),
00126                r_hash_fn, comb_hash_fn, prober),
00127   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
00128   m_entries(s_entry_allocator.allocate(m_num_e))
00129 {
00130   initialize();
00131   PB_DS_ASSERT_VALID((*this))
00132 }
00133 
00134 PB_DS_CLASS_T_DEC
00135 PB_DS_CLASS_C_DEC::
00136 PB_DS_GP_HASH_NAME(const PB_DS_CLASS_C_DEC& other) :
00137 #ifdef _GLIBCXX_DEBUG
00138   debug_base(other),
00139 #endif 
00140   hash_eq_fn_base(other),
00141   resize_base(other),
00142   ranged_probe_fn_base(other),
00143   m_num_e(other.m_num_e),
00144   m_num_used_e(other.m_num_used_e),
00145   m_entries(s_entry_allocator.allocate(m_num_e))
00146 {
00147   for (size_type i = 0; i < m_num_e; ++i)
00148     m_entries[i].m_stat = (entry_status)empty_entry_status;
00149 
00150   __try
00151     {
00152       for (size_type i = 0; i < m_num_e; ++i)
00153         {
00154       m_entries[i].m_stat = other.m_entries[i].m_stat;
00155       if (m_entries[i].m_stat == valid_entry_status)
00156         new (m_entries + i) entry(other.m_entries[i]);
00157         }
00158     }
00159   __catch(...)
00160     {
00161       deallocate_all();
00162       __throw_exception_again;
00163     }
00164   PB_DS_ASSERT_VALID((*this))
00165 }
00166 
00167 PB_DS_CLASS_T_DEC
00168 PB_DS_CLASS_C_DEC::
00169 ~PB_DS_GP_HASH_NAME()
00170 { deallocate_all(); }
00171 
00172 PB_DS_CLASS_T_DEC
00173 void
00174 PB_DS_CLASS_C_DEC::
00175 swap(PB_DS_CLASS_C_DEC& other)
00176 {
00177   PB_DS_ASSERT_VALID((*this))
00178   PB_DS_ASSERT_VALID(other)
00179   std::swap(m_num_e, other.m_num_e);
00180   std::swap(m_num_used_e, other.m_num_used_e);
00181   std::swap(m_entries, other.m_entries);
00182   ranged_probe_fn_base::swap(other);
00183   hash_eq_fn_base::swap(other);
00184   resize_base::swap(other);
00185   _GLIBCXX_DEBUG_ONLY(debug_base::swap(other));
00186   PB_DS_ASSERT_VALID((*this))
00187   PB_DS_ASSERT_VALID(other)
00188 }
00189 
00190 PB_DS_CLASS_T_DEC
00191 void
00192 PB_DS_CLASS_C_DEC::
00193 deallocate_all()
00194 {
00195   clear();
00196   erase_all_valid_entries(m_entries, m_num_e);
00197   s_entry_allocator.deallocate(m_entries, m_num_e);
00198 }
00199 
00200 PB_DS_CLASS_T_DEC
00201 void
00202 PB_DS_CLASS_C_DEC::
00203 erase_all_valid_entries(entry_array a_entries_resized, size_type len)
00204 {
00205   for (size_type pos = 0; pos < len; ++pos)
00206     {
00207       entry_pointer p_e = &a_entries_resized[pos];
00208       if (p_e->m_stat == valid_entry_status)
00209     p_e->m_value.~value_type();
00210     }
00211 }
00212 
00213 PB_DS_CLASS_T_DEC
00214 void
00215 PB_DS_CLASS_C_DEC::
00216 initialize()
00217 {
00218   Resize_Policy::notify_resized(m_num_e);
00219   Resize_Policy::notify_cleared();
00220   ranged_probe_fn_base::notify_resized(m_num_e);
00221   for (size_type i = 0; i < m_num_e; ++i)
00222     m_entries[i].m_stat = empty_entry_status;
00223 }
00224