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