3 // Copyright (C) 2005, 2006 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file hash_load_check_resize_trigger_imp.hpp
44 * Contains a resize trigger implementation.
47 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
48 typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> \
49 UNIQUE##static_assert_type
53 hash_load_check_resize_trigger(float load_min, float load_max) :
56 m_next_shrink_size(0),
58 m_resize_needed(false)
60 PB_DS_DBG_ONLY(assert_valid();)
66 notify_find_search_start()
68 PB_DS_DBG_ONLY(assert_valid();)
74 notify_find_search_collision()
76 PB_DS_DBG_ONLY(assert_valid();)
82 notify_find_search_end()
84 PB_DS_DBG_ONLY(assert_valid();)
90 notify_insert_search_start()
92 PB_DS_DBG_ONLY(assert_valid();)
98 notify_insert_search_collision()
100 PB_DS_DBG_ONLY(assert_valid();)
106 notify_insert_search_end()
108 PB_DS_DBG_ONLY(assert_valid();)
114 notify_erase_search_start()
116 PB_DS_DBG_ONLY(assert_valid();)
122 notify_erase_search_collision()
124 PB_DS_DBG_ONLY(assert_valid();)
130 notify_erase_search_end()
132 PB_DS_DBG_ONLY(assert_valid();)
138 notify_inserted(size_type num_entries)
140 m_resize_needed = (num_entries >= m_next_grow_size);
142 size_base::set_size(num_entries);
144 PB_DS_DBG_ONLY(assert_valid();)
150 notify_erased(size_type num_entries)
152 size_base::set_size(num_entries);
155 num_entries <= m_next_shrink_size;
157 PB_DS_DBG_ONLY(assert_valid();)
163 is_resize_needed() const
165 PB_DS_DBG_ONLY(assert_valid();)
167 return (m_resize_needed);
173 is_grow_needed(size_type /*size*/, size_type num_entries) const
175 PB_DS_DBG_ASSERT(m_resize_needed);
177 return (num_entries >= m_next_grow_size);
182 ~hash_load_check_resize_trigger()
188 notify_resized(size_type new_size)
190 m_resize_needed = false;
193 size_type(m_load_max* new_size - 1);
196 size_type(m_load_min* new_size);
198 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
199 std::cerr << "hlcrt::notify_resized " <<
200 static_cast<unsigned long>(new_size) << " " <<
201 static_cast<unsigned long>(m_load_min) << " " <<
202 static_cast<unsigned long>(m_load_max) << " " <<
203 static_cast<unsigned long>(m_next_shrink_size) << " " <<
204 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
205 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
207 PB_DS_DBG_ONLY(assert_valid();)
213 notify_externally_resized(size_type new_size)
215 m_resize_needed = false;
217 size_type new_grow_size =
218 size_type(m_load_max* new_size - 1);
220 size_type new_shrink_size =
221 size_type(m_load_min* new_size );
223 if (new_grow_size >= m_next_grow_size)
225 PB_DS_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
227 m_next_grow_size = new_grow_size;
229 PB_DS_DBG_ONLY(assert_valid();)
231 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
232 std::cerr << "hlcrt::notify_externally_resized1 " <<
233 static_cast<unsigned long>(new_size) << " " <<
234 static_cast<unsigned long>(m_load_min) << " " <<
235 static_cast<unsigned long>(m_load_max) << " " <<
236 static_cast<unsigned long>(m_next_shrink_size) << " " <<
237 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
238 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
243 PB_DS_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
245 m_next_shrink_size = new_shrink_size;
247 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
248 std::cerr << "hlcrt::notify_externally_resized2 " <<
249 static_cast<unsigned long>(new_size) << " " <<
250 static_cast<unsigned long>(m_load_min) << " " <<
251 static_cast<unsigned long>(m_load_max) << " " <<
252 static_cast<unsigned long>(m_next_shrink_size) << " " <<
253 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
254 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
256 PB_DS_DBG_ONLY(assert_valid();)
264 PB_DS_DBG_ONLY(assert_valid();)
266 size_base::set_size(0);
268 m_resize_needed = (0 < m_next_shrink_size);
270 PB_DS_DBG_ONLY(assert_valid();)
276 swap(PB_DS_CLASS_C_DEC& other)
278 PB_DS_DBG_ONLY(assert_valid();)
279 PB_DS_DBG_ONLY(other.assert_valid();)
281 size_base::swap(other);
283 std::swap(m_load_min, other.m_load_min);
284 std::swap(m_load_max, other.m_load_max);
286 std::swap(m_resize_needed, other.m_resize_needed);
288 std::swap(m_next_grow_size, other.m_next_grow_size);
289 std::swap(m_next_shrink_size, other.m_next_shrink_size);
291 PB_DS_DBG_ONLY(assert_valid();)
292 PB_DS_DBG_ONLY(other.assert_valid();)
296 inline std::pair<float, float>
300 PB_DS_STATIC_ASSERT(access, external_load_access);
302 return (std::make_pair(m_load_min, m_load_max));
308 set_loads(std::pair<float, float> load_pair)
310 PB_DS_STATIC_ASSERT(access, external_load_access);
312 const float old_load_min = m_load_min;
313 const float old_load_max = m_load_max;
314 const size_type old_next_shrink_size = m_next_shrink_size;
315 const size_type old_next_grow_size = m_next_grow_size;
316 const bool old_resize_needed = m_resize_needed;
320 m_load_min = load_pair.first;
321 m_load_max = load_pair.second;
323 do_resize(static_cast<size_type>(
324 size_base::get_size() / ((m_load_min + m_load_max) / 2)));
328 m_load_min = old_load_min;
329 m_load_max = old_load_max;
330 m_next_shrink_size = old_next_shrink_size;
331 m_next_grow_size = old_next_grow_size;
332 m_resize_needed = old_resize_needed;
341 do_resize(size_type /*new_size*/)
346 #ifdef PB_DS_HASH_POLICY_DEBUG
352 PB_DS_DBG_ASSERT(m_load_max > m_load_min);
354 PB_DS_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size);
356 #endif // #ifdef PB_DS_HASH_POLICY_DEBUG
358 #undef PB_DS_STATIC_ASSERT