summaryrefslogtreecommitdiff
path: root/src/hashtable.cpp
blob: 725ebc4dc8cc5b920754dc467a8c03b33bfa3109 (plain)
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
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "hashtable.h"

HashTable::HashTable( HashFunction *hNewFunc, unsigned long int nInitSize, bool bAllowDupes )
{
	hFunc = hNewFunc;
	nTableSize = nextPrime( nInitSize );
	aTable = new HashNode[nTableSize];
	//for( int j = 0; j < nTableSize; j++ ) if( aTable[j].id || aTable[j].data || aTable[j].bDeleted ) printf("Unclean entry\n");
	nSize = 0;
	nFilled = 0;
	this->bAllowDupes = bAllowDupes;
}

HashTable::~HashTable()
{
	delete[] aTable;
	delete hFunc;
}

void HashTable::set( int j, const void *newID, const void *newData )
{
	if( newData == NULL )
	{
		printf("Inserting NULL data is indestinguishable from uninserted data!\n");
	}
	aTable[j].id = newID;
	aTable[j].data = newData;
}

void HashTable::clear()
{
	memset( aTable, 0, sizeof(HashNode) * nTableSize );
}

bool HashTable::isFilled( int j )
{
	return (aTable[j].id != NULL)||(aTable[j].bDeleted);
}

bool HashTable::reHash( unsigned long int nNewSize )
{
	HashNode *aOldTable = aTable;
	unsigned long int oldSize = nTableSize;

	// If the table can still be used if we just get rid of deleted items, don't
	// change the size of the table, otherwise, go ahead and use the number
	// passed in.
	if( nSize > nTableSize>>1 )
	{
		nTableSize = nextPrime( nNewSize );
	}

	aTable = newTable( nTableSize );
	//for( int j = 0; j < nTableSize; j++ ) if( aTable[j].id || aTable[j].data || aTable[j].bDeleted ) printf("Unclean entry\n");

	nSize = 0;
	nFilled = 0;

	for( unsigned long int j = 0; j < oldSize; j++ )
	{
		if( aOldTable[j].id != NULL && aOldTable[j].bDeleted == false )
		{
			insert( aOldTable[j].id, aOldTable[j].data );
		}
	}

	delete[] aOldTable;
}

unsigned long int HashTable::probe( unsigned long int nStart, const void *id )
{
	int nHash = nStart;
	nStart = nStart%nTableSize;
	if( bAllowDupes == true )
	{
		for(
			unsigned long int j=0;
			isFilled( nStart ) && j < 32;
			nStart = (nStart+(1<<j))%nTableSize, j++
			);

		/**
		 * This is an ugly little hack.  If the hash table is too full in allow-
		 * dups mode we have to fall back on a linear search, otherwise you can
		 * only get up to 32 entries with the same name.
		 */
		if( isFilled( nStart ) )
		{
			int nOldStart = nStart;
			for(
				nStart++;
				isFilled( nStart ) && nStart != nOldStart;
				nStart = (nStart+1)%nTableSize
			   );
		}
	}
	else
	{
		for(
			unsigned long int j=0;
			isFilled( nStart ) && j < 32;
			nStart = (nStart+(1<<j))%nTableSize, j++
			)
		{
			if( isFilled( nStart ) )
			{
				if( hFunc->cmpIDs( aTable[nStart].id, id ) == true &&
					aTable[nStart].bDeleted == false )
				{
					return nStart;
				}
			}
		}
	}
	// This is our insurance, if the table is full, then go ahead and rehash,
	// then try again.
	if( isFilled( nStart ) )
	{
		reHash( getCapacity()*2 );
		return probe( nHash, id );
	}
	return nStart;
}

HashTable::HashNode *HashTable::newTable( unsigned long int nNewSize )
{
	return new HashNode[nNewSize];
}

#ifdef HASH_DEBUG_VIS
void HashTable::printDebugLine( const char *exData )
{
	char *buf = new char[getCapacity()+3];
	int j;
	buf[0] = '[';
	for( j = 0; j < getCapacity(); j++ )
	{
		buf[j+1] = (aTable[j].bDeleted)?('X'):((isFilled( j ))?('#'):('-'));
	}
	buf[j+1] = ']';
	buf[j+2] = '\0';
	printf("%s %s\n", buf, exData );
	delete[] buf;
}
#endif

bool HashTable::insert( const void *id, const void *data )
{
	unsigned long int nPos = probe( hFunc->hash( id ), id )%nTableSize;

	if( bAllowDupes == true )
	{
		if( aTable[nPos].id == NULL && aTable[nPos].bDeleted == false )
		{
			set( nPos, id, data );
#ifdef HASH_DEBUG_VIS
			printDebugLine( (const char *)id );
#endif
			nSize++;
			nFilled++;
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if( aTable[nPos].id == NULL && aTable[nPos].bDeleted == false )
		{
			set( nPos, id, data );
#ifdef HASH_DEBUG_VIS
			printDebugLine( (const char *)id );
#endif
			nSize++;
			nFilled++;
			return true;
		}
		else if( hFunc->cmpIDs( aTable[nPos].id, id ) == true )
		{
			set( nPos, id, data );
#ifdef HASH_DEBUG_VIS
			printDebugLine( (const char *)id );
#endif
			return true;
		}
		else
		{
			return false;
		}
	}
}

const void *HashTable::get( const void *id, unsigned long int nSkip )
{
	unsigned long int nPos = hFunc->hash( id )%nTableSize;

	for( unsigned long int j=0; j < 32; nPos = (nPos+(1<<j))%nTableSize, j++ )
	{
		if( !isFilled( nPos ) ) return NULL;
		if( aTable[nPos].bDeleted == false )
		{
			if( hFunc->cmpIDs( id, aTable[nPos].id ) )
			{
				if( nSkip == 0 )
				{
					return aTable[nPos].data;
				}
				else
				{
					nSkip--;
				}
			}
		}
	}

	if( bAllowDupes )
	{
		int nOldPos = nPos;
		for( nPos++; nPos != nOldPos; nPos=(nPos+1)%nTableSize )
		{
			if( !isFilled( nPos ) ) return NULL;
			if( aTable[nPos].bDeleted == false )
			{
				if( hFunc->cmpIDs( id, aTable[nPos].id ) )
				{
					if( nSkip == 0 )
					{
						return aTable[nPos].data;
					}
					else
					{
						nSkip--;
					}
				}
			}
		}
	}

	return NULL;
}

const void *HashTable::getKey( const void *id, unsigned long int nSkip )
{
	unsigned long int nPos = hFunc->hash( id )%nTableSize;

	for( unsigned long int j=0; j < 32; nPos = (nPos+(1<<j))%nTableSize, j++ )
	{
		if( !isFilled( nPos ) ) return NULL;
		if( aTable[nPos].bDeleted == false )
		{
			if( hFunc->cmpIDs( id, aTable[nPos].id ) )
			{
				if( nSkip == 0 )
				{
					return aTable[nPos].id;
				}
				else
				{
					nSkip--;
				}
			}
		}
	}

	if( bAllowDupes )
	{
		int nOldPos = nPos;
		for( nPos++; nPos != nOldPos; nPos=(nPos+1)%nTableSize )
		{
			if( !isFilled( nPos ) ) return NULL;
			if( aTable[nPos].bDeleted == false )
			{
				if( hFunc->cmpIDs( id, aTable[nPos].id ) )
				{
					if( nSkip == 0 )
					{
						return aTable[nPos].id;
					}
					else
					{
						nSkip--;
					}
				}
			}
		}
	}

	return NULL;
}

void *HashTable::getFirstItemPos()
{
	HashPos *pos = new HashPos;
	return pos;
}

const void *HashTable::getItemData( void *xPos )
{
	return aTable[((HashPos *)xPos)->nPos].data;
}

const void *HashTable::getItemID( void *xPos )
{
	return aTable[((HashPos *)xPos)->nPos].id;
}

void *HashTable::getNextItemPos( void *xPos )
{
	HashPos *pos = (HashPos *)xPos;
	if( pos->bStarted == false )
	{
		pos->bStarted = true;
		pos->nPos = 0;
	}
	else
	{
		pos->nPos++;
	}
	if( pos->nPos < nTableSize )
	{
		for( ; pos->nPos < nTableSize; pos->nPos++ )
		{
			if( isFilled( pos->nPos ) &&
				aTable[pos->nPos].bDeleted == false )
			{
				return xPos;
			}
		}
	}

	delete pos;

	return NULL;
}

// Big-O sqrt(n)
// Change this to be erethpothynies table with a storage
// lookup later on.
bool HashTable::isPrime (int num)
{
    if (num == 2)         // the only even prime
        return true;
    else if (num % 2 == 0)     // other even numbers are composite
        return false;
    else
    {
        //bool prime = true;
        int divisor = 3;
        int upperLimit = static_cast<int>(sqrt(num) + 1);
        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
				return false;
            //    prime = false;
            divisor +=2;
        }
        return true;
    }
}

// Big-O n^(3/2)
int HashTable::nextPrime( int base )
{
	int nPrime;
	for( nPrime = base; isPrime( nPrime ) == false; nPrime++ );
	return nPrime;
}

unsigned long int HashTable::getCapacity()
{
	return nTableSize;
}

unsigned long int HashTable::getSize()
{
	return nSize;
}

double HashTable::getLoad()
{
	return (double)(nFilled)/(double)(nTableSize);
}

const void *HashTable::operator[](const void *id)
{
	return get( id );
}

bool HashTable::del( const void *id, int nSkip )
{
	unsigned long int nPos = hFunc->hash( id )%nTableSize;

	for( unsigned long int j=0; j < 32; nPos = (nPos+(1<<j))%nTableSize, j++ )
	{
		if( !isFilled( nPos ) ) return false;
		//printf("0x%08X \"%s\" == 0x%08X \"%s\" (%d)\n", id, id, aTable[nPos].id, aTable[nPos].id, nPos );
		if( hFunc->cmpIDs( id, aTable[nPos].id ) &&
			aTable[nPos].bDeleted == false )
		{
			if( nSkip == 0 )
			{
				aTable[nPos].bDeleted = true;
				nSize--;
#ifdef HASH_DEBUG_VIS
				printDebugLine( (const char *)id );
#endif
				return true;
			}
			else
			{
				nSkip--;
			}
		}
	}

	return false;
}