]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp
typelist_assoc_container.hpp: Remove, unused.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / resize_policy / hash_load_check_resize_trigger_imp.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4//
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
9// version.
10
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.
15
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.
20
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
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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
40// warranty.
41
42/**
43 * @file hash_load_check_resize_trigger_imp.hpp
44 * Contains a resize trigger implementation.
45 */
46
d7f245b1
BK
47#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
48 typedef detail::static_assert_dumclass<sizeof(detail::static_assert<bool(E)>)> UNIQUE##static_assert_type
4569a895
AT
49
50PB_DS_CLASS_T_DEC
51PB_DS_CLASS_C_DEC::
81ee09de
BK
52hash_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)
47bea7b8 55{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
56
57PB_DS_CLASS_T_DEC
58inline void
59PB_DS_CLASS_C_DEC::
60notify_find_search_start()
47bea7b8 61{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
62
63PB_DS_CLASS_T_DEC
64inline void
65PB_DS_CLASS_C_DEC::
66notify_find_search_collision()
47bea7b8 67{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
68
69PB_DS_CLASS_T_DEC
70inline void
71PB_DS_CLASS_C_DEC::
72notify_find_search_end()
47bea7b8 73{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
74
75PB_DS_CLASS_T_DEC
76inline void
77PB_DS_CLASS_C_DEC::
78notify_insert_search_start()
47bea7b8 79{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
80
81PB_DS_CLASS_T_DEC
82inline void
83PB_DS_CLASS_C_DEC::
84notify_insert_search_collision()
47bea7b8 85{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
86
87PB_DS_CLASS_T_DEC
88inline void
89PB_DS_CLASS_C_DEC::
90notify_insert_search_end()
47bea7b8 91{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
92
93PB_DS_CLASS_T_DEC
94inline void
95PB_DS_CLASS_C_DEC::
96notify_erase_search_start()
47bea7b8 97{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
98
99PB_DS_CLASS_T_DEC
100inline void
101PB_DS_CLASS_C_DEC::
102notify_erase_search_collision()
47bea7b8 103{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
104
105PB_DS_CLASS_T_DEC
106inline void
107PB_DS_CLASS_C_DEC::
108notify_erase_search_end()
47bea7b8 109{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
110
111PB_DS_CLASS_T_DEC
112inline void
113PB_DS_CLASS_C_DEC::
114notify_inserted(size_type num_entries)
115{
116 m_resize_needed = (num_entries >= m_next_grow_size);
4569a895 117 size_base::set_size(num_entries);
47bea7b8 118 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 119}
4569a895
AT
120
121PB_DS_CLASS_T_DEC
122inline void
123PB_DS_CLASS_C_DEC::
124notify_erased(size_type num_entries)
125{
126 size_base::set_size(num_entries);
81ee09de 127 m_resize_needed = num_entries <= m_next_shrink_size;
47bea7b8 128 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 129}
4569a895
AT
130
131PB_DS_CLASS_T_DEC
132inline bool
133PB_DS_CLASS_C_DEC::
134is_resize_needed() const
135{
47bea7b8 136 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 137 return m_resize_needed;
4569a895
AT
138}
139
140PB_DS_CLASS_T_DEC
141inline bool
142PB_DS_CLASS_C_DEC::
143is_grow_needed(size_type /*size*/, size_type num_entries) const
144{
47bea7b8 145 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
81ee09de 146 return num_entries >= m_next_grow_size;
4569a895
AT
147}
148
149PB_DS_CLASS_T_DEC
150PB_DS_CLASS_C_DEC::
81ee09de 151~hash_load_check_resize_trigger() { }
4569a895
AT
152
153PB_DS_CLASS_T_DEC
154void
155PB_DS_CLASS_C_DEC::
156notify_resized(size_type new_size)
157{
158 m_resize_needed = false;
81ee09de
BK
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);
4569a895
AT
161
162#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
163 std::cerr << "hlcrt::notify_resized " <<
81ee09de
BK
164 static_cast<unsigned long>(new_size) << " " <<
165 static_cast<unsigned long>(m_load_min) << " " <<
166 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 167 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
168 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
169#endif
4569a895 170
47bea7b8 171 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 172}
4569a895
AT
173
174PB_DS_CLASS_T_DEC
175void
176PB_DS_CLASS_C_DEC::
177notify_externally_resized(size_type new_size)
178{
179 m_resize_needed = false;
81ee09de
BK
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 );
4569a895
AT
182 if (new_grow_size >= m_next_grow_size)
183 {
47bea7b8 184 _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
4569a895 185 m_next_grow_size = new_grow_size;
47bea7b8 186 _GLIBCXX_DEBUG_ONLY(assert_valid();)
4569a895
AT
187
188#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
189 std::cerr << "hlcrt::notify_externally_resized1 " <<
81ee09de
BK
190 static_cast<unsigned long>(new_size) << " " <<
191 static_cast<unsigned long>(m_load_min) << " " <<
192 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 193 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
194 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
195#endif
4569a895
AT
196 return;
197 }
198
47bea7b8 199 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
4569a895
AT
200 m_next_shrink_size = new_shrink_size;
201
202#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
203 std::cerr << "hlcrt::notify_externally_resized2 " <<
81ee09de
BK
204 static_cast<unsigned long>(new_size) << " " <<
205 static_cast<unsigned long>(m_load_min) << " " <<
206 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 207 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
208 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
209#endif
4569a895 210
47bea7b8 211 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 212}
4569a895
AT
213
214PB_DS_CLASS_T_DEC
215void
216PB_DS_CLASS_C_DEC::
217notify_cleared()
218{
47bea7b8 219 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 220 size_base::set_size(0);
4569a895 221 m_resize_needed = (0 < m_next_shrink_size);
47bea7b8 222 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 223}
4569a895
AT
224
225PB_DS_CLASS_T_DEC
226void
227PB_DS_CLASS_C_DEC::
228swap(PB_DS_CLASS_C_DEC& other)
229{
47bea7b8
BK
230 _GLIBCXX_DEBUG_ONLY(assert_valid();)
231 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
81ee09de
BK
232
233 size_base::swap(other);
4569a895
AT
234 std::swap(m_load_min, other.m_load_min);
235 std::swap(m_load_max, other.m_load_max);
4569a895 236 std::swap(m_resize_needed, other.m_resize_needed);
4569a895
AT
237 std::swap(m_next_grow_size, other.m_next_grow_size);
238 std::swap(m_next_shrink_size, other.m_next_shrink_size);
239
47bea7b8
BK
240 _GLIBCXX_DEBUG_ONLY(assert_valid();)
241 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
81ee09de 242}
4569a895
AT
243
244PB_DS_CLASS_T_DEC
245inline std::pair<float, float>
246PB_DS_CLASS_C_DEC::
247get_loads() const
248{
249 PB_DS_STATIC_ASSERT(access, external_load_access);
81ee09de 250 return std::make_pair(m_load_min, m_load_max);
4569a895
AT
251}
252
253PB_DS_CLASS_T_DEC
254void
255PB_DS_CLASS_C_DEC::
256set_loads(std::pair<float, float> load_pair)
257{
258 PB_DS_STATIC_ASSERT(access, external_load_access);
4569a895
AT
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;
264
265 try
266 {
267 m_load_min = load_pair.first;
268 m_load_max = load_pair.second;
81ee09de 269 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
4569a895 270 }
81ee09de 271 catch (...)
4569a895
AT
272 {
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;
4569a895
AT
278 throw;
279 }
280}
281
282PB_DS_CLASS_T_DEC
283void
284PB_DS_CLASS_C_DEC::
81ee09de
BK
285do_resize(size_type)
286{ abort(); }
4569a895 287
47bea7b8 288#ifdef _GLIBCXX_DEBUG
4569a895
AT
289PB_DS_CLASS_T_DEC
290void
291PB_DS_CLASS_C_DEC::
292assert_valid() const
293{
47bea7b8
BK
294 _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
295 _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);
4569a895 296}
81ee09de 297#endif
4569a895
AT
298
299#undef PB_DS_STATIC_ASSERT
300