~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Classes/Compound/hashmap.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : hashmap.hpp
 
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
******************************************************************************/
 
12
 
 
13
#ifndef HASHMAP_H
 
14
#define HASHMAP_H
 
15
#include "list.hpp"
 
16
 
 
17
class tree;
 
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;
 
23
 
 
24
template<class T, class U> struct hashentry {
 
25
  T key;
 
26
  U im;
 
27
  hashentry<T,U> () { }
 
28
  hashentry<T,U> (T key2, U im2);
 
29
  operator tree ();
 
30
};
 
31
 
 
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
 
38
 
 
39
public:
 
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; }
 
43
  void resize (int n);
 
44
  void reset (T x);
 
45
  void generate (void (*routine) (T));
 
46
  bool contains (T x);
 
47
  bool empty ();
 
48
  U    bracket_ro (T x);
 
49
  U&   bracket_rw (T x);
 
50
  void join (hashmap<T,U> H);
 
51
 
 
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);
 
58
 
 
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>
 
68
 
 
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);
 
71
};
 
72
 
 
73
template<class T, class U> class hashmap {
 
74
  CONCRETE_TEMPLATE_2(hashmap,T,U);
 
75
  inline hashmap ():
 
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); }
 
84
  operator tree ();
 
85
};
 
86
CONCRETE_TEMPLATE_2_CODE(hashmap,class,T,class,U);
 
87
 
 
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);
 
92
#undef TMPL
 
93
 
 
94
#include "hashmap.cpp"
 
95
#include "hashmap_extra.cpp"
 
96
 
 
97
#endif // defined HASHMAP_H