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