1 /* 2 * Copyright 2014 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FRUIT_HASH_HELPERS_H 18 #define FRUIT_HASH_HELPERS_H 19 20 #include <fruit/impl/data_structures/arena_allocator.h> 21 #include <fruit/impl/fruit-config.h> 22 23 #if !IN_FRUIT_CPP_FILE 24 // We don't want to include it in public headers to save some compile time. 25 #error "hash_helpers included in non-cpp file." 26 #endif 27 28 #if FRUIT_USES_BOOST 29 #include <boost/unordered_map.hpp> 30 #include <boost/unordered_set.hpp> 31 #else 32 #include <unordered_map> 33 #include <unordered_set> 34 #endif 35 36 namespace fruit { 37 namespace impl { 38 39 #if FRUIT_USES_BOOST 40 template <typename T, typename Hasher = std::hash<T>, typename EqualityComparator = std::equal_to<T>> 41 using HashSet = boost::unordered_set<T, Hasher, EqualityComparator>; 42 43 template <typename T, typename Hasher = std::hash<T>, typename EqualityComparator = std::equal_to<T>> 44 using HashSetWithArenaAllocator = boost::unordered_set<T, Hasher, EqualityComparator, ArenaAllocator<T>>; 45 46 template <typename Key, typename Value, typename Hasher = std::hash<Key>> 47 using HashMap = boost::unordered_map<Key, Value, Hasher>; 48 49 template <typename Key, typename Value, typename Hasher = std::hash<Key>, 50 typename EqualityComparator = std::equal_to<Key>> 51 using HashMapWithArenaAllocator = 52 boost::unordered_map<Key, Value, Hasher, EqualityComparator, ArenaAllocator<std::pair<const Key, Value>>>; 53 54 #else 55 template <typename T, typename Hasher = std::hash<T>, typename EqualityComparator = std::equal_to<T>> 56 using HashSet = std::unordered_set<T, Hasher, EqualityComparator>; 57 58 template <typename T, typename Hasher = std::hash<T>, typename EqualityComparator = std::equal_to<T>> 59 using HashSetWithArenaAllocator = std::unordered_set<T, Hasher, EqualityComparator, ArenaAllocator<T>>; 60 61 template <typename Key, typename Value, typename Hasher = std::hash<Key>> 62 using HashMap = std::unordered_map<Key, Value, Hasher>; 63 64 template <typename Key, typename Value, typename Hasher = std::hash<Key>, 65 typename EqualityComparator = std::equal_to<Key>> 66 using HashMapWithArenaAllocator = 67 std::unordered_map<Key, Value, Hasher, EqualityComparator, ArenaAllocator<std::pair<const Key, Value>>>; 68 69 #endif 70 71 template <typename T> 72 HashSet<T> createHashSet(); 73 74 template <typename T> 75 HashSet<T> createHashSet(size_t capacity); 76 77 template <typename T> 78 HashSetWithArenaAllocator<T> createHashSetWithArenaAllocator(size_t capacity, MemoryPool& memory_pool); 79 80 template <typename T, typename Hasher, typename EqualityComparator> 81 HashSetWithArenaAllocator<T, Hasher, EqualityComparator> 82 createHashSetWithArenaAllocatorAndCustomFunctors(size_t capacity, MemoryPool& memory_pool, Hasher, EqualityComparator); 83 84 template <typename Key, typename Value> 85 HashMap<Key, Value> createHashMap(); 86 87 template <typename Key, typename Value> 88 HashMap<Key, Value> createHashMap(size_t capacity); 89 90 template <typename Key, typename Value> 91 HashMapWithArenaAllocator<Key, Value> createHashMapWithArenaAllocator(size_t capacity, MemoryPool& memory_pool); 92 93 template <typename Key, typename Value, typename Hasher, typename EqualityComparator> 94 HashMapWithArenaAllocator<Key, Value, Hasher, EqualityComparator> 95 createHashMapWithArenaAllocatorAndCustomFunctors(size_t capacity, MemoryPool& memory_pool, Hasher, EqualityComparator); 96 97 } // namespace impl 98 } // namespace fruit 99 100 #include <fruit/impl/util/hash_helpers.defn.h> 101 102 #endif // FRUIT_HASH_HELPERS_H 103