]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp
Update copyright years.
[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
7adcbafe 3// Copyright (C) 2005-2022 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
748086b7
JJ
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.
19
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/>.
4569a895
AT
24
25// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
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
34// warranty.
35
36/**
37 * @file hash_load_check_resize_trigger_imp.hpp
38 * Contains a resize trigger implementation.
39 */
40
574dfb67
JW
41#ifdef PB_DS_CLASS_C_DEC
42
f5886803
FD
43#define PB_DS_ASSERT_VALID(X) \
44 _GLIBCXX_DEBUG_ONLY(X.assert_valid(__FILE__, __LINE__);)
45
4569a895
AT
46PB_DS_CLASS_T_DEC
47PB_DS_CLASS_C_DEC::
191e7a30 48hash_load_check_resize_trigger(float load_min, float load_max)
81ee09de
BK
49: m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
50 m_next_grow_size(0), m_resize_needed(false)
f5886803 51{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
52
53PB_DS_CLASS_T_DEC
54inline void
55PB_DS_CLASS_C_DEC::
56notify_find_search_start()
f5886803 57{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
58
59PB_DS_CLASS_T_DEC
60inline void
61PB_DS_CLASS_C_DEC::
62notify_find_search_collision()
f5886803 63{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
64
65PB_DS_CLASS_T_DEC
66inline void
67PB_DS_CLASS_C_DEC::
68notify_find_search_end()
f5886803 69{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
70
71PB_DS_CLASS_T_DEC
72inline void
73PB_DS_CLASS_C_DEC::
74notify_insert_search_start()
f5886803 75{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
76
77PB_DS_CLASS_T_DEC
78inline void
79PB_DS_CLASS_C_DEC::
80notify_insert_search_collision()
f5886803 81{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
82
83PB_DS_CLASS_T_DEC
84inline void
85PB_DS_CLASS_C_DEC::
86notify_insert_search_end()
f5886803 87{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
88
89PB_DS_CLASS_T_DEC
90inline void
91PB_DS_CLASS_C_DEC::
92notify_erase_search_start()
f5886803 93{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
94
95PB_DS_CLASS_T_DEC
96inline void
97PB_DS_CLASS_C_DEC::
98notify_erase_search_collision()
f5886803 99{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
100
101PB_DS_CLASS_T_DEC
102inline void
103PB_DS_CLASS_C_DEC::
104notify_erase_search_end()
f5886803 105{ PB_DS_ASSERT_VALID((*this)) }
4569a895
AT
106
107PB_DS_CLASS_T_DEC
108inline void
109PB_DS_CLASS_C_DEC::
110notify_inserted(size_type num_entries)
111{
112 m_resize_needed = (num_entries >= m_next_grow_size);
4569a895 113 size_base::set_size(num_entries);
f5886803 114 PB_DS_ASSERT_VALID((*this))
81ee09de 115}
4569a895
AT
116
117PB_DS_CLASS_T_DEC
118inline void
119PB_DS_CLASS_C_DEC::
120notify_erased(size_type num_entries)
121{
122 size_base::set_size(num_entries);
81ee09de 123 m_resize_needed = num_entries <= m_next_shrink_size;
f5886803 124 PB_DS_ASSERT_VALID((*this))
81ee09de 125}
4569a895
AT
126
127PB_DS_CLASS_T_DEC
128inline bool
129PB_DS_CLASS_C_DEC::
130is_resize_needed() const
131{
f5886803 132 PB_DS_ASSERT_VALID((*this))
81ee09de 133 return m_resize_needed;
4569a895
AT
134}
135
136PB_DS_CLASS_T_DEC
137inline bool
138PB_DS_CLASS_C_DEC::
139is_grow_needed(size_type /*size*/, size_type num_entries) const
140{
47bea7b8 141 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
81ee09de 142 return num_entries >= m_next_grow_size;
4569a895
AT
143}
144
145PB_DS_CLASS_T_DEC
146PB_DS_CLASS_C_DEC::
81ee09de 147~hash_load_check_resize_trigger() { }
4569a895
AT
148
149PB_DS_CLASS_T_DEC
150void
151PB_DS_CLASS_C_DEC::
152notify_resized(size_type new_size)
153{
154 m_resize_needed = false;
81ee09de
BK
155 m_next_grow_size = size_type(m_load_max * new_size - 1);
156 m_next_shrink_size = size_type(m_load_min * new_size);
4569a895
AT
157
158#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
191e7a30
BK
159 std::cerr << "hlcrt::notify_resized " << std::endl
160 << "1 " << new_size << std::endl
161 << "2 " << m_load_min << std::endl
162 << "3 " << m_load_max << std::endl
163 << "4 " << m_next_shrink_size << std::endl
164 << "5 " << m_next_grow_size << std::endl;
165#endif
4569a895 166
f5886803 167 PB_DS_ASSERT_VALID((*this))
81ee09de 168}
4569a895
AT
169
170PB_DS_CLASS_T_DEC
171void
172PB_DS_CLASS_C_DEC::
173notify_externally_resized(size_type new_size)
174{
175 m_resize_needed = false;
81ee09de 176 size_type new_grow_size = size_type(m_load_max * new_size - 1);
551fe1a2 177 size_type new_shrink_size = size_type(m_load_min * new_size);
191e7a30
BK
178
179#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
180 std::cerr << "hlcrt::notify_externally_resized " << std::endl
181 << "1 " << new_size << std::endl
182 << "2 " << m_load_min << std::endl
183 << "3 " << m_load_max << std::endl
184 << "4 " << m_next_shrink_size << std::endl
185 << "5 " << m_next_grow_size << std::endl
186 << "6 " << new_shrink_size << std::endl
187 << "7 " << new_grow_size << std::endl;
188#endif
189
4569a895
AT
190 if (new_grow_size >= m_next_grow_size)
191 {
191e7a30 192 _GLIBCXX_DEBUG_ASSERT(new_shrink_size >= m_next_shrink_size);
4569a895 193 m_next_grow_size = new_grow_size;
4569a895 194 }
191e7a30
BK
195 else
196 {
197 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
198 m_next_shrink_size = new_shrink_size;
199 }
4569a895 200
f5886803 201 PB_DS_ASSERT_VALID((*this))
81ee09de 202}
4569a895
AT
203
204PB_DS_CLASS_T_DEC
205void
206PB_DS_CLASS_C_DEC::
207notify_cleared()
208{
f5886803 209 PB_DS_ASSERT_VALID((*this))
81ee09de 210 size_base::set_size(0);
4569a895 211 m_resize_needed = (0 < m_next_shrink_size);
f5886803 212 PB_DS_ASSERT_VALID((*this))
81ee09de 213}
4569a895
AT
214
215PB_DS_CLASS_T_DEC
216void
217PB_DS_CLASS_C_DEC::
218swap(PB_DS_CLASS_C_DEC& other)
219{
f5886803
FD
220 PB_DS_ASSERT_VALID((*this))
221 PB_DS_ASSERT_VALID(other)
191e7a30 222
81ee09de 223 size_base::swap(other);
4569a895
AT
224 std::swap(m_load_min, other.m_load_min);
225 std::swap(m_load_max, other.m_load_max);
4569a895 226 std::swap(m_resize_needed, other.m_resize_needed);
4569a895
AT
227 std::swap(m_next_grow_size, other.m_next_grow_size);
228 std::swap(m_next_shrink_size, other.m_next_shrink_size);
229
f5886803
FD
230 PB_DS_ASSERT_VALID((*this))
231 PB_DS_ASSERT_VALID(other)
81ee09de 232}
4569a895
AT
233
234PB_DS_CLASS_T_DEC
235inline std::pair<float, float>
236PB_DS_CLASS_C_DEC::
237get_loads() const
238{
239 PB_DS_STATIC_ASSERT(access, external_load_access);
81ee09de 240 return std::make_pair(m_load_min, m_load_max);
4569a895
AT
241}
242
243PB_DS_CLASS_T_DEC
244void
245PB_DS_CLASS_C_DEC::
246set_loads(std::pair<float, float> load_pair)
247{
248 PB_DS_STATIC_ASSERT(access, external_load_access);
4569a895
AT
249 const float old_load_min = m_load_min;
250 const float old_load_max = m_load_max;
251 const size_type old_next_shrink_size = m_next_shrink_size;
252 const size_type old_next_grow_size = m_next_grow_size;
253 const bool old_resize_needed = m_resize_needed;
254
bc2631e0 255 __try
4569a895
AT
256 {
257 m_load_min = load_pair.first;
258 m_load_max = load_pair.second;
81ee09de 259 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
4569a895 260 }
bc2631e0 261 __catch(...)
4569a895
AT
262 {
263 m_load_min = old_load_min;
264 m_load_max = old_load_max;
265 m_next_shrink_size = old_next_shrink_size;
266 m_next_grow_size = old_next_grow_size;
267 m_resize_needed = old_resize_needed;
8fafc2d3 268 __throw_exception_again;
4569a895
AT
269 }
270}
271
272PB_DS_CLASS_T_DEC
273void
274PB_DS_CLASS_C_DEC::
81ee09de 275do_resize(size_type)
4ba851b5 276{ std::abort(); }
4569a895 277
47bea7b8 278#ifdef _GLIBCXX_DEBUG
f5886803
FD
279# define PB_DS_DEBUG_VERIFY(_Cond) \
280 _GLIBCXX_DEBUG_VERIFY_AT(_Cond, \
281 _M_message(#_Cond" assertion from %1;:%2;") \
282 ._M_string(__FILE__)._M_integer(__LINE__) \
283 ,__file,__line)
284
4569a895
AT
285PB_DS_CLASS_T_DEC
286void
287PB_DS_CLASS_C_DEC::
f5886803 288assert_valid(const char* __file, int __line) const
4569a895 289{
cfca3f72
FD
290 PB_DS_DEBUG_VERIFY(m_load_max > m_load_min);
291 PB_DS_DEBUG_VERIFY(m_next_grow_size >= m_next_shrink_size);
4569a895 292}
f5886803 293# undef PB_DS_DEBUG_VERIFY
191e7a30 294#endif
f5886803 295#undef PB_DS_ASSERT_VALID
574dfb67 296#endif