• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_CONTAINERS_HASH_TABLES_H_
6 #define BASE_CONTAINERS_HASH_TABLES_H_
7 
8 #include <cstddef>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <utility>
12 
13 #include "base/hash.h"
14 
15 // This header file is deprecated. Use the corresponding C++11 type
16 // instead. https://crbug.com/576864
17 
18 // Use a custom hasher instead.
19 #define BASE_HASH_NAMESPACE base_hash
20 
21 namespace BASE_HASH_NAMESPACE {
22 
23 // A separate hasher which, by default, forwards to std::hash. This is so legacy
24 // uses of BASE_HASH_NAMESPACE with base::hash_map do not interfere with
25 // std::hash mid-transition.
26 template<typename T>
27 struct hash {
operatorhash28   std::size_t operator()(const T& value) const { return std::hash<T>()(value); }
29 };
30 
31 // Use base::IntPairHash from base/hash.h as a custom hasher instead.
32 template <typename Type1, typename Type2>
33 struct hash<std::pair<Type1, Type2>> {
34   std::size_t operator()(std::pair<Type1, Type2> value) const {
35     return base::HashInts(value.first, value.second);
36   }
37 };
38 
39 }  // namespace BASE_HASH_NAMESPACE
40 
41 namespace base {
42 
43 // Use std::unordered_map instead.
44 template <class Key,
45           class T,
46           class Hash = BASE_HASH_NAMESPACE::hash<Key>,
47           class Pred = std::equal_to<Key>,
48           class Alloc = std::allocator<std::pair<const Key, T>>>
49 using hash_map = std::unordered_map<Key, T, Hash, Pred, Alloc>;
50 
51 // Use std::unordered_multimap instead.
52 template <class Key,
53           class T,
54           class Hash = BASE_HASH_NAMESPACE::hash<Key>,
55           class Pred = std::equal_to<Key>,
56           class Alloc = std::allocator<std::pair<const Key, T>>>
57 using hash_multimap = std::unordered_multimap<Key, T, Hash, Pred, Alloc>;
58 
59 // Use std::unordered_multiset instead.
60 template <class Key,
61           class Hash = BASE_HASH_NAMESPACE::hash<Key>,
62           class Pred = std::equal_to<Key>,
63           class Alloc = std::allocator<Key>>
64 using hash_multiset = std::unordered_multiset<Key, Hash, Pred, Alloc>;
65 
66 // Use std::unordered_set instead.
67 template <class Key,
68           class Hash = BASE_HASH_NAMESPACE::hash<Key>,
69           class Pred = std::equal_to<Key>,
70           class Alloc = std::allocator<Key>>
71 using hash_set = std::unordered_set<Key, Hash, Pred, Alloc>;
72 
73 }  // namespace base
74 
75 #endif  // BASE_CONTAINERS_HASH_TABLES_H_
76