open-source-search-engine/HashTable.h

92 lines
2.1 KiB
C
Raw Normal View History

2013-08-03 00:12:24 +04:00
// 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:
2014-11-11 01:45:11 +03:00
bool set ( int32_t initialNumSlots = 0 ,
2013-08-03 00:12:24 +04:00
char *buf = NULL ,
2014-11-11 01:45:11 +03:00
int32_t bufSize = 0 ,
char *label = NULL );
2013-08-03 00:12:24 +04:00
HashTable ( );
~HashTable ( );
void setLabel ( char *label ) { m_label = label; };
2013-08-03 00:12:24 +04:00
// . add key/value entry to hash table
// . will grow hash table if it needs to
2014-11-11 01:45:11 +03:00
bool addKey ( int32_t key , int32_t value , int32_t *slot = NULL );
2013-08-03 00:12:24 +04:00
// remove key/value entry to hash table
2014-11-11 01:45:11 +03:00
bool removeKey ( int32_t key );
2013-08-03 00:12:24 +04:00
// like removeKey
2014-11-11 01:45:11 +03:00
void removeSlot ( int32_t n );
2013-08-03 00:12:24 +04:00
// . used by ../english/Bits.h to store stop words, abbr's, ...
// . returns the score for this termId (0 means empty usually)
2014-11-11 01:45:11 +03:00
int32_t getValue ( int32_t key );
2013-08-03 00:12:24 +04:00
// value of 0 means empty
2014-11-11 01:45:11 +03:00
bool isEmpty ( int32_t key ) { return (getValue(key) == 0); };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t getKey ( int32_t n ) { return m_keys[n]; };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t getSlot ( int32_t key ) { return getOccupiedSlotNum ( key ); };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
void setValue ( int32_t n , int32_t val ) { m_vals[n] = val; };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t getValueFromSlot ( int32_t n ) { return m_vals[n]; };
2013-08-03 00:12:24 +04:00
// frees the used memory, etc.
void reset ( );
// removes all key/value pairs from hash table, vacates all slots
void clear ( );
// how many are occupied?
2014-11-11 01:45:11 +03:00
int32_t getNumSlotsUsed ( ) { return m_numSlotsUsed; };
2013-08-03 00:12:24 +04:00
// how many are there total? used and unused.
2014-11-11 01:45:11 +03:00
int32_t getNumSlots ( ) { return m_numSlots; };
2013-08-03 00:12:24 +04:00
// both return false and set g_errno on error, true otherwise
bool load ( char *dir , char *filename );
bool save ( char *dir , char *filename );
private:
2014-11-11 01:45:11 +03:00
bool setTableSize ( int32_t numSlots , char *buf , int32_t bufSize );
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t getOccupiedSlotNum ( int32_t key ) ;
2013-08-03 00:12:24 +04:00
// . the array of buckets in which we store the terms
// . scores are allowed to exceed 8 bits for weighting purposes
2014-11-11 01:45:11 +03:00
int32_t *m_keys;
int32_t *m_vals;
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t m_numSlots;
int32_t m_numSlotsUsed;
uint32_t m_mask;
2013-08-03 00:12:24 +04:00
//char m_needsSave;
2013-08-03 00:12:24 +04:00
char m_doFree;
char *m_label;
2013-08-03 00:12:24 +04:00
};
#endif