3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011
4 // Free Software Foundation, Inc.
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
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.
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.
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/>.
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
38 * @file hash_load_check_resize_trigger_imp.hpp
39 * Contains a resize trigger implementation.
42 #define PB_DS_ASSERT_VALID(X) \
43 _GLIBCXX_DEBUG_ONLY(X.assert_valid(__FILE__, __LINE__);)
47 hash_load_check_resize_trigger(float load_min, float load_max)
48 : m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
49 m_next_grow_size(0), m_resize_needed(false)
50 { PB_DS_ASSERT_VALID((*this)) }
55 notify_find_search_start()
56 { PB_DS_ASSERT_VALID((*this)) }
61 notify_find_search_collision()
62 { PB_DS_ASSERT_VALID((*this)) }
67 notify_find_search_end()
68 { PB_DS_ASSERT_VALID((*this)) }
73 notify_insert_search_start()
74 { PB_DS_ASSERT_VALID((*this)) }
79 notify_insert_search_collision()
80 { PB_DS_ASSERT_VALID((*this)) }
85 notify_insert_search_end()
86 { PB_DS_ASSERT_VALID((*this)) }
91 notify_erase_search_start()
92 { PB_DS_ASSERT_VALID((*this)) }
97 notify_erase_search_collision()
98 { PB_DS_ASSERT_VALID((*this)) }
103 notify_erase_search_end()
104 { PB_DS_ASSERT_VALID((*this)) }
109 notify_inserted(size_type num_entries)
111 m_resize_needed = (num_entries >= m_next_grow_size);
112 size_base::set_size(num_entries);
113 PB_DS_ASSERT_VALID((*this))
119 notify_erased(size_type num_entries)
121 size_base::set_size(num_entries);
122 m_resize_needed = num_entries <= m_next_shrink_size;
123 PB_DS_ASSERT_VALID((*this))
129 is_resize_needed() const
131 PB_DS_ASSERT_VALID((*this))
132 return m_resize_needed;
138 is_grow_needed(size_type /*size*/, size_type num_entries) const
140 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
141 return num_entries >= m_next_grow_size;
146 ~hash_load_check_resize_trigger() { }
151 notify_resized(size_type new_size)
153 m_resize_needed = false;
154 m_next_grow_size = size_type(m_load_max * new_size - 1);
155 m_next_shrink_size = size_type(m_load_min * new_size);
157 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
158 std::cerr << "hlcrt::notify_resized " << std::endl
159 << "1 " << new_size << std::endl
160 << "2 " << m_load_min << std::endl
161 << "3 " << m_load_max << std::endl
162 << "4 " << m_next_shrink_size << std::endl
163 << "5 " << m_next_grow_size << std::endl;
166 PB_DS_ASSERT_VALID((*this))
172 notify_externally_resized(size_type new_size)
174 m_resize_needed = false;
175 size_type new_grow_size = size_type(m_load_max * new_size - 1);
176 size_type new_shrink_size = size_type(m_load_min * new_size);
178 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
179 std::cerr << "hlcrt::notify_externally_resized " << std::endl
180 << "1 " << new_size << std::endl
181 << "2 " << m_load_min << std::endl
182 << "3 " << m_load_max << std::endl
183 << "4 " << m_next_shrink_size << std::endl
184 << "5 " << m_next_grow_size << std::endl
185 << "6 " << new_shrink_size << std::endl
186 << "7 " << new_grow_size << std::endl;
189 if (new_grow_size >= m_next_grow_size)
191 _GLIBCXX_DEBUG_ASSERT(new_shrink_size >= m_next_shrink_size);
192 m_next_grow_size = new_grow_size;
196 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
197 m_next_shrink_size = new_shrink_size;
200 PB_DS_ASSERT_VALID((*this))
208 PB_DS_ASSERT_VALID((*this))
209 size_base::set_size(0);
210 m_resize_needed = (0 < m_next_shrink_size);
211 PB_DS_ASSERT_VALID((*this))
217 swap(PB_DS_CLASS_C_DEC& other)
219 PB_DS_ASSERT_VALID((*this))
220 PB_DS_ASSERT_VALID(other)
222 size_base::swap(other);
223 std::swap(m_load_min, other.m_load_min);
224 std::swap(m_load_max, other.m_load_max);
225 std::swap(m_resize_needed, other.m_resize_needed);
226 std::swap(m_next_grow_size, other.m_next_grow_size);
227 std::swap(m_next_shrink_size, other.m_next_shrink_size);
229 PB_DS_ASSERT_VALID((*this))
230 PB_DS_ASSERT_VALID(other)
234 inline std::pair<float, float>
238 PB_DS_STATIC_ASSERT(access, external_load_access);
239 return std::make_pair(m_load_min, m_load_max);
245 set_loads(std::pair<float, float> load_pair)
247 PB_DS_STATIC_ASSERT(access, external_load_access);
248 const float old_load_min = m_load_min;
249 const float old_load_max = m_load_max;
250 const size_type old_next_shrink_size = m_next_shrink_size;
251 const size_type old_next_grow_size = m_next_grow_size;
252 const bool old_resize_needed = m_resize_needed;
256 m_load_min = load_pair.first;
257 m_load_max = load_pair.second;
258 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
262 m_load_min = old_load_min;
263 m_load_max = old_load_max;
264 m_next_shrink_size = old_next_shrink_size;
265 m_next_grow_size = old_next_grow_size;
266 m_resize_needed = old_resize_needed;
267 __throw_exception_again;
277 #ifdef _GLIBCXX_DEBUG
278 # define PB_DS_DEBUG_VERIFY(_Cond) \
279 _GLIBCXX_DEBUG_VERIFY_AT(_Cond, \
280 _M_message(#_Cond" assertion from %1;:%2;") \
281 ._M_string(__FILE__)._M_integer(__LINE__) \
287 assert_valid(const char* __file, int __line) const
289 PB_DS_DEBUG_VERIFY(m_load_max > m_load_min);
290 PB_DS_DEBUG_VERIFY(m_next_grow_size >= m_next_shrink_size);
292 # undef PB_DS_DEBUG_VERIFY
294 #undef PB_DS_ASSERT_VALID