3 // Copyright (C) 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file hash_load_check_resize_trigger_imp.hpp.hpp
42 * Contains an implementation of hash_load_check_resize_trigger..
46 pb_assoc::detail::int_to_type<External_Load_Access>
47 PB_ASSOC_CLASS_C_DEC::s_external_load_access_ind;
50 PB_ASSOC_CLASS_C_DEC::
51 hash_load_check_resize_trigger(float load_min, float load_max) :
54 m_next_shrink_size(0),
56 m_resize_needed(false)
58 PB_ASSOC_DBG_ONLY(assert_valid();)
63 PB_ASSOC_CLASS_C_DEC::
64 notify_find_search_start()
66 PB_ASSOC_DBG_ONLY(assert_valid();)
71 PB_ASSOC_CLASS_C_DEC::
72 notify_find_search_collision()
74 PB_ASSOC_DBG_ONLY(assert_valid();)
79 PB_ASSOC_CLASS_C_DEC::
80 notify_find_search_end()
82 PB_ASSOC_DBG_ONLY(assert_valid();)
87 PB_ASSOC_CLASS_C_DEC::
88 notify_insert_search_start()
90 PB_ASSOC_DBG_ONLY(assert_valid();)
95 PB_ASSOC_CLASS_C_DEC::
96 notify_insert_search_collision()
98 PB_ASSOC_DBG_ONLY(assert_valid();)
103 PB_ASSOC_CLASS_C_DEC::
104 notify_insert_search_end()
106 PB_ASSOC_DBG_ONLY(assert_valid();)
111 PB_ASSOC_CLASS_C_DEC::
112 notify_erase_search_start()
114 PB_ASSOC_DBG_ONLY(assert_valid();)
119 PB_ASSOC_CLASS_C_DEC::
120 notify_erase_search_collision()
122 PB_ASSOC_DBG_ONLY(assert_valid();)
127 PB_ASSOC_CLASS_C_DEC::
128 notify_erase_search_end()
130 PB_ASSOC_DBG_ONLY(assert_valid();)
135 PB_ASSOC_CLASS_C_DEC::
136 notify_inserted(size_type num_entries)
138 PB_ASSOC_DBG_ASSERT(num_entries <= m_next_grow_size);
140 my_size_base::set_size(num_entries);
142 m_resize_needed = (num_entries == m_next_grow_size);
144 PB_ASSOC_DBG_ONLY(assert_valid();)
149 PB_ASSOC_CLASS_C_DEC::
150 notify_erased(size_type num_entries)
152 PB_ASSOC_DBG_ASSERT(num_entries >= m_next_shrink_size);
154 my_size_base::set_size(num_entries);
156 m_resize_needed = (num_entries == m_next_shrink_size);
158 PB_ASSOC_DBG_ONLY(assert_valid();)
160 PB_ASSOC_DBG_ONLY(assert_valid();)
165 PB_ASSOC_CLASS_C_DEC::
166 is_resize_needed() const
168 PB_ASSOC_DBG_ONLY(assert_valid();)
170 return (m_resize_needed);
175 PB_ASSOC_CLASS_C_DEC::
176 is_grow_needed(size_type /*size*/, size_type num_entries) const
178 PB_ASSOC_DBG_ASSERT(m_resize_needed);
180 return (num_entries >= m_next_grow_size);
185 PB_ASSOC_CLASS_C_DEC::
186 is_shrink_needed(size_type /*size*/, size_type num_entries) const
188 PB_ASSOC_DBG_ASSERT(m_resize_needed);
190 return (num_entries <= m_next_shrink_size);
194 PB_ASSOC_CLASS_C_DEC::
195 ~hash_load_check_resize_trigger()
200 PB_ASSOC_CLASS_C_DEC::
201 notify_resized(size_type new_size)
203 m_resize_needed = false;
206 size_type(m_load_max* new_size - 1);
209 size_type(m_load_min* new_size );
211 PB_ASSOC_DBG_ONLY(assert_valid();)
216 PB_ASSOC_CLASS_C_DEC::
217 notify_externally_resized(size_type new_size)
219 m_resize_needed = false;
221 size_type new_grow_size =
222 size_type(m_load_max* new_size - 1);
224 size_type new_shrink_size =
225 size_type(m_load_min* new_size );
227 if (new_grow_size >= m_next_grow_size)
229 PB_ASSOC_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
231 m_next_grow_size = new_grow_size;
233 PB_ASSOC_DBG_ONLY(assert_valid();)
238 PB_ASSOC_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
240 m_next_shrink_size = new_shrink_size;
242 PB_ASSOC_DBG_ONLY(assert_valid();)
247 PB_ASSOC_CLASS_C_DEC::
250 my_size_base::set_size(0);
252 m_resize_needed = (0 < m_next_shrink_size);
254 PB_ASSOC_DBG_ONLY(assert_valid();)
259 PB_ASSOC_CLASS_C_DEC::
260 swap(PB_ASSOC_CLASS_C_DEC& r_other)
262 PB_ASSOC_DBG_ONLY(assert_valid();)
263 PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
265 my_size_base::swap(r_other);
267 std::swap(m_load_min, r_other.m_load_min);
268 std::swap(m_load_max, r_other.m_load_max);
270 std::swap(m_resize_needed, r_other.m_resize_needed);
272 std::swap(m_next_grow_size, r_other.m_next_grow_size);
273 std::swap(m_next_shrink_size, r_other.m_next_shrink_size);
275 PB_ASSOC_DBG_ONLY(assert_valid();)
276 PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
280 inline std::pair<float, float>
281 PB_ASSOC_CLASS_C_DEC::
284 return (get_loads_imp(s_external_load_access_ind));
289 PB_ASSOC_CLASS_C_DEC::
290 set_loads(std::pair<float, float> load_pair)
292 set_loads_imp(load_pair, s_external_load_access_ind);
296 inline std::pair<float, float>
297 PB_ASSOC_CLASS_C_DEC::
298 get_loads_imp(pb_assoc::detail::int_to_type<true>) const
300 return (std::make_pair(m_load_min, m_load_max));
305 PB_ASSOC_CLASS_C_DEC::
306 set_loads_imp(std::pair<float, float> load_pair, pb_assoc::detail::int_to_type<true>)
308 const float old_load_min = m_load_min;
309 const float old_load_max = m_load_max;
310 const size_type old_next_shrink_size = m_next_shrink_size;
311 const size_type old_next_grow_size = m_next_grow_size;
312 const bool old_resize_needed = m_resize_needed;
316 m_load_min = load_pair.first;
317 m_load_max = load_pair.second;
319 do_resize(static_cast<size_type>(
320 my_size_base::get_size() / ((m_load_min + m_load_max) / 2)));
324 m_load_min = old_load_min;
325 m_load_max = old_load_max;
326 m_next_shrink_size = old_next_shrink_size;
327 m_next_grow_size = old_next_grow_size;
328 m_resize_needed = old_resize_needed;
336 PB_ASSOC_CLASS_C_DEC::
337 do_resize(size_type /*new_size*/)
342 #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
345 PB_ASSOC_CLASS_C_DEC::
348 PB_ASSOC_DBG_ASSERT(m_load_max > m_load_min);
350 PB_ASSOC_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size);
352 #endif // #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG