]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / ext / pb_ds / detail / resize_policy / hash_load_check_resize_trigger_imp.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007 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 PB_DS_CLASS_T_DEC
48 PB_DS_CLASS_C_DEC::
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)
52 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
53
54 PB_DS_CLASS_T_DEC
55 inline void
56 PB_DS_CLASS_C_DEC::
57 notify_find_search_start()
58 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
59
60 PB_DS_CLASS_T_DEC
61 inline void
62 PB_DS_CLASS_C_DEC::
63 notify_find_search_collision()
64 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
65
66 PB_DS_CLASS_T_DEC
67 inline void
68 PB_DS_CLASS_C_DEC::
69 notify_find_search_end()
70 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
71
72 PB_DS_CLASS_T_DEC
73 inline void
74 PB_DS_CLASS_C_DEC::
75 notify_insert_search_start()
76 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
77
78 PB_DS_CLASS_T_DEC
79 inline void
80 PB_DS_CLASS_C_DEC::
81 notify_insert_search_collision()
82 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
83
84 PB_DS_CLASS_T_DEC
85 inline void
86 PB_DS_CLASS_C_DEC::
87 notify_insert_search_end()
88 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
89
90 PB_DS_CLASS_T_DEC
91 inline void
92 PB_DS_CLASS_C_DEC::
93 notify_erase_search_start()
94 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
95
96 PB_DS_CLASS_T_DEC
97 inline void
98 PB_DS_CLASS_C_DEC::
99 notify_erase_search_collision()
100 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
101
102 PB_DS_CLASS_T_DEC
103 inline void
104 PB_DS_CLASS_C_DEC::
105 notify_erase_search_end()
106 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
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);
114   size_base::set_size(num_entries);
115   _GLIBCXX_DEBUG_ONLY(assert_valid();)
116 }
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);
124   m_resize_needed = num_entries <= m_next_shrink_size;
125   _GLIBCXX_DEBUG_ONLY(assert_valid();)
126 }
127
128 PB_DS_CLASS_T_DEC
129 inline bool
130 PB_DS_CLASS_C_DEC::
131 is_resize_needed() const
132 {
133   _GLIBCXX_DEBUG_ONLY(assert_valid();)
134   return m_resize_needed;
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 {
142   _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
143   return num_entries >= m_next_grow_size;
144 }
145
146 PB_DS_CLASS_T_DEC
147 PB_DS_CLASS_C_DEC::
148 ~hash_load_check_resize_trigger() { }
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;
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);
158
159 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
160   std::cerr << "hlcrt::notify_resized " <<
161     static_cast<unsigned long>(new_size) << "    " <<
162     static_cast<unsigned long>(m_load_min) << "    " <<
163     static_cast<unsigned long>(m_load_max) << "    " <<
164     static_cast<unsigned long>(m_next_shrink_size) << " " <<
165     static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
166 #endif 
167
168   _GLIBCXX_DEBUG_ONLY(assert_valid();)
169 }
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;
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);
179   if (new_grow_size >= m_next_grow_size)
180     {
181       _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
182       m_next_grow_size = new_grow_size;
183       _GLIBCXX_DEBUG_ONLY(assert_valid();)
184
185 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
186         std::cerr << "hlcrt::notify_externally_resized1 " <<
187         static_cast<unsigned long>(new_size) << "    " <<
188         static_cast<unsigned long>(m_load_min) << "    " <<
189         static_cast<unsigned long>(m_load_max) << "    " <<
190         static_cast<unsigned long>(m_next_shrink_size) << " " <<
191         static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
192 #endif 
193       return;
194     }
195
196   _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
197   m_next_shrink_size = new_shrink_size;
198
199 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
200   std::cerr << "hlcrt::notify_externally_resized2 " <<
201     static_cast<unsigned long>(new_size) << "    " <<
202     static_cast<unsigned long>(m_load_min) << "    " <<
203     static_cast<unsigned long>(m_load_max) << "    " <<
204     static_cast<unsigned long>(m_next_shrink_size) << " " <<
205     static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
206 #endif 
207
208   _GLIBCXX_DEBUG_ONLY(assert_valid();)
209 }
210
211 PB_DS_CLASS_T_DEC
212 void
213 PB_DS_CLASS_C_DEC::
214 notify_cleared()
215 {
216   _GLIBCXX_DEBUG_ONLY(assert_valid();)
217   size_base::set_size(0);
218   m_resize_needed = (0 < m_next_shrink_size);
219   _GLIBCXX_DEBUG_ONLY(assert_valid();)
220 }
221
222 PB_DS_CLASS_T_DEC
223 void
224 PB_DS_CLASS_C_DEC::
225 swap(PB_DS_CLASS_C_DEC& other)
226 {
227   _GLIBCXX_DEBUG_ONLY(assert_valid();)
228   _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
229     
230   size_base::swap(other);
231   std::swap(m_load_min, other.m_load_min);
232   std::swap(m_load_max, other.m_load_max);
233   std::swap(m_resize_needed, other.m_resize_needed);
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
237   _GLIBCXX_DEBUG_ONLY(assert_valid();)
238   _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
239 }
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);
247   return std::make_pair(m_load_min, m_load_max);
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);
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;
266       do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
267     }
268   catch(...)
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;
275       __throw_exception_again;
276     }
277 }
278
279 PB_DS_CLASS_T_DEC
280 void
281 PB_DS_CLASS_C_DEC::
282 do_resize(size_type)
283 { std::abort(); }
284
285 #ifdef _GLIBCXX_DEBUG
286 PB_DS_CLASS_T_DEC
287 void
288 PB_DS_CLASS_C_DEC::
289 assert_valid() const
290 {
291   _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
292   _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);
293 }
294 #endif 
295