关闭

SegWord::IHash

1058人阅读 评论(0) 收藏 举报

 

#ifndef __IHASH_H__

#define __IHASH_H__

#include "../../include/UString.h"

using namespace UStr;

 

/*

* IHash接口及其默认实现:ELFhash算法(默认hash算法)

*************************************************************************/

 

const unsigned long    MAX_HASHTABLE_SIZE = 100000;     //默认hash表最大元素数

 

class IHash

{

public:

     virtual void init(unsigned long MaxHashTableSize) = 0;

     virtual unsigned long operator ()(UString& Str, Section& StrSect) = 0;

};

 

class ELFHash

     :public IHash

{

public:

     ELFHash()

     {

         _hashTableSize = MAX_HASHTABLE_SIZE;

     }

     void init(unsigned long MaxHashTableSize)

     {

         _hashTableSize = MaxHashTableSize;

         if( 0 == _hashTableSize )

              _hashTableSize = MAX_HASHTABLE_SIZE;

     }

     unsigned long operator ()(UString& Str, Section& StrSect)

     {

         register unsigned long h = 0, g = 0;

         if( Str.length() < StrSect._sect._begin+StrSect._sect._length )

              return h;//所给区间错误

         wchar_t* pStr = Str.u_str()+StrSect._sect._begin;

         unsigned int length = StrSect._sect._length;

         unsigned int pos = 0;

 

         while( *pStr && pos < length )

         {

              pos ++;

              h = (h << 4) + *pStr++;

              g = h & 0xF0000000L;

              if( g )

                   h ^= g>>24;

              h &= ~g;

         }

 

         return h%_hashTableSize;

     }

private:

     unsigned long _hashTableSize;

};

 

 

#endif //__IHAS_H__

 

0
0

  相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:128777次
    • 积分:1904
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:3篇
    • 译文:0篇
    • 评论:55条
    最新评论
    友情blog