ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
etiss
include
etiss
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
14
namespace
etiss
15
{
16
17
CONSTEXPR
size_t
set_lsbs
(
size_t
val)
18
{
19
return
val ? ((
set_lsbs
(val - 1) << 1) | 1) : 0;
20
}
21
22
template
<
typename
K>
23
struct
pointerHash
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
41
template
<
typename
K,
typename
V,
typename
hashFunc =
typename
std::hash<K>,
size_t
log2_buckets = 16>
42
class
FixedSizeHashMap
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
;
56
Entry
*
next
;
57
};
58
etiss::ObjectPool<Entry>
pool
;
59
Entry
*
map
[1 << log2_buckets];
60
hashFunc
hash
;
61
62
public
:
63
FixedSizeHashMap
()
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
}
71
~FixedSizeHashMap
()
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
ObjectPool.h
etiss::FixedSizeHashMap::Entry
Definition
FixedSizeHashMap.h:46
etiss::FixedSizeHashMap::Entry::operator=
Entry & operator=(const Entry &)=delete
etiss::FixedSizeHashMap::Entry::Entry
Entry(const Entry &)=delete
etiss::FixedSizeHashMap::Entry::operator=
Entry & operator=(Entry &&)=delete
etiss::FixedSizeHashMap::Entry::next
Entry * next
Definition
FixedSizeHashMap.h:56
etiss::FixedSizeHashMap::Entry::Entry
Entry()=delete
etiss::FixedSizeHashMap::Entry::Entry
Entry(Entry &&)=delete
etiss::FixedSizeHashMap::Entry::Entry
Entry(const K &key, const V &val)
Definition
FixedSizeHashMap.h:49
etiss::FixedSizeHashMap::Entry::val
V val
Definition
FixedSizeHashMap.h:55
etiss::FixedSizeHashMap::Entry::key
K key
Definition
FixedSizeHashMap.h:54
etiss::FixedSizeHashMap
Definition
FixedSizeHashMap.h:43
etiss::FixedSizeHashMap::etiss_del_como
etiss_del_como(FixedSizeHashMap) V *insert(const K &key
etiss::FixedSizeHashMap::map
Entry * map[1<< log2_buckets]
Definition
FixedSizeHashMap.h:59
etiss::FixedSizeHashMap::val
const V & val
Definition
FixedSizeHashMap.h:89
etiss::FixedSizeHashMap::find
V * find(const K &key)
Definition
FixedSizeHashMap.h:144
etiss::FixedSizeHashMap::pool
etiss::ObjectPool< Entry > pool
Definition
FixedSizeHashMap.h:58
etiss::FixedSizeHashMap::~FixedSizeHashMap
~FixedSizeHashMap()
Definition
FixedSizeHashMap.h:71
etiss::FixedSizeHashMap::FixedSizeHashMap
FixedSizeHashMap()
Definition
FixedSizeHashMap.h:63
etiss::FixedSizeHashMap::find
const V * find(const K &key) const
Definition
FixedSizeHashMap.h:158
etiss::FixedSizeHashMap::hash
hashFunc hash
Definition
FixedSizeHashMap.h:60
etiss::FixedSizeHashMap::ptr
Entry ** ptr
Definition
FixedSizeHashMap.h:94
etiss::FixedSizeHashMap::erase
void erase(const K &key)
Definition
FixedSizeHashMap.h:124
etiss::ObjectPool
prealloc_inc defines the number of objects that is availabe within ObjectPools memory; default: 100
Definition
ObjectPool.h:60
CONSTEXPR
#define CONSTEXPR
Definition
config.h:53
etiss
forwards: include/jit/*
Definition
Benchmark.h:17
etiss::VERBOSE
@ VERBOSE
Definition
Misc.h:88
etiss::set_lsbs
CONSTEXPR size_t set_lsbs(size_t val)
Definition
FixedSizeHashMap.h:17
etiss::log
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition
Misc.cpp:94
intptr_t
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
Definition
opencl-c-base.h:55
etiss::pointerHash
Definition
FixedSizeHashMap.h:24
etiss::pointerHash::operator()
size_t operator()(const K *const &k_) const
Definition
FixedSizeHashMap.h:25
Generated on Sat Nov 15 2025 19:30:10 for ETISS 0.11.2 by
1.9.8