BRE12
_tbb_hash_compare_impl.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 // must be included outside namespaces.
22 #ifndef __TBB_tbb_hash_compare_impl_H
23 #define __TBB_tbb_hash_compare_impl_H
24 
25 #include <string>
26 
27 namespace tbb {
28 namespace interface5 {
29 namespace internal {
30 
31 // Template class for hash compare
32 template<typename Key, typename Hasher, typename Key_equality>
34 {
35 public:
36  typedef Hasher hasher;
37  typedef Key_equality key_equal;
38 
39  hash_compare() {}
40 
41  hash_compare(Hasher a_hasher) : my_hash_object(a_hasher) {}
42 
43  hash_compare(Hasher a_hasher, Key_equality a_keyeq) : my_hash_object(a_hasher), my_key_compare_object(a_keyeq) {}
44 
45  size_t operator()(const Key& key) const {
46  return ((size_t)my_hash_object(key));
47  }
48 
49  bool operator()(const Key& key1, const Key& key2) const {
50  return (!my_key_compare_object(key1, key2));
51  }
52 
53  Hasher my_hash_object; // The hash object
54  Key_equality my_key_compare_object; // The equality comparator object
55 };
56 
58 static const size_t hash_multiplier = tbb::internal::select_size_t_constant<2654435769U, 11400714819323198485ULL>::value;
59 
60 } // namespace internal
61 
63 template<typename T>
64 inline size_t tbb_hasher( const T& t ) {
65  return static_cast<size_t>( t ) * internal::hash_multiplier;
66 }
67 template<typename P>
68 inline size_t tbb_hasher( P* ptr ) {
69  size_t const h = reinterpret_cast<size_t>( ptr );
70  return (h >> 3) ^ h;
71 }
72 template<typename E, typename S, typename A>
73 inline size_t tbb_hasher( const std::basic_string<E,S,A>& s ) {
74  size_t h = 0;
75  for( const E* c = s.c_str(); *c; ++c )
76  h = static_cast<size_t>(*c) ^ (h * internal::hash_multiplier);
77  return h;
78 }
79 template<typename F, typename S>
80 inline size_t tbb_hasher( const std::pair<F,S>& p ) {
81  return tbb_hasher(p.first) ^ tbb_hasher(p.second);
82 }
83 
84 } // namespace interface5
85 using interface5::tbb_hasher;
86 
87 // Template class for hash compare
88 template<typename Key>
89 class tbb_hash
90 {
91 public:
92  tbb_hash() {}
93 
94  size_t operator()(const Key& key) const
95  {
96  return tbb_hasher(key);
97  }
98 };
99 
101 template<typename Key>
103  static size_t hash( const Key& a ) { return tbb_hasher(a); }
104  static bool equal( const Key& a, const Key& b ) { return a == b; }
105 };
106 
107 } // namespace tbb
108 #endif /* __TBB_tbb_hash_compare_impl_H */
Definition: _tbb_hash_compare_impl.h:89
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
Definition: _tbb_hash_compare_impl.h:33
hash_compare that is default argument for concurrent_hash_map
Definition: _tbb_hash_compare_impl.h:102