]>
Commit | Line | Data |
---|---|---|
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 | |
50 | PB_DS_CLASS_T_DEC | |
51 | PB_DS_CLASS_C_DEC:: | |
81ee09de BK |
52 | hash_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 | |
57 | PB_DS_CLASS_T_DEC | |
58 | inline void | |
59 | PB_DS_CLASS_C_DEC:: | |
60 | notify_find_search_start() | |
47bea7b8 | 61 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
62 | |
63 | PB_DS_CLASS_T_DEC | |
64 | inline void | |
65 | PB_DS_CLASS_C_DEC:: | |
66 | notify_find_search_collision() | |
47bea7b8 | 67 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
68 | |
69 | PB_DS_CLASS_T_DEC | |
70 | inline void | |
71 | PB_DS_CLASS_C_DEC:: | |
72 | notify_find_search_end() | |
47bea7b8 | 73 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
74 | |
75 | PB_DS_CLASS_T_DEC | |
76 | inline void | |
77 | PB_DS_CLASS_C_DEC:: | |
78 | notify_insert_search_start() | |
47bea7b8 | 79 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
80 | |
81 | PB_DS_CLASS_T_DEC | |
82 | inline void | |
83 | PB_DS_CLASS_C_DEC:: | |
84 | notify_insert_search_collision() | |
47bea7b8 | 85 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
86 | |
87 | PB_DS_CLASS_T_DEC | |
88 | inline void | |
89 | PB_DS_CLASS_C_DEC:: | |
90 | notify_insert_search_end() | |
47bea7b8 | 91 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
92 | |
93 | PB_DS_CLASS_T_DEC | |
94 | inline void | |
95 | PB_DS_CLASS_C_DEC:: | |
96 | notify_erase_search_start() | |
47bea7b8 | 97 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
98 | |
99 | PB_DS_CLASS_T_DEC | |
100 | inline void | |
101 | PB_DS_CLASS_C_DEC:: | |
102 | notify_erase_search_collision() | |
47bea7b8 | 103 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
104 | |
105 | PB_DS_CLASS_T_DEC | |
106 | inline void | |
107 | PB_DS_CLASS_C_DEC:: | |
108 | notify_erase_search_end() | |
47bea7b8 | 109 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
110 | |
111 | PB_DS_CLASS_T_DEC | |
112 | inline void | |
113 | PB_DS_CLASS_C_DEC:: | |
114 | notify_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 | |
121 | PB_DS_CLASS_T_DEC | |
122 | inline void | |
123 | PB_DS_CLASS_C_DEC:: | |
124 | notify_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 | |
131 | PB_DS_CLASS_T_DEC | |
132 | inline bool | |
133 | PB_DS_CLASS_C_DEC:: | |
134 | is_resize_needed() const | |
135 | { | |
47bea7b8 | 136 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
81ee09de | 137 | return m_resize_needed; |
4569a895 AT |
138 | } |
139 | ||
140 | PB_DS_CLASS_T_DEC | |
141 | inline bool | |
142 | PB_DS_CLASS_C_DEC:: | |
143 | is_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 | ||
149 | PB_DS_CLASS_T_DEC | |
150 | PB_DS_CLASS_C_DEC:: | |
81ee09de | 151 | ~hash_load_check_resize_trigger() { } |
4569a895 AT |
152 | |
153 | PB_DS_CLASS_T_DEC | |
154 | void | |
155 | PB_DS_CLASS_C_DEC:: | |
156 | notify_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 | |
174 | PB_DS_CLASS_T_DEC | |
175 | void | |
176 | PB_DS_CLASS_C_DEC:: | |
177 | notify_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 | |
214 | PB_DS_CLASS_T_DEC | |
215 | void | |
216 | PB_DS_CLASS_C_DEC:: | |
217 | notify_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 | |
225 | PB_DS_CLASS_T_DEC | |
226 | void | |
227 | PB_DS_CLASS_C_DEC:: | |
228 | swap(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 | |
244 | PB_DS_CLASS_T_DEC | |
245 | inline std::pair<float, float> | |
246 | PB_DS_CLASS_C_DEC:: | |
247 | get_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 | ||
253 | PB_DS_CLASS_T_DEC | |
254 | void | |
255 | PB_DS_CLASS_C_DEC:: | |
256 | set_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 | ||
282 | PB_DS_CLASS_T_DEC | |
283 | void | |
284 | PB_DS_CLASS_C_DEC:: | |
81ee09de BK |
285 | do_resize(size_type) |
286 | { abort(); } | |
4569a895 | 287 | |
47bea7b8 | 288 | #ifdef _GLIBCXX_DEBUG |
4569a895 AT |
289 | PB_DS_CLASS_T_DEC |
290 | void | |
291 | PB_DS_CLASS_C_DEC:: | |
292 | assert_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 |