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