open-source-search-engine/HashTableT.h

116 lines
2.9 KiB
C
Raw Normal View History

2013-08-03 00:12:24 +04:00
// . 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:
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 ,
2013-08-03 00:12:24 +04:00
bool allowDupKeys = false );
HashTableT( ) ;
~HashTableT( );
void constructor();
// . 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 ( Key_t key , Val_t value , int32_t *slot = NULL );
2013-08-03 00:12:24 +04:00
// remove key/value entry to hash table
bool removeKey ( Key_t key );
2014-11-11 01:45:11 +03:00
void removeSlot ( int32_t n );
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
bool load ( char *filename , char **textBuf = NULL , int32_t *textBufSize = NULL );
bool save ( char *filename , char *textBuf = NULL , int32_t textBufSize = 0 );
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)
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); };
2014-11-11 01:45:11 +03:00
Key_t getKey ( int32_t n ) const { return m_keys[n]; };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
int32_t getSlot ( Key_t key ) const { return getOccupiedSlotNum ( key ); };
2013-08-03 00:12:24 +04:00
// for hash tables that m_allowDupKeys
2014-11-11 01:45:11 +03:00
int32_t getNextSlot ( Key_t& key , int32_t n ) const ;
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
void setValue ( int32_t n , Val_t val ) { m_vals[n] = val; };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
bool isEmpty ( int32_t n ) { return ( m_keys[n] == 0 ); };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
Val_t getValueFromSlot ( int32_t n ) const { return m_vals[n]; };
2013-08-03 00:12:24 +04:00
2014-11-11 01:45:11 +03:00
Val_t* getValuePointerFromSlot ( int32_t n ) { return &m_vals[n]; };
2013-08-03 00:12:24 +04:00
// 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?
2014-11-11 01:45:11 +03:00
int32_t getNumSlotsUsed ( ) const { 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 ( ) const { return m_numSlots; };
2013-08-03 00:12:24 +04:00
void setAllowDupKeys(char allow) { m_allowDupKeys = allow; };
char getAllowDupKeys( ) const { return m_allowDupKeys; };
bool serialize(SafeBuf& sb);
2014-11-11 01:45:11 +03:00
int32_t deserialize(char* s);
2013-08-03 00:12:24 +04:00
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 ( Key_t& key ) const;
2013-08-03 00:12:24 +04:00
//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;
2014-11-11 01:45:11 +03:00
int32_t m_numSlots;
int32_t m_numSlotsUsed;
2013-08-03 00:12:24 +04:00
char m_allowDupKeys;
char m_doFree;
char *m_buf; //. hash table buffer points to callers buffer on stack
2014-11-11 01:45:11 +03:00
int32_t m_bufSize;
2013-08-03 00:12:24 +04:00
// char m_buf1 [ HT_BUF_SIZE ];
// char m_buf2 [ HT_BUF_SIZE ];
};
2014-10-30 22:36:39 +03:00
bool hashFromString ( HashTableT<int64_t,char> *ht , char *x ) ;
2013-08-03 00:12:24 +04:00
#endif