Lolly 1.4.28
Loading...
Searching...
No Matches
hashmap_extra.ipp
Go to the documentation of this file.
1
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 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_EXTRA_CC
13#define HASHMAP_EXTRA_CC
14
15#include "hashmap.hpp"
16#define TMPL template <class T, class U>
17#define H hashentry<T, U>
18
19TMPL void
21 int hv= hash (x);
22 list<hashentry<T, U>> l (a[hv & (n - 1)]);
23 while (!is_nil (l)) {
24 if (l->item.code == hv && l->item.key == x) return;
25 l= l->next;
26 }
27 if (size >= n * max) resize (n << 1);
28 list<hashentry<T, U>>& rl= a[hv & (n - 1)];
29 rl = list<hashentry<T, U>> (H (hv, x, init), rl);
30 size++;
31
32 list<hashentry<T, U>> bl (base->a[hv & (base->n - 1)]);
33 while (!is_nil (bl)) {
34 if (bl->item.code == hv && bl->item.key == x) {
35 rl->item.im= bl->item.im;
36 return;
37 }
38 bl= bl->next;
39 }
40 rl->item.im= base->init;
41}
42
43TMPL void
45 int i= 0, n= patch->n;
46 for (; i < n; i++) {
48 for (; !is_nil (l); l= l->next) {
49 T x= l->item.key;
50 U y= contains (x) ? bracket_ro (x) : l->item.im;
51 if (base[x] == y) reset (x);
52 else bracket_rw (x)= y;
53 }
54 }
55}
56
57TMPL void
59 int i= 0, n= patch->n;
60 for (; i < n; i++) {
62 for (; !is_nil (l); l= l->next) {
63 T x= l->item.key;
64 U y= l->item.im;
65 if (base[x] == y) reset (x);
66 else bracket_rw (x)= y;
67 }
68 }
69}
70
73 if (is_nil (l)) return l;
74 else
75 return list<hashentry<T, U>> (
76 hashentry<T, U> (l->item.code, l->item.key, l->item.im),
77 copy_list (l->next));
78}
79
82 int i, n= h->n;
83 hashmap<T, U> h2 (h->init, n, h->max);
84 h2->size= h->size;
85 for (i= 0; i < n; i++)
86 h2->a[i]= copy_list (h->a[i]);
87 return h2;
88}
89
92 int i;
94 for (i= 0; i < patch->n; i++) {
95 list<hashentry<T, U>> l (patch->a[i]);
96 while (!is_nil (l)) {
97 if (l->item.im != base[l->item.key]) h (l->item.key)= l->item.im;
98 l= l->next;
99 }
100 }
101 return h;
102}
103
106 int i;
108 for (i= 0; i < patch->n; i++) {
109 list<hashentry<T, U>> l (patch->a[i]);
110 while (!is_nil (l)) {
111 if (l->item.im != base[l->item.key]) h (l->item.key)= base[l->item.key];
112 l= l->next;
113 }
114 }
115 return h;
116}
117
118#undef H
119#undef TMPL
120#endif // defined HASHMAP_EXTRA_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 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.
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.
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
#define H
Definition hashmap.ipp:17
list< hashentry< T, U > > copy_list(list< hashentry< T, U > > l)
hashmap< T, U > copy(hashmap< T, U > h)
hashmap< T, U > changes(hashmap< T, U > patch, hashmap< T, U > base)
Creates a new hashmap containing entries that have changed in the 'patch' compared to 'base'.
hashmap< T, U > invert(hashmap< T, U > patch, hashmap< T, U > base)
Creates a new hashmap containing entries that are different in the 'patch' compared to 'base',...
#define TMPL
bool is_nil(list< T > l)
Check if a list is nil (i.e., an empty list).
bool contains(list< T > l, T what)
Check if a list contains a specific item.
Definition list.ipp:222
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40