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_standard_resize_policy_imp.hpp
44 * Contains a resize policy 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::
hash_standard_resize_policy()52 hash_standard_resize_policy()
53 : m_size(Size_Policy::get_nearest_larger_size(1))
54 { trigger_policy_base::notify_externally_resized(m_size); }
55
56 PB_DS_CLASS_T_DEC
57 PB_DS_CLASS_C_DEC::
hash_standard_resize_policy(const Size_Policy & r_size_policy)58 hash_standard_resize_policy(const Size_Policy& r_size_policy)
59 : Size_Policy(r_size_policy), m_size(Size_Policy::get_nearest_larger_size(1))
60 { trigger_policy_base::notify_externally_resized(m_size); }
61
62 PB_DS_CLASS_T_DEC
63 PB_DS_CLASS_C_DEC::
hash_standard_resize_policy(const Size_Policy & r_size_policy,const Trigger_Policy & r_trigger_policy)64 hash_standard_resize_policy(const Size_Policy& r_size_policy,
65 const Trigger_Policy& r_trigger_policy)
66 : Size_Policy(r_size_policy), Trigger_Policy(r_trigger_policy),
67 m_size(Size_Policy::get_nearest_larger_size(1))
68 { trigger_policy_base::notify_externally_resized(m_size); }
69
70 PB_DS_CLASS_T_DEC
71 PB_DS_CLASS_C_DEC::
~hash_standard_resize_policy()72 ~hash_standard_resize_policy()
73 { }
74
75 PB_DS_CLASS_T_DEC
76 void
77 PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC & other)78 swap(PB_DS_CLASS_C_DEC& other)
79 {
80 trigger_policy_base::swap(other);
81 size_policy_base::swap(other);
82 std::swap(m_size, other.m_size);
83 }
84
85 PB_DS_CLASS_T_DEC
86 inline void
87 PB_DS_CLASS_C_DEC::
notify_find_search_start()88 notify_find_search_start()
89 { trigger_policy_base::notify_find_search_start(); }
90
91 PB_DS_CLASS_T_DEC
92 inline void
93 PB_DS_CLASS_C_DEC::
notify_find_search_collision()94 notify_find_search_collision()
95 { trigger_policy_base::notify_find_search_collision(); }
96
97 PB_DS_CLASS_T_DEC
98 inline void
99 PB_DS_CLASS_C_DEC::
notify_find_search_end()100 notify_find_search_end()
101 { trigger_policy_base::notify_find_search_end(); }
102
103 PB_DS_CLASS_T_DEC
104 inline void
105 PB_DS_CLASS_C_DEC::
notify_insert_search_start()106 notify_insert_search_start()
107 { trigger_policy_base::notify_insert_search_start(); }
108
109 PB_DS_CLASS_T_DEC
110 inline void
111 PB_DS_CLASS_C_DEC::
notify_insert_search_collision()112 notify_insert_search_collision()
113 { trigger_policy_base::notify_insert_search_collision(); }
114
115 PB_DS_CLASS_T_DEC
116 inline void
117 PB_DS_CLASS_C_DEC::
notify_insert_search_end()118 notify_insert_search_end()
119 { trigger_policy_base::notify_insert_search_end(); }
120
121 PB_DS_CLASS_T_DEC
122 inline void
123 PB_DS_CLASS_C_DEC::
notify_erase_search_start()124 notify_erase_search_start()
125 { trigger_policy_base::notify_erase_search_start(); }
126
127 PB_DS_CLASS_T_DEC
128 inline void
129 PB_DS_CLASS_C_DEC::
notify_erase_search_collision()130 notify_erase_search_collision()
131 { trigger_policy_base::notify_erase_search_collision(); }
132
133 PB_DS_CLASS_T_DEC
134 inline void
135 PB_DS_CLASS_C_DEC::
notify_erase_search_end()136 notify_erase_search_end()
137 { trigger_policy_base::notify_erase_search_end(); }
138
139 PB_DS_CLASS_T_DEC
140 inline void
141 PB_DS_CLASS_C_DEC::
notify_inserted(size_type num_e)142 notify_inserted(size_type num_e)
143 { trigger_policy_base::notify_inserted(num_e); }
144
145 PB_DS_CLASS_T_DEC
146 inline void
147 PB_DS_CLASS_C_DEC::
notify_erased(size_type num_e)148 notify_erased(size_type num_e)
149 { trigger_policy_base::notify_erased(num_e); }
150
151 PB_DS_CLASS_T_DEC
152 void
153 PB_DS_CLASS_C_DEC::
notify_cleared()154 notify_cleared()
155 { trigger_policy_base::notify_cleared(); }
156
157 PB_DS_CLASS_T_DEC
158 inline bool
159 PB_DS_CLASS_C_DEC::
is_resize_needed() const160 is_resize_needed() const
161 { return trigger_policy_base::is_resize_needed(); }
162
163 PB_DS_CLASS_T_DEC
164 typename PB_DS_CLASS_C_DEC::size_type
165 PB_DS_CLASS_C_DEC::
get_new_size(size_type size,size_type num_used_e) const166 get_new_size(size_type size, size_type num_used_e) const
167 {
168 if (trigger_policy_base::is_grow_needed(size, num_used_e))
169 return size_policy_base::get_nearest_larger_size(size);
170 return size_policy_base::get_nearest_smaller_size(size);
171 }
172
173 PB_DS_CLASS_T_DEC
174 void
175 PB_DS_CLASS_C_DEC::
notify_resized(size_type new_size)176 notify_resized(size_type new_size)
177 {
178 trigger_policy_base::notify_resized(new_size);
179 m_size = new_size;
180 }
181
182 PB_DS_CLASS_T_DEC
183 inline typename PB_DS_CLASS_C_DEC::size_type
184 PB_DS_CLASS_C_DEC::
get_actual_size() const185 get_actual_size() const
186 {
187 PB_DS_STATIC_ASSERT(access, external_size_access);
188 return m_size;
189 }
190
191 PB_DS_CLASS_T_DEC
192 void
193 PB_DS_CLASS_C_DEC::
resize(size_type new_size)194 resize(size_type new_size)
195 {
196 PB_DS_STATIC_ASSERT(access, external_size_access);
197 size_type actual_size = size_policy_base::get_nearest_larger_size(1);
198 while (actual_size < new_size)
199 {
200 const size_type pot = size_policy_base::get_nearest_larger_size(actual_size);
201
202 if (pot == actual_size && pot < new_size)
203 __throw_resize_error();
204 actual_size = pot;
205 }
206
207 if (actual_size > 0)
208 --actual_size;
209
210 const size_type old_size = m_size;
211 try
212 {
213 do_resize(actual_size - 1);
214 }
215 catch(insert_error& )
216 {
217 m_size = old_size;
218 __throw_resize_error();
219 }
220 catch(...)
221 {
222 m_size = old_size;
223 __throw_exception_again;
224 }
225 }
226
227 PB_DS_CLASS_T_DEC
228 void
229 PB_DS_CLASS_C_DEC::
do_resize(size_type)230 do_resize(size_type)
231 {
232 // Do nothing
233 }
234
235 PB_DS_CLASS_T_DEC
236 Trigger_Policy&
237 PB_DS_CLASS_C_DEC::
get_trigger_policy()238 get_trigger_policy()
239 { return *this; }
240
241 PB_DS_CLASS_T_DEC
242 const Trigger_Policy&
243 PB_DS_CLASS_C_DEC::
get_trigger_policy() const244 get_trigger_policy() const
245 { return *this; }
246
247 PB_DS_CLASS_T_DEC
248 Size_Policy&
249 PB_DS_CLASS_C_DEC::
get_size_policy()250 get_size_policy()
251 { return *this; }
252
253 PB_DS_CLASS_T_DEC
254 const Size_Policy&
255 PB_DS_CLASS_C_DEC::
get_size_policy() const256 get_size_policy() const
257 { return *this; }
258
259 #undef PB_DS_STATIC_ASSERT
260
261