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> struct hashentry {
28
hashentry<T,U> (T key2, U im2);
32
template<class T, class U> class hashmap_rep: concrete_struct {
33
int size; // size of hashmap (nr of entries)
34
int n; // nr of keys (a power of two)
35
int max; // mean number of entries per key
36
U init; // default entry
37
list<hashentry<T,U> >* a; // the array of entries
40
inline hashmap_rep<T,U>(U init2, int n2=1, int max2=1):
41
size(0), n(n2), max(max2), init(init2), a(new list<hashentry<T,U> >[n]) {}
42
inline ~hashmap_rep<T,U> () { delete[] a; }
45
void generate (void (*routine) (T));
50
void join (hashmap<T,U> H);
52
friend class hashmap<T,U>;
53
friend class rel_hashmap<T,U>;
54
friend class rel_hashmap_rep<T,U>;
55
friend class hashmap_iterator_rep<T,U>;
56
friend int N LESSGTR (hashmap<T,U> h);
57
friend ostream& operator << LESSGTR (ostream& out, hashmap<T,U> h);
59
// only for hashmap<string,tree>
60
void write_back (T x, hashmap<T,U> base);
61
void pre_patch (hashmap<T,U> patch, hashmap<T,U> base);
62
void post_patch (hashmap<T,U> patch, hashmap<T,U> base);
63
friend hashmap<T,U> copy LESSGTR (hashmap<T,U> h);
64
friend hashmap<T,U> changes LESSGTR (hashmap<T,U> patch, hashmap<T,U> base);
65
friend hashmap<T,U> invert LESSGTR (hashmap<T,U> patch, hashmap<T,U> base);
66
friend class edit_env_rep; // FIXME: broken encapsulation
67
// end only for hashmap<string,tree>
69
friend bool operator == LESSGTR (hashmap<T,U> h1, hashmap<T,U> h2);
70
friend bool operator != LESSGTR (hashmap<T,U> h1, hashmap<T,U> h2);
73
template<class T, class U> class hashmap {
74
CONCRETE_TEMPLATE_2(hashmap,T,U);
76
rep (new hashmap_rep<T,U>(U::init, 1, 1)) {}
77
inline hashmap (U init, int n=1, int max=1):
78
rep (new hashmap_rep<T,U>(init, n, max)) {}
79
// only for hashmap<string,tree>
80
hashmap (U init, tree t);
81
// end only for hashmap<string,tree>
82
inline U operator [] (T x) { return rep->bracket_ro (x); }
83
inline U& operator () (T x) { return rep->bracket_rw (x); }
86
CONCRETE_TEMPLATE_2_CODE(hashmap,class,T,class,U);
88
#define TMPL template<class T, class U>
89
TMPL inline int N (hashmap<T,U> h) { return h->size; }
90
TMPL hashmap<T,U> changes (hashmap<T,U> patch, hashmap<T,U> base);
91
TMPL hashmap<T,U> invert (hashmap<T,U> patch, hashmap<T,U> base);
94
#include "hashmap.cpp"
95
#include "hashmap_extra.cpp"
97
#endif // defined HASHMAP_H