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