]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2005-2021 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. | |
19 | ||
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 cc_hash_max_collision_check_resize_trigger_imp.hpp | |
38 | * Contains a resize trigger implementation. | |
39 | */ | |
40 | ||
574dfb67 JW |
41 | #ifdef PB_DS_CLASS_C_DEC |
42 | ||
4569a895 AT |
43 | PB_DS_CLASS_T_DEC |
44 | PB_DS_CLASS_C_DEC:: | |
45 | cc_hash_max_collision_check_resize_trigger(float load) : | |
46 | m_load(load), | |
47 | m_size(0), | |
48 | m_num_col(0), | |
49 | m_max_col(0), | |
50 | m_resize_needed(false) | |
51 | { } | |
52 | ||
53 | PB_DS_CLASS_T_DEC | |
54 | inline void | |
55 | PB_DS_CLASS_C_DEC:: | |
56 | notify_find_search_start() | |
57 | { } | |
58 | ||
59 | PB_DS_CLASS_T_DEC | |
60 | inline void | |
61 | PB_DS_CLASS_C_DEC:: | |
62 | notify_find_search_collision() | |
63 | { } | |
64 | ||
65 | PB_DS_CLASS_T_DEC | |
66 | inline void | |
67 | PB_DS_CLASS_C_DEC:: | |
68 | notify_find_search_end() | |
69 | { } | |
70 | ||
71 | PB_DS_CLASS_T_DEC | |
72 | inline void | |
73 | PB_DS_CLASS_C_DEC:: | |
74 | notify_insert_search_start() | |
d7f245b1 | 75 | { m_num_col = 0; } |
4569a895 AT |
76 | |
77 | PB_DS_CLASS_T_DEC | |
78 | inline void | |
79 | PB_DS_CLASS_C_DEC:: | |
80 | notify_insert_search_collision() | |
d7f245b1 | 81 | { ++m_num_col; } |
4569a895 AT |
82 | |
83 | PB_DS_CLASS_T_DEC | |
84 | inline void | |
85 | PB_DS_CLASS_C_DEC:: | |
86 | notify_insert_search_end() | |
d7f245b1 | 87 | { calc_resize_needed(); } |
4569a895 AT |
88 | |
89 | PB_DS_CLASS_T_DEC | |
90 | inline void | |
91 | PB_DS_CLASS_C_DEC:: | |
92 | notify_erase_search_start() | |
93 | { } | |
94 | ||
95 | PB_DS_CLASS_T_DEC | |
96 | inline void | |
97 | PB_DS_CLASS_C_DEC:: | |
98 | notify_erase_search_collision() | |
99 | { } | |
100 | ||
101 | PB_DS_CLASS_T_DEC | |
102 | inline void | |
103 | PB_DS_CLASS_C_DEC:: | |
104 | notify_erase_search_end() | |
105 | { } | |
106 | ||
107 | PB_DS_CLASS_T_DEC | |
108 | inline void | |
109 | PB_DS_CLASS_C_DEC:: | |
d7f245b1 | 110 | notify_inserted(size_type) |
4569a895 AT |
111 | { } |
112 | ||
113 | PB_DS_CLASS_T_DEC | |
114 | inline void | |
115 | PB_DS_CLASS_C_DEC:: | |
d7f245b1 BK |
116 | notify_erased(size_type) |
117 | { m_resize_needed = true; } | |
4569a895 AT |
118 | |
119 | PB_DS_CLASS_T_DEC | |
120 | void | |
121 | PB_DS_CLASS_C_DEC:: | |
122 | notify_cleared() | |
d7f245b1 | 123 | { m_resize_needed = false; } |
4569a895 AT |
124 | |
125 | PB_DS_CLASS_T_DEC | |
126 | inline bool | |
127 | PB_DS_CLASS_C_DEC:: | |
128 | is_resize_needed() const | |
d7f245b1 | 129 | { return m_resize_needed; } |
4569a895 AT |
130 | |
131 | PB_DS_CLASS_T_DEC | |
132 | inline bool | |
133 | PB_DS_CLASS_C_DEC:: | |
134 | is_grow_needed(size_type /*size*/, size_type /*num_used_e*/) const | |
d7f245b1 | 135 | { return m_num_col >= m_max_col; } |
4569a895 AT |
136 | |
137 | PB_DS_CLASS_T_DEC | |
138 | void | |
139 | PB_DS_CLASS_C_DEC:: | |
140 | notify_resized(size_type new_size) | |
141 | { | |
142 | m_size = new_size; | |
143 | ||
144 | #ifdef PB_DS_HT_MAP_RESIZE_TRACE_ | |
d7f245b1 BK |
145 | std::cerr << "chmccrt::notify_resized " |
146 | << static_cast<unsigned long>(new_size) << std::endl; | |
147 | #endif | |
4569a895 AT |
148 | |
149 | calc_max_num_coll(); | |
4569a895 | 150 | calc_resize_needed(); |
4569a895 AT |
151 | m_num_col = 0; |
152 | } | |
153 | ||
154 | PB_DS_CLASS_T_DEC | |
155 | void | |
156 | PB_DS_CLASS_C_DEC:: | |
157 | calc_max_num_coll() | |
158 | { | |
159 | // max_col <-- \sqrt{2 load \ln( 2 m \ln( m ) ) } | |
113008b5 BK |
160 | const double ln_arg = 2 * m_size * std::log(double(m_size)); |
161 | m_max_col = size_type(std::ceil(std::sqrt(2 * m_load * std::log(ln_arg)))); | |
4569a895 AT |
162 | |
163 | #ifdef PB_DS_HT_MAP_RESIZE_TRACE_ | |
d7f245b1 BK |
164 | std::cerr << "chmccrt::calc_max_num_coll " |
165 | << static_cast<unsigned long>(m_size) << " " | |
166 | << static_cast<unsigned long>(m_max_col) << std::endl; | |
167 | #endif | |
4569a895 AT |
168 | } |
169 | ||
170 | PB_DS_CLASS_T_DEC | |
171 | void | |
172 | PB_DS_CLASS_C_DEC:: | |
173 | notify_externally_resized(size_type new_size) | |
d7f245b1 | 174 | { notify_resized(new_size); } |
4569a895 AT |
175 | |
176 | PB_DS_CLASS_T_DEC | |
177 | void | |
178 | PB_DS_CLASS_C_DEC:: | |
179 | swap(PB_DS_CLASS_C_DEC& other) | |
180 | { | |
181 | std::swap(m_load, other.m_load); | |
4569a895 | 182 | std::swap(m_size, other.m_size); |
4569a895 | 183 | std::swap(m_num_col, other.m_num_col); |
4569a895 | 184 | std::swap(m_max_col, other.m_max_col); |
4569a895 AT |
185 | std::swap(m_resize_needed, other.m_resize_needed); |
186 | } | |
187 | ||
188 | PB_DS_CLASS_T_DEC | |
189 | inline float | |
190 | PB_DS_CLASS_C_DEC:: | |
191 | get_load() const | |
192 | { | |
193 | PB_DS_STATIC_ASSERT(access, external_load_access); | |
d7f245b1 | 194 | return m_load; |
4569a895 AT |
195 | } |
196 | ||
197 | PB_DS_CLASS_T_DEC | |
198 | inline void | |
199 | PB_DS_CLASS_C_DEC:: | |
200 | calc_resize_needed() | |
d7f245b1 | 201 | { m_resize_needed = m_resize_needed || m_num_col >= m_max_col; } |
4569a895 AT |
202 | |
203 | PB_DS_CLASS_T_DEC | |
204 | void | |
205 | PB_DS_CLASS_C_DEC:: | |
206 | set_load(float load) | |
207 | { | |
208 | PB_DS_STATIC_ASSERT(access, external_load_access); | |
4569a895 | 209 | m_load = load; |
4569a895 | 210 | calc_max_num_coll(); |
4569a895 AT |
211 | calc_resize_needed(); |
212 | } | |
213 | ||
574dfb67 | 214 | #endif |