libstdc++
hash_prime_size_policy_imp.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2019 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.
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 // 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/>.
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_prime_size_policy_imp.hpp
38  * Contains a resize size policy implementation.
39  */
40 
41 #pragma GCC system_header
42 
43 namespace detail
44 {
45  enum
46  {
47  num_distinct_sizes_32_bit = 30,
48  num_distinct_sizes_64_bit = 62,
49  num_distinct_sizes = sizeof(std::size_t) != 8 ?
50  num_distinct_sizes_32_bit : num_distinct_sizes_64_bit,
51  };
52 
53  // Originally taken from the SGI implementation; acknowledged in the docs.
54  // Further modified (for 64 bits) from tr1's hashtable.
55  static const std::size_t g_a_sizes[num_distinct_sizes_64_bit] =
56  {
57  /* 0 */ 5ul,
58  /* 1 */ 11ul,
59  /* 2 */ 23ul,
60  /* 3 */ 47ul,
61  /* 4 */ 97ul,
62  /* 5 */ 199ul,
63  /* 6 */ 409ul,
64  /* 7 */ 823ul,
65  /* 8 */ 1741ul,
66  /* 9 */ 3469ul,
67  /* 10 */ 6949ul,
68  /* 11 */ 14033ul,
69  /* 12 */ 28411ul,
70  /* 13 */ 57557ul,
71  /* 14 */ 116731ul,
72  /* 15 */ 236897ul,
73  /* 16 */ 480881ul,
74  /* 17 */ 976369ul,
75  /* 18 */ 1982627ul,
76  /* 19 */ 4026031ul,
77  /* 20 */ 8175383ul,
78  /* 21 */ 16601593ul,
79  /* 22 */ 33712729ul,
80  /* 23 */ 68460391ul,
81  /* 24 */ 139022417ul,
82  /* 25 */ 282312799ul,
83  /* 26 */ 573292817ul,
84  /* 27 */ 1164186217ul,
85  /* 28 */ 2364114217ul,
86  /* 29 */ 4294967291ul,
87  /* 30 */ (std::size_t)8589934583ull,
88  /* 31 */ (std::size_t)17179869143ull,
89  /* 32 */ (std::size_t)34359738337ull,
90  /* 33 */ (std::size_t)68719476731ull,
91  /* 34 */ (std::size_t)137438953447ull,
92  /* 35 */ (std::size_t)274877906899ull,
93  /* 36 */ (std::size_t)549755813881ull,
94  /* 37 */ (std::size_t)1099511627689ull,
95  /* 38 */ (std::size_t)2199023255531ull,
96  /* 39 */ (std::size_t)4398046511093ull,
97  /* 40 */ (std::size_t)8796093022151ull,
98  /* 41 */ (std::size_t)17592186044399ull,
99  /* 42 */ (std::size_t)35184372088777ull,
100  /* 43 */ (std::size_t)70368744177643ull,
101  /* 44 */ (std::size_t)140737488355213ull,
102  /* 45 */ (std::size_t)281474976710597ull,
103  /* 46 */ (std::size_t)562949953421231ull,
104  /* 47 */ (std::size_t)1125899906842597ull,
105  /* 48 */ (std::size_t)2251799813685119ull,
106  /* 49 */ (std::size_t)4503599627370449ull,
107  /* 50 */ (std::size_t)9007199254740881ull,
108  /* 51 */ (std::size_t)18014398509481951ull,
109  /* 52 */ (std::size_t)36028797018963913ull,
110  /* 53 */ (std::size_t)72057594037927931ull,
111  /* 54 */ (std::size_t)144115188075855859ull,
112  /* 55 */ (std::size_t)288230376151711717ull,
113  /* 56 */ (std::size_t)576460752303423433ull,
114  /* 57 */ (std::size_t)1152921504606846883ull,
115  /* 58 */ (std::size_t)2305843009213693951ull,
116  /* 59 */ (std::size_t)4611686018427387847ull,
117  /* 60 */ (std::size_t)9223372036854775783ull,
118  /* 61 */ (std::size_t)18446744073709551557ull,
119  };
120 
121 } // namespace detail
122 
123 PB_DS_CLASS_T_DEC
124 inline
125 PB_DS_CLASS_C_DEC::
126 hash_prime_size_policy(size_type n) : m_start_size(n)
127 { m_start_size = get_nearest_larger_size(n); }
128 
129 PB_DS_CLASS_T_DEC
130 inline void
131 PB_DS_CLASS_C_DEC::
132 swap(PB_DS_CLASS_C_DEC& other)
133 { std::swap(m_start_size, other.m_start_size); }
134 
135 PB_DS_CLASS_T_DEC
136 inline PB_DS_CLASS_C_DEC::size_type
137 PB_DS_CLASS_C_DEC::
138 get_nearest_larger_size(size_type n) const
139 {
140  const std::size_t* const p_upper = std::upper_bound(detail::g_a_sizes,
141  detail::g_a_sizes + detail::num_distinct_sizes, n);
142 
143  if (p_upper == detail::g_a_sizes + detail::num_distinct_sizes)
144  __throw_resize_error();
145  return *p_upper;
146 }
147 
148 PB_DS_CLASS_T_DEC
149 inline PB_DS_CLASS_C_DEC::size_type
150 PB_DS_CLASS_C_DEC::
151 get_nearest_smaller_size(size_type n) const
152 {
153  const std::size_t* p_lower = std::lower_bound(detail::g_a_sizes,
154  detail::g_a_sizes + detail::num_distinct_sizes, n);
155 
156  if (*p_lower >= n && p_lower != detail::g_a_sizes)
157  --p_lower;
158  if (*p_lower < m_start_size)
159  return m_start_size;
160  return *p_lower;
161 }
_ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)
Finds the first position in which val could be inserted without changing the ordering.
_ForwardIterator upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val, _Compare __comp)
Finds the last position in which __val could be inserted without changing the ordering.
Definition: stl_algo.h:2106