1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 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 2, 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// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free
22// software library without restriction.  Specifically, if other files
23// instantiate templates or use macros or inline functions from this
24// file, or you compile this file and link it with other files to
25// produce an executable, this file does not by itself cause the
26// resulting executable to be covered by the GNU General Public
27// License.  This exception does not however invalidate any other
28// reasons why the executable file might be covered by the GNU General
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33// Permission to use, copy, modify, sell, and distribute this software
34// is hereby granted without fee, provided that the above copyright
35// notice appears in all copies, and that both that copyright notice
36// and this permission notice appear in supporting documentation. None
37// of the above authors, nor IBM Haifa Research Laboratories, make any
38// representation about the suitability of this software for any
39// purpose. It is provided "as is" without express or implied
40// warranty.
41
42/**
43 * @file hash_prime_size_policy_imp.hpp
44 * Contains a resize size policy implementation.
45 */
46
47namespace detail
48{
49  enum
50    {
51      num_distinct_sizes_32_bit = 30,
52      num_distinct_sizes_64_bit = 62,
53      num_distinct_sizes = sizeof(std::size_t) != 8 ?
54            num_distinct_sizes_32_bit : num_distinct_sizes_64_bit,
55    };
56
57  // Originally taken from the SGI implementation; acknowledged in the docs.
58  // Further modified (for 64 bits) from tr1's hashtable.
59  static const std::size_t g_a_sizes[num_distinct_sizes_64_bit] =
60    {
61      /* 0     */              5ul,
62      /* 1     */              11ul,
63      /* 2     */              23ul,
64      /* 3     */              47ul,
65      /* 4     */              97ul,
66      /* 5     */              199ul,
67      /* 6     */              409ul,
68      /* 7     */              823ul,
69      /* 8     */              1741ul,
70      /* 9     */              3469ul,
71      /* 10    */              6949ul,
72      /* 11    */              14033ul,
73      /* 12    */              28411ul,
74      /* 13    */              57557ul,
75      /* 14    */              116731ul,
76      /* 15    */              236897ul,
77      /* 16    */              480881ul,
78      /* 17    */              976369ul,
79      /* 18    */              1982627ul,
80      /* 19    */              4026031ul,
81      /* 20    */              8175383ul,
82      /* 21    */              16601593ul,
83      /* 22    */              33712729ul,
84      /* 23    */              68460391ul,
85      /* 24    */              139022417ul,
86      /* 25    */              282312799ul,
87      /* 26    */              573292817ul,
88      /* 27    */              1164186217ul,
89      /* 28    */              2364114217ul,
90      /* 29    */              4294967291ul,
91      /* 30    */ (std::size_t)8589934583ull,
92      /* 31    */ (std::size_t)17179869143ull,
93      /* 32    */ (std::size_t)34359738337ull,
94      /* 33    */ (std::size_t)68719476731ull,
95      /* 34    */ (std::size_t)137438953447ull,
96      /* 35    */ (std::size_t)274877906899ull,
97      /* 36    */ (std::size_t)549755813881ull,
98      /* 37    */ (std::size_t)1099511627689ull,
99      /* 38    */ (std::size_t)2199023255531ull,
100      /* 39    */ (std::size_t)4398046511093ull,
101      /* 40    */ (std::size_t)8796093022151ull,
102      /* 41    */ (std::size_t)17592186044399ull,
103      /* 42    */ (std::size_t)35184372088777ull,
104      /* 43    */ (std::size_t)70368744177643ull,
105      /* 44    */ (std::size_t)140737488355213ull,
106      /* 45    */ (std::size_t)281474976710597ull,
107      /* 46    */ (std::size_t)562949953421231ull,
108      /* 47    */ (std::size_t)1125899906842597ull,
109      /* 48    */ (std::size_t)2251799813685119ull,
110      /* 49    */ (std::size_t)4503599627370449ull,
111      /* 50    */ (std::size_t)9007199254740881ull,
112      /* 51    */ (std::size_t)18014398509481951ull,
113      /* 52    */ (std::size_t)36028797018963913ull,
114      /* 53    */ (std::size_t)72057594037927931ull,
115      /* 54    */ (std::size_t)144115188075855859ull,
116      /* 55    */ (std::size_t)288230376151711717ull,
117      /* 56    */ (std::size_t)576460752303423433ull,
118      /* 57    */ (std::size_t)1152921504606846883ull,
119      /* 58    */ (std::size_t)2305843009213693951ull,
120      /* 59    */ (std::size_t)4611686018427387847ull,
121      /* 60    */ (std::size_t)9223372036854775783ull,
122      /* 61    */ (std::size_t)18446744073709551557ull,
123    };
124
125} // namespace detail
126
127PB_DS_CLASS_T_DEC
128inline
129PB_DS_CLASS_C_DEC::
130hash_prime_size_policy(size_type n) : m_start_size(n)
131{ m_start_size = get_nearest_larger_size(n); }
132
133PB_DS_CLASS_T_DEC
134inline void
135PB_DS_CLASS_C_DEC::
136swap(PB_DS_CLASS_C_DEC& other)
137{ std::swap(m_start_size, other.m_start_size); }
138
139PB_DS_CLASS_T_DEC
140inline PB_DS_CLASS_C_DEC::size_type
141PB_DS_CLASS_C_DEC::
142get_nearest_larger_size(size_type n) const
143{
144  const std::size_t* const p_upper = std::upper_bound(detail::g_a_sizes,
145		     detail::g_a_sizes + detail::num_distinct_sizes, n);
146
147  if (p_upper == detail::g_a_sizes + detail::num_distinct_sizes)
148    __throw_resize_error();
149  return *p_upper;
150}
151
152PB_DS_CLASS_T_DEC
153inline PB_DS_CLASS_C_DEC::size_type
154PB_DS_CLASS_C_DEC::
155get_nearest_smaller_size(size_type n) const
156{
157  const size_t* p_lower = std::lower_bound(detail::g_a_sizes,
158		       detail::g_a_sizes + detail::num_distinct_sizes, n);
159
160  if (*p_lower >= n &&  p_lower != detail::g_a_sizes)
161    --p_lower;
162  if (*p_lower < m_start_size)
163    return m_start_size;
164  return *p_lower;
165}
166