1 // Hash sets (hash tables with 1 key and no value)
7 #include "cl_iterator.h"
10 // - function bool equal (key1_type,key1_type);
11 // - function unsigned long hashcode (key1_type);
13 template <class key1_type>
14 struct cl_htsetentry {
15 ALLOCATE_ANYWHERE(cl_htsetentry)
17 cl_htsetentry (const key1_type& k)
21 template <class key1_type>
22 struct cl_heap_hashtable_set : public cl_heap_hashtable <cl_htsetentry <key1_type> > {
24 void* operator new (size_t size) { return cl_malloc_hook(size); }
26 void operator delete (void* ptr) { cl_free_hook(ptr); }
28 // Lookup (htref alias gethash).
29 bool get (const key1_type& key)
31 var long index = _slots[hashcode(key) % _modulus] - 1;
35 if (equal(key,_entries[index].entry.key))
37 index = _entries[index].next - 1;
41 // Store (htset alias puthash).
42 void put (const key1_type& key)
44 var unsigned long hcode = hashcode(key);
45 // Search whether it is already there.
47 var long index = _slots[hcode % _modulus] - 1;
51 if (equal(key,_entries[index].entry.key))
53 index = _entries[index].next - 1;
56 // Put it into the table.
58 var long hindex = hcode % _modulus; // _modulus may have changed!
59 var long index = get_free_index();
60 new (&_entries[index].entry) cl_htsetentry<key1_type> (key);
61 _entries[index].next = _slots[hindex];
62 _slots[hindex] = 1+index;
65 // Remove (htrem alias remhash).
66 void remove (const key1_type& key)
68 var long* _index = &_slots[hashcode(key) % _modulus];
70 var long index = *_index - 1;
73 if (equal(key,_entries[index].entry.key)) {
74 // Remove _entries[index].entry
75 *_index = _entries[index].next;
76 _entries[index].~htxentry();
77 // The entry is now free.
78 put_free_index(index);
83 _index = &_entries[index].next;
86 // Iterate through the table.
87 // No stuff should be inserted into the table during the iteration,
88 // or you may find yourself iterating over an entry vector which has
89 // already been freed!
92 // Prepare a store operation: make sure that the free list is non-empty.
93 // This may change the table's size!
96 #if !(defined(__sparc__) && !defined(__GNUC__))
100 if (_garcol_fun(this))
103 // No! Have to grow the hash table.
106 // workaround Sun C++ 4.1 inline function compiler bug
107 if (_freelist >= -1) {
108 if (!_garcol_fun(this) || (_freelist >= -1))
115 var long new_size = _size + (_size >> 1) + 1; // _size*1.5
116 var long new_modulus = compute_modulus(new_size);
117 var void* new_total_vector = cl_malloc_hook(new_modulus*sizeof(long) + new_size*sizeof(htxentry));
118 var long* new_slots = (long*) ((char*)new_total_vector + 0);
119 var htxentry* new_entries = (htxentry *) ((char*)new_total_vector + new_modulus*sizeof(long));
120 for (var long hi = new_modulus-1; hi >= 0; hi--)
122 var long free_list_head = -1;
123 for (var long i = new_size-1; i >= 0; i--) {
124 new_entries[i].next = free_list_head;
125 free_list_head = -2-i;
127 var htxentry* old_entries = _entries;
128 for (var long old_index = 0; old_index < _size; old_index++)
129 if (old_entries[old_index].next >= 0) {
130 var key1_type& key = old_entries[old_index].entry.key;
131 var long hindex = hashcode(key) % new_modulus;
132 var long index = -2-free_list_head;
133 free_list_head = new_entries[index].next;
134 new (&new_entries[index].entry) cl_htsetentry<key1_type> (key);
135 new_entries[index].next = new_slots[hindex];
136 new_slots[hindex] = 1+index;
137 old_entries[old_index].~htxentry();
139 cl_free_hook(_total_vector);
140 _modulus = new_modulus;
142 _freelist = free_list_head;
144 _entries = new_entries;
145 _total_vector = new_total_vector;
149 #endif /* _CL_HASHSET_H */