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