]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp
re PR libstdc++/25191 (exception_defines.h #defines try/catch)
[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
bc2631e0 3// Copyright (C) 2005, 2006, 2007, 2008, 2009 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
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
4569a895
AT
47PB_DS_CLASS_T_DEC
48PB_DS_CLASS_C_DEC::
81ee09de
BK
49hash_load_check_resize_trigger(float load_min, float load_max)
50: m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
51 m_next_grow_size(0), m_resize_needed(false)
47bea7b8 52{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
53
54PB_DS_CLASS_T_DEC
55inline void
56PB_DS_CLASS_C_DEC::
57notify_find_search_start()
47bea7b8 58{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
59
60PB_DS_CLASS_T_DEC
61inline void
62PB_DS_CLASS_C_DEC::
63notify_find_search_collision()
47bea7b8 64{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
65
66PB_DS_CLASS_T_DEC
67inline void
68PB_DS_CLASS_C_DEC::
69notify_find_search_end()
47bea7b8 70{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
71
72PB_DS_CLASS_T_DEC
73inline void
74PB_DS_CLASS_C_DEC::
75notify_insert_search_start()
47bea7b8 76{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
77
78PB_DS_CLASS_T_DEC
79inline void
80PB_DS_CLASS_C_DEC::
81notify_insert_search_collision()
47bea7b8 82{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
83
84PB_DS_CLASS_T_DEC
85inline void
86PB_DS_CLASS_C_DEC::
87notify_insert_search_end()
47bea7b8 88{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
89
90PB_DS_CLASS_T_DEC
91inline void
92PB_DS_CLASS_C_DEC::
93notify_erase_search_start()
47bea7b8 94{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
95
96PB_DS_CLASS_T_DEC
97inline void
98PB_DS_CLASS_C_DEC::
99notify_erase_search_collision()
47bea7b8 100{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
101
102PB_DS_CLASS_T_DEC
103inline void
104PB_DS_CLASS_C_DEC::
105notify_erase_search_end()
47bea7b8 106{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
4569a895
AT
107
108PB_DS_CLASS_T_DEC
109inline void
110PB_DS_CLASS_C_DEC::
111notify_inserted(size_type num_entries)
112{
113 m_resize_needed = (num_entries >= m_next_grow_size);
4569a895 114 size_base::set_size(num_entries);
47bea7b8 115 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 116}
4569a895
AT
117
118PB_DS_CLASS_T_DEC
119inline void
120PB_DS_CLASS_C_DEC::
121notify_erased(size_type num_entries)
122{
123 size_base::set_size(num_entries);
81ee09de 124 m_resize_needed = num_entries <= m_next_shrink_size;
47bea7b8 125 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 126}
4569a895
AT
127
128PB_DS_CLASS_T_DEC
129inline bool
130PB_DS_CLASS_C_DEC::
131is_resize_needed() const
132{
47bea7b8 133 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 134 return m_resize_needed;
4569a895
AT
135}
136
137PB_DS_CLASS_T_DEC
138inline bool
139PB_DS_CLASS_C_DEC::
140is_grow_needed(size_type /*size*/, size_type num_entries) const
141{
47bea7b8 142 _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
81ee09de 143 return num_entries >= m_next_grow_size;
4569a895
AT
144}
145
146PB_DS_CLASS_T_DEC
147PB_DS_CLASS_C_DEC::
81ee09de 148~hash_load_check_resize_trigger() { }
4569a895
AT
149
150PB_DS_CLASS_T_DEC
151void
152PB_DS_CLASS_C_DEC::
153notify_resized(size_type new_size)
154{
155 m_resize_needed = false;
81ee09de
BK
156 m_next_grow_size = size_type(m_load_max * new_size - 1);
157 m_next_shrink_size = size_type(m_load_min * new_size);
4569a895
AT
158
159#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
160 std::cerr << "hlcrt::notify_resized " <<
81ee09de
BK
161 static_cast<unsigned long>(new_size) << " " <<
162 static_cast<unsigned long>(m_load_min) << " " <<
163 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 164 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
165 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
166#endif
4569a895 167
47bea7b8 168 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 169}
4569a895
AT
170
171PB_DS_CLASS_T_DEC
172void
173PB_DS_CLASS_C_DEC::
174notify_externally_resized(size_type new_size)
175{
176 m_resize_needed = false;
81ee09de 177 size_type new_grow_size = size_type(m_load_max * new_size - 1);
551fe1a2 178 size_type new_shrink_size = size_type(m_load_min * new_size);
4569a895
AT
179 if (new_grow_size >= m_next_grow_size)
180 {
47bea7b8 181 _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
4569a895 182 m_next_grow_size = new_grow_size;
47bea7b8 183 _GLIBCXX_DEBUG_ONLY(assert_valid();)
4569a895
AT
184
185#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
186 std::cerr << "hlcrt::notify_externally_resized1 " <<
81ee09de
BK
187 static_cast<unsigned long>(new_size) << " " <<
188 static_cast<unsigned long>(m_load_min) << " " <<
189 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 190 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
191 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
192#endif
4569a895
AT
193 return;
194 }
195
47bea7b8 196 _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
4569a895
AT
197 m_next_shrink_size = new_shrink_size;
198
199#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
200 std::cerr << "hlcrt::notify_externally_resized2 " <<
81ee09de
BK
201 static_cast<unsigned long>(new_size) << " " <<
202 static_cast<unsigned long>(m_load_min) << " " <<
203 static_cast<unsigned long>(m_load_max) << " " <<
4569a895 204 static_cast<unsigned long>(m_next_shrink_size) << " " <<
81ee09de
BK
205 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
206#endif
4569a895 207
47bea7b8 208 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 209}
4569a895
AT
210
211PB_DS_CLASS_T_DEC
212void
213PB_DS_CLASS_C_DEC::
214notify_cleared()
215{
47bea7b8 216 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 217 size_base::set_size(0);
4569a895 218 m_resize_needed = (0 < m_next_shrink_size);
47bea7b8 219 _GLIBCXX_DEBUG_ONLY(assert_valid();)
81ee09de 220}
4569a895
AT
221
222PB_DS_CLASS_T_DEC
223void
224PB_DS_CLASS_C_DEC::
225swap(PB_DS_CLASS_C_DEC& other)
226{
47bea7b8
BK
227 _GLIBCXX_DEBUG_ONLY(assert_valid();)
228 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
81ee09de
BK
229
230 size_base::swap(other);
4569a895
AT
231 std::swap(m_load_min, other.m_load_min);
232 std::swap(m_load_max, other.m_load_max);
4569a895 233 std::swap(m_resize_needed, other.m_resize_needed);
4569a895
AT
234 std::swap(m_next_grow_size, other.m_next_grow_size);
235 std::swap(m_next_shrink_size, other.m_next_shrink_size);
236
47bea7b8
BK
237 _GLIBCXX_DEBUG_ONLY(assert_valid();)
238 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
81ee09de 239}
4569a895
AT
240
241PB_DS_CLASS_T_DEC
242inline std::pair<float, float>
243PB_DS_CLASS_C_DEC::
244get_loads() const
245{
246 PB_DS_STATIC_ASSERT(access, external_load_access);
81ee09de 247 return std::make_pair(m_load_min, m_load_max);
4569a895
AT
248}
249
250PB_DS_CLASS_T_DEC
251void
252PB_DS_CLASS_C_DEC::
253set_loads(std::pair<float, float> load_pair)
254{
255 PB_DS_STATIC_ASSERT(access, external_load_access);
4569a895
AT
256 const float old_load_min = m_load_min;
257 const float old_load_max = m_load_max;
258 const size_type old_next_shrink_size = m_next_shrink_size;
259 const size_type old_next_grow_size = m_next_grow_size;
260 const bool old_resize_needed = m_resize_needed;
261
bc2631e0 262 __try
4569a895
AT
263 {
264 m_load_min = load_pair.first;
265 m_load_max = load_pair.second;
81ee09de 266 do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
4569a895 267 }
bc2631e0 268 __catch(...)
4569a895
AT
269 {
270 m_load_min = old_load_min;
271 m_load_max = old_load_max;
272 m_next_shrink_size = old_next_shrink_size;
273 m_next_grow_size = old_next_grow_size;
274 m_resize_needed = old_resize_needed;
8fafc2d3 275 __throw_exception_again;
4569a895
AT
276 }
277}
278
279PB_DS_CLASS_T_DEC
280void
281PB_DS_CLASS_C_DEC::
81ee09de 282do_resize(size_type)
4ba851b5 283{ std::abort(); }
4569a895 284
47bea7b8 285#ifdef _GLIBCXX_DEBUG
4569a895
AT
286PB_DS_CLASS_T_DEC
287void
288PB_DS_CLASS_C_DEC::
289assert_valid() const
290{
47bea7b8
BK
291 _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
292 _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);
4569a895 293}
81ee09de 294#endif
4569a895 295