Lolly 1.4.27
Loading...
Searching...
No Matches
rel_hashmap.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : rel_hashmap.cpp
4 * DESCRIPTION: Relative hashmaps are lists of hashmaps
5 * the value of an entry is the value relative to
6 * the last hashmap in the list for which the value is defined.
7 * Relative hashmaps can be used to perform small local
8 * changes on a hashmap, which may be discarded or
9 * confirmed afterwards.
10 * COPYRIGHT : (C) 1999 Joris van der Hoeven
11 *******************************************************************************
12 * This software falls under the GNU general public license version 3 or later.
13 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
14 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
15 ******************************************************************************/
16
17#ifndef REL_HASHMAP_CC
18#define REL_HASHMAP_CC
19#include "rel_hashmap.hpp"
20
21template <class T, class U>
22U
24 ASSERT (rep != NULL, "invalid relative hashmap");
25 if (rep->item->contains (x) || is_nil (rep->next)) return rep->item[x];
26 return rep->next[x];
27}
28
29template <class T, class U>
30U&
32 ASSERT (rep != NULL, "invalid relative hashmap");
33 if (rep->item->contains (x)) return rep->item (x);
34 if ((!is_nil (rep->next)) && rep->next->contains (x))
35 rep->item (x)= copy (rep->next[x]);
36 return rep->item (x);
37}
38
39template <class T, class U>
40bool
42 if (item->contains (x)) return true;
43 if (is_nil (next)) return false;
44 return next->contains (x);
45}
46
47template <class T, class U>
48void
50 next= rel_hashmap<T, U> (item, next);
51 item= hashmap<T, U> (item->init);
52}
53
54template <class T, class U>
55void
57 ASSERT (!is_nil (next), "relative hashmap cannot be shortened");
58 item= next->item;
59 next= next->next;
60}
61
62template <class T, class U>
63void
65 ASSERT (!is_nil (next), "relative hashmap cannot be merged");
66 next->change (item);
67 shorten ();
68}
69
70template <class T, class U>
71void
73 int i;
74 rel_hashmap<T, U> h (item, next);
76 for (i= 0; i < CH->n; i++) {
77 list<hashentry<T, U>> l (CH->a[i]);
78 while (!is_nil (l)) {
79 if (h[l->item.key] == l->item.im)
81 l= l->next;
82 }
83 }
84 while (!is_nil (remove)) {
85 CH->reset (remove->item.key);
86 remove= remove->next;
87 }
88}
89
90template <class T, class U>
91void
93 int i;
95 for (i= 0; i < item->n; i++) {
96 list<hashentry<T, U>> l (item->a[i]);
97 while (!is_nil (l)) {
98 if (!CH->contains (l->item.key))
99 add= list<hashentry<T, U>> (l->item, add);
100 l= l->next;
101 }
102 }
103 while (!is_nil (add)) {
104 CH (add->item.key)= next[add->item.key];
105 add = add->next;
106 }
107 find_changes (CH);
108}
109
110template <class T, class U>
111void
113 int i;
114 for (i= 0; i < CH->n; i++) {
115 list<hashentry<T, U>> l (CH->a[i]);
116 while (!is_nil (l)) {
117 item (l->item.key)= l->item.im;
118 l = l->next;
119 }
120 }
121}
122
123template <class T, class U>
126 if (is_nil (H)) out << "(null)";
127 else {
128 while (!is_nil (H->next)) {
129 out << H->item << LF;
130 out << HRULE << LF;
131 H= H->next;
132 }
133 out << H->item << LF;
134 }
135 return out;
136}
137
138#endif // defined REL_HASHMAP_CC
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
@ LF
Definition basic.hpp:287
@ HRULE
Definition basic.hpp:287
bool is_nil(blackbox x)
Definition blackbox.hpp:29
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
void find_changes(hashmap< T, U > &CH)
void change(hashmap< T, U > CH)
void find_differences(hashmap< T, U > &CH)
bool contains(T x)
U & operator()(T x)
U operator[](T x)
#define H
Definition hashmap.ipp:17
list< T > remove(list< T > l, T what)
Create a new list with a specific item removed.
Definition list.ipp:214
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
tm_ostream & operator<<(tm_ostream &out, rel_hashmap< T, U > H)
base class of resources
Definition resource.hpp:23