libstdc++
hash_prime_size_policy_imp.hpp
Go to the documentation of this file.
1// -*- C++ -*-
2
3// Copyright (C) 2005-2025 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#ifdef PB_DS_CLASS_C_DEC
42
43#ifdef _GLIBCXX_SYSHDR
44#pragma GCC system_header
45#endif
46
47namespace detail
48{
49 enum
50 {
51 num_distinct_sizes_16_bit = 14,
52 num_distinct_sizes_32_bit = 30,
53 num_distinct_sizes_64_bit = 62,
54 // The number of values is limited by the width of size_t.
55 // Maybe we could just use (__SIZE_WIDTH__ - 2) here.
56#if __SIZE_WIDTH__ >= 64
57 num_distinct_sizes = num_distinct_sizes_64_bit
58#elif __SIZE_WIDTH__ >= 32
59 num_distinct_sizes = num_distinct_sizes_32_bit
60#else
61 num_distinct_sizes = num_distinct_sizes_16_bit
62#endif
63 };
64
65#pragma GCC diagnostic push
66#pragma GCC diagnostic ignored "-Wlong-long"
67 // Originally taken from the SGI implementation; acknowledged in the docs.
68 // Further modified (for 64 bits) from tr1's hashtable.
69 static const std::size_t g_a_sizes[num_distinct_sizes] =
70 {
71 /* 0 */ 5ul,
72 /* 1 */ 11ul,
73 /* 2 */ 23ul,
74 /* 3 */ 47ul,
75 /* 4 */ 97ul,
76 /* 5 */ 199ul,
77 /* 6 */ 409ul,
78 /* 7 */ 823ul,
79 /* 8 */ 1741ul,
80 /* 9 */ 3469ul,
81 /* 10 */ 6949ul,
82 /* 11 */ 14033ul,
83 /* 12 */ 28411ul,
84 /* 13 */ 57557ul,
85#if __SIZE_WIDTH__ >= 32
86 /* 14 */ 116731ul,
87 /* 15 */ 236897ul,
88 /* 16 */ 480881ul,
89 /* 17 */ 976369ul,
90 /* 18 */ 1982627ul,
91 /* 19 */ 4026031ul,
92 /* 20 */ 8175383ul,
93 /* 21 */ 16601593ul,
94 /* 22 */ 33712729ul,
95 /* 23 */ 68460391ul,
96 /* 24 */ 139022417ul,
97 /* 25 */ 282312799ul,
98 /* 26 */ 573292817ul,
99 /* 27 */ 1164186217ul,
100 /* 28 */ 2364114217ul,
101 /* 29 */ 4294967291ul,
102#if __SIZE_WIDTH__ >= 64
103 /* 30 */ (std::size_t)8589934583ull,
104 /* 31 */ (std::size_t)17179869143ull,
105 /* 32 */ (std::size_t)34359738337ull,
106 /* 33 */ (std::size_t)68719476731ull,
107 /* 34 */ (std::size_t)137438953447ull,
108 /* 35 */ (std::size_t)274877906899ull,
109 /* 36 */ (std::size_t)549755813881ull,
110 /* 37 */ (std::size_t)1099511627689ull,
111 /* 38 */ (std::size_t)2199023255531ull,
112 /* 39 */ (std::size_t)4398046511093ull,
113 /* 40 */ (std::size_t)8796093022151ull,
114 /* 41 */ (std::size_t)17592186044399ull,
115 /* 42 */ (std::size_t)35184372088777ull,
116 /* 43 */ (std::size_t)70368744177643ull,
117 /* 44 */ (std::size_t)140737488355213ull,
118 /* 45 */ (std::size_t)281474976710597ull,
119 /* 46 */ (std::size_t)562949953421231ull,
120 /* 47 */ (std::size_t)1125899906842597ull,
121 /* 48 */ (std::size_t)2251799813685119ull,
122 /* 49 */ (std::size_t)4503599627370449ull,
123 /* 50 */ (std::size_t)9007199254740881ull,
124 /* 51 */ (std::size_t)18014398509481951ull,
125 /* 52 */ (std::size_t)36028797018963913ull,
126 /* 53 */ (std::size_t)72057594037927931ull,
127 /* 54 */ (std::size_t)144115188075855859ull,
128 /* 55 */ (std::size_t)288230376151711717ull,
129 /* 56 */ (std::size_t)576460752303423433ull,
130 /* 57 */ (std::size_t)1152921504606846883ull,
131 /* 58 */ (std::size_t)2305843009213693951ull,
132 /* 59 */ (std::size_t)4611686018427387847ull,
133 /* 60 */ (std::size_t)9223372036854775783ull,
134 /* 61 */ (std::size_t)18446744073709551557ull,
135#endif
136#endif
137 };
138#pragma GCC diagnostic pop
139
140} // namespace detail
141
142PB_DS_CLASS_T_DEC
143inline
144PB_DS_CLASS_C_DEC::
145hash_prime_size_policy(size_type n) : m_start_size(n)
146{ m_start_size = get_nearest_larger_size(n); }
147
148PB_DS_CLASS_T_DEC
149inline void
150PB_DS_CLASS_C_DEC::
151swap(PB_DS_CLASS_C_DEC& other)
152{ std::swap(m_start_size, other.m_start_size); }
153
154PB_DS_CLASS_T_DEC
155inline PB_DS_CLASS_C_DEC::size_type
156PB_DS_CLASS_C_DEC::
157get_nearest_larger_size(size_type n) const
158{
159 const std::size_t* const p_upper = std::upper_bound(detail::g_a_sizes,
160 detail::g_a_sizes + detail::num_distinct_sizes, n);
161
162 if (p_upper == detail::g_a_sizes + detail::num_distinct_sizes)
163 __throw_resize_error();
164 return *p_upper;
165}
166
167PB_DS_CLASS_T_DEC
168inline PB_DS_CLASS_C_DEC::size_type
169PB_DS_CLASS_C_DEC::
170get_nearest_smaller_size(size_type n) const
171{
172 const std::size_t* p_lower = std::lower_bound(detail::g_a_sizes,
173 detail::g_a_sizes + detail::num_distinct_sizes, n);
174
175 if (*p_lower >= n && p_lower != detail::g_a_sizes)
176 --p_lower;
177 if (*p_lower < m_start_size)
178 return m_start_size;
179 return *p_lower;
180}
181#endif