]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libstdc++/include/ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libstdc++ / 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)>)> UNIQUE##static_assert_type
49
50 PB_DS_CLASS_T_DEC
51 PB_DS_CLASS_C_DEC::
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)
55 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
56
57 PB_DS_CLASS_T_DEC
58 inline void
59 PB_DS_CLASS_C_DEC::
60 notify_find_search_start()
61 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
62
63 PB_DS_CLASS_T_DEC
64 inline void
65 PB_DS_CLASS_C_DEC::
66 notify_find_search_collision()
67 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
68
69 PB_DS_CLASS_T_DEC
70 inline void
71 PB_DS_CLASS_C_DEC::
72 notify_find_search_end()
73 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
74
75 PB_DS_CLASS_T_DEC
76 inline void
77 PB_DS_CLASS_C_DEC::
78 notify_insert_search_start()
79 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
80
81 PB_DS_CLASS_T_DEC
82 inline void
83 PB_DS_CLASS_C_DEC::
84 notify_insert_search_collision()
85 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
86
87 PB_DS_CLASS_T_DEC
88 inline void
89 PB_DS_CLASS_C_DEC::
90 notify_insert_search_end()
91 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
92
93 PB_DS_CLASS_T_DEC
94 inline void
95 PB_DS_CLASS_C_DEC::
96 notify_erase_search_start()
97 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
98
99 PB_DS_CLASS_T_DEC
100 inline void
101 PB_DS_CLASS_C_DEC::
102 notify_erase_search_collision()
103 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
104
105 PB_DS_CLASS_T_DEC
106 inline void
107 PB_DS_CLASS_C_DEC::
108 notify_erase_search_end()
109 { _GLIBCXX_DEBUG_ONLY(assert_valid();) }
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);
117   size_base::set_size(num_entries);
118   _GLIBCXX_DEBUG_ONLY(assert_valid();)
119 }
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);
127   m_resize_needed = num_entries <= m_next_shrink_size;
128   _GLIBCXX_DEBUG_ONLY(assert_valid();)
129 }
130
131 PB_DS_CLASS_T_DEC
132 inline bool
133 PB_DS_CLASS_C_DEC::
134 is_resize_needed() const
135 {
136   _GLIBCXX_DEBUG_ONLY(assert_valid();)
137   return m_resize_needed;
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 {
145   _GLIBCXX_DEBUG_ASSERT(m_resize_needed);
146   return num_entries >= m_next_grow_size;
147 }
148
149 PB_DS_CLASS_T_DEC
150 PB_DS_CLASS_C_DEC::
151 ~hash_load_check_resize_trigger() { }
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;
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);
161
162 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
163   std::cerr << "hlcrt::notify_resized " <<
164     static_cast<unsigned long>(new_size) << "    " <<
165     static_cast<unsigned long>(m_load_min) << "    " <<
166     static_cast<unsigned long>(m_load_max) << "    " <<
167     static_cast<unsigned long>(m_next_shrink_size) << " " <<
168     static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
169 #endif 
170
171   _GLIBCXX_DEBUG_ONLY(assert_valid();)
172 }
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;
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 );
182   if (new_grow_size >= m_next_grow_size)
183     {
184       _GLIBCXX_DEBUG_ASSERT(new_shrink_size > m_next_shrink_size);
185       m_next_grow_size = new_grow_size;
186       _GLIBCXX_DEBUG_ONLY(assert_valid();)
187
188 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
189         std::cerr << "hlcrt::notify_externally_resized1 " <<
190         static_cast<unsigned long>(new_size) << "    " <<
191         static_cast<unsigned long>(m_load_min) << "    " <<
192         static_cast<unsigned long>(m_load_max) << "    " <<
193         static_cast<unsigned long>(m_next_shrink_size) << " " <<
194         static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
195 #endif 
196       return;
197     }
198
199   _GLIBCXX_DEBUG_ASSERT(new_shrink_size <= m_next_shrink_size);
200   m_next_shrink_size = new_shrink_size;
201
202 #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
203   std::cerr << "hlcrt::notify_externally_resized2 " <<
204     static_cast<unsigned long>(new_size) << "    " <<
205     static_cast<unsigned long>(m_load_min) << "    " <<
206     static_cast<unsigned long>(m_load_max) << "    " <<
207     static_cast<unsigned long>(m_next_shrink_size) << " " <<
208     static_cast<unsigned long>(m_next_grow_size) << "    " << std::endl;
209 #endif 
210
211   _GLIBCXX_DEBUG_ONLY(assert_valid();)
212 }
213
214 PB_DS_CLASS_T_DEC
215 void
216 PB_DS_CLASS_C_DEC::
217 notify_cleared()
218 {
219   _GLIBCXX_DEBUG_ONLY(assert_valid();)
220   size_base::set_size(0);
221   m_resize_needed = (0 < m_next_shrink_size);
222   _GLIBCXX_DEBUG_ONLY(assert_valid();)
223 }
224
225 PB_DS_CLASS_T_DEC
226 void
227 PB_DS_CLASS_C_DEC::
228 swap(PB_DS_CLASS_C_DEC& other)
229 {
230   _GLIBCXX_DEBUG_ONLY(assert_valid();)
231   _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
232     
233   size_base::swap(other);
234   std::swap(m_load_min, other.m_load_min);
235   std::swap(m_load_max, other.m_load_max);
236   std::swap(m_resize_needed, other.m_resize_needed);
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
240   _GLIBCXX_DEBUG_ONLY(assert_valid();)
241   _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
242 }
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);
250   return std::make_pair(m_load_min, m_load_max);
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);
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;
269       do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
270     }
271   catch (...)
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;
278       __throw_exception_again;
279     }
280 }
281
282 PB_DS_CLASS_T_DEC
283 void
284 PB_DS_CLASS_C_DEC::
285 do_resize(size_type)
286 { abort(); }
287
288 #ifdef _GLIBCXX_DEBUG
289 PB_DS_CLASS_T_DEC
290 void
291 PB_DS_CLASS_C_DEC::
292 assert_valid() const
293 {
294   _GLIBCXX_DEBUG_ASSERT(m_load_max > m_load_min);
295   _GLIBCXX_DEBUG_ASSERT(m_next_grow_size >= m_next_shrink_size);
296 }
297 #endif 
298
299 #undef PB_DS_STATIC_ASSERT
300