Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
HashMap.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_HASH_MAP_H_
34#define DVD_HASH_MAP_H_
35
37
38#include <EASTL/unordered_map.h>
39#include <EASTL/intrusive_hash_map.h>
40
41namespace Divide {
42
43 template<class T>
44 struct EnumHash;
45
46 template <typename Key>
48 namespace hashAlg = eastl;
49
50
51 template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = eastl::equal_to<K>>
52 using hashMapDefaultAlloc = hashAlg::unordered_map<K, V, HashFun, Predicate>;
53
54 template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = eastl::equal_to<K>>
55 using hashMap = hashAlg::unordered_map<K, V, HashFun, Predicate>;
56 template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = eastl::equal_to<K>>
57 using hashPairReturn = hashAlg::pair<typename hashMap<K, V, HashFun, Predicate>::iterator, bool>;
58
59 template <typename K, typename V>
60 using hashMapIntrusive = hashAlg::intrusive_hash_map<K, V, 37>;
61
62 template<class T, bool>
63 struct hasher
64 {
65 FORCE_INLINE size_t operator() (const T& elem)
66 {
67 return hashAlg::hash<T>()(elem);
68 }
69 };
70
71 template<class T>
72 struct hasher<T, true>
73 {
74 FORCE_INLINE size_t operator() (const T& elem)
75 {
76 using EnumType = BaseType<T>;
77 return hashAlg::hash<EnumType>()(static_cast<EnumType>(elem));
78 }
79 };
80
81 template<class T>
82 struct EnumHash
83 {
84 FORCE_INLINE size_t operator()(const T& elem) const
85 {
87 }
88 };
89
90 template<class T>
91 struct NoHash
92 {
93 FORCE_INLINE size_t operator()(const T& elem) const noexcept
94 {
95 return static_cast<size_t>(elem);
96 }
97 };
98
99}; //namespace Divide
100
101namespace eastl {
102
103template <> struct hash<std::string>
104{
105 size_t operator()(const std::string& x) const noexcept
106 {
107 const char* p = x.c_str();
108 uint32_t c = 0u, result = 2166136261U; // Intentionally uint32_t instead of size_t, so the behavior is the same regardless of size.
109
110 while ((c = static_cast<uint8_t>(*p++)) != 0) // cast to unsigned 8 bit.
111 {
112 result = result * 16777619 ^ c;
113 }
114
115 return static_cast<size_t>(result);
116 }
117};
118
119
120template <typename K, typename V, typename ... Args, typename HashFun = Divide::HashType<K>, typename Predicate = equal_to<K>>
122{
123 return map.try_emplace(key, eastl::forward<Args>(args)...);
124}
125
126template <typename K, typename V, typename ... Args, typename HashFun = Divide::HashType<K>, typename Predicate = equal_to<K>>
128{
129 return map.emplace(eastl::forward<Args>(args)...);
130}
131
132template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = equal_to<K>>
134{
135 return map.insert(valuePair);
136}
137
138template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = equal_to<K>>
140{
141 return map.emplace(key, value);
142}
143
144template <typename K, typename V, typename HashFun = Divide::HashType<K>, typename Predicate = equal_to<K>>
146{
147 return map.emplace(key, eastl::move(value));
148}
149
150} //namespace hashAlg
151
152#endif //DVD_HASH_MAP_H_
#define FORCE_INLINE
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
hashAlg::pair< typename hashMap< K, V, HashFun, Predicate >::iterator, bool > hashPairReturn
Definition: HashMap.h:57
hashAlg::intrusive_hash_map< K, V, 37 > hashMapIntrusive
Definition: HashMap.h:60
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
std::underlying_type_t< Type > BaseType
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMapDefaultAlloc
Definition: HashMap.h:52
Divide::hashPairReturn< K, V, HashFun > insert(Divide::hashMap< K, V, HashFun, Predicate > &map, const pair< K, V > &valuePair)
Definition: HashMap.h:133
Divide::hashPairReturn< K, V, HashFun > emplace(Divide::hashMap< K, V, HashFun, Predicate > &map, K key, Args &&... args)
Definition: HashMap.h:121
FORCE_INLINE size_t operator()(const T &elem) const
Definition: HashMap.h:84
FORCE_INLINE size_t operator()(const T &elem) const noexcept
Definition: HashMap.h:93
FORCE_INLINE size_t operator()(const T &elem)
Definition: HashMap.h:65
size_t operator()(const std::string &x) const noexcept
Definition: HashMap.h:105