GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / toolchains / hndtools-arm-linux-2.6.36-uclibc-4.5.3 / arm-brcm-linux-uclibcgnueabi / include / c++ / 4.5.3 / ext / pb_ds / hash_policy.hpp
blob24c0c4562ccc046896ecaeff077bd13d61c36a77
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2009 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 3, or (at your option) any later
9 // version.
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
36 /**
37 * @file hash_policy.hpp
38 * Contains hash-related policies.
41 #ifndef PB_DS_HASH_POLICY_HPP
42 #define PB_DS_HASH_POLICY_HPP
44 #include <algorithm>
45 #include <vector>
46 #include <cmath>
47 #include <ext/pb_ds/exception.hpp>
48 #include <ext/pb_ds/detail/type_utils.hpp>
49 #include <ext/pb_ds/detail/hash_fn/mask_based_range_hashing.hpp>
50 #include <ext/pb_ds/detail/hash_fn/mod_based_range_hashing.hpp>
51 #include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_size_base.hpp>
53 namespace __gnu_pbds
55 // A null hash function, indicating that the combining hash function
56 // is actually a ranged hash function.
57 struct null_hash_fn
58 { };
60 // A null probe function, indicating that the combining probe
61 // function is actually a ranged probe function.
62 struct null_probe_fn
63 { };
65 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
66 #define PB_DS_CLASS_C_DEC linear_probe_fn<Size_Type>
68 // A probe sequence policy using fixed increments.
69 template<typename Size_Type = size_t>
70 class linear_probe_fn
72 public:
73 typedef Size_Type size_type;
75 void
76 swap(PB_DS_CLASS_C_DEC& other);
78 protected:
79 // Returns the i-th offset from the hash value.
80 inline size_type
81 operator()(size_type i) const;
84 #include <ext/pb_ds/detail/hash_fn/linear_probe_fn_imp.hpp>
86 #undef PB_DS_CLASS_T_DEC
87 #undef PB_DS_CLASS_C_DEC
89 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
90 #define PB_DS_CLASS_C_DEC quadratic_probe_fn<Size_Type>
92 // A probe sequence policy using square increments.
93 template<typename Size_Type = size_t>
94 class quadratic_probe_fn
96 public:
97 typedef Size_Type size_type;
99 void
100 swap(PB_DS_CLASS_C_DEC& other);
102 protected:
103 // Returns the i-th offset from the hash value.
104 inline size_type
105 operator()(size_type i) const;
108 #include <ext/pb_ds/detail/hash_fn/quadratic_probe_fn_imp.hpp>
110 #undef PB_DS_CLASS_T_DEC
111 #undef PB_DS_CLASS_C_DEC
113 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
114 #define PB_DS_CLASS_C_DEC direct_mask_range_hashing<Size_Type>
116 // A mask range-hashing class (uses a bit-mask).
117 template<typename Size_Type = size_t>
118 class direct_mask_range_hashing
119 : public detail::mask_based_range_hashing<Size_Type>
121 private:
122 typedef detail::mask_based_range_hashing<Size_Type> mask_based_base;
124 public:
125 typedef Size_Type size_type;
127 void
128 swap(PB_DS_CLASS_C_DEC& other);
130 protected:
131 void
132 notify_resized(size_type size);
134 // Transforms the __hash value hash into a ranged-hash value
135 // (using a bit-mask).
136 inline size_type
137 operator()(size_type hash) const;
140 #include <ext/pb_ds/detail/hash_fn/direct_mask_range_hashing_imp.hpp>
142 #undef PB_DS_CLASS_T_DEC
143 #undef PB_DS_CLASS_C_DEC
145 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
146 #define PB_DS_CLASS_C_DEC direct_mod_range_hashing<Size_Type>
148 // A mod range-hashing class (uses the modulo function).
149 template<typename Size_Type = size_t>
150 class direct_mod_range_hashing
151 : public detail::mod_based_range_hashing<Size_Type>
153 public:
154 typedef Size_Type size_type;
156 void
157 swap(PB_DS_CLASS_C_DEC& other);
159 protected:
160 void
161 notify_resized(size_type size);
163 // Transforms the __hash value hash into a ranged-hash value
164 // (using a modulo operation).
165 inline size_type
166 operator()(size_type hash) const;
168 private:
169 typedef detail::mod_based_range_hashing<size_type> mod_based_base;
172 #include <ext/pb_ds/detail/hash_fn/direct_mod_range_hashing_imp.hpp>
174 #undef PB_DS_CLASS_T_DEC
175 #undef PB_DS_CLASS_C_DEC
177 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
178 #define PB_DS_CLASS_C_DEC hash_load_check_resize_trigger<External_Load_Access, Size_Type>
179 #define PB_DS_SIZE_BASE_C_DEC detail::hash_load_check_resize_trigger_size_base<Size_Type, External_Load_Access>
181 // A resize trigger policy based on a load check. It keeps the
182 // load factor between some load factors load_min and load_max.
183 template<bool External_Load_Access = false, typename Size_Type = size_t>
184 class hash_load_check_resize_trigger : private PB_DS_SIZE_BASE_C_DEC
186 public:
187 typedef Size_Type size_type;
189 enum
191 external_load_access = External_Load_Access
194 // Default constructor, or constructor taking load_min and
195 // load_max load factors between which this policy will keep the
196 // actual load.
197 hash_load_check_resize_trigger(float load_min = 0.125,
198 float load_max = 0.5);
200 void
201 swap(hash_load_check_resize_trigger& other);
203 virtual
204 ~hash_load_check_resize_trigger();
206 // Returns a pair of the minimal and maximal loads, respectively.
207 inline std::pair<float, float>
208 get_loads() const;
210 // Sets the loads through a pair of the minimal and maximal
211 // loads, respectively.
212 void
213 set_loads(std::pair<float, float> load_pair);
215 protected:
216 inline void
217 notify_insert_search_start();
219 inline void
220 notify_insert_search_collision();
222 inline void
223 notify_insert_search_end();
225 inline void
226 notify_find_search_start();
228 inline void
229 notify_find_search_collision();
231 inline void
232 notify_find_search_end();
234 inline void
235 notify_erase_search_start();
237 inline void
238 notify_erase_search_collision();
240 inline void
241 notify_erase_search_end();
243 // Notifies an element was inserted. The total number of entries
244 // in the table is num_entries.
245 inline void
246 notify_inserted(size_type num_entries);
248 inline void
249 notify_erased(size_type num_entries);
251 // Notifies the table was cleared.
252 void
253 notify_cleared();
255 // Notifies the table was resized as a result of this object's
256 // signifying that a resize is needed.
257 void
258 notify_resized(size_type new_size);
260 void
261 notify_externally_resized(size_type new_size);
263 inline bool
264 is_resize_needed() const;
266 inline bool
267 is_grow_needed(size_type size, size_type num_entries) const;
269 private:
270 virtual void
271 do_resize(size_type new_size);
273 typedef PB_DS_SIZE_BASE_C_DEC size_base;
275 #ifdef _GLIBCXX_DEBUG
276 void
277 assert_valid() const;
278 #endif
280 float m_load_min;
281 float m_load_max;
282 size_type m_next_shrink_size;
283 size_type m_next_grow_size;
284 bool m_resize_needed;
287 #include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp>
289 #undef PB_DS_CLASS_T_DEC
290 #undef PB_DS_CLASS_C_DEC
291 #undef PB_DS_SIZE_BASE_C_DEC
293 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
294 #define PB_DS_CLASS_C_DEC cc_hash_max_collision_check_resize_trigger<External_Load_Access, Size_Type>
296 // A resize trigger policy based on collision checks. It keeps the
297 // simulated load factor lower than some given load factor.
298 template<bool External_Load_Access = false, typename Size_Type = size_t>
299 class cc_hash_max_collision_check_resize_trigger
301 public:
302 typedef Size_Type size_type;
304 enum
306 external_load_access = External_Load_Access
309 // Default constructor, or constructor taking load, a __load
310 // factor which it will attempt to maintain.
311 cc_hash_max_collision_check_resize_trigger(float load = 0.5);
313 void
314 swap(PB_DS_CLASS_C_DEC& other);
316 // Returns the current load.
317 inline float
318 get_load() const;
320 // Sets the load; does not resize the container.
321 void
322 set_load(float load);
324 protected:
325 inline void
326 notify_insert_search_start();
328 inline void
329 notify_insert_search_collision();
331 inline void
332 notify_insert_search_end();
334 inline void
335 notify_find_search_start();
337 inline void
338 notify_find_search_collision();
340 inline void
341 notify_find_search_end();
343 inline void
344 notify_erase_search_start();
346 inline void
347 notify_erase_search_collision();
349 inline void
350 notify_erase_search_end();
352 inline void
353 notify_inserted(size_type num_entries);
355 inline void
356 notify_erased(size_type num_entries);
358 void
359 notify_cleared();
361 // Notifies the table was resized as a result of this object's
362 // signifying that a resize is needed.
363 void
364 notify_resized(size_type new_size);
366 void
367 notify_externally_resized(size_type new_size);
369 inline bool
370 is_resize_needed() const;
372 inline bool
373 is_grow_needed(size_type size, size_type num_entries) const;
375 private:
376 void
377 calc_max_num_coll();
379 inline void
380 calc_resize_needed();
382 float m_load;
383 size_type m_size;
384 size_type m_num_col;
385 size_type m_max_col;
386 bool m_resize_needed;
389 #include <ext/pb_ds/detail/resize_policy/cc_hash_max_collision_check_resize_trigger_imp.hpp>
391 #undef PB_DS_CLASS_T_DEC
392 #undef PB_DS_CLASS_C_DEC
394 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
395 #define PB_DS_CLASS_C_DEC hash_exponential_size_policy<Size_Type>
397 // A size policy whose sequence of sizes form an exponential
398 // sequence (typically powers of 2.
399 template<typename Size_Type = size_t>
400 class hash_exponential_size_policy
402 public:
403 typedef Size_Type size_type;
405 // Default constructor, or onstructor taking a start_size, or
406 // constructor taking a start size and grow_factor. The policy
407 // will use the sequence of sizes start_size, start_size*
408 // grow_factor, start_size* grow_factor^2, ...
409 hash_exponential_size_policy(size_type start_size = 8,
410 size_type grow_factor = 2);
412 void
413 swap(PB_DS_CLASS_C_DEC& other);
415 protected:
416 size_type
417 get_nearest_larger_size(size_type size) const;
419 size_type
420 get_nearest_smaller_size(size_type size) const;
422 private:
423 size_type m_start_size;
424 size_type m_grow_factor;
427 #include <ext/pb_ds/detail/resize_policy/hash_exponential_size_policy_imp.hpp>
429 #undef PB_DS_CLASS_T_DEC
430 #undef PB_DS_CLASS_C_DEC
432 #define PB_DS_CLASS_T_DEC
433 #define PB_DS_CLASS_C_DEC hash_prime_size_policy
435 // A size policy whose sequence of sizes form a nearly-exponential
436 // sequence of primes.
437 class hash_prime_size_policy
439 public:
440 // Size type.
441 typedef size_t size_type;
443 // Default constructor, or onstructor taking a start_size The
444 // policy will use the sequence of sizes approximately
445 // start_size, start_size* 2, start_size* 2^2, ...
446 hash_prime_size_policy(size_type start_size = 8);
448 inline void
449 swap(PB_DS_CLASS_C_DEC& other);
451 protected:
452 size_type
453 get_nearest_larger_size(size_type size) const;
455 size_type
456 get_nearest_smaller_size(size_type size) const;
458 private:
459 size_type m_start_size;
462 #include <ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp>
464 #undef PB_DS_CLASS_T_DEC
465 #undef PB_DS_CLASS_C_DEC
467 #define PB_DS_CLASS_T_DEC template<typename Size_Policy, typename Trigger_Policy, bool External_Size_Access, typename Size_Type>
469 #define PB_DS_CLASS_C_DEC hash_standard_resize_policy<Size_Policy, Trigger_Policy, External_Size_Access, Size_Type>
471 // A resize policy which delegates operations to size and trigger policies.
472 template<typename Size_Policy = hash_exponential_size_policy<>,
473 typename Trigger_Policy = hash_load_check_resize_trigger<>,
474 bool External_Size_Access = false,
475 typename Size_Type = size_t>
476 class hash_standard_resize_policy
477 : public Size_Policy, public Trigger_Policy
479 public:
480 typedef Size_Type size_type;
481 typedef Trigger_Policy trigger_policy;
482 typedef Size_Policy size_policy;
484 enum
486 external_size_access = External_Size_Access
489 // Default constructor.
490 hash_standard_resize_policy();
492 // constructor taking some policies r_size_policy will be copied
493 // by the Size_Policy object of this object.
494 hash_standard_resize_policy(const Size_Policy& r_size_policy);
496 // constructor taking some policies. r_size_policy will be
497 // copied by the Size_Policy object of this
498 // object. r_trigger_policy will be copied by the Trigger_Policy
499 // object of this object.
500 hash_standard_resize_policy(const Size_Policy& r_size_policy,
501 const Trigger_Policy& r_trigger_policy);
503 virtual
504 ~hash_standard_resize_policy();
506 inline void
507 swap(PB_DS_CLASS_C_DEC& other);
509 // Access to the Size_Policy object used.
510 Size_Policy&
511 get_size_policy();
513 // Const access to the Size_Policy object used.
514 const Size_Policy&
515 get_size_policy() const;
517 // Access to the Trigger_Policy object used.
518 Trigger_Policy&
519 get_trigger_policy();
521 // Access to the Trigger_Policy object used.
522 const Trigger_Policy&
523 get_trigger_policy() const;
525 // Returns the actual size of the container.
526 inline size_type
527 get_actual_size() const;
529 // Resizes the container to suggested_new_size, a suggested size
530 // (the actual size will be determined by the Size_Policy
531 // object).
532 void
533 resize(size_type suggested_new_size);
535 protected:
536 inline void
537 notify_insert_search_start();
539 inline void
540 notify_insert_search_collision();
542 inline void
543 notify_insert_search_end();
545 inline void
546 notify_find_search_start();
548 inline void
549 notify_find_search_collision();
551 inline void
552 notify_find_search_end();
554 inline void
555 notify_erase_search_start();
557 inline void
558 notify_erase_search_collision();
560 inline void
561 notify_erase_search_end();
563 inline void
564 notify_inserted(size_type num_e);
566 inline void
567 notify_erased(size_type num_e);
569 void
570 notify_cleared();
572 void
573 notify_resized(size_type new_size);
575 inline bool
576 is_resize_needed() const;
578 // Queries what the new size should be, when the container is
579 // resized naturally. The current __size of the container is
580 // size, and the number of used entries within the container is
581 // num_used_e.
582 size_type
583 get_new_size(size_type size, size_type num_used_e) const;
585 private:
586 // Resizes to new_size.
587 virtual void
588 do_resize(size_type new_size);
590 typedef Trigger_Policy trigger_policy_base;
592 typedef Size_Policy size_policy_base;
594 size_type m_size;
597 #include <ext/pb_ds/detail/resize_policy/hash_standard_resize_policy_imp.hpp>
599 #undef PB_DS_CLASS_T_DEC
600 #undef PB_DS_CLASS_C_DEC
602 } // namespace __gnu_pbds
604 #endif