1 // Hash tables for making objects unique
7 #include "cl_iterator.h"
11 // In such a hash table an entry's key is determined by its value
12 // and not stored explicitly.
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);
20 template <class key1_type, class value_type>
21 struct cl_htuniqentry {
22 ALLOCATE_ANYWHERE(cl_htuniqentry)
24 const value_type& htvalue () { return val; }
25 cl_htuniqentry (const value_type& v)
27 #if (defined(__rs6000__) && !defined(__GNUC__))
32 template <class key1_type, class value_type>
33 struct cl_heap_hashtable_uniq : public cl_heap_hashtable <cl_htuniqentry <key1_type,value_type> > {
36 typedef typename cl_heap_hashtable <cl_htuniqentry <key1_type,value_type> >::htxentry htxentry;
39 void* operator new (size_t size) { return malloc_hook(size); }
41 void operator delete (void* ptr) { free_hook(ptr); }
43 // Lookup (htref alias gethash).
44 // Returns a pointer which you should immediately dereference
46 value_type * get (const key1_type& key)
48 var long index = this->_slots[hashcode(key) % this->_modulus] - 1;
50 if (!(index < this->_size))
52 if (equal(key,hashkey(this->_entries[index].entry.val)))
53 return &this->_entries[index].entry.val;
54 index = this->_entries[index].next - 1;
58 // Store (htset alias puthash).
59 void put (const key1_type& key)
61 var unsigned long hcode = hashcode(key);
62 // Search whether it is already there.
64 var long index = this->_slots[hcode % this->_modulus] - 1;
66 if (!(index < this->_size))
68 if (equal(key,hashkey(this->_entries[index].entry.val)))
70 index = this->_entries[index].next - 1;
73 // Put it into the table.
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;
82 // Remove (htrem alias remhash).
83 void remove (const key1_type& key)
85 var long* _index = &this->_slots[hashcode(key) % this->_modulus];
87 var long index = *_index - 1;
88 if (!(index < this->_size))
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);
100 _index = &this->_entries[index].next;
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!
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 ()
113 #if !(defined(__sparc__) && !defined(__GNUC__))
114 if (this->_freelist < -1)
117 if (_garcol_fun(this))
118 if (this->_freelist < -1)
120 // No! Have to grow the hash table.
123 // workaround Sun C++ 4.1 inline function compiler bug
124 if (this->_freelist >= -1) {
125 if (!_garcol_fun(this) || (this->_freelist >= -1))
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--)
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;
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();
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;
168 #endif /* _CL_HASHUNIQ_H */