Lolly 1.4.27
Loading...
Searching...
No Matches
hashmap.hpp
Go to the documentation of this file.
1
2/**
3 * \file hashmap.h
4 * \details fixed size hashmaps with reference counting
5 * \copyright GPLv3
6 * \author Joris van der Hoeven
7 * \date 1999
8 */
9
10#ifndef HASHMAP_H
11#define HASHMAP_H
12#include "list.hpp"
13
14template <class T> class list;
15template <class T, class U> class hashmap;
16template <class T, class U> class rel_hashmap;
17template <class T, class U> class rel_hashmap_rep;
18template <class T, class U> class hashmap_iterator_rep;
19
20template <class T, class U> int N (hashmap<T, U> a);
21template <class T, class U>
23template <class T, class U> hashmap<T, U> copy (hashmap<T, U> h);
24template <class T, class U>
26template <class T, class U>
28template <class T, class U>
30template <class T, class U>
32
33/**
34 * @brief Hash entry template for key-value pairs.
35 *
36 * @tparam T Type of the key.
37 * @tparam U Type of the value.
38 */
39template <class T, class U> struct hashentry {
40 int code;
41 T key;
42 U im;
43 hashentry<T, U> () {}
44 hashentry<T, U> (int code, T key2, U im2);
45};
46
47template <class T, class U> class hashmap_rep : concrete_struct {
48 int size; // size of hashmap (nr of entries)
49 int n; // nr of keys (a power of two)
50 int max; // mean number of entries per key
51 U init; // default entry
52 list<hashentry<T, U>>* a; // the array of entries
53
54public:
55 /**
56 * @brief Constructor for the hashmap_rep class.
57 *
58 * @tparam T Type of the keys in the hash map.
59 * @tparam U Type of the values in the hash map.
60 * @param init2 Initial value for hash entries. * @param n2 Initial size of
61 * the hash table array.
62 * @param max2 Maximum allowable size for dynamic resizing.
63 */
64 inline hashmap_rep<T, U> (U init2, int n2= 1, int max2= 1)
65 : size (0), n (n2), max (max2), init (init2),
66 a (tm_new_array<list<hashentry<T, U>>> (n)) {}
67
68 /**
69 * @brief Destructor for the hashmap_rep class.
70 *
71 * @tparam T Type of the keys in the hash map.
72 * @tparam U Type of the values in the hash map.
73 */
74 inline ~hashmap_rep<T, U> () { tm_delete_array (a); }
75
76 /**
77 * @brief Resizes the hashmap and rehashes all existing keys.
78 *
79 * @param n The new size of the hashmap.
80 * @note This operation could be costly as it rehashes all keys.
81 */
82 void resize (int n);
83
84 /**
85 * @brief Remove a specific key from the hashmap, if it exists.
86 *
87 * @tparam T The type of the key in the hashmap.
88 * @param x The key to be removed from the hashmap.
89 */
90 void reset (T x);
91
92 /**
93 * @brief Applies a given routine to each key in the hashmap.
94 *
95 * @tparam T The type of the key in the hashmap.
96 * @param routine The function pointer to the routine to be applied to each
97 * key in the hashmap.
98 */
99 void generate (void (*routine) (T));
100
101 bool contains (T x);
102 bool empty ();
103 U bracket_ro (T x);
104 U& bracket_rw (T x);
106
107 /**
108 * @brief Joins another hashmap into the current hashmap.
109 *
110 * @tparam T The type of the key in the hashmap.
111 * @tparam U The type of the value in the hashmap.
112 * @param h The hashmap to be joined into the current hashmap.
113 */
115
116 friend class hashmap<T, U>;
117 friend class rel_hashmap<T, U>;
118 friend class rel_hashmap_rep<T, U>;
119 friend class hashmap_iterator_rep<T, U>;
120 friend int N LESSGTR (hashmap<T, U> h);
121 friend tm_ostream& operator<< LESSGTR (tm_ostream& out, hashmap<T, U> h);
122
123 // only for hashmap<string,tree>
124 /**
125 * @brief Writes back a key-value pair into the current hashmap using a base
126 * hashmap as a reference.
127 *
128 * @tparam T The type of the key in the hashmap.
129 * @tparam U The type of the value in the hashmap.
130 * @param x The key to be inserted or updated in the current hashmap.
131 * @param base The base hashmap used as a reference for the value of the key.
132 */
133 void write_back (T x, hashmap<T, U> base);
134
135 /**
136 * @brief Applies a patch to the current hashmap using another hashmap as a
137 * base reference.
138 *
139 * @tparam T The type of the key in the hashmap.
140 * @tparam U The type of the value in the hashmap.
141 * @param patch The hashmap containing the key-value pairs that serve as the
142 * patch.
143 * @param base The base hashmap used as a reference for the patching process.
144 */
146
147 /**
148 * @brief Applies a post-patch to the current hashmap using another hashmap as
149 * a base reference.
150 *
151 * @tparam T The type of the key in the hashmap.
152 * @tparam U The type of the value in the hashmap.
153 * @param patch The hashmap containing the key-value pairs that serve as the
154 * post-patch.
155 * @param base The base hashmap used as a reference for the post-patching
156 * process.
157 */
159
164 friend class edit_env_rep; // FIXME: broken encapsulation
165 // end only for hashmap<string,tree>
166
167 friend bool operator== LESSGTR (hashmap<T, U> h1, hashmap<T, U> h2);
168 friend bool operator!= LESSGTR (hashmap<T, U> h1, hashmap<T, U> h2);
169};
170
171/**
172 * @brief A simple hashmap class implementation.
173 * @tparam T The type of the key in the hashmap.
174 * @tparam U The type of the value in the hashmap.
175 */
176template <class T, class U> class hashmap {
179
180 /**
181 * @brief Default constructor that initializes the hashmap with default
182 * parameters.
183 */
184 inline hashmap ()
185 : rep (tm_new<hashmap_rep<T, U>> (type_helper<U>::init_val (), 1, 1)) {}
186
187 /**
188 * @brief Constructor that allows custom initial value, size, and maximum load
189 * factor.
190 *
191 * @param init The initial value for the type U.
192 * @param n The initial size of the hashmap.
193 * @param max The maximum load factor of the hashmap.
194 */
195 inline hashmap (U init, int n= 1, int max= 1)
196 : rep (tm_new<hashmap_rep<T, U>> (init, n, max)) {}
197
198 /**
199 * @brief Read-only access operator.
200 *
201 * @param x The key whose value is to be returned.
202 * @return The value corresponding to the provided key.
203 */
204 inline U operator[] (T x) { return rep->bracket_ro (x); }
205
206 /**
207 * @brief Read-write access to the value mapped by the key x.
208 *
209 * @param x The key to look up in the hashmap.
210 * @return A reference to the value associated with the key x.
211 */
212 inline U& operator() (T x) { return rep->bracket_rw (x); }
213};
214CONCRETE_TEMPLATE_2_CODE (hashmap, class, T, class, U);
215
216#define TMPL template <class T, class U>
217
218/**
219 * @brief Returns the size of the given hashmap.
220 *
221 * @tparam T The type of the key in the hashmap.
222 * @tparam U The type of the value in the hashmap.
223 * @param h The hashmap whose size is to be determined.
224 * @return The size of the hashmap.
225 */
226TMPL inline int
228 return h->size;
229}
230
231/**
232 * @brief Creates a new hashmap containing entries that have changed in the
233 * 'patch' compared to 'base'.
234 *
235 * @tparam T The type of the key in the hashmap.
236 * @tparam U The type of the value in the hashmap.
237 * @param patch The hashmap containing updated key-value pairs.
238 * @param base The original hashmap that serves as the reference.
239 * @return A new hashmap containing only the entries that have changed from
240 * 'base' to 'patch'.
241 */
243
244/**
245 * @brief Creates a new hashmap containing entries that are different in the
246 * 'patch' compared to 'base', but with values taken from 'base'.
247 *
248 * @tparam T The type of the key in the hashmap.
249 * @tparam U The type of the value in the hashmap.
250 * @param patch The hashmap containing potentially updated key-value pairs.
251 * @param base The original hashmap that serves as the reference.
252 * @return A new hashmap containing only the entries that are different in
253 * 'patch' but using the values from 'base'.
254 */
256#undef TMPL
257
258#include "hashmap.ipp"
259#include "hashmap_extra.ipp"
260
261#endif // defined HASHMAP_H
blackbox b[13]
#define CONCRETE_TEMPLATE_2_CODE(PTR, TT1, T1, TT2, T2)
Macro used to define the implementation of a concrete smart pointer with reference counting for two t...
Definition classdef.hpp:278
hashmap< T, U > h
Definition iterator.ipp:107
void resize(int n)
Resizes the hashmap and rehashes all existing keys.
Definition hashmap.ipp:48
void reset(T x)
Remove a specific key from the hashmap, if it exists.
Definition hashmap.ipp:108
bool empty()
Definition hashmap.ipp:77
void write_back(T x, hashmap< T, U > base)
Writes back a key-value pair into the current hashmap using a base hashmap as a reference.
void post_patch(hashmap< T, U > patch, hashmap< T, U > base)
Applies a post-patch to the current hashmap using another hashmap as a base reference.
U & bracket_rw_debug(T x)
U & bracket_rw(T x)
Definition hashmap.ipp:82
bool contains(T x)
Definition hashmap.ipp:66
friend hashmap< T, U > copy LESSGTR(hashmap< T, U > h)
U bracket_ro(T x)
Definition hashmap.ipp:97
friend hashmap< T, U > changes LESSGTR(hashmap< T, U > patch, hashmap< T, U > base)
friend int N LESSGTR(hashmap< T, U > h)
void generate(void(*routine)(T))
Applies a given routine to each key in the hashmap.
Definition hashmap.ipp:123
friend class edit_env_rep
Definition hashmap.hpp:164
list< hashentry< T, U > > * a
Definition hashmap.hpp:52
void pre_patch(hashmap< T, U > patch, hashmap< T, U > base)
Applies a patch to the current hashmap using another hashmap as a base reference.
friend hashmap< T, U > invert LESSGTR(hashmap< T, U > patch, hashmap< T, U > base)
void join(hashmap< T, U > H)
Joins another hashmap into the current hashmap.
Definition hashmap.ipp:150
A simple hashmap class implementation.
Definition hashmap.hpp:176
U operator[](T x)
Read-only access operator.
Definition hashmap.hpp:204
hashmap(U init, int n=1, int max=1)
Constructor that allows custom initial value, size, and maximum load factor.
Definition hashmap.hpp:195
U & operator()(T x)
Read-write access to the value mapped by the key x.
Definition hashmap.hpp:212
CONCRETE_TEMPLATE_2(hashmap, T, U)
hashmap()
Default constructor that initializes the hashmap with default parameters.
Definition hashmap.hpp:184
static hashmap< T, U > init
Definition hashmap.hpp:178
The list class represents a linked list.
Definition list.hpp:48
C * tm_new_array(int n)
C * tm_new()
void tm_delete_array(C *Ptr)
bool operator!=(hashmap< T, U > h1, hashmap< T, U > h2)
Definition hashmap.ipp:172
hashmap< T, U > copy(hashmap< T, U > h)
hashmap< T, U > changes(hashmap< T, U > p, hashmap< T, U > b)
Creates a new hashmap containing entries that have changed in the 'patch' compared to 'base'.
tm_ostream & operator<<(tm_ostream &out, hashmap< T, U > h)
Definition hashmap.ipp:135
bool operator==(hashmap< T, U > h1, hashmap< T, U > h2)
Definition hashmap.ipp:160
int N(hashmap< T, U > a)
Returns the size of the given hashmap.
Definition hashmap.hpp:227
hashmap< T, U > invert(hashmap< T, U > p, hashmap< T, U > b)
Creates a new hashmap containing entries that are different in the 'patch' compared to 'base',...
#define TMPL
Definition hashmap.hpp:216
#define H
Definition hashmap.ipp:17
void routine(int key)
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
Hash entry template for key-value pairs.
Definition hashmap.hpp:39
base class of resources
Definition resource.hpp:23
Helper struct for type identification and initialization.
Definition basic.hpp:303