ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
FixedSizeHashMap.h
Go to the documentation of this file.
1 /*
2 
3  @copyright
4 
5  <pre>
6 
7  Copyright 2018 Infineon Technologies AG
8 
9  This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
10 
11  The initial version of this software has been created with the funding support by the German Federal
12  Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
13 
14  Redistribution and use in source and binary forms, with or without modification, are permitted
15  provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice, this list of conditions and
18  the following disclaimer.
19 
20  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
21  and the following disclaimer in the documentation and/or other materials provided with the distribution.
22 
23  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
24  or promote products derived from this software without specific prior written permission.
25 
26  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
27  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  POSSIBILITY OF SUCH DAMAGE.
34 
35  </pre>
36 
37  @author Marc Greim <marc.greim@mytum.de>, Chair of Electronic Design Automation, TUM
38 
39  @version 0.1
40 
41 */
42 
43 #ifndef FIXEDSIZEHASHTABLE_H_INCLUDED
44 #define FIXEDSIZEHASHTABLE_H_INCLUDED
45 
46 #include <functional>
47 #include "etiss/ObjectPool.h"
48 #include <functional>
49 
50 namespace etiss
51 {
52 
53 CONSTEXPR size_t set_lsbs(size_t val)
54 {
55  return val ? ((set_lsbs(val - 1) << 1) | 1) : 0;
56 }
57 
58 template <typename K>
60 {
61  size_t inline operator()(const K *const &k_) const
62  {
63  intptr_t k = (intptr_t)k_;
64  if (sizeof(intptr_t) > 4)
65  {
66 // disable wrong warning
67 #ifdef _MSC_VER
68 #pragma warning(suppress : 4293)
69 #endif
70  k = k + (k >> 32);
71  }
72  k = k + (k >> 16);
73  return (size_t)k;
74  }
75 };
76 
77 template <typename K, typename V, typename hashFunc = typename std::hash<K>, size_t log2_buckets = 16>
79 {
80  private:
81  class Entry
82  {
83  public:
84  Entry() = delete;
85  Entry(const K &key, const V &val) : key(key), val(val), next(0) {}
86  Entry(const Entry &) = delete;
87  Entry(Entry &&) = delete;
88  Entry &operator=(const Entry &) = delete;
89  Entry &operator=(Entry &&) = delete;
90  K key;
91  V val;
93  };
95  Entry *map[1 << log2_buckets];
96  hashFunc hash;
97 
98  public:
100  {
101  for (size_t i = 0; i < (1 << log2_buckets); i++)
102  {
103  map[i] = 0;
104  }
105  // memset(map,0,sizeof(Entry*)*(1<<log2_buckets));
106  }
108  {
109  for (size_t i = 0; i < (1 << log2_buckets); i++)
110  {
111  if (map[i])
112  {
113  Entry *del = map[i];
114  do
115  {
116  pool.destroy(del);
117  pool.deallocate(del);
118  Entry *n = del->next;
119  del = n;
120  } while (del);
121  }
122  }
123  }
124  etiss_del_como(FixedSizeHashMap) V *insert(const K &key, const V &val)
125  {
126 #if DEBUG
127  size_t bucket_size = 0;
128 #endif
129  size_t hk = hash(key);
130  Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
131  // check if key exists
132  {
133  while (*ptr)
134  {
135  if ((*ptr)->key == key)
136  {
137  (*ptr)->val = val;
138  return &(*ptr)->val;
139  }
140  ptr = &(*ptr)->next;
141 #if DEBUG
142  bucket_size++;
143 #endif
144  }
145  }
146  // add new key
147  {
148  *ptr = pool.allocate(1);
149  pool.construct(*ptr, key, val);
150  }
151 #if DEBUG
152  bucket_size++;
153  if (bucket_size > 2)
154  {
155  etiss::log(etiss::VERBOSE, "Large bucket size detected.");
156  }
157 #endif
158  return &(*ptr)->val;
159  }
160  void erase(const K &key)
161  {
162  size_t hk = hash(key);
163  Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
164  // check if key exists
165  {
166  while (*ptr)
167  {
168  if ((*ptr)->key == key)
169  {
170  Entry *del = *ptr;
171  *ptr = (*ptr)->next; // link to next element
172  pool.destroy(del);
173  pool.deallocate(del);
174  return;
175  }
176  ptr = &(*ptr)->next;
177  }
178  }
179  }
180  V *find(const K &key)
181  {
182  size_t hk = hash(key);
183  Entry **ptr = &(map[hk & set_lsbs(log2_buckets)]);
184  while (*ptr)
185  {
186  if ((*ptr)->key == key)
187  {
188  return &(*ptr)->val;
189  }
190  ptr = &(*ptr)->next;
191  }
192  return 0;
193  }
194  const V *find(const K &key) const
195  {
196  size_t hk = hash(key);
197  Entry *const *ptr = &(map[hk & set_lsbs(log2_buckets)]);
198  while (*ptr)
199  {
200  if ((*ptr)->key == key)
201  {
202  return &(*ptr)->val;
203  }
204  ptr = &(*ptr)->next;
205  }
206  return 0;
207  }
208 };
209 
210 } // namespace etiss
211 
212 #endif // FIXEDSIZEHASHTABLE_H_INCLUDED
Entry(const Entry &)=delete
Entry & operator=(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]
V * find(const K &key)
const V * find(const K &key) const
etiss::ObjectPool< Entry > pool
void erase(const K &key)
prealloc_inc defines the number of objects that is availabe within ObjectPools memory; default: 100
Definition: ObjectPool.h:98
#define CONSTEXPR
Definition: config.h:91
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
@ VERBOSE
Definition: Misc.h:130
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:125
__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
size_t operator()(const K *const &k_) const