TrueReality  v0.1.1912
HashMap.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author David Guthrie
25 */
26 #pragma once
27 
28 #if defined( _LIBCPP_VERSION ) || (defined(_MSC_VER) && _MSC_VER >= 1700) || defined(__GNUG__)
29 # include <unordered_map>
30 # define _UNORDERED_MAP
31 #elif defined(_MSC_VER)
32 # include <hash_map>
33 #else
34 # include <map>
35 #endif
36 
37 #include <trUtil/Hash.h>
38 
39 namespace trUtil
40 {
41 
42 #if defined(_MSC_VER) && ! defined(_UNORDERED_MAP)
43  template<class _Key, class _HashFcn, class _LessKey>
44  struct HashCompare
45  {
46  size_t operator() (const _Key& k) const
47  {
48  _HashFcn h;
49  return h(k);
50  }
51  bool operator() (const _Key& k1, const _Key& k2) const
52  {
53  _LessKey l;
54  return l(k1, k2);
55  }
56 
57  enum
58  { // parameters for hash table
59  bucket_size = 4, // 0 < bucket_size
60  min_buckets = 8
61  }; // min_buckets = 2 ^^ N, 0 < N
62 
63  };
64 #else
65  template<class _Key, class _LessKey>
66  struct HashEqual
67  {
68  bool operator() (const _Key& k1, const _Key& k2) const
69  {
70  _LessKey l;
71  return !l(k1, k2) && !l(k2, k1);
72  }
73  };
74 #endif
75 
76  template<class _Key, class _Tp, class _HashFcn = trUtil::hash<_Key>, class _LessKey = std::less<_Key>, class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
77  class HashMap : public
78 #if defined(_UNORDERED_MAP)
79  std::unordered_map<_Key, _Tp, _HashFcn, trUtil::HashEqual<_Key, _LessKey>, _Alloc >
80  {
81  public:
82  using BaseClass = std::unordered_map<_Key, _Tp, _HashFcn, trUtil::HashEqual<_Key, _LessKey>, _Alloc>;
83 #elif defined(_MSC_VER)
84  stdext::hash_map<_Key, _Tp, trUtil::HashCompare<_Key, _HashFcn, _LessKey>, _Alloc >
85  {
86  public:
87  using BaseClass = stdext::hash_map<_Key, _Tp, trUtil::HashCompare<_Key, _HashFcn, _LessKey>, _Alloc>;
88 #else
89  std::map<_Key, _Tp, _LessKey, _Alloc >
90  {
91  public:
92  using BaseClass = std::map<_Key, _Tp, _LessKey, _Alloc>;
93 #endif
94  using typename BaseClass::iterator;
95  using typename BaseClass::const_iterator;
96  HashMap() {}
97  };
98 
99  template<class _Key, class _Tp, class _HashFcn = trUtil::hash<_Key>, class _LessKey = std::less<_Key>, class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
100  class HashMultiMap : public
101 #if defined(_UNORDERED_MAP)
102  std::unordered_multimap<_Key, _Tp, _HashFcn, trUtil::HashEqual<_Key, _LessKey>, _Alloc >
103  {
104  public:
105  using BaseClass = std::unordered_multimap<_Key, _Tp, _HashFcn, trUtil::HashEqual<_Key, _LessKey>, _Alloc>;
106 #elif defined(_MSC_VER)
107  stdext::hash_multimap<_Key, _Tp, trUtil::HashCompare<_Key, _HashFcn, _LessKey>, _Alloc >
108  {
109  public:
110  using BaseClass = stdext::hash_multimap<_Key, _Tp, trUtil::HashCompare<_Key, _HashFcn, _LessKey>, _Alloc>;
111 #else
112  std::multimap<_Key, _Tp, _LessKey, _Alloc >
113  {
114  public:
115  using BaseClass = std::multimap<_Key, _Tp, _LessKey, _Alloc>;
116 #endif
117  using typename BaseClass::iterator;
118  using typename BaseClass::const_iterator;
120  };
121 }
std::multimap< _Key, _Tp, _LessKey, _Alloc > BaseClass
Definition: HashMap.h:115
std::map< _Key, _Tp, _LessKey, _Alloc > BaseClass
Definition: HashMap.h:92
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208