1 // Hash tables with 1 key and a value
7 #include "cl_iterator.h"
10 // - function bool equal (key1_type,key1_type);
11 // - function unsigned long hashcode (key1_type);
13 #if (defined(__alpha__) && !defined(__GNUC__))
14 template <class key1_type, class value_type> struct cl_htentry1;
17 template <class key1_type, class value_type>
19 ALLOCATE_ANYWHERE(cl_htentry1)
22 const value_type& htvalue () { return val; }
23 cl_htentry1 (const key1_type& k, const value_type& v)
25 #if (defined(__rs6000__) && !defined(__GNUC__))
30 template <class key1_type, class value_type>
31 struct cl_heap_hashtable_1 : public cl_heap_hashtable <cl_htentry1 <key1_type,value_type> > {
33 void* operator new (size_t size) { return cl_malloc_hook(size); }
35 void operator delete (void* ptr) { cl_free_hook(ptr); }
37 // Lookup (htref alias gethash).
38 // Returns a pointer which you should immediately dereference
40 value_type* get (const key1_type& key)
42 var long index = _slots[hashcode(key) % _modulus] - 1;
46 if (equal(key,_entries[index].entry.key))
47 return &_entries[index].entry.val;
48 index = _entries[index].next - 1;
52 // Store (htset alias puthash).
53 void put (const key1_type& key, const value_type& val)
55 var unsigned long hcode = hashcode(key);
56 // Search whether it is already there.
58 var long index = _slots[hcode % _modulus] - 1;
62 if (equal(key,_entries[index].entry.key)) {
63 _entries[index].entry.val = val;
66 index = _entries[index].next - 1;
69 // Put it into the table.
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;
78 // Remove (htrem alias remhash).
79 void remove (const key1_type& key)
81 var long* _index = &_slots[hashcode(key) % _modulus];
83 var long index = *_index - 1;
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);
96 _index = &_entries[index].next;
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!
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 ()
109 #if !(defined(__sparc__) && !defined(__GNUC__))
113 if (_garcol_fun(this))
116 // No! Have to grow the hash table.
119 // workaround Sun C++ 4.1 inline function compiler bug
120 if (_freelist >= -1) {
121 if (!_garcol_fun(this) || (_freelist >= -1))
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--)
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;
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();
153 cl_free_hook(_total_vector);
154 _modulus = new_modulus;
156 _freelist = free_list_head;
158 _entries = new_entries;
159 _total_vector = new_total_vector;
163 #endif /* _CL_HASH1_H */