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