Home | History | Annotate | Download | only in cc_hash_table_map_
      1 // -*- C++ -*-
      2 
      3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
      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 constructor_destructor_fn_imps.hpp
     39  * Contains implementations of cc_ht_map_'s constructors, destructor,
     40  *    and related functions.
     41  */
     42 
     43 PB_DS_CLASS_T_DEC
     44 typename PB_DS_CLASS_C_DEC::entry_allocator
     45 PB_DS_CLASS_C_DEC::s_entry_allocator;
     46 
     47 PB_DS_CLASS_T_DEC
     48 typename PB_DS_CLASS_C_DEC::entry_pointer_allocator
     49 PB_DS_CLASS_C_DEC::s_entry_pointer_allocator;
     50 
     51 PB_DS_CLASS_T_DEC
     52 template<typename It>
     53 void
     54 PB_DS_CLASS_C_DEC::
     55 copy_from_range(It first_it, It last_it)
     56 {
     57   while (first_it != last_it)
     58     insert(*(first_it++));
     59 }
     60 
     61 PB_DS_CLASS_T_DEC
     62 PB_DS_CLASS_C_DEC::
     63 PB_DS_CLASS_NAME() :
     64   ranged_hash_fn_base(resize_base::get_nearest_larger_size(1)),
     65   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
     66   m_entries(s_entry_pointer_allocator.allocate(m_num_e))
     67 {
     68   initialize();
     69   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
     70 }
     71 
     72 PB_DS_CLASS_T_DEC
     73 PB_DS_CLASS_C_DEC::
     74 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn) :
     75   ranged_hash_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
     76   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
     77   m_entries(s_entry_pointer_allocator.allocate(m_num_e))
     78 {
     79   initialize();
     80   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
     81 }
     82 
     83 PB_DS_CLASS_T_DEC
     84 PB_DS_CLASS_C_DEC::
     85 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn) :
     86   PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
     87   ranged_hash_fn_base(resize_base::get_nearest_larger_size(1), r_hash_fn),
     88   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
     89   m_entries(s_entry_pointer_allocator.allocate(m_num_e))
     90 {
     91   std::fill(m_entries, m_entries + m_num_e, (entry_pointer)0);
     92   Resize_Policy::notify_cleared();
     93   ranged_hash_fn_base::notify_resized(m_num_e);
     94   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
     95 }
     96 
     97 PB_DS_CLASS_T_DEC
     98 PB_DS_CLASS_C_DEC::
     99 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn) :
    100   PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
    101   ranged_hash_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_pointer_allocator.allocate(m_num_e))
    105 {
    106   initialize();
    107   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
    108 }
    109 
    110 PB_DS_CLASS_T_DEC
    111 PB_DS_CLASS_C_DEC::
    112 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn, const Resize_Policy& r_resize_policy) :
    113   PB_DS_HASH_EQ_FN_C_DEC(r_eq_fn),
    114   Resize_Policy(r_resize_policy),
    115   ranged_hash_fn_base(resize_base::get_nearest_larger_size(1),
    116 		      r_hash_fn, r_comb_hash_fn),
    117   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
    118   m_entries(s_entry_pointer_allocator.allocate(m_num_e))
    119 {
    120   initialize();
    121   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
    122 }
    123 
    124 PB_DS_CLASS_T_DEC
    125 PB_DS_CLASS_C_DEC::
    126 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
    127 #ifdef _GLIBCXX_DEBUG
    128   debug_base(other),
    129 #endif
    130   PB_DS_HASH_EQ_FN_C_DEC(other),
    131   resize_base(other), ranged_hash_fn_base(other),
    132   m_num_e(resize_base::get_nearest_larger_size(1)), m_num_used_e(0),
    133   m_entries(s_entry_pointer_allocator.allocate(m_num_e))
    134 {
    135   initialize();
    136   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
    137     __try
    138       {
    139         copy_from_range(other.begin(), other.end());
    140       }
    141     __catch(...)
    142       {
    143         deallocate_all();
    144         __throw_exception_again;
    145       }
    146   _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
    147 }
    148 
    149 PB_DS_CLASS_T_DEC
    150 PB_DS_CLASS_C_DEC::
    151 ~PB_DS_CLASS_NAME()
    152 { deallocate_all(); }
    153 
    154 PB_DS_CLASS_T_DEC
    155 void
    156 PB_DS_CLASS_C_DEC::
    157 swap(PB_DS_CLASS_C_DEC& other)
    158 {
    159   _GLIBCXX_DEBUG_ONLY(assert_valid());
    160   _GLIBCXX_DEBUG_ONLY(other.assert_valid());
    161 
    162   std::swap(m_entries, other.m_entries);
    163   std::swap(m_num_e, other.m_num_e);
    164   std::swap(m_num_used_e, other.m_num_used_e);
    165   ranged_hash_fn_base::swap(other);
    166   hash_eq_fn_base::swap(other);
    167   resize_base::swap(other);
    168 
    169   _GLIBCXX_DEBUG_ONLY(debug_base::swap(other));
    170   _GLIBCXX_DEBUG_ONLY(assert_valid());
    171   _GLIBCXX_DEBUG_ONLY(other.assert_valid());
    172 }
    173 
    174 PB_DS_CLASS_T_DEC
    175 void
    176 PB_DS_CLASS_C_DEC::
    177 deallocate_all()
    178 {
    179   clear();
    180   s_entry_pointer_allocator.deallocate(m_entries, m_num_e);
    181 }
    182 
    183 PB_DS_CLASS_T_DEC
    184 void
    185 PB_DS_CLASS_C_DEC::
    186 initialize()
    187 {
    188   std::fill(m_entries, m_entries + m_num_e, entry_pointer(0));
    189   Resize_Policy::notify_resized(m_num_e);
    190   Resize_Policy::notify_cleared();
    191   ranged_hash_fn_base::notify_resized(m_num_e);
    192 }
    193