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