pb_assoc: Delete.
[gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / resize_policy / hash_load_check_resize_trigger_imp.hpp
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
47 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
48 typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> \
49 UNIQUE##static_assert_type
50
51 PB_DS_CLASS_T_DEC
52 PB_DS_CLASS_C_DEC::
53 hash_load_check_resize_trigger(float load_min, float load_max) :
54 m_load_min(load_min),
55 m_load_max(load_max),
56 m_next_shrink_size(0),
57 m_next_grow_size(0),
58 m_resize_needed(false)
59 {
60 PB_DS_DBG_ONLY(assert_valid();)
61 }
62
63 PB_DS_CLASS_T_DEC
64 inline void
65 PB_DS_CLASS_C_DEC::
66 notify_find_search_start()
67 {
68 PB_DS_DBG_ONLY(assert_valid();)
69 }
70
71 PB_DS_CLASS_T_DEC
72 inline void
73 PB_DS_CLASS_C_DEC::
74 notify_find_search_collision()
75 {
76 PB_DS_DBG_ONLY(assert_valid();)
77 }
78
79 PB_DS_CLASS_T_DEC
80 inline void
81 PB_DS_CLASS_C_DEC::
82 notify_find_search_end()
83 {
84 PB_DS_DBG_ONLY(assert_valid();)
85 }
86
87 PB_DS_CLASS_T_DEC
88 inline void
89 PB_DS_CLASS_C_DEC::
90 notify_insert_search_start()
91 {
92 PB_DS_DBG_ONLY(assert_valid();)
93 }
94
95 PB_DS_CLASS_T_DEC
96 inline void
97 PB_DS_CLASS_C_DEC::
98 notify_insert_search_collision()
99 {
100 PB_DS_DBG_ONLY(assert_valid();)
101 }
102
103 PB_DS_CLASS_T_DEC
104 inline void
105 PB_DS_CLASS_C_DEC::
106 notify_insert_search_end()
107 {
108 PB_DS_DBG_ONLY(assert_valid();)
109 }
110
111 PB_DS_CLASS_T_DEC
112 inline void
113 PB_DS_CLASS_C_DEC::
114 notify_erase_search_start()
115 {
116 PB_DS_DBG_ONLY(assert_valid();)
117 }
118
119 PB_DS_CLASS_T_DEC
120 inline void
121 PB_DS_CLASS_C_DEC::
122 notify_erase_search_collision()
123 {
124 PB_DS_DBG_ONLY(assert_valid();)
125 }
126
127 PB_DS_CLASS_T_DEC
128 inline void
129 PB_DS_CLASS_C_DEC::
130 notify_erase_search_end()
131 {
132 PB_DS_DBG_ONLY(assert_valid();)
133 }
134
135 PB_DS_CLASS_T_DEC
136 inline void
137 PB_DS_CLASS_C_DEC::
138 notify_inserted(size_type num_entries)
139 {
140 m_resize_needed = (num_entries >= m_next_grow_size);
141
142 size_base::set_size(num_entries);
143
144 PB_DS_DBG_ONLY(assert_valid();)
145 }
146
147 PB_DS_CLASS_T_DEC
148 inline void
149 PB_DS_CLASS_C_DEC::
150 notify_erased(size_type num_entries)
151 {
152 size_base::set_size(num_entries);
153
154 m_resize_needed =
155 num_entries <= m_next_shrink_size;
156
157 PB_DS_DBG_ONLY(assert_valid();)
158 }
159
160 PB_DS_CLASS_T_DEC
161 inline bool
162 PB_DS_CLASS_C_DEC::
163 is_resize_needed() const
164 {
165 PB_DS_DBG_ONLY(assert_valid();)
166
167 return (m_resize_needed);
168 }
169
170 PB_DS_CLASS_T_DEC
171 inline bool
172 PB_DS_CLASS_C_DEC::
173 is_grow_needed(size_type /*size*/, size_type num_entries) const
174 {
175 PB_DS_DBG_ASSERT(m_resize_needed);
176
177 return (num_entries >= m_next_grow_size);
178 }
179
180 PB_DS_CLASS_T_DEC
181 PB_DS_CLASS_C_DEC::
182 ~hash_load_check_resize_trigger()
183 { }
184
185 PB_DS_CLASS_T_DEC
186 void
187 PB_DS_CLASS_C_DEC::
188 notify_resized(size_type new_size)
189 {
190 m_resize_needed = false;
191
192 m_next_grow_size =
193 size_type(m_load_max* new_size - 1);
194
195 m_next_shrink_size =
196 size_type(m_load_min* new_size);
197
198 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
199 std::cerr << "hlcrt::notify_resized " <<
200 static_cast<unsigned long>(new_size) << " " <<
201 static_cast<unsigned long>(m_load_min) << " " <<
202 static_cast<unsigned long>(m_load_max) << " " <<
203 static_cast<unsigned long>(m_next_shrink_size) << " " <<
204 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
205 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
206
207 PB_DS_DBG_ONLY(assert_valid();)
208 }
209
210 PB_DS_CLASS_T_DEC
211 void
212 PB_DS_CLASS_C_DEC::
213 notify_externally_resized(size_type new_size)
214 {
215 m_resize_needed = false;
216
217 size_type new_grow_size =
218 size_type(m_load_max* new_size - 1);
219
220 size_type new_shrink_size =
221 size_type(m_load_min* new_size );
222
223 if (new_grow_size >= m_next_grow_size)
224 {
225 PB_DS_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
226
227 m_next_grow_size = new_grow_size;
228
229 PB_DS_DBG_ONLY(assert_valid();)
230
231 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
232 std::cerr << "hlcrt::notify_externally_resized1 " <<
233 static_cast<unsigned long>(new_size) << " " <<
234 static_cast<unsigned long>(m_load_min) << " " <<
235 static_cast<unsigned long>(m_load_max) << " " <<
236 static_cast<unsigned long>(m_next_shrink_size) << " " <<
237 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
238 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
239
240 return;
241 }
242
243 PB_DS_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
244
245 m_next_shrink_size = new_shrink_size;
246
247 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
248 std::cerr << "hlcrt::notify_externally_resized2 " <<
249 static_cast<unsigned long>(new_size) << " " <<
250 static_cast<unsigned long>(m_load_min) << " " <<
251 static_cast<unsigned long>(m_load_max) << " " <<
252 static_cast<unsigned long>(m_next_shrink_size) << " " <<
253 static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
254 #endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
255
256 PB_DS_DBG_ONLY(assert_valid();)
257 }
258
259 PB_DS_CLASS_T_DEC
260 void
261 PB_DS_CLASS_C_DEC::
262 notify_cleared()
263 {
264 PB_DS_DBG_ONLY(assert_valid();)
265
266 size_base::set_size(0);
267
268 m_resize_needed = (0 < m_next_shrink_size);
269
270 PB_DS_DBG_ONLY(assert_valid();)
271 }
272
273 PB_DS_CLASS_T_DEC
274 void
275 PB_DS_CLASS_C_DEC::
276 swap(PB_DS_CLASS_C_DEC& other)
277 {
278 PB_DS_DBG_ONLY(assert_valid();)
279 PB_DS_DBG_ONLY(other.assert_valid();)
280
281 size_base::swap(other);
282
283 std::swap(m_load_min, other.m_load_min);
284 std::swap(m_load_max, other.m_load_max);
285
286 std::swap(m_resize_needed, other.m_resize_needed);
287
288 std::swap(m_next_grow_size, other.m_next_grow_size);
289 std::swap(m_next_shrink_size, other.m_next_shrink_size);
290
291 PB_DS_DBG_ONLY(assert_valid();)
292 PB_DS_DBG_ONLY(other.assert_valid();)
293 }
294
295 PB_DS_CLASS_T_DEC
296 inline std::pair<float, float>
297 PB_DS_CLASS_C_DEC::
298 get_loads() const
299 {
300 PB_DS_STATIC_ASSERT(access, external_load_access);
301
302 return (std::make_pair(m_load_min, m_load_max));
303 }
304
305 PB_DS_CLASS_T_DEC
306 void
307 PB_DS_CLASS_C_DEC::
308 set_loads(std::pair<float, float> load_pair)
309 {
310 PB_DS_STATIC_ASSERT(access, external_load_access);
311
312 const float old_load_min = m_load_min;
313 const float old_load_max = m_load_max;
314 const size_type old_next_shrink_size = m_next_shrink_size;
315 const size_type old_next_grow_size = m_next_grow_size;
316 const bool old_resize_needed = m_resize_needed;
317
318 try
319 {
320 m_load_min = load_pair.first;
321 m_load_max = load_pair.second;
322
323 do_resize(static_cast<size_type>(
324 size_base::get_size() / ((m_load_min + m_load_max) / 2)));
325 }
326 catch(...)
327 {
328 m_load_min = old_load_min;
329 m_load_max = old_load_max;
330 m_next_shrink_size = old_next_shrink_size;
331 m_next_grow_size = old_next_grow_size;
332 m_resize_needed = old_resize_needed;
333
334 throw;
335 }
336 }
337
338 PB_DS_CLASS_T_DEC
339 void
340 PB_DS_CLASS_C_DEC::
341 do_resize(size_type /*new_size*/)
342 {
343 abort();
344 }
345
346 #ifdef PB_DS_HASH_POLICY_DEBUG
347 PB_DS_CLASS_T_DEC
348 void
349 PB_DS_CLASS_C_DEC::
350 assert_valid() const
351 {
352 PB_DS_DBG_ASSERT(m_load_max > m_load_min);
353
354 PB_DS_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size);
355 }
356 #endif // #ifdef PB_DS_HASH_POLICY_DEBUG
357
358 #undef PB_DS_STATIC_ASSERT
359