Lolly 1.4.27
Loading...
Searching...
No Matches
hashmap.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : hashmap.cpp
4 * DESCRIPTION: fixed size hashmaps with reference counting
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#ifndef HASHMAP_CC
13#define HASHMAP_CC
14
15#include "hashmap.hpp"
16#define TMPL template <class T, class U>
17#define H hashentry<T, U>
18
19/******************************************************************************
20 * Hashmap entries
21 ******************************************************************************/
22
23TMPL
24H::hashentry (int code2, T key2, U im2)
25 : code (code2), key (key2), im (im2) {}
26
28operator<< (tm_ostream& out, H h) {
29 out << h.key << "->" << h.im;
30 return out;
31}
32
33TMPL bool
35 return (h1.code == h2.code) && (h1.key == h2.key) && (h1.im == h2.im);
36}
37
38TMPL bool
40 return (h1.code != h2.code) || (h1.key != h2.key) || (h1.im != h2.im);
41}
42
43/******************************************************************************
44 * Routines for hashmaps
45 ******************************************************************************/
46
47TMPL void
49 int i;
50 int oldn= n;
52 n = n2;
54 for (i= 0; i < oldn; i++) {
56 while (!is_nil (l)) {
57 list<hashentry<T, U>>& newl= a[hash (l->item.key) & (n - 1)];
58 newl = list<hashentry<T, U>> (l->item, newl);
59 l = l->next;
60 }
61 }
63}
64
65TMPL bool
67 int hv= hash (x);
68 list<hashentry<T, U>> l (a[hv & (n - 1)]);
69 while (!is_nil (l)) {
70 if (l->item.code == hv && l->item.key == x) return true;
71 l= l->next;
72 }
73 return false;
74}
75
76TMPL bool
78 return size == 0;
79}
80
81TMPL U&
83 int hv= hash (x);
84 list<hashentry<T, U>> l (a[hv & (n - 1)]);
85 while (!is_nil (l)) {
86 if (l->item.code == hv && l->item.key == x) return l->item.im;
87 l= l->next;
88 }
89 if (size >= n * max) resize (n << 1);
90 list<hashentry<T, U>>& rl= a[hv & (n - 1)];
91 rl = list<hashentry<T, U>> (H (hv, x, init), rl);
92 size++;
93 return rl->item.im;
94}
95
96TMPL U
98 int hv= hash (x);
99 list<hashentry<T, U>> l (a[hv & (n - 1)]);
100 while (!is_nil (l)) {
101 if (l->item.code == hv && l->item.key == x) return l->item.im;
102 l= l->next;
103 }
104 return init;
105}
106
107TMPL void
109 int hv= hash (x);
110 list<hashentry<T, U>>* l = &(a[hv & (n - 1)]);
111 while (!is_nil (*l)) {
112 if ((*l)->item.code == hv && (*l)->item.key == x) {
113 *l= (*l)->next;
114 size--;
115 if (size < (n >> 1) * max) resize (n >> 1);
116 return;
117 }
118 l= &((*l)->next);
119 }
120}
121
122TMPL void
124 int i;
125 for (i= 0; i < n; i++) {
126 list<hashentry<T, U>> l (a[i]);
127 while (!is_nil (l)) {
128 routine (l->item.key);
129 l= l->next;
130 }
131 }
132}
133
136 int i= 0, j= 0, n= h->n, size= h->size;
137 out << "{ ";
138 for (; i < n; i++) {
139 list<hashentry<T, U>> l= h->a[i];
140 for (; !is_nil (l); l= l->next, j++) {
141 out << l->item;
142 if (j != size - 1) out << ", ";
143 }
144 }
145 out << " }";
146 return out;
147}
148
149TMPL void
151 int i= 0, n= h->n;
152 for (; i < n; i++) {
153 list<hashentry<T, U>> l= h->a[i];
154 for (; !is_nil (l); l= l->next)
155 bracket_rw (l->item.key)= copy (l->item.im);
156 }
157}
158
159TMPL bool
161 if (h1->size != h2->size) return false;
162 int i= 0, n= h1->n;
163 for (; i < n; i++) {
164 list<hashentry<T, U>> l= h1->a[i];
165 for (; !is_nil (l); l= l->next)
166 if (h2[l->item.key] != l->item.im) return false;
167 }
168 return true;
169}
170
171TMPL bool
173 return !(h1 == h2);
174}
175
176#undef H
177#undef TMPL
178#endif // defined HASHMAP_CC
int hash(pointer ptr)
Computes a hash value for a pointer.
Definition basic.cpp:17
bool is_nil(blackbox x)
Definition blackbox.hpp:29
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
U & bracket_rw(T x)
Definition hashmap.ipp:82
bool contains(T x)
Definition hashmap.ipp:66
U bracket_ro(T x)
Definition hashmap.ipp:97
void generate(void(*routine)(T))
Applies a given routine to each key in the hashmap.
Definition hashmap.ipp:123
void join(hashmap< T, U > H)
Joins another hashmap into the current hashmap.
Definition hashmap.ipp:150
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
static list< T > init
A static list object used for initializing new list objects.
Definition list.hpp:100
void tm_delete_array(C *Ptr)
bool operator==(H h1, H h2)
Definition hashmap.ipp:34
#define H
Definition hashmap.ipp:17
tm_ostream & operator<<(tm_ostream &out, H h)
Definition hashmap.ipp:28
#define TMPL
Definition hashmap.ipp:16
bool operator!=(H h1, H h2)
Definition hashmap.ipp:39
void routine(int key)
bool is_nil(list< T > l)
Check if a list is nil (i.e., an empty list).
list< T > copy(list< T > l)
Create a copy of a list.
Definition list.ipp:164
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40