forked from gigablast/open-source-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 9
/
HashTableT.h
115 lines (72 loc) · 2.88 KB
/
HashTableT.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
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
// . generic hash table class
#ifndef _HASHTABLET_H_
#define _HASHTABLET_H_
#include "Mem.h" // for mcalloc and mmalloc
#include "SafeBuf.h"
#include "types.h"
using namespace std;
#define HT_BUF_SIZE (4*1024)
template<class Key_t, class Val_t>
class HashTableT {
public:
bool set ( int32_t initialNumSlots = 0 ,
char *buf = NULL ,
int32_t bufSize = 0 ,
bool allowDupKeys = false );
HashTableT( ) ;
~HashTableT( );
void constructor();
// . add key/value entry to hash table
// . will grow hash table if it needs to
bool addKey ( Key_t key , Val_t value , int32_t *slot = NULL );
// remove key/value entry to hash table
bool removeKey ( Key_t key );
void removeSlot ( int32_t n );
bool load ( char *filename , char **textBuf = NULL , int32_t *textBufSize = NULL );
bool save ( char *filename , char *textBuf = NULL , int32_t textBufSize = 0 );
// . used by ../english/Bits.h to store stop words, abbr's, ...
// . returns the score for this termId (0 means empty usually)
Val_t* getValuePointer ( Key_t key ) const;
Val_t* getValuePtr ( Key_t key ) { return getValuePointer ( key ); };
// value of 0 means empty
//bool isEmpty ( Key_t key ) { return (getValue(key) == 0); };
Key_t getKey ( int32_t n ) const { return m_keys[n]; };
int32_t getSlot ( Key_t key ) const { return getOccupiedSlotNum ( key ); };
// for hash tables that m_allowDupKeys
int32_t getNextSlot ( Key_t& key , int32_t n ) const ;
void setValue ( int32_t n , Val_t val ) { m_vals[n] = val; };
bool isEmpty ( int32_t n ) { return ( m_keys[n] == 0 ); };
Val_t getValueFromSlot ( int32_t n ) const { return m_vals[n]; };
Val_t* getValuePointerFromSlot ( int32_t n ) { return &m_vals[n]; };
// frees the used memory, etc.
void reset ( );
bool copy(HashTableT<Key_t, Val_t>* retval);
// removes all key/value pairs from hash table, vacates all slots
void clear ( );
// how many are occupied?
int32_t getNumSlotsUsed ( ) const { return m_numSlotsUsed; };
// how many are there total? used and unused.
int32_t getNumSlots ( ) const { return m_numSlots; };
void setAllowDupKeys(char allow) { m_allowDupKeys = allow; };
char getAllowDupKeys( ) const { return m_allowDupKeys; };
bool serialize(SafeBuf& sb);
int32_t deserialize(char* s);
bool setTableSize ( int32_t numSlots, char *buf, int32_t bufSize );
int32_t getOccupiedSlotNum ( Key_t& key ) const;
//private:
//friend class RequestTable;
// . the array of buckets in which we store the terms
// . scores are allowed to exceed 8 bits for weighting purposes
Key_t *m_keys;
Val_t *m_vals;
int32_t m_numSlots;
int32_t m_numSlotsUsed;
char m_allowDupKeys;
char m_doFree;
char *m_buf; //. hash table buffer points to callers buffer on stack
int32_t m_bufSize;
// char m_buf1 [ HT_BUF_SIZE ];
// char m_buf2 [ HT_BUF_SIZE ];
};
bool hashFromString ( HashTableT<int64_t,char> *ht , char *x ) ;
#endif