ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
FixedSizeHashMap.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
6
7#ifndef FIXEDSIZEHASHTABLE_H_INCLUDED
8#define FIXEDSIZEHASHTABLE_H_INCLUDED
9
10#include <functional>
11#include "etiss/ObjectPool.h"
12#include <functional>
13
14namespace etiss
15{
16
17CONSTEXPR size_t set_lsbs(size_t val)
18{
19 return val ? ((set_lsbs(val - 1) << 1) | 1) : 0;
20}
21
22template <typename K>
24{
25 size_t inline operator()(const K *const &k_) const
26 {
27 intptr_t k = (intptr_t)k_;
28 if (sizeof(intptr_t) > 4)
29 {
30// disable wrong warning
31#ifdef _MSC_VER
32#pragma warning(suppress : 4293)
33#endif
34 k = k + (k >> 32);
35 }
36 k = k + (k >> 16);
37 return (size_t)k;
38 }
39};
40
41template <typename K, typename V, typename hashFunc = typename std::hash<K>, size_t log2_buckets = 16>
43{
44 private:
45 class Entry
46 {
47 public:
48 Entry() = delete;
49 Entry(const K &key, const V &val) : key(key), val(val), next(0) {}
50 Entry(const Entry &) = delete;
51 Entry(Entry &&) = delete;
52 Entry &operator=(const Entry &) = delete;
53 Entry &operator=(Entry &&) = delete;
54 K key;
55 V val;
57 };
59 Entry *map[1 << log2_buckets];
60 hashFunc hash;
61
62 public:
64 {
65 for (size_t i = 0; i < (1 << log2_buckets); i++)
66 {
67 map[i] = 0;
68 }
69 // memset(map,0,sizeof(Entry*)*(1<<log2_buckets));
70 }
72 {
73 for (size_t i = 0; i < (1 << log2_buckets); i++)
74 {
75 if (map[i])
76 {
77 Entry *del = map[i];
78 do
79 {
80 pool.destroy(del);
81 pool.deallocate(del);
82 Entry *n = del->next;
83 del = n;
84 } while (del);
85 }
86 }
87 }
88 etiss_del_como(FixedSizeHashMap) V *insert(const K &key, const V &val)
89 {
90#if DEBUG
91 size_t bucket_size = 0;
92#endif
93 size_t hk = hash(key);
94 Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
95 // check if key exists
96 {
97 while (*ptr)
98 {
99 if ((*ptr)->key == key)
100 {
101 (*ptr)->val = val;
102 return &(*ptr)->val;
103 }
104 ptr = &(*ptr)->next;
105#if DEBUG
106 bucket_size++;
107#endif
108 }
109 }
110 // add new key
111 {
112 *ptr = pool.allocate(1);
113 pool.construct(*ptr, key, val);
114 }
115#if DEBUG
116 bucket_size++;
117 if (bucket_size > 2)
118 {
119 etiss::log(etiss::VERBOSE, "Large bucket size detected.");
120 }
121#endif
122 return &(*ptr)->val;
123 }
124 void erase(const K &key)
125 {
126 size_t hk = hash(key);
127 Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
128 // check if key exists
129 {
130 while (*ptr)
131 {
132 if ((*ptr)->key == key)
133 {
134 Entry *del = *ptr;
135 *ptr = (*ptr)->next; // link to next element
136 pool.destroy(del);
137 pool.deallocate(del);
138 return;
139 }
140 ptr = &(*ptr)->next;
141 }
142 }
143 }
144 V *find(const K &key)
145 {
146 size_t hk = hash(key);
147 Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
148 while (*ptr)
149 {
150 if ((*ptr)->key == key)
151 {
152 return &(*ptr)->val;
153 }
154 ptr = &(*ptr)->next;
155 }
156 return 0;
157 }
158 const V *find(const K &key) const
159 {
160 size_t hk = hash(key);
161 Entry *const *ptr = &(map[hk & set_lsbs(log2_buckets)]);
162 while (*ptr)
163 {
164 if ((*ptr)->key == key)
165 {
166 return &(*ptr)->val;
167 }
168 ptr = &(*ptr)->next;
169 }
170 return 0;
171 }
172};
173
174} // namespace etiss
175
176#endif // FIXEDSIZEHASHTABLE_H_INCLUDED
Entry & operator=(const Entry &)=delete
Entry(const Entry &)=delete
Entry & operator=(Entry &&)=delete
Entry(const K &key, const V &val)
etiss_del_como(FixedSizeHashMap) V *insert(const K &key
Entry * map[1<< log2_buckets]
etiss::ObjectPool< Entry > pool
const V * find(const K &key) const
void erase(const K &key)
prealloc_inc defines the number of objects that is availabe within ObjectPools memory; default: 100
Definition ObjectPool.h:60
#define CONSTEXPR
Definition config.h:53
forwards: include/jit/*
Definition Benchmark.h:17
@ VERBOSE
Definition Misc.h:88
CONSTEXPR size_t set_lsbs(size_t val)
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition Misc.cpp:94
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
size_t operator()(const K *const &k_) const