]> www.ginac.de Git - cln.git/blob - src/base/hash/cl_hash1.h
5494b00e9634634a32ebbdeb25d5ae5255c0cedb
[cln.git] / src / base / hash / cl_hash1.h
1 // Hash tables with 1 key and a value
2
3 #ifndef _CL_HASH1_H
4 #define _CL_HASH1_H
5
6 #include "cl_hash.h"
7 #include "cl_iterator.h"
8
9 // Requirements:
10 // - function  bool equal (key1_type,key1_type);
11 // - function  unsigned long hashcode (key1_type);
12
13 #if (defined(__alpha__) && !defined(__GNUC__))
14 template <class key1_type, class value_type> struct cl_htentry1;
15 #endif
16
17 template <class key1_type, class value_type>
18 struct cl_htentry1 {
19     ALLOCATE_ANYWHERE(cl_htentry1)
20     key1_type key;
21     value_type val;
22     const value_type& htvalue () { return val; }
23     cl_htentry1 (const key1_type& k, const value_type& v)
24         : key (k), val (v) {}
25 #if (defined(__rs6000__) && !defined(__GNUC__))
26     cl_htentry1 () {}
27 #endif
28 };
29
30 template <class key1_type, class value_type>
31 struct cl_heap_hashtable_1 : public cl_heap_hashtable <cl_htentry1 <key1_type,value_type> > {
32     // Allocation.
33     void* operator new (size_t size) { return cl_malloc_hook(size); }
34     // Deallocation.
35     void operator delete (void* ptr) { cl_free_hook(ptr); }
36 public:
37     // Lookup (htref alias gethash).
38     // Returns a pointer which you should immediately dereference
39     // if it is not NULL.
40     value_type* get (const key1_type& key)
41     {
42         var long index = _slots[hashcode(key) % _modulus] - 1;
43         while (index >= 0) {
44             if (!(index < _size))
45                 cl_abort();
46             if (equal(key,_entries[index].entry.key))
47                 return &_entries[index].entry.val;
48             index = _entries[index].next - 1;
49         }
50         return NULL;
51     }
52     // Store (htset alias puthash).
53     void put (const key1_type& key, const value_type& val)
54     {
55         var unsigned long hcode = hashcode(key);
56         // Search whether it is already there.
57         {
58             var long index = _slots[hcode % _modulus] - 1;
59             while (index >= 0) {
60                 if (!(index < _size))
61                     cl_abort();
62                 if (equal(key,_entries[index].entry.key)) {
63                     _entries[index].entry.val = val;
64                     return;
65                 }
66                 index = _entries[index].next - 1;
67             }
68         }
69         // Put it into the table.
70         prepare_store();
71         var long hindex = hcode % _modulus; // _modulus may have changed!
72         var long index = get_free_index();
73         new (&_entries[index].entry) cl_htentry1<key1_type,value_type> (key,val);
74         _entries[index].next = _slots[hindex];
75         _slots[hindex] = 1+index;
76         _count++;
77     }
78     // Remove (htrem alias remhash).
79     void remove (const key1_type& key)
80     {
81         var long* _index = &_slots[hashcode(key) % _modulus];
82         while (*_index > 0) {
83             var long index = *_index - 1;
84             if (!(index < _size))
85                 cl_abort();
86             if (equal(key,_entries[index].entry.key)) {
87                 // Remove _entries[index].entry
88                 *_index = _entries[index].next;
89                 _entries[index].~htxentry();
90                 // The entry is now free.
91                 put_free_index(index);
92                 // That's it.
93                 _count--;
94                 return;
95             }
96             _index = &_entries[index].next;
97         }
98     }
99     // Iterate through the table.
100     // No stuff should be inserted into the table during the iteration,
101     // or you may find yourself iterating over an entry vector which has
102     // already been freed!
103     // ??
104 private:
105     // Prepare a store operation: make sure that the free list is non-empty.
106     // This may change the table's size!
107     void prepare_store ()
108     {
109       #if !(defined(__sparc__) && !defined(__GNUC__))
110         if (_freelist < -1)
111             return;
112         // Can we make room?
113         if (_garcol_fun(this))
114             if (_freelist < -1)
115                 return;
116         // No! Have to grow the hash table.
117         grow();
118       #else
119         // workaround Sun C++ 4.1 inline function compiler bug
120         if (_freelist >= -1) {
121             if (!_garcol_fun(this) || (_freelist >= -1))
122                 grow();
123         }
124       #endif
125     }
126     void grow ()
127     {
128         var long new_size = _size + (_size >> 1) + 1; // _size*1.5
129         var long new_modulus = compute_modulus(new_size);
130         var void* new_total_vector = cl_malloc_hook(new_modulus*sizeof(long) + new_size*sizeof(htxentry));
131         var long* new_slots = (long*) ((char*)new_total_vector + 0);
132         var htxentry* new_entries = (htxentry *) ((char*)new_total_vector + new_modulus*sizeof(long));
133         for (var long hi = new_modulus-1; hi >= 0; hi--)
134             new_slots[hi] = 0;
135         var long free_list_head = -1;
136         for (var long i = new_size-1; i >= 0; i--) {
137             new_entries[i].next = free_list_head;
138             free_list_head = -2-i;
139         }
140         var htxentry* old_entries = _entries;
141         for (var long old_index = 0; old_index < _size; old_index++)
142             if (old_entries[old_index].next >= 0) {
143                 var key1_type& key = old_entries[old_index].entry.key;
144                 var value_type& val = old_entries[old_index].entry.val;
145                 var long hindex = hashcode(key) % new_modulus;
146                 var long index = -2-free_list_head;
147                 free_list_head = new_entries[index].next;
148                 new (&new_entries[index].entry) cl_htentry1<key1_type,value_type> (key,val);
149                 new_entries[index].next = new_slots[hindex];
150                 new_slots[hindex] = 1+index;
151                 old_entries[old_index].~htxentry();
152             }
153         cl_free_hook(_total_vector);
154         _modulus = new_modulus;
155         _size = new_size;
156         _freelist = free_list_head;
157         _slots = new_slots;
158         _entries = new_entries;
159         _total_vector = new_total_vector;
160     }
161 };
162
163 #endif /* _CL_HASH1_H */