1 // Hash tables with 1 key and a value
7 #include "cl_iterator.h"
12 // - function bool equal (key1_type,key1_type);
13 // - function unsigned long hashcode (key1_type);
15 #if (defined(__alpha__) && !defined(__GNUC__))
16 template <class key1_type, class value_type> struct cl_htentry1;
19 template <class key1_type, class value_type>
21 ALLOCATE_ANYWHERE(cl_htentry1)
24 const value_type& htvalue () { return val; }
25 cl_htentry1 (const key1_type& k, const value_type& v)
27 #if (defined(__rs6000__) && !defined(__GNUC__))
32 template <class key1_type, class value_type>
33 struct cl_heap_hashtable_1 : public cl_heap_hashtable <cl_htentry1 <key1_type,value_type> > {
36 typedef typename cl_heap_hashtable <cl_htentry1 <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,this->_entries[index].entry.key))
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, const value_type& val)
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,this->_entries[index].entry.key)) {
69 this->_entries[index].entry.val = val;
72 index = this->_entries[index].next - 1;
75 // Put it into the table.
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_htentry1<key1_type,value_type> (key,val);
80 this->_entries[index].next = this->_slots[hindex];
81 this->_slots[hindex] = 1+index;
84 // Remove (htrem alias remhash).
85 void remove (const key1_type& key)
87 var long* _index = &this->_slots[hashcode(key) % this->_modulus];
89 var long index = *_index - 1;
90 if (!(index < this->_size))
92 if (equal(key,this->_entries[index].entry.key)) {
93 // Remove _entries[index].entry
94 *_index = this->_entries[index].next;
95 this->_entries[index].~htxentry();
96 // The entry is now free.
97 put_free_index(index);
102 _index = &this->_entries[index].next;
105 // Iterate through the table.
106 // No stuff should be inserted into the table during the iteration,
107 // or you may find yourself iterating over an entry vector which has
108 // already been freed!
111 // Prepare a store operation: make sure that the free list is non-empty.
112 // This may change the table's size!
113 void prepare_store ()
115 #if !(defined(__sparc__) && !defined(__GNUC__))
116 if (this->_freelist < -1)
119 if (this->_garcol_fun(this))
120 if (this->_freelist < -1)
122 // No! Have to grow the hash table.
125 // workaround Sun C++ 4.1 inline function compiler bug
126 if (this->_freelist >= -1) {
127 if (!this->_garcol_fun(this) || (this->_freelist >= -1))
134 var long new_size = this->_size + (this->_size >> 1) + 1; // _size*1.5
135 var long new_modulus = compute_modulus(new_size);
136 var void* new_total_vector = malloc_hook(new_modulus*sizeof(long) + new_size*sizeof(htxentry));
137 var long* new_slots = (long*) ((char*)new_total_vector + 0);
138 var htxentry* new_entries = (htxentry *) ((char*)new_total_vector + new_modulus*sizeof(long));
139 for (var long hi = new_modulus-1; hi >= 0; hi--)
141 var long free_list_head = -1;
142 for (var long i = new_size-1; i >= 0; i--) {
143 new_entries[i].next = free_list_head;
144 free_list_head = -2-i;
146 var htxentry* old_entries = this->_entries;
147 for (var long old_index = 0; old_index < this->_size; old_index++)
148 if (old_entries[old_index].next >= 0) {
149 var key1_type& key = old_entries[old_index].entry.key;
150 var value_type& val = old_entries[old_index].entry.val;
151 var long hindex = hashcode(key) % new_modulus;
152 var long index = -2-free_list_head;
153 free_list_head = new_entries[index].next;
154 new (&new_entries[index].entry) cl_htentry1<key1_type,value_type> (key,val);
155 new_entries[index].next = new_slots[hindex];
156 new_slots[hindex] = 1+index;
157 old_entries[old_index].~htxentry();
159 free_hook(this->_total_vector);
160 this->_modulus = new_modulus;
161 this->_size = new_size;
162 this->_freelist = free_list_head;
163 this->_slots = new_slots;
164 this->_entries = new_entries;
165 this->_total_vector = new_total_vector;
171 #endif /* _CL_HASH1_H */