forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.h
86 lines (53 loc) · 1.91 KB
/
HashTable.h
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
// Matt Wells, Copyright, Dec. 2002
// . generic hash table class
#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#include "Mem.h" // for mcalloc and mmalloc
class HashTable {
public:
bool set ( long initialNumSlots = 0 ,
char *buf = NULL ,
long bufSize = 0 );
HashTable ( );
~HashTable ( );
// . add key/value entry to hash table
// . will grow hash table if it needs to
bool addKey ( long key , long value , long *slot = NULL );
// remove key/value entry to hash table
bool removeKey ( long key );
// like removeKey
void removeSlot ( long n );
// . used by ../english/Bits.h to store stop words, abbr's, ...
// . returns the score for this termId (0 means empty usually)
long getValue ( long key );
// value of 0 means empty
bool isEmpty ( long key ) { return (getValue(key) == 0); };
long getKey ( long n ) { return m_keys[n]; };
long getSlot ( long key ) { return getOccupiedSlotNum ( key ); };
void setValue ( long n , long val ) { m_vals[n] = val; };
long getValueFromSlot ( long n ) { return m_vals[n]; };
// frees the used memory, etc.
void reset ( );
// removes all key/value pairs from hash table, vacates all slots
void clear ( );
// how many are occupied?
long getNumSlotsUsed ( ) { return m_numSlotsUsed; };
// how many are there total? used and unused.
long getNumSlots ( ) { return m_numSlots; };
// both return false and set g_errno on error, true otherwise
bool load ( char *dir , char *filename );
bool save ( char *dir , char *filename );
private:
bool setTableSize ( long numSlots , char *buf , long bufSize );
long getOccupiedSlotNum ( long key ) ;
// . the array of buckets in which we store the terms
// . scores are allowed to exceed 8 bits for weighting purposes
long *m_keys;
long *m_vals;
long m_numSlots;
long m_numSlotsUsed;
unsigned long m_mask;
char m_needsSave;
char m_doFree;
};
#endif