]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
aa118a03 | 3 | // Copyright (C) 2005-2014 Free Software Foundation, Inc. |
4569a895 AT |
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 | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
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 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
4569a895 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
4569a895 AT |
24 | |
25 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
26 | ||
27 | // Permission to use, copy, modify, sell, and distribute this software | |
28 | // is hereby granted without fee, provided that the above copyright | |
29 | // notice appears in all copies, and that both that copyright notice | |
30 | // and this permission notice appear in supporting documentation. None | |
31 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
32 | // representation about the suitability of this software for any | |
33 | // purpose. It is provided "as is" without express or implied | |
34 | // warranty. | |
35 | ||
36 | /** | |
37 | * @file hash_standard_resize_policy_imp.hpp | |
38 | * Contains a resize policy implementation. | |
39 | */ | |
40 | ||
4569a895 AT |
41 | PB_DS_CLASS_T_DEC |
42 | PB_DS_CLASS_C_DEC:: | |
3441f106 BK |
43 | hash_standard_resize_policy() |
44 | : m_size(Size_Policy::get_nearest_larger_size(1)) | |
d7f245b1 | 45 | { trigger_policy_base::notify_externally_resized(m_size); } |
4569a895 AT |
46 | |
47 | PB_DS_CLASS_T_DEC | |
48 | PB_DS_CLASS_C_DEC:: | |
3441f106 BK |
49 | hash_standard_resize_policy(const Size_Policy& r_size_policy) |
50 | : Size_Policy(r_size_policy), m_size(Size_Policy::get_nearest_larger_size(1)) | |
d7f245b1 | 51 | { trigger_policy_base::notify_externally_resized(m_size); } |
4569a895 AT |
52 | |
53 | PB_DS_CLASS_T_DEC | |
54 | PB_DS_CLASS_C_DEC:: | |
d7f245b1 | 55 | hash_standard_resize_policy(const Size_Policy& r_size_policy, |
3441f106 BK |
56 | const Trigger_Policy& r_trigger_policy) |
57 | : Size_Policy(r_size_policy), Trigger_Policy(r_trigger_policy), | |
4569a895 | 58 | m_size(Size_Policy::get_nearest_larger_size(1)) |
d7f245b1 | 59 | { trigger_policy_base::notify_externally_resized(m_size); } |
4569a895 AT |
60 | |
61 | PB_DS_CLASS_T_DEC | |
62 | PB_DS_CLASS_C_DEC:: | |
63 | ~hash_standard_resize_policy() | |
64 | { } | |
65 | ||
66 | PB_DS_CLASS_T_DEC | |
67 | void | |
68 | PB_DS_CLASS_C_DEC:: | |
69 | swap(PB_DS_CLASS_C_DEC& other) | |
70 | { | |
71 | trigger_policy_base::swap(other); | |
4569a895 | 72 | size_policy_base::swap(other); |
4569a895 AT |
73 | std::swap(m_size, other.m_size); |
74 | } | |
75 | ||
76 | PB_DS_CLASS_T_DEC | |
77 | inline void | |
78 | PB_DS_CLASS_C_DEC:: | |
79 | notify_find_search_start() | |
d7f245b1 | 80 | { trigger_policy_base::notify_find_search_start(); } |
4569a895 AT |
81 | |
82 | PB_DS_CLASS_T_DEC | |
83 | inline void | |
84 | PB_DS_CLASS_C_DEC:: | |
85 | notify_find_search_collision() | |
d7f245b1 | 86 | { trigger_policy_base::notify_find_search_collision(); } |
4569a895 AT |
87 | |
88 | PB_DS_CLASS_T_DEC | |
89 | inline void | |
90 | PB_DS_CLASS_C_DEC:: | |
91 | notify_find_search_end() | |
d7f245b1 | 92 | { trigger_policy_base::notify_find_search_end(); } |
4569a895 AT |
93 | |
94 | PB_DS_CLASS_T_DEC | |
95 | inline void | |
96 | PB_DS_CLASS_C_DEC:: | |
97 | notify_insert_search_start() | |
d7f245b1 | 98 | { trigger_policy_base::notify_insert_search_start(); } |
4569a895 AT |
99 | |
100 | PB_DS_CLASS_T_DEC | |
101 | inline void | |
102 | PB_DS_CLASS_C_DEC:: | |
103 | notify_insert_search_collision() | |
d7f245b1 | 104 | { trigger_policy_base::notify_insert_search_collision(); } |
4569a895 AT |
105 | |
106 | PB_DS_CLASS_T_DEC | |
107 | inline void | |
108 | PB_DS_CLASS_C_DEC:: | |
109 | notify_insert_search_end() | |
d7f245b1 | 110 | { trigger_policy_base::notify_insert_search_end(); } |
4569a895 AT |
111 | |
112 | PB_DS_CLASS_T_DEC | |
113 | inline void | |
114 | PB_DS_CLASS_C_DEC:: | |
115 | notify_erase_search_start() | |
d7f245b1 | 116 | { trigger_policy_base::notify_erase_search_start(); } |
4569a895 AT |
117 | |
118 | PB_DS_CLASS_T_DEC | |
119 | inline void | |
120 | PB_DS_CLASS_C_DEC:: | |
121 | notify_erase_search_collision() | |
d7f245b1 | 122 | { trigger_policy_base::notify_erase_search_collision(); } |
4569a895 AT |
123 | |
124 | PB_DS_CLASS_T_DEC | |
125 | inline void | |
126 | PB_DS_CLASS_C_DEC:: | |
127 | notify_erase_search_end() | |
d7f245b1 | 128 | { trigger_policy_base::notify_erase_search_end(); } |
4569a895 AT |
129 | |
130 | PB_DS_CLASS_T_DEC | |
131 | inline void | |
132 | PB_DS_CLASS_C_DEC:: | |
133 | notify_inserted(size_type num_e) | |
d7f245b1 | 134 | { trigger_policy_base::notify_inserted(num_e); } |
4569a895 AT |
135 | |
136 | PB_DS_CLASS_T_DEC | |
137 | inline void | |
138 | PB_DS_CLASS_C_DEC:: | |
139 | notify_erased(size_type num_e) | |
d7f245b1 | 140 | { trigger_policy_base::notify_erased(num_e); } |
4569a895 AT |
141 | |
142 | PB_DS_CLASS_T_DEC | |
143 | void | |
144 | PB_DS_CLASS_C_DEC:: | |
145 | notify_cleared() | |
d7f245b1 | 146 | { trigger_policy_base::notify_cleared(); } |
4569a895 AT |
147 | |
148 | PB_DS_CLASS_T_DEC | |
149 | inline bool | |
150 | PB_DS_CLASS_C_DEC:: | |
151 | is_resize_needed() const | |
d7f245b1 | 152 | { return trigger_policy_base::is_resize_needed(); } |
4569a895 AT |
153 | |
154 | PB_DS_CLASS_T_DEC | |
155 | typename PB_DS_CLASS_C_DEC::size_type | |
156 | PB_DS_CLASS_C_DEC:: | |
157 | get_new_size(size_type size, size_type num_used_e) const | |
158 | { | |
3441f106 | 159 | if (trigger_policy_base::is_grow_needed(size, num_used_e)) |
d7f245b1 BK |
160 | return size_policy_base::get_nearest_larger_size(size); |
161 | return size_policy_base::get_nearest_smaller_size(size); | |
4569a895 AT |
162 | } |
163 | ||
164 | PB_DS_CLASS_T_DEC | |
165 | void | |
166 | PB_DS_CLASS_C_DEC:: | |
167 | notify_resized(size_type new_size) | |
168 | { | |
169 | trigger_policy_base::notify_resized(new_size); | |
4569a895 AT |
170 | m_size = new_size; |
171 | } | |
172 | ||
173 | PB_DS_CLASS_T_DEC | |
174 | inline typename PB_DS_CLASS_C_DEC::size_type | |
175 | PB_DS_CLASS_C_DEC:: | |
176 | get_actual_size() const | |
177 | { | |
178 | PB_DS_STATIC_ASSERT(access, external_size_access); | |
d7f245b1 | 179 | return m_size; |
4569a895 AT |
180 | } |
181 | ||
182 | PB_DS_CLASS_T_DEC | |
183 | void | |
184 | PB_DS_CLASS_C_DEC:: | |
185 | resize(size_type new_size) | |
186 | { | |
187 | PB_DS_STATIC_ASSERT(access, external_size_access); | |
3441f106 BK |
188 | size_type actual_size = size_policy_base::get_nearest_larger_size(1); |
189 | while (actual_size < new_size) | |
4569a895 | 190 | { |
3441f106 | 191 | const size_type pot = size_policy_base::get_nearest_larger_size(actual_size); |
4569a895 | 192 | |
3441f106 | 193 | if (pot == actual_size && pot < new_size) |
8fafc2d3 | 194 | __throw_resize_error(); |
3441f106 | 195 | actual_size = pot; |
4569a895 AT |
196 | } |
197 | ||
3441f106 BK |
198 | if (actual_size > 0) |
199 | --actual_size; | |
4569a895 AT |
200 | |
201 | const size_type old_size = m_size; | |
bc2631e0 | 202 | __try |
4569a895 | 203 | { |
3441f106 | 204 | do_resize(actual_size - 1); |
4569a895 | 205 | } |
bc2631e0 | 206 | __catch(insert_error& ) |
4569a895 AT |
207 | { |
208 | m_size = old_size; | |
8fafc2d3 | 209 | __throw_resize_error(); |
4569a895 | 210 | } |
bc2631e0 | 211 | __catch(...) |
4569a895 AT |
212 | { |
213 | m_size = old_size; | |
8fafc2d3 | 214 | __throw_exception_again; |
4569a895 AT |
215 | } |
216 | } | |
217 | ||
218 | PB_DS_CLASS_T_DEC | |
219 | void | |
220 | PB_DS_CLASS_C_DEC:: | |
d7f245b1 | 221 | do_resize(size_type) |
4569a895 AT |
222 | { |
223 | // Do nothing | |
224 | } | |
225 | ||
226 | PB_DS_CLASS_T_DEC | |
227 | Trigger_Policy& | |
228 | PB_DS_CLASS_C_DEC:: | |
229 | get_trigger_policy() | |
d7f245b1 | 230 | { return *this; } |
4569a895 AT |
231 | |
232 | PB_DS_CLASS_T_DEC | |
233 | const Trigger_Policy& | |
234 | PB_DS_CLASS_C_DEC:: | |
235 | get_trigger_policy() const | |
d7f245b1 | 236 | { return *this; } |
4569a895 AT |
237 | |
238 | PB_DS_CLASS_T_DEC | |
239 | Size_Policy& | |
240 | PB_DS_CLASS_C_DEC:: | |
241 | get_size_policy() | |
d7f245b1 | 242 | { return *this; } |
4569a895 AT |
243 | |
244 | PB_DS_CLASS_T_DEC | |
245 | const Size_Policy& | |
246 | PB_DS_CLASS_C_DEC:: | |
247 | get_size_policy() const | |
d7f245b1 | 248 | { return *this; } |
4569a895 | 249 |