]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
e894edef | 3 | // Copyright (C) 2005, 2006, 2007 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 |
47 | PB_DS_CLASS_T_DEC |
48 | PB_DS_CLASS_C_DEC:: | |
81ee09de BK |
49 | hash_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 | |
54 | PB_DS_CLASS_T_DEC | |
55 | inline void | |
56 | PB_DS_CLASS_C_DEC:: | |
57 | notify_find_search_start() | |
47bea7b8 | 58 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
59 | |
60 | PB_DS_CLASS_T_DEC | |
61 | inline void | |
62 | PB_DS_CLASS_C_DEC:: | |
63 | notify_find_search_collision() | |
47bea7b8 | 64 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
65 | |
66 | PB_DS_CLASS_T_DEC | |
67 | inline void | |
68 | PB_DS_CLASS_C_DEC:: | |
69 | notify_find_search_end() | |
47bea7b8 | 70 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
71 | |
72 | PB_DS_CLASS_T_DEC | |
73 | inline void | |
74 | PB_DS_CLASS_C_DEC:: | |
75 | notify_insert_search_start() | |
47bea7b8 | 76 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
77 | |
78 | PB_DS_CLASS_T_DEC | |
79 | inline void | |
80 | PB_DS_CLASS_C_DEC:: | |
81 | notify_insert_search_collision() | |
47bea7b8 | 82 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
83 | |
84 | PB_DS_CLASS_T_DEC | |
85 | inline void | |
86 | PB_DS_CLASS_C_DEC:: | |
87 | notify_insert_search_end() | |
47bea7b8 | 88 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
89 | |
90 | PB_DS_CLASS_T_DEC | |
91 | inline void | |
92 | PB_DS_CLASS_C_DEC:: | |
93 | notify_erase_search_start() | |
47bea7b8 | 94 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
95 | |
96 | PB_DS_CLASS_T_DEC | |
97 | inline void | |
98 | PB_DS_CLASS_C_DEC:: | |
99 | notify_erase_search_collision() | |
47bea7b8 | 100 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
101 | |
102 | PB_DS_CLASS_T_DEC | |
103 | inline void | |
104 | PB_DS_CLASS_C_DEC:: | |
105 | notify_erase_search_end() | |
47bea7b8 | 106 | { _GLIBCXX_DEBUG_ONLY(assert_valid();) } |
4569a895 AT |
107 | |
108 | PB_DS_CLASS_T_DEC | |
109 | inline void | |
110 | PB_DS_CLASS_C_DEC:: | |
111 | notify_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 | |
118 | PB_DS_CLASS_T_DEC | |
119 | inline void | |
120 | PB_DS_CLASS_C_DEC:: | |
121 | notify_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 | |
128 | PB_DS_CLASS_T_DEC | |
129 | inline bool | |
130 | PB_DS_CLASS_C_DEC:: | |
131 | is_resize_needed() const | |
132 | { | |
47bea7b8 | 133 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
81ee09de | 134 | return m_resize_needed; |
4569a895 AT |
135 | } |
136 | ||
137 | PB_DS_CLASS_T_DEC | |
138 | inline bool | |
139 | PB_DS_CLASS_C_DEC:: | |
140 | is_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 | ||
146 | PB_DS_CLASS_T_DEC | |
147 | PB_DS_CLASS_C_DEC:: | |
81ee09de | 148 | ~hash_load_check_resize_trigger() { } |
4569a895 AT |
149 | |
150 | PB_DS_CLASS_T_DEC | |
151 | void | |
152 | PB_DS_CLASS_C_DEC:: | |
153 | notify_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 | |
171 | PB_DS_CLASS_T_DEC | |
172 | void | |
173 | PB_DS_CLASS_C_DEC:: | |
174 | notify_externally_resized(size_type new_size) | |
175 | { | |
176 | m_resize_needed = false; | |
81ee09de BK |
177 | size_type new_grow_size = size_type(m_load_max * new_size - 1); |
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 | |
211 | PB_DS_CLASS_T_DEC | |
212 | void | |
213 | PB_DS_CLASS_C_DEC:: | |
214 | notify_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 | |
222 | PB_DS_CLASS_T_DEC | |
223 | void | |
224 | PB_DS_CLASS_C_DEC:: | |
225 | swap(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 | |
241 | PB_DS_CLASS_T_DEC | |
242 | inline std::pair<float, float> | |
243 | PB_DS_CLASS_C_DEC:: | |
244 | get_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 | ||
250 | PB_DS_CLASS_T_DEC | |
251 | void | |
252 | PB_DS_CLASS_C_DEC:: | |
253 | set_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 | ||
262 | try | |
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 | } |
d05f74f1 | 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 | ||
279 | PB_DS_CLASS_T_DEC | |
280 | void | |
281 | PB_DS_CLASS_C_DEC:: | |
81ee09de | 282 | do_resize(size_type) |
4ba851b5 | 283 | { std::abort(); } |
4569a895 | 284 | |
47bea7b8 | 285 | #ifdef _GLIBCXX_DEBUG |
4569a895 AT |
286 | PB_DS_CLASS_T_DEC |
287 | void | |
288 | PB_DS_CLASS_C_DEC:: | |
289 | assert_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 |