2
/******************************************************************************
3
* MODULE : hashmap_extra.cpp
4
* DESCRIPTION: extra routines for hashmap<string,tree>
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
******************************************************************************/
13
#ifndef HASHMAP_EXTRA_CC
14
#define HASHMAP_EXTRA_CC
15
#include "hashmap.hpp"
16
#define TMPL template<class T, class U>
17
#define H hashentry<T,U>
20
hashmap_rep<T,U>::write_back (T x, hashmap<T,U> base) {
21
register int hv= hash (x);
22
list<hashentry<T,U> > l (a [hv & (n-1)]);
24
if (l->item.code == hv && l->item.key == x)
28
if (size >= n*max) resize (n<<1);
29
list<hashentry<T,U> >& rl= a[hv & (n-1)];
30
rl= list<hashentry<T,U> > (H (hv, x, init), rl);
33
list<hashentry<T,U> > bl (base->a [hv & (base->n-1)]);
35
if (bl->item.code == hv && bl->item.key == x) {
36
rl->item.im= bl->item.im;
41
rl->item.im= base->init;
45
hashmap_rep<T,U>::pre_patch (hashmap<T,U> patch, hashmap<T,U> base) {
46
int i= 0, n= patch->n;
48
list<hashentry<T,U> > l= patch->a[i];
49
for (; !nil (l); l= l->next) {
51
U y= contains (x)? bracket_ro (x): l->item.im;
52
if (base[x] == y) reset (x);
53
else bracket_rw (x)= y;
59
hashmap_rep<T,U>::post_patch (hashmap<T,U> patch, hashmap<T,U> base) {
60
int i= 0, n= patch->n;
62
list<hashentry<T,U> > l= patch->a[i];
63
for (; !nil (l); l= l->next) {
66
if (base[x] == y) reset (x);
67
else bracket_rw (x)= y;
72
TMPL list<hashentry<T,U> >
73
copy_list (list<hashentry<T,U> > l) {
74
if (nil (l)) return l;
75
else return list<hashentry<T,U> >
76
(hashentry<T,U> (l->item.code, l->item.key, l->item.im),
81
copy (hashmap<T,U> h) {
83
hashmap<T,U> h2 (h->init, n, h->max);
86
h2->a[i]= copy_list (h->a[i]);
91
changes (hashmap<T,U> patch, hashmap<T,U> base) {
93
hashmap<T,U> h (base->init);
94
for (i=0; i<patch->n; i++) {
95
list<hashentry<T,U> > l (patch->a[i]);
97
if (l->item.im != base [l->item.key])
98
h (l->item.key)= l->item.im;
106
invert (hashmap<T,U> patch, hashmap<T,U> base) {
108
hashmap<T,U> h (base->init);
109
for (i=0; i<patch->n; i++) {
110
list<hashentry<T,U> > l (patch->a[i]);
112
if (l->item.im != base [l->item.key])
113
h (l->item.key)= base [l->item.key];
120
TMPL hashmap<T,U>::hashmap (U init, tree t):
121
rep (new hashmap_rep<T,U>(init, 1, 1))
125
if (is_func (t[i], ASSOCIATE, 2))
126
rep->bracket_rw (get_label (t[i][0]))= copy (t[i][1]);
131
#endif // defined HASHMAP_EXTRA_CC