/
stdExt.h
224 lines (196 loc) · 6.41 KB
/
stdExt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (c) 2006-2015, Stefan Eilemann <eile@equalizergraphics.com>
* Daniel Nachbaur <danielnachbaur@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file lunchbox/stdExt.h
*
* Include extensions to the STL and define a uniform interface to them.
* @deprecated For containers and most functionality use boost classes.
*/
#ifndef LUNCHBOX_STDEXT_H
#define LUNCHBOX_STDEXT_H
#include <lunchbox/algorithm.h> // usort
#include <lunchbox/compiler.h>
#include <lunchbox/uint128_t.h>
#include <boost/config.hpp>
#include <algorithm>
#include <string>
#include <vector>
#ifdef LB_USE_STD_PARALLEL
# include <parallel/algorithm>
#endif
//----- Common extensions of the STL
#ifdef BOOST_NO_STD_UNORDERED
# if defined __GNUC__
# if defined LB_GCC_4_3_OR_LATER && !defined __INTEL_COMPILER
# define LB_STDEXT_TR1
# elif defined __clang__
# define LB_STDEXT_TR1
# else
# define LB_STDEXT_EXT
# endif
# elif defined _MSC_VER
# define LB_STDEXT_MSVC
# elif defined __xlC__
# define LB_STDEXT_TR1
# define LB_STDEXT_TR1_BOOST
# else
# define LB_STDEXT_STD
# endif
#else
# define LB_STDEXT_STD
# define LB_STDEXT_STD11
#endif
#ifdef LB_STDEXT_TR1
# ifdef LB_STDEXT_TR1_BOOST
# include <boost/tr1/functional.hpp>
# include <boost/tr1/unordered_map.hpp>
# include <boost/tr1/unordered_set.hpp>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
# endif
/* Alias stde namespace to uniformly access stl extensions. */
namespace stde = std::tr1;
# define LB_STDEXT_NAMESPACE_OPEN namespace std { namespace tr1 {
# define LB_STDEXT_NAMESPACE_CLOSE }}
#endif
#ifdef LB_STDEXT_EXT
# include <ext/hash_map>
# include <ext/hash_set>
/* Alias stde namespace to uniformly access stl extensions. */
namespace stde = __gnu_cxx;
# define LB_STDEXT_NAMESPACE_OPEN namespace __gnu_cxx {
# define LB_STDEXT_NAMESPACE_CLOSE }
#endif
#ifdef LB_STDEXT_MSVC
# include <hash_map>
# include <hash_set>
/* Alias stde namespace to uniformly access stl extensions. */
namespace stde = stdext;
# define LB_STDEXT_NAMESPACE_OPEN namespace stdext {
# define LB_STDEXT_NAMESPACE_CLOSE }
#endif
#ifdef LB_STDEXT_STD
# ifdef LB_STDEXT_STD11
# include <unordered_map>
# include <unordered_set>
# else
# include <hash_map>
# include <hash_set>
# endif
/* Alias stde namespace to uniformly access stl extensions. */
namespace stde = std;
# define LB_STDEXT_NAMESPACE_OPEN namespace std {
# define LB_STDEXT_NAMESPACE_CLOSE }
#endif
LB_STDEXT_NAMESPACE_OPEN
//----- Our extensions of the STL
#if defined LB_STDEXT_TR1 || defined LB_STDEXT_STD11
# ifndef LB_HAVE_HASH_MAP
# ifdef CXX_TEMPLATE_ALIAS_SUPPORTED
template< class K, class T, class H = hash< K >, class P = std::equal_to< K >,
class A = std::allocator< std::pair< const K, T > > >
using hash_map = unordered_map< K, T, H, P, A >;
# else
template< class K, class T, class H = hash< K >, class P = std::equal_to< K >,
class A = std::allocator< std::pair< const K, T > > >
class hash_map : public unordered_map< K, T, H, P, A > {};
# endif
# endif // LB_HAVE_HASH_MAP
# ifndef LB_HAVE_HASH_SET
# ifdef CXX_TEMPLATE_ALIAS_SUPPORTED
template< class T, class H = hash< T >,
class P = std::equal_to< T >, class A = std::allocator< T > >
using hash_set = unordered_set< T, H, P, A >;
# else
template< class T, class H = hash< T >,
class P = std::equal_to< T >, class A = std::allocator< T > >
class hash_set : public unordered_set< T, H, P, A > {};
# endif
# endif // LB_HAVE_HASH_SET
#endif
#ifdef LB_STDEXT_EXT
# ifndef LB_HAVE_STRING_HASH
/** std::string hash function. @version 1.0 */
template<> struct hash< std::string >
{
size_t operator()( const std::string& str ) const
{ return hash< const char* >()( str.c_str() ); }
};
# endif // LB_HAVE_STRING_HASH
# if !defined __INTEL_COMPILER
# ifndef LB_HAVE_LONG_HASH
/** uint64_t hash function. @version 1.0 */
template<> struct hash< uint64_t >
{
size_t operator()( const uint64_t& val ) const
{
// OPT: tr1 does the same, however it seems suboptimal on 32 bits if the
// lower 32 bits never change, e.g., for ObjectVersion
return static_cast< size_t >( val );
}
};
# endif
# endif // !__INTEL_COMPILER
# ifndef LB_HAVE_VOID_PTR_HASH
/** void* hash functions. @version 1.0 */
template<> struct hash< void* >
{
template< typename P > size_t operator()( const P& key ) const
{ return reinterpret_cast<size_t>(key); }
};
template<> struct hash< const void* >
{
template< typename P > size_t operator()( const P& key ) const
{ return reinterpret_cast<size_t>(key); }
};
# endif // LB_HAVE_VOID_PTR_HASH
#endif // LB_STDEXT_EXT
#ifdef LB_STDEXT_MSVC
# ifndef LB_HAVE_STRING_HASH
/** std::string hash function. @version 1.0 */
template<> inline
size_t hash_compare< std::string >::operator() ( const std::string& key ) const
{ return hash_value( key.c_str( )); }
# endif
template<> inline size_t hash_compare< lunchbox::uint128_t >::operator()
( const lunchbox::uint128_t& key ) const
{
return static_cast< size_t >( key.high() ^ key.low() );
}
template<> inline size_t hash_value( const lunchbox::uint128_t& key )
{ return static_cast< size_t >( key.high() ^ key.low() ); }
#else // MSVC
/** uint128_t hash function. @version 1.0 */
template<> struct hash< lunchbox::uint128_t >
{
size_t operator()( const lunchbox::uint128_t& key ) const
{ return key.high() ^ key.low(); }
};
#endif //! MSVC
/** @deprecated use lunchbox::usort() */
template< typename C > void usort( C& c ) { lunchbox::usort( c ); }
#ifndef LB_STDEXT_STD
/** @deprecated Use lunchbox::sort() */
# ifdef LB_GCC_4_4_OR_LATER
using __gnu_parallel::sort;
# else
using std::sort;
# endif
#endif
LB_STDEXT_NAMESPACE_CLOSE
#endif // LUNCHBOX_STDEXT_H