2
/******************************************************************************
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 and comes WITHOUT
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
11
******************************************************************************/
15
#include "hashmap.hpp"
16
#define TMPL template<class T, class U>
17
#define H hashentry<T,U>
19
/******************************************************************************
21
******************************************************************************/
23
TMPL H::hashentry (T key2, U im2): key (key2), im (im2) {}
25
TMPL H::operator tree () {
26
return tree (ASSOCIATE, as_tree(key), as_tree(im)); }
29
operator << (ostream& out, H h) {
30
out << h.key << "->" << h.im;
35
operator == (H h1, H h2) {
36
return (h1.key==h2.key) && (h1.im==h2.im);
40
operator != (H h1, H h2) {
41
return (h1.key!=h2.key) || (h1.im!=h2.im);
44
/******************************************************************************
45
* Routines for hashmaps
46
******************************************************************************/
49
hashmap_rep<T,U>::resize (int n2) {
52
list<hashentry<T,U> >* olda= a;
54
a= new list<hashentry<T,U> >[n];
55
for (i=0; i<oldn; i++) {
56
list<hashentry<T,U> > l(olda[i]);
58
list<hashentry<T,U> >& newl= a[hash(l->item.key)&(n-1)];
59
newl= list<hashentry<T,U> > (l->item, newl);
67
hashmap_rep<T,U>::contains (T x) {
68
list<hashentry<T,U> > l (a[hash(x)&(n-1)]);
70
if (l->item.key==x) return true;
77
hashmap_rep<T,U>::empty () {
82
hashmap_rep<T,U>::bracket_rw (T x) {
83
register int hv= hash(x);
84
list<hashentry<T,U> > l (a[hv&(n-1)]);
86
if (l->item.key==x) return l->item.im;
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 (x, init), rl);
97
hashmap_rep<T,U>::bracket_ro (T x) {
98
list<hashentry<T,U> > l (a[hash(x)&(n-1)]);
100
if (l->item.key==x) return l->item.im;
107
hashmap_rep<T,U>::reset (T x) {
108
list<hashentry<T,U> > *l= &(a[hash(x)&(n-1)]);
110
if ((*l)->item.key==x) {
113
if (size<(n>>1)*max) resize (n>>1);
121
hashmap_rep<T,U>::generate (void (*routine) (T)) {
123
for (i=0; i<n; i++) {
124
list<hashentry<T,U> > l (a[i]);
126
routine (l->item.key);
133
operator << (ostream& out, hashmap<T,U> h) {
134
int i=0, j=0, n=h->n, size=h->size;
137
list<hashentry<T,U> > l=h->a[i];
138
for (; !nil(l); l=l->next, j++) {
140
if (j!=size-1) out << ", ";
147
TMPL hashmap<T,U>::operator tree () {
148
int i=0, j=0, n=rep->n, size=rep->size;
149
tree t (COLLECTION, size);
151
list<hashentry<T,U> > l=rep->a[i];
152
for (; !nil(l); l=l->next, j++)
153
t[j]= (tree) l->item;
159
hashmap_rep<T,U>::join (hashmap<T,U> h) {
162
list<hashentry<T,U> > l=h->a[i];
163
for (; !nil(l); l=l->next)
164
bracket_rw (l->item.key)= copy (l->item.im);
169
operator == (hashmap<T,U> h1, hashmap<T,U> h2) {
170
if (h1->size != h2->size) return false;
173
list<hashentry<T,U> > l=h1->a[i];
174
for (; !nil(l); l=l->next)
175
if (h2[l->item.key] != l->item.im) return false;
181
operator != (hashmap<T,U> h1, hashmap<T,U> h2) {
187
#endif // defined HASHMAP_CC