1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the class library */
/* SoPlex --- the Sequential object-oriented simPlex. */
/* */
/* Copyright 1996-2022 Zuse Institute Berlin */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* You should have received a copy of the Apache-2.0 license */
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file datahashtable.h
* @brief Generic hash table for data objects.
*/
#ifndef _DATAHASHTABLE_H_
#define _DATAHASHTABLE_H_
#include <iostream>
#include <assert.h>
#include <limits.h>
#include "soplex/spxdefines.h"
#include "soplex/array.h"
#define HASHTABLE_FILLFACTOR 0.7
namespace soplex
{
/**@brief Generic hash table for data objects.
@ingroup Elementary
Class DataHashTable provides a generic hash table for
\ref DataObjects "Data Objects",
i.e., a map that maps arguments called \a HashItems to values called \a Infos.
HashItem and Info types are passed as template arguments. HashItems
must provide a comparison operator==(). Furthermore, both the HashItem and
Info must be data objects in the sense that the assignment operator is
equivalent to a <tt>memcpy()</tt> of the structure and no destructor is
required.
The construction of a DataHashTable requires a \em hash \em function that
assigns an integer value to every HashItem. Provided this, pairs of a
HashItem and a Info can be added to the DataHashTable. No more
than one Info can be assigned to the same HashItem at a time. The Info
to a HashItem can be accessed through the subscript operator[]() with
the Info object as a subscript.
The maximum number of elemens a DataHashTable can hold can be
specified upon construction and may be reset with reMax() later on.
Further, a value hash size value is required. This value must be less then
the maximum number of elements and must not have a common dominator with
the maximum number of elements. If not specified explicitely, it
is set automatically to a reasonable value.
The implementation relies on an array of DataHashTable::Element%s, from
now on referred to as elements. Upon construction, all elements are
marked as \c FREE. When an entry is added
to the DataHashTable, the hash value is computed by calling #m_hashfun
for its HashItem. If this array element is unused, it is
taken right away. Otherwise, the array index is incremented by
the hash size (modulo the element array size()) until an unused element
is found.
Removing elements is simply done by marking it as \c RELEASED. Hence,
when searching for an element, the search loop may not stop, when a
\c RELEASED element is encountered. However, such an element may be
reused when adding a new element to the DataHashTable.
Further, memory management with resizing of the element array is
straight forward.
*/
template < class HashItem, class Info >
class DataHashTable
{
private:
//-----------------------------------
/**@name Types */
///@{
/// template class for elements stored in the hash table
template < class ElemHashItem, class ElemInfo >
class Element
{
public:
///
ElemHashItem item;
///
ElemInfo info;
/// States of an element
enum states
{
FREE, ///< element has never been used
RELEASED, ///< element had been used, but released
USED ///< element is in use
} stat;
};
typedef Element< HashItem, Info > Elem;
///@}
//-----------------------------------
/**@name Data */
///@{
/// stores all elements of the hash table
Array < Elem > m_elem;
/// increment added to hash index, if allready used
int m_hashsize;
/// current number of entries in the hash table
int m_used;
/// pointer to hash function (mapping: \a HashItem -> int)
int (*m_hashfun)(const HashItem*);
/// memory is \ref soplex::DataHashTable::reMax() "reMax()"ed by this factor if a new element does't fit
Real m_memfactor;
/// some prime numbers for fast access
int primes[50];
/// number of stored prime numbers
int nprimes;
///@}
public:
//-----------------------------------
/**@name Access / modification */
///@{
/// Is item \p h present in DataHashTable?
bool has(const HashItem& h) const
{
return index(h) >= 0;
}
/// returns const pointer to \a Info of \a HashItem \p h or 0,
/// if item is not found.
/** Returns a pointer to \a Info component of hash element \p h or a zero
* pointer if element \p h is not in the table.
*/
const Info* get(const HashItem& h) const
{
int i = index(h);
return (i >= 0) ? &m_elem[i].info : nullptr;
}
/// references \a Info of \a HashItem \p h.
/** Index operator for accessing the \a Info associated to
* \a HashItem \p h. It is required that \p h belongs to the
* DataHashTable, otherwise it core dumps. Methods has() or
* get() can be used for inquiring wheater \p h belongs to the
* DataHashTable or not.
*/
const Info& operator[](const HashItem& h) const
{
assert(has(h));
return m_elem[index(h)].info;
}
/// adds a new entry to the hash table.
/** Adds a new entry consisting of \a HashItem \p h and \a Info \p info to the
* DataHashTable. No entry with HashItem \p h must be in the
* DataHashTable yet. After completion, \p info may be accessed via get() or
* operator[]() with \p h as parameter. The DataHashTable is #reMax()%ed
* if it becomes neccessary.
*/
void add(const HashItem& h, const Info& info)
{
assert(!has(h));
if(m_used >= m_elem.size() * HASHTABLE_FILLFACTOR)
reMax(int(m_memfactor * m_used) + 1);
assert(m_used < m_elem.size());
decltype(m_elem.size()) i;
for(i = (*m_hashfun)(&h) % m_elem.size();
m_elem[i].stat == Elem::USED;
i = (i + m_hashsize) % m_elem.size())
;
assert(m_elem[i].stat != Elem::USED);
m_elem[i].stat = Elem::USED;
m_elem[i].info = info;
m_elem[i].item = h;
m_used++;
assert(has(h));
}
/// remove \a HashItem \p h from the DataHashTable.
void remove(const HashItem& h)
{
int i = index(h);
if(i < 0)
return;
m_elem[i].stat = Elem::RELEASED;
m_used--;
assert(!has(h));
}
/// remove all entries from DataHashTable.
void clear()
{
for(int i = 0; i < m_elem.size(); i++)
m_elem[i].stat = Elem::FREE;
m_used = 0;
}
/// reset size of the DataHashTable.
/** Reset the maximum number of elements of a DataHashTable to \p newSize.
* However, if \p newSize < #m_used, it is resized to #m_used only.
* If \p newHashSize < 1, a new hash size is computed automatically.
* Otherwise, the specified value will be taken.
*/
void reMax(int newSize = -1, int newHashSize = 0)
{
Array< Elem > save(m_elem);
m_elem.reSize(newSize < m_used ? m_used : newSize);
clear();
m_hashsize = (newHashSize < 1) ? autoHashSize() : newHashSize;
for(int i = 0; i < save.size(); i++)
if(save[i].stat == Elem::USED)
add(save[i].item, save[i].info);
}
///@}
//-----------------------------------
/**@name Debugging */
///@{
/// checks whether DataHashTable is consistent
bool isConsistent() const
{
#ifdef ENABLE_CONSISTENCY_CHECKS
int total = 0;
for(int i = 0; i < m_elem.size(); i++)
{
if(m_elem[i].stat == Elem::USED)
{
total++;
if(!has(m_elem[i].item))
return MSGinconsistent("DataHashTable");
}
}
if(total != m_used)
return MSGinconsistent("DataHashTable");
return m_elem.isConsistent();
#else
return true;
#endif
}
///@}
//-----------------------------------
/**@name Construction / destruction */
///@{
/// default constructor.
/** Allocates a DataHashTable for \p maxsize entries using \p hashfun
* as hash function. If \p hashsize > 0, #m_hashsize is set to the
* specified value, otherwise a suitable hash size is computed
* automatically. Parameter \p factor is used for memory management:
* If more than \p maxsize entries are added to the DataHashTable, it
* will automatically be #reMax()%ed by a factor of \p factor.
*
* @param hashfun pointer to hash function.
* @param maxsize maximum number of hash elements.
* @param hashsize hash size.
* @param factor factor for increasing data block.
*/
explicit DataHashTable(
int (*hashfun)(const HashItem*),
int maxsize = 265,
int hashsize = 0,
Real factor = 2.0)
: m_elem(maxsize)
, m_hashfun(hashfun)
, m_memfactor(factor)
{
clear();
primes[0] = 1523;
primes[1] = 3547;
primes[2] = 8011;
primes[3] = 17707;
primes[4] = 38723;
primes[5] = 83833;
primes[6] = 180317;
primes[7] = 385897;
primes[8] = 821411;
primes[9] = 1742369;
primes[10] = 3680893;
primes[11] = 5693959;
primes[12] = 7753849;
primes[13] = 9849703;
primes[14] = 11973277;
primes[15] = 14121853;
primes[16] = 17643961;
primes[17] = 24273817;
primes[18] = 32452843;
primes[19] = 49979687;
primes[20] = 67867967;
primes[21] = 86028121;
primes[22] = 104395301;
primes[23] = 122949823;
primes[24] = 141650939;
primes[25] = 160481183;
primes[26] = 179424673;
primes[27] = 198491317;
primes[28] = 217645177;
primes[29] = 256203161;
primes[30] = 314606869;
primes[31] = 373587883;
primes[32] = 433024223;
primes[33] = 492876847;
primes[34] = 553105243;
primes[35] = 613651349;
primes[36] = 694847533;
primes[37] = 756065159;
primes[38] = 817504243;
primes[39] = 879190747;
primes[40] = 941083981;
primes[41] = 982451653;
primes[42] = INT_MAX;
nprimes = 43;
m_hashsize = (hashsize < 1) ? autoHashSize() : hashsize;
assert(m_memfactor > 1.0);
assert(isConsistent());
}
/// assignment operator.
DataHashTable& operator=(const DataHashTable& base)
{
m_elem = base.m_elem;
m_hashfun = base.m_hashfun;
m_memfactor = base.m_memfactor;
m_used = base.m_used;
m_hashsize = base.m_hashsize;
primes = base.primes;
nprimes = base.nprimes;
assert(m_memfactor > 1.0);
assert(isConsistent());
return *this;
}
/// copy constructor.
DataHashTable(const DataHashTable& base)
: m_elem(base.m_elem)
, m_hashfun(base.m_hashfun)
, m_memfactor(base.m_memfactor)
, m_used(base.m_used)
, m_hashsize(base.m_hashsize)
, primes(base.primes)
, nprimes(base.nprimes)
{
assert(m_memfactor > 1.0);
assert(isConsistent());
}
///@}
private:
//-----------------------------------
/**@name Helpers */
///@{
/// determine a good \ref soplex::DataHashTable::m_hashsize "m_hashsize".
/** Determine next larger prime number for new #m_hashsize
* @return good value for #m_hashsize
*/
int autoHashSize() const
{
auto oldsize = m_elem.size();
int left = 0;
int right = nprimes - 1;
int middle;
while(left <= right)
{
middle = (left + right) / 2;
if(oldsize < primes[middle])
{
right = middle - 1;
}
else if(oldsize > primes[middle])
{
left = middle + 1;
}
else
{
assert(oldsize == primes[middle]);
return primes[middle + 1];
}
}
assert(left == right + 1);
return primes[left];
}
/// automatically computes a good \ref soplex::DataHashTable::m_hashsize "m_hashsize".
/** Computes a good #m_hashsize as the product of all prime numbers
* not divisors of the number of elements that are <=
* the maximum divisor of the number of elemens.
* @return good value for #m_hashsize
*/
int autoHashSizeold() const
{
DataArray< bool > prime(m_elem.size());
int hashsize = 1;
int maxsize = m_elem.size();
int i;
for(i = 2; i < maxsize; i++)
prime[i] = true;
for(i = 2; i < maxsize; ++i)
{
if(prime[i])
{
for(int j = i; j < maxsize; j += i)
prime[j] = false;
if(m_elem.size() % i != 0)
{
hashsize *= i;
if(hashsize > maxsize)
{
hashsize /= i;
break;
}
}
}
}
return hashsize;
}
/// returns hash index of \a HashItem \p h or -1, if \p h is not present.
/** Using the hash function #m_hashfun, the hash value of \p h
* is calculated.
* Starting with this hash index, every #m_hashsize%-th element is
* compared with \p h until \p h is found or all elements have been checked.
*
* @param h \a HashItem, for which the hash index should be calculated
* @return hash index of \p h or -1,
* if \p h is not a member of the hash table
*/
int index(const HashItem& h) const
{
if(m_used == 0)
return -1;
assert(m_elem.size() > 0);
auto i = (*m_hashfun)(&h) % m_elem.size();
auto j = i;
while(m_elem[i].stat != Elem::FREE)
{
if((m_elem[i].stat == Elem::USED)
&& (m_elem[i].item == h))
return i;
i = (i + m_hashsize) % m_elem.size();
if(i == j)
break;
}
return -1;
}
///@}
};
} // namespace soplex
#endif // _DATAHASHTABLE_H_