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)>)> UNIQUE##static_assert_type
52 hash_load_check_resize_trigger(float load_min, float load_max)
53 : m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
54 m_next_grow_size(0), m_resize_needed(false)
55 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
60 notify_find_search_start()
61 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
66 notify_find_search_collision()
67 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
72 notify_find_search_end()
73 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
78 notify_insert_search_start()
79 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
84 notify_insert_search_collision()
85 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
90 notify_insert_search_end()
91 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
96 notify_erase_search_start()
97 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
102 notify_erase_search_collision()
103 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
108 notify_erase_search_end()
109 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
114 notify_inserted(size_type num_entries)
116 m_resize_needed = (num_entries >= m_next_grow_size);
117 size_base::set_size(num_entries);
118 _GLIBCXX_DEBUG_ONLY(assert_valid();)
124 notify_erased(size_type num_entries)
126 size_base::set_size(num_entries);
127 m_resize_needed = num_entries <= m_next_shrink_size;
128 _GLIBCXX_DEBUG_ONLY(assert_valid();)
134 is_resize_needed() const
136 _GLIBCXX_DEBUG_ONLY(assert_valid();)
137 return m_resize_needed;
143 is_grow_needed(size_type /*size*/, size_type num_entries) const
145 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
146 return num_entries >= m_next_grow_size;
151 ~hash_load_check_resize_trigger() { }
156 notify_resized(size_type new_size)
158 m_resize_needed = false;
159 m_next_grow_size = size_type(m_load_max * new_size - 1);
160 m_next_shrink_size = size_type(m_load_min * new_size);
162 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
163 std::cerr << "hlcrt::notify_resized " <<
164 static_cast<unsigned long>(new_size) << " " <<
165 static_cast<unsigned long>(m_load_min) << " " <<
166 static_cast<unsigned long>(m_load_max) << " " <<
167 static_cast<unsigned long>(m_next_shrink_size) << " " <<
168 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
171 _GLIBCXX_DEBUG_ONLY(assert_valid();)
177 notify_externally_resized(size_type new_size)
179 m_resize_needed = false;
180 size_type new_grow_size = size_type(m_load_max * new_size - 1);
181 size_type new_shrink_size = size_type(m_load_min * new_size );
182 if (new_grow_size >= m_next_grow_size)
184 _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
185 m_next_grow_size = new_grow_size;
186 _GLIBCXX_DEBUG_ONLY(assert_valid();)
188 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
189 std::cerr << "hlcrt::notify_externally_resized1 " <<
190 static_cast<unsigned long>(new_size) << " " <<
191 static_cast<unsigned long>(m_load_min) << " " <<
192 static_cast<unsigned long>(m_load_max) << " " <<
193 static_cast<unsigned long>(m_next_shrink_size) << " " <<
194 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
199 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
200 m_next_shrink_size = new_shrink_size;
202 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
203 std::cerr << "hlcrt::notify_externally_resized2 " <<
204 static_cast<unsigned long>(new_size) << " " <<
205 static_cast<unsigned long>(m_load_min) << " " <<
206 static_cast<unsigned long>(m_load_max) << " " <<
207 static_cast<unsigned long>(m_next_shrink_size) << " " <<
208 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
211 _GLIBCXX_DEBUG_ONLY(assert_valid();)
219 _GLIBCXX_DEBUG_ONLY(assert_valid();)
220 size_base::set_size(0);
221 m_resize_needed = (0 < m_next_shrink_size);
222 _GLIBCXX_DEBUG_ONLY(assert_valid();)
228 swap(PB_DS_CLASS_C_DEC& other)
230 _GLIBCXX_DEBUG_ONLY(assert_valid();)
231 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
233 size_base::swap(other);
234 std::swap(m_load_min, other.m_load_min);
235 std::swap(m_load_max, other.m_load_max);
236 std::swap(m_resize_needed, other.m_resize_needed);
237 std::swap(m_next_grow_size, other.m_next_grow_size);
238 std::swap(m_next_shrink_size, other.m_next_shrink_size);
240 _GLIBCXX_DEBUG_ONLY(assert_valid();)
241 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
245 inline std::pair<float, float>
249 PB_DS_STATIC_ASSERT(access, external_load_access);
250 return std::make_pair(m_load_min, m_load_max);
256 set_loads(std::pair<float, float> load_pair)
258 PB_DS_STATIC_ASSERT(access, external_load_access);
259 const float old_load_min = m_load_min;
260 const float old_load_max = m_load_max;
261 const size_type old_next_shrink_size = m_next_shrink_size;
262 const size_type old_next_grow_size = m_next_grow_size;
263 const bool old_resize_needed = m_resize_needed;
267 m_load_min = load_pair.first;
268 m_load_max = load_pair.second;
269 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
273 m_load_min = old_load_min;
274 m_load_max = old_load_max;
275 m_next_shrink_size = old_next_shrink_size;
276 m_next_grow_size = old_next_grow_size;
277 m_resize_needed = old_resize_needed;
278 __throw_exception_again;
288 #ifdef _GLIBCXX_DEBUG
294 _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
295 _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);
299 #undef PB_DS_STATIC_ASSERT