nhashtable.h

来自「奇迹世界公用文件源代码,研究网络游戏的朋友可以研究下」· C头文件 代码 · 共 93 行

H
93
字号
#ifndef N_HASHTABLE_H
#define N_HASHTABLE_H
//------------------------------------------------------------------------------
/**
    @brief Implements a simple string hash table.

    @author
    - RadonLabs GmbH 

    @since
    - 2005.6.30
    @remarks
    - 瘤肯 眠啊 
*/

#include "../ProgramCommon/Define.h"
#include "nstrlist.h"

//------------------------------------------------------------------------------
class nHashTable 
{
public:
    /// constructor
    nHashTable(int size);
    /// destructor
    ~nHashTable();
    /// add an entry to the hashtable
    void Add(nStrNode* n);
    /// search hash table for entry
    nStrNode* Find(const char* str) const;

private:
    int htable_size;
    nStrList *htable;
};

//------------------------------------------------------------------------------
/**
*/
inline 
nHashTable::nHashTable(int size)
{
    this->htable_size = size;
    this->htable = new nStrList[size];
}

//------------------------------------------------------------------------------
/**
*/
inline nHashTable::~nHashTable()
{
    delete[] this->htable;
}

//------------------------------------------------------------------------------
/**
*/
static 
inline 
int hash(const char *str, int htable_size)
{
    int i = 0;
    int j = 1;
    char c;
    while ((c = *str++)) i += ((uchar)c) * j++; 
    return (i % htable_size);
}

//------------------------------------------------------------------------------
/**
*/
inline 
void 
nHashTable::Add(nStrNode* n)
{
    int h_index = hash(n->GetName(), this->htable_size);
    this->htable[h_index].AddHead(n);
}

//------------------------------------------------------------------------------
/**
*/
inline 
nStrNode*
nHashTable::Find(const char* str) const
{
    int h_index = hash(str, this->htable_size);
    return this->htable[h_index].Find(str);
}

//------------------------------------------------------------------------------
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?