3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
37 * @file hash_load_check_resize_trigger_imp.hpp
38 * Contains a resize trigger implementation.
43 hash_load_check_resize_trigger(float load_min, float load_max)
44 : m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
45 m_next_grow_size(0), m_resize_needed(false)
46 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
51 notify_find_search_start()
52 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
57 notify_find_search_collision()
58 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
63 notify_find_search_end()
64 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
69 notify_insert_search_start()
70 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
75 notify_insert_search_collision()
76 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
81 notify_insert_search_end()
82 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
87 notify_erase_search_start()
88 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
93 notify_erase_search_collision()
94 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
99 notify_erase_search_end()
100 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
105 notify_inserted(size_type num_entries)
107 m_resize_needed = (num_entries >= m_next_grow_size);
108 size_base::set_size(num_entries);
109 _GLIBCXX_DEBUG_ONLY(assert_valid();)
115 notify_erased(size_type num_entries)
117 size_base::set_size(num_entries);
118 m_resize_needed = num_entries <= m_next_shrink_size;
119 _GLIBCXX_DEBUG_ONLY(assert_valid();)
125 is_resize_needed() const
127 _GLIBCXX_DEBUG_ONLY(assert_valid();)
128 return m_resize_needed;
134 is_grow_needed(size_type /*size*/, size_type num_entries) const
136 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
137 return num_entries >= m_next_grow_size;
142 ~hash_load_check_resize_trigger() { }
147 notify_resized(size_type new_size)
149 m_resize_needed = false;
150 m_next_grow_size = size_type(m_load_max * new_size - 1);
151 m_next_shrink_size = size_type(m_load_min * new_size);
153 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
154 std::cerr << "hlcrt::notify_resized " <<
155 static_cast<unsigned long>(new_size) << " " <<
156 static_cast<unsigned long>(m_load_min) << " " <<
157 static_cast<unsigned long>(m_load_max) << " " <<
158 static_cast<unsigned long>(m_next_shrink_size) << " " <<
159 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
162 _GLIBCXX_DEBUG_ONLY(assert_valid();)
168 notify_externally_resized(size_type new_size)
170 m_resize_needed = false;
171 size_type new_grow_size = size_type(m_load_max * new_size - 1);
172 size_type new_shrink_size = size_type(m_load_min * new_size);
173 if (new_grow_size >= m_next_grow_size)
175 _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
176 m_next_grow_size = new_grow_size;
177 _GLIBCXX_DEBUG_ONLY(assert_valid();)
179 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
180 std::cerr << "hlcrt::notify_externally_resized1 " <<
181 static_cast<unsigned long>(new_size) << " " <<
182 static_cast<unsigned long>(m_load_min) << " " <<
183 static_cast<unsigned long>(m_load_max) << " " <<
184 static_cast<unsigned long>(m_next_shrink_size) << " " <<
185 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
190 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
191 m_next_shrink_size = new_shrink_size;
193 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
194 std::cerr << "hlcrt::notify_externally_resized2 " <<
195 static_cast<unsigned long>(new_size) << " " <<
196 static_cast<unsigned long>(m_load_min) << " " <<
197 static_cast<unsigned long>(m_load_max) << " " <<
198 static_cast<unsigned long>(m_next_shrink_size) << " " <<
199 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
202 _GLIBCXX_DEBUG_ONLY(assert_valid();)
210 _GLIBCXX_DEBUG_ONLY(assert_valid();)
211 size_base::set_size(0);
212 m_resize_needed = (0 < m_next_shrink_size);
213 _GLIBCXX_DEBUG_ONLY(assert_valid();)
219 swap(PB_DS_CLASS_C_DEC& other)
221 _GLIBCXX_DEBUG_ONLY(assert_valid();)
222 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
224 size_base::swap(other);
225 std::swap(m_load_min, other.m_load_min);
226 std::swap(m_load_max, other.m_load_max);
227 std::swap(m_resize_needed, other.m_resize_needed);
228 std::swap(m_next_grow_size, other.m_next_grow_size);
229 std::swap(m_next_shrink_size, other.m_next_shrink_size);
231 _GLIBCXX_DEBUG_ONLY(assert_valid();)
232 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
236 inline std::pair<float, float>
240 PB_DS_STATIC_ASSERT(access, external_load_access);
241 return std::make_pair(m_load_min, m_load_max);
247 set_loads(std::pair<float, float> load_pair)
249 PB_DS_STATIC_ASSERT(access, external_load_access);
250 const float old_load_min = m_load_min;
251 const float old_load_max = m_load_max;
252 const size_type old_next_shrink_size = m_next_shrink_size;
253 const size_type old_next_grow_size = m_next_grow_size;
254 const bool old_resize_needed = m_resize_needed;
258 m_load_min = load_pair.first;
259 m_load_max = load_pair.second;
260 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
264 m_load_min = old_load_min;
265 m_load_max = old_load_max;
266 m_next_shrink_size = old_next_shrink_size;
267 m_next_grow_size = old_next_grow_size;
268 m_resize_needed = old_resize_needed;
269 __throw_exception_again;
279 #ifdef _GLIBCXX_DEBUG
285 _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
286 _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);