amxmodx/dlls/nvault/hash.h

105 lines
2.6 KiB
C
Raw Normal View History

2005-04-02 07:33:25 +00:00
#ifndef _INCLUDE_AMXX_HASH_H
#define _INCLUDE_AMXX_HASH_H
#include <time.h>
#include "sdk/amxxmodule.h"
#include "sdk/CString.h"
/**
* (C)2005 David "BAILOPAN" Anderson
* Licensed under the GNU General Public License, version 2
*
* Small hash table implementation
*/
2005-04-02 07:33:25 +00:00
#if defined WIN32 || defined _WIN32
typedef unsigned __int8 uint8_t;
typedef __int8 int8_t;
#else
#include <stdint.h>
#endif
/**
* Hash Table implementation
2005-04-02 07:33:25 +00:00
* This is not designed to be flexible for size/type, it's hardcoded.
* The table size is 2^11, which should be good enough for fast small lookups.
* A hash is computed by two chopped up 8bit versions of CRC32, which is then combined to
* form an 11bit key. That key is then an index into the appropriate bucket.
*/
extern const uint8_t Hash_CRCTable1[];
extern const uint8_t Hash_CRCTable2[];
//You should not change this without understanding the hash algorithm
#define HT_SIZE 2048
class HashTable
{
public: //STRUCTORS
HashTable();
~HashTable();
public: //PRE-DEF
class iterator;
struct htNode;
2005-04-03 02:01:42 +00:00
friend class Vault;
2005-04-02 07:33:25 +00:00
private: //PRE-DEF
struct htNodeSet;
public: //PUBLIC FUNCTIONS
void Store(const char *key, const char *value, bool temporary=true);
2005-04-03 02:01:42 +00:00
void Store(const char *key, const char *value, time_t stamp);
2005-04-02 07:33:25 +00:00
htNode *Retrieve(const char *key);
iterator Enumerate();
size_t Prune(time_t begin, time_t end, bool all=false);
void Clear();
2005-04-02 16:04:08 +00:00
bool KeyExists(const char *key);
2005-04-03 02:01:42 +00:00
size_t UsedHashes();
void EraseKey(const char *key);
2005-04-02 07:33:25 +00:00
public: //PUBLIC CLASSES
class iterator
{
public:
iterator(htNodeSet **table, uint32_t tableSize);
const char *key();
const char *val();
time_t stamp();
bool done();
void next();
uint32_t hash();
htNode *operator *();
private:
htNodeSet **t; //table
uint32_t s; //size
uint32_t r; //current
htNode *d; //delta
};
private: //PRIVATE API
void _Insert(const char *key, const char *val, time_t stamp);
2005-04-02 16:04:08 +00:00
htNode *_FindNode(const char *key, bool autoMake=true);
2005-04-02 07:33:25 +00:00
htNodeSet *_FindNodeSet(const char *key);
htNode *_InsertIntoNodeSet(htNodeSet *set, const char *key, bool skip=false);
void _Unlink(htNodeSet *set, htNode *node);
public: //PUBLIC STATIC API
static uint16_t HashString(const char *str);
private: //PRIVATE STATIC API
static uint8_t _HashString1(const char *str);
static uint8_t _HashString2(const char *str);
public: //PUBLIC STRUCTURES
struct htNode
{
String key;
String val;
time_t stamp;
htNode *next;
};
private: //PRIVATE STRUCTURES
struct htNodeSet
{
htNode *head;
htNode *tail;
};
private: //INTERNAL VARIABLES
htNodeSet *m_Table[HT_SIZE];
};
#endif //_INCLUDE_AMXX_HASH_H