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