1*38fd1498Szrj // -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2005-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the terms
7*38fd1498Szrj // of the GNU General Public License as published by the Free Software
8*38fd1498Szrj // Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj // version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful, but
12*38fd1498Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*38fd1498Szrj // General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26*38fd1498Szrj
27*38fd1498Szrj // Permission to use, copy, modify, sell, and distribute this software
28*38fd1498Szrj // is hereby granted without fee, provided that the above copyright
29*38fd1498Szrj // notice appears in all copies, and that both that copyright notice
30*38fd1498Szrj // and this permission notice appear in supporting documentation. None
31*38fd1498Szrj // of the above authors, nor IBM Haifa Research Laboratories, make any
32*38fd1498Szrj // representation about the suitability of this software for any
33*38fd1498Szrj // purpose. It is provided "as is" without express or implied
34*38fd1498Szrj // warranty.
35*38fd1498Szrj
36*38fd1498Szrj /**
37*38fd1498Szrj * @file hash_prime_size_policy_imp.hpp
38*38fd1498Szrj * Contains a resize size policy implementation.
39*38fd1498Szrj */
40*38fd1498Szrj
41*38fd1498Szrj #pragma GCC system_header
42*38fd1498Szrj
43*38fd1498Szrj namespace detail
44*38fd1498Szrj {
45*38fd1498Szrj enum
46*38fd1498Szrj {
47*38fd1498Szrj num_distinct_sizes_32_bit = 30,
48*38fd1498Szrj num_distinct_sizes_64_bit = 62,
49*38fd1498Szrj num_distinct_sizes = sizeof(std::size_t) != 8 ?
50*38fd1498Szrj num_distinct_sizes_32_bit : num_distinct_sizes_64_bit,
51*38fd1498Szrj };
52*38fd1498Szrj
53*38fd1498Szrj // Originally taken from the SGI implementation; acknowledged in the docs.
54*38fd1498Szrj // Further modified (for 64 bits) from tr1's hashtable.
55*38fd1498Szrj static const std::size_t g_a_sizes[num_distinct_sizes_64_bit] =
56*38fd1498Szrj {
57*38fd1498Szrj /* 0 */ 5ul,
58*38fd1498Szrj /* 1 */ 11ul,
59*38fd1498Szrj /* 2 */ 23ul,
60*38fd1498Szrj /* 3 */ 47ul,
61*38fd1498Szrj /* 4 */ 97ul,
62*38fd1498Szrj /* 5 */ 199ul,
63*38fd1498Szrj /* 6 */ 409ul,
64*38fd1498Szrj /* 7 */ 823ul,
65*38fd1498Szrj /* 8 */ 1741ul,
66*38fd1498Szrj /* 9 */ 3469ul,
67*38fd1498Szrj /* 10 */ 6949ul,
68*38fd1498Szrj /* 11 */ 14033ul,
69*38fd1498Szrj /* 12 */ 28411ul,
70*38fd1498Szrj /* 13 */ 57557ul,
71*38fd1498Szrj /* 14 */ 116731ul,
72*38fd1498Szrj /* 15 */ 236897ul,
73*38fd1498Szrj /* 16 */ 480881ul,
74*38fd1498Szrj /* 17 */ 976369ul,
75*38fd1498Szrj /* 18 */ 1982627ul,
76*38fd1498Szrj /* 19 */ 4026031ul,
77*38fd1498Szrj /* 20 */ 8175383ul,
78*38fd1498Szrj /* 21 */ 16601593ul,
79*38fd1498Szrj /* 22 */ 33712729ul,
80*38fd1498Szrj /* 23 */ 68460391ul,
81*38fd1498Szrj /* 24 */ 139022417ul,
82*38fd1498Szrj /* 25 */ 282312799ul,
83*38fd1498Szrj /* 26 */ 573292817ul,
84*38fd1498Szrj /* 27 */ 1164186217ul,
85*38fd1498Szrj /* 28 */ 2364114217ul,
86*38fd1498Szrj /* 29 */ 4294967291ul,
87*38fd1498Szrj /* 30 */ (std::size_t)8589934583ull,
88*38fd1498Szrj /* 31 */ (std::size_t)17179869143ull,
89*38fd1498Szrj /* 32 */ (std::size_t)34359738337ull,
90*38fd1498Szrj /* 33 */ (std::size_t)68719476731ull,
91*38fd1498Szrj /* 34 */ (std::size_t)137438953447ull,
92*38fd1498Szrj /* 35 */ (std::size_t)274877906899ull,
93*38fd1498Szrj /* 36 */ (std::size_t)549755813881ull,
94*38fd1498Szrj /* 37 */ (std::size_t)1099511627689ull,
95*38fd1498Szrj /* 38 */ (std::size_t)2199023255531ull,
96*38fd1498Szrj /* 39 */ (std::size_t)4398046511093ull,
97*38fd1498Szrj /* 40 */ (std::size_t)8796093022151ull,
98*38fd1498Szrj /* 41 */ (std::size_t)17592186044399ull,
99*38fd1498Szrj /* 42 */ (std::size_t)35184372088777ull,
100*38fd1498Szrj /* 43 */ (std::size_t)70368744177643ull,
101*38fd1498Szrj /* 44 */ (std::size_t)140737488355213ull,
102*38fd1498Szrj /* 45 */ (std::size_t)281474976710597ull,
103*38fd1498Szrj /* 46 */ (std::size_t)562949953421231ull,
104*38fd1498Szrj /* 47 */ (std::size_t)1125899906842597ull,
105*38fd1498Szrj /* 48 */ (std::size_t)2251799813685119ull,
106*38fd1498Szrj /* 49 */ (std::size_t)4503599627370449ull,
107*38fd1498Szrj /* 50 */ (std::size_t)9007199254740881ull,
108*38fd1498Szrj /* 51 */ (std::size_t)18014398509481951ull,
109*38fd1498Szrj /* 52 */ (std::size_t)36028797018963913ull,
110*38fd1498Szrj /* 53 */ (std::size_t)72057594037927931ull,
111*38fd1498Szrj /* 54 */ (std::size_t)144115188075855859ull,
112*38fd1498Szrj /* 55 */ (std::size_t)288230376151711717ull,
113*38fd1498Szrj /* 56 */ (std::size_t)576460752303423433ull,
114*38fd1498Szrj /* 57 */ (std::size_t)1152921504606846883ull,
115*38fd1498Szrj /* 58 */ (std::size_t)2305843009213693951ull,
116*38fd1498Szrj /* 59 */ (std::size_t)4611686018427387847ull,
117*38fd1498Szrj /* 60 */ (std::size_t)9223372036854775783ull,
118*38fd1498Szrj /* 61 */ (std::size_t)18446744073709551557ull,
119*38fd1498Szrj };
120*38fd1498Szrj
121*38fd1498Szrj } // namespace detail
122*38fd1498Szrj
123*38fd1498Szrj PB_DS_CLASS_T_DEC
124*38fd1498Szrj inline
125*38fd1498Szrj PB_DS_CLASS_C_DEC::
hash_prime_size_policy(size_type n)126*38fd1498Szrj hash_prime_size_policy(size_type n) : m_start_size(n)
127*38fd1498Szrj { m_start_size = get_nearest_larger_size(n); }
128*38fd1498Szrj
129*38fd1498Szrj PB_DS_CLASS_T_DEC
130*38fd1498Szrj inline void
131*38fd1498Szrj PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC & other)132*38fd1498Szrj swap(PB_DS_CLASS_C_DEC& other)
133*38fd1498Szrj { std::swap(m_start_size, other.m_start_size); }
134*38fd1498Szrj
135*38fd1498Szrj PB_DS_CLASS_T_DEC
136*38fd1498Szrj inline PB_DS_CLASS_C_DEC::size_type
137*38fd1498Szrj PB_DS_CLASS_C_DEC::
get_nearest_larger_size(size_type n) const138*38fd1498Szrj get_nearest_larger_size(size_type n) const
139*38fd1498Szrj {
140*38fd1498Szrj const std::size_t* const p_upper = std::upper_bound(detail::g_a_sizes,
141*38fd1498Szrj detail::g_a_sizes + detail::num_distinct_sizes, n);
142*38fd1498Szrj
143*38fd1498Szrj if (p_upper == detail::g_a_sizes + detail::num_distinct_sizes)
144*38fd1498Szrj __throw_resize_error();
145*38fd1498Szrj return *p_upper;
146*38fd1498Szrj }
147*38fd1498Szrj
148*38fd1498Szrj PB_DS_CLASS_T_DEC
149*38fd1498Szrj inline PB_DS_CLASS_C_DEC::size_type
150*38fd1498Szrj PB_DS_CLASS_C_DEC::
get_nearest_smaller_size(size_type n) const151*38fd1498Szrj get_nearest_smaller_size(size_type n) const
152*38fd1498Szrj {
153*38fd1498Szrj const std::size_t* p_lower = std::lower_bound(detail::g_a_sizes,
154*38fd1498Szrj detail::g_a_sizes + detail::num_distinct_sizes, n);
155*38fd1498Szrj
156*38fd1498Szrj if (*p_lower >= n && p_lower != detail::g_a_sizes)
157*38fd1498Szrj --p_lower;
158*38fd1498Szrj if (*p_lower < m_start_size)
159*38fd1498Szrj return m_start_size;
160*38fd1498Szrj return *p_lower;
161*38fd1498Szrj }
162