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
******************************************************************************/
18
template<class T> class list;
19
template<class T,class U> class hashmap;
20
template<class T,class U> class rel_hashmap;
21
template<class T,class U> class rel_hashmap_rep;
22
template<class T,class U> class hashmap_iterator_rep;
24
template<class T,class U> int N (hashmap<T,U> a);
25
template<class T,class U> ostream& operator << (ostream& out, hashmap<T,U> h);
26
template<class T,class U> hashmap<T,U> copy (hashmap<T,U> h);
27
template<class T,class U> hashmap<T,U> changes (hashmap<T,U> p,hashmap<T,U> b);
28
template<class T,class U> hashmap<T,U> invert (hashmap<T,U> p, hashmap<T,U> b);
29
template<class T,class U> bool operator == (hashmap<T,U> h1, hashmap<T,U> h2);
30
template<class T,class U> bool operator != (hashmap<T,U> h1, hashmap<T,U> h2);
32
template<class T, class U> struct hashentry {
37
hashentry<T,U> (int code, T key2, U im2);
41
template<class T, class U> class hashmap_rep: concrete_struct {
42
int size; // size of hashmap (nr of entries)
43
int n; // nr of keys (a power of two)
44
int max; // mean number of entries per key
45
U init; // default entry
46
list<hashentry<T,U> >* a; // the array of entries
49
inline hashmap_rep<T,U>(U init2, int n2=1, int max2=1):
50
size(0), n(n2), max(max2), init(init2), a(new list<hashentry<T,U> >[n]) {}
51
inline ~hashmap_rep<T,U> () { delete[] a; }
54
void generate (void (*routine) (T));
59
void join (hashmap<T,U> H);
61
friend class hashmap<T,U>;
62
friend class rel_hashmap<T,U>;
63
friend class rel_hashmap_rep<T,U>;
64
friend class hashmap_iterator_rep<T,U>;
65
friend int N LESSGTR (hashmap<T,U> h);
66
friend ostream& operator << LESSGTR (ostream& out, hashmap<T,U> h);
68
// only for hashmap<string,tree>
69
void write_back (T x, hashmap<T,U> base);
70
void pre_patch (hashmap<T,U> patch, hashmap<T,U> base);
71
void post_patch (hashmap<T,U> patch, hashmap<T,U> base);
72
friend hashmap<T,U> copy LESSGTR (hashmap<T,U> h);
73
friend hashmap<T,U> changes LESSGTR (hashmap<T,U> patch, hashmap<T,U> base);
74
friend hashmap<T,U> invert LESSGTR (hashmap<T,U> patch, hashmap<T,U> base);
75
friend class edit_env_rep; // FIXME: broken encapsulation
76
// end only for hashmap<string,tree>
78
friend bool operator == LESSGTR (hashmap<T,U> h1, hashmap<T,U> h2);
79
friend bool operator != LESSGTR (hashmap<T,U> h1, hashmap<T,U> h2);
82
template<class T, class U> class hashmap {
83
CONCRETE_TEMPLATE_2(hashmap,T,U);
84
static hashmap<T,U> init;
86
rep (new hashmap_rep<T,U> (type_helper<U>::init, 1, 1)) {}
87
inline hashmap (U init, int n=1, int max=1):
88
rep (new hashmap_rep<T,U> (init, n, max)) {}
89
// only for hashmap<string,tree>
90
hashmap (U init, tree t);
91
// end only for hashmap<string,tree>
92
inline U operator [] (T x) { return rep->bracket_ro (x); }
93
inline U& operator () (T x) { return rep->bracket_rw (x); }
96
CONCRETE_TEMPLATE_2_CODE(hashmap,class,T,class,U);
98
#define TMPL template<class T, class U>
99
TMPL inline int N (hashmap<T,U> h) { return h->size; }
100
TMPL hashmap<T,U> changes (hashmap<T,U> patch, hashmap<T,U> base);
101
TMPL hashmap<T,U> invert (hashmap<T,U> patch, hashmap<T,U> base);
104
#include "hashmap.cpp"
105
#include "hashmap_extra.cpp"
107
#endif // defined HASHMAP_H