hash_load_check_resize_trigger_imp.hpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00031 
00032 // Permission to use, copy, modify, sell, and distribute this software
00033 // is hereby granted without fee, provided that the above copyright
00034 // notice appears in all copies, and that both that copyright notice and
00035 // this permission notice appear in supporting documentation. None of
00036 // the above authors, nor IBM Haifa Research Laboratories, make any
00037 // representation about the suitability of this software for any
00038 // purpose. It is provided "as is" without express or implied warranty.
00039 
00040 /*
00041  * @file hash_load_check_resize_trigger_imp.hpp.hpp
00042  * Contains an implementation of hash_load_check_resize_trigger..
00043  */
00044 
00045 PB_ASSOC_CLASS_T_DEC
00046 pb_assoc::detail::int_to_type<External_Load_Access>
00047 PB_ASSOC_CLASS_C_DEC::s_external_load_access_ind;
00048 
00049 PB_ASSOC_CLASS_T_DEC
00050 PB_ASSOC_CLASS_C_DEC::
00051 hash_load_check_resize_trigger(float load_min, float load_max) :
00052   m_load_min(load_min),
00053   m_load_max(load_max),
00054   m_next_shrink_size(0),
00055   m_next_grow_size(0),
00056   m_resize_needed(false)
00057 {
00058   PB_ASSOC_DBG_ONLY(assert_valid();)
00059     }
00060 
00061 PB_ASSOC_CLASS_T_DEC
00062 inline void
00063 PB_ASSOC_CLASS_C_DEC::
00064 notify_find_search_start()
00065 {
00066   PB_ASSOC_DBG_ONLY(assert_valid();)
00067     }
00068 
00069 PB_ASSOC_CLASS_T_DEC
00070 inline void
00071 PB_ASSOC_CLASS_C_DEC::
00072 notify_find_search_collision()
00073 {
00074   PB_ASSOC_DBG_ONLY(assert_valid();)
00075     }
00076 
00077 PB_ASSOC_CLASS_T_DEC
00078 inline void
00079 PB_ASSOC_CLASS_C_DEC::
00080 notify_find_search_end()
00081 {
00082   PB_ASSOC_DBG_ONLY(assert_valid();)
00083     }
00084 
00085 PB_ASSOC_CLASS_T_DEC
00086 inline void
00087 PB_ASSOC_CLASS_C_DEC::
00088 notify_insert_search_start()
00089 {
00090   PB_ASSOC_DBG_ONLY(assert_valid();)
00091     }
00092 
00093 PB_ASSOC_CLASS_T_DEC
00094 inline void
00095 PB_ASSOC_CLASS_C_DEC::
00096 notify_insert_search_collision()
00097 {
00098   PB_ASSOC_DBG_ONLY(assert_valid();)
00099     }
00100 
00101 PB_ASSOC_CLASS_T_DEC
00102 inline void
00103 PB_ASSOC_CLASS_C_DEC::
00104 notify_insert_search_end()
00105 {
00106   PB_ASSOC_DBG_ONLY(assert_valid();)
00107     }
00108 
00109 PB_ASSOC_CLASS_T_DEC
00110 inline void
00111 PB_ASSOC_CLASS_C_DEC::
00112 notify_erase_search_start()
00113 {
00114   PB_ASSOC_DBG_ONLY(assert_valid();)
00115     }
00116 
00117 PB_ASSOC_CLASS_T_DEC
00118 inline void
00119 PB_ASSOC_CLASS_C_DEC::
00120 notify_erase_search_collision()
00121 {
00122   PB_ASSOC_DBG_ONLY(assert_valid();)
00123     }
00124 
00125 PB_ASSOC_CLASS_T_DEC
00126 inline void
00127 PB_ASSOC_CLASS_C_DEC::
00128 notify_erase_search_end()
00129 {
00130   PB_ASSOC_DBG_ONLY(assert_valid();)
00131     }
00132 
00133 PB_ASSOC_CLASS_T_DEC
00134 inline void
00135 PB_ASSOC_CLASS_C_DEC::
00136 notify_inserted(size_type num_entries)
00137 {
00138   PB_ASSOC_DBG_ASSERT(num_entries <= m_next_grow_size);
00139 
00140   my_size_base::set_size(num_entries);
00141 
00142   m_resize_needed = (num_entries == m_next_grow_size);
00143 
00144   PB_ASSOC_DBG_ONLY(assert_valid();)
00145     }
00146 
00147 PB_ASSOC_CLASS_T_DEC
00148 inline void
00149 PB_ASSOC_CLASS_C_DEC::
00150 notify_erased(size_type num_entries)
00151 {
00152   PB_ASSOC_DBG_ASSERT(num_entries >= m_next_shrink_size);
00153 
00154   my_size_base::set_size(num_entries);
00155 
00156   m_resize_needed = (num_entries == m_next_shrink_size);
00157 
00158   PB_ASSOC_DBG_ONLY(assert_valid();)
00159 
00160     PB_ASSOC_DBG_ONLY(assert_valid();)
00161     }
00162 
00163 PB_ASSOC_CLASS_T_DEC
00164 inline bool
00165 PB_ASSOC_CLASS_C_DEC::
00166 is_resize_needed() const
00167 {
00168   PB_ASSOC_DBG_ONLY(assert_valid();)
00169 
00170     return (m_resize_needed);
00171 }
00172 
00173 PB_ASSOC_CLASS_T_DEC
00174 inline bool
00175 PB_ASSOC_CLASS_C_DEC::
00176 is_grow_needed(size_type /*size*/, size_type num_entries) const
00177 {
00178   PB_ASSOC_DBG_ASSERT(m_resize_needed);
00179 
00180   return (num_entries >= m_next_grow_size);
00181 }
00182 
00183 PB_ASSOC_CLASS_T_DEC
00184 inline bool
00185 PB_ASSOC_CLASS_C_DEC::
00186 is_shrink_needed(size_type /*size*/, size_type num_entries) const
00187 {
00188   PB_ASSOC_DBG_ASSERT(m_resize_needed);
00189 
00190   return (num_entries <= m_next_shrink_size);
00191 }
00192 
00193 PB_ASSOC_CLASS_T_DEC
00194 PB_ASSOC_CLASS_C_DEC::
00195 ~hash_load_check_resize_trigger()
00196 { }
00197 
00198 PB_ASSOC_CLASS_T_DEC
00199 void
00200 PB_ASSOC_CLASS_C_DEC::
00201 notify_resized(size_type new_size)
00202 {
00203   m_resize_needed = false;
00204 
00205   m_next_grow_size =
00206     size_type(m_load_max*  new_size - 1);
00207 
00208   m_next_shrink_size =
00209     size_type(m_load_min*  new_size );
00210 
00211   PB_ASSOC_DBG_ONLY(assert_valid();)
00212     }
00213 
00214 PB_ASSOC_CLASS_T_DEC
00215 void
00216 PB_ASSOC_CLASS_C_DEC::
00217 notify_externally_resized(size_type new_size)
00218 {
00219   m_resize_needed = false;
00220 
00221   size_type new_grow_size =
00222     size_type(m_load_max*  new_size - 1);
00223 
00224   size_type new_shrink_size =
00225     size_type(m_load_min*  new_size );
00226 
00227   if (new_grow_size >= m_next_grow_size)
00228     {
00229       PB_ASSOC_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
00230 
00231       m_next_grow_size = new_grow_size;
00232 
00233       PB_ASSOC_DBG_ONLY(assert_valid();)
00234 
00235     return;
00236     }
00237 
00238   PB_ASSOC_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
00239 
00240   m_next_shrink_size = new_shrink_size;
00241 
00242   PB_ASSOC_DBG_ONLY(assert_valid();)
00243     }
00244 
00245 PB_ASSOC_CLASS_T_DEC
00246 void
00247 PB_ASSOC_CLASS_C_DEC::
00248 notify_cleared()
00249 {
00250   my_size_base::set_size(0);
00251 
00252   m_resize_needed = (0 < m_next_shrink_size);
00253 
00254   PB_ASSOC_DBG_ONLY(assert_valid();)
00255     }
00256 
00257 PB_ASSOC_CLASS_T_DEC
00258 void
00259 PB_ASSOC_CLASS_C_DEC::
00260 swap(PB_ASSOC_CLASS_C_DEC& r_other)
00261 {
00262   PB_ASSOC_DBG_ONLY(assert_valid();)
00263     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00264 
00265     my_size_base::swap(r_other);
00266 
00267   std::swap(m_load_min, r_other.m_load_min);
00268   std::swap(m_load_max, r_other.m_load_max);
00269 
00270   std::swap(m_resize_needed, r_other.m_resize_needed);
00271 
00272   std::swap(m_next_grow_size, r_other.m_next_grow_size);
00273   std::swap(m_next_shrink_size, r_other.m_next_shrink_size);
00274 
00275   PB_ASSOC_DBG_ONLY(assert_valid();)
00276     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00277     }
00278 
00279 PB_ASSOC_CLASS_T_DEC
00280 inline std::pair<float, float>
00281 PB_ASSOC_CLASS_C_DEC::
00282 get_loads() const
00283 {
00284   return (get_loads_imp(s_external_load_access_ind));
00285 }
00286 
00287 PB_ASSOC_CLASS_T_DEC
00288 void
00289 PB_ASSOC_CLASS_C_DEC::
00290 set_loads(std::pair<float, float> load_pair)
00291 {
00292   set_loads_imp(load_pair, s_external_load_access_ind);
00293 }
00294 
00295 PB_ASSOC_CLASS_T_DEC
00296 inline std::pair<float, float>
00297 PB_ASSOC_CLASS_C_DEC::
00298 get_loads_imp(pb_assoc::detail::int_to_type<true>) const
00299 {
00300   return (std::make_pair(m_load_min, m_load_max));
00301 }
00302 
00303 PB_ASSOC_CLASS_T_DEC
00304 void
00305 PB_ASSOC_CLASS_C_DEC::
00306 set_loads_imp(std::pair<float, float> load_pair, pb_assoc::detail::int_to_type<true>)
00307 {
00308   const float old_load_min = m_load_min;
00309   const float old_load_max = m_load_max;
00310   const size_type old_next_shrink_size = m_next_shrink_size;
00311   const size_type old_next_grow_size = m_next_grow_size;
00312   const bool old_resize_needed = m_resize_needed;
00313 
00314   try
00315     {
00316       m_load_min = load_pair.first;
00317       m_load_max = load_pair.second;
00318 
00319       do_resize(static_cast<size_type>(
00320                        my_size_base::get_size() / ((m_load_min + m_load_max) / 2)));
00321     }
00322   catch(...)
00323     {
00324       m_load_min = old_load_min;
00325       m_load_max = old_load_max;
00326       m_next_shrink_size = old_next_shrink_size;
00327       m_next_grow_size = old_next_grow_size;
00328       m_resize_needed = old_resize_needed;
00329 
00330       throw;
00331     }
00332 }
00333 
00334 PB_ASSOC_CLASS_T_DEC
00335 void
00336 PB_ASSOC_CLASS_C_DEC::
00337 do_resize(size_type /*new_size*/)
00338 {
00339   abort();
00340 }
00341 
00342 #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
00343 PB_ASSOC_CLASS_T_DEC
00344 void
00345 PB_ASSOC_CLASS_C_DEC::
00346 assert_valid() const
00347 {
00348   PB_ASSOC_DBG_ASSERT(m_load_max > m_load_min);
00349 
00350   PB_ASSOC_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size);
00351 }
00352 #endif // #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG

Generated on Tue Feb 2 16:55:59 2010 for GNU C++ STL by  doxygen 1.4.7