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