|
|

楼主 |
发表于 2004-11-2 17:55:00
|
显示全部楼层
Re: 谁知道SLT Ext 中hash_map的hash函数的方法?
看了一下代码,不知道我的理解对不对
STD.NET的hash算法都在构造函数里,hash table都是存储的vector里的
构造函数一共有4个,针对std的string,char *,wchar *,其他类型
size_t hash_value(const _STD basic_string<_Elem, _Traits, _Alloc>& _Str)
{ // hash string to size_t value
typedef typename _STD basic_string<_Elem, _Traits, _Alloc>::size_type _Strsize;
size_t _Val = _HASH_SEED;
_Strsize _Size = _Str.size();
if (0 < _Size)
{ // add one or more elements
_Strsize _Stride = (_Size / 16) + 1;
_Size -= _Stride; // protect against _Size near _Str.max_size()
for(_Strsize _Idx = 0; _Idx <= _Size; _Idx += _Stride)
_Val += (size_t)_Str[_Idx];
}
return (_Val);
}
template<class _Kty> inline
size_t hash_value(const _Kty& _Keyval)
{ // hash _Keyval to size_t value one-to-one
return ((size_t)_Keyval ^ _HASH_SEED);
}
inline size_t hash_value(const char *_Str)
{ // hash NTBS to size_t value
typedef size_t _Strsize;
size_t _Val = _HASH_SEED;
_Strsize _Size = ::strlen(_Str);
if (0 < _Size)
{ // add one or more elements
_Strsize _Stride = (_Size / 16) + 1;
_Size -= _Stride; // protect against _Size near _Str.max_size()
for(_Strsize _Idx = 0; _Idx <= _Size; _Idx += _Stride)
_Val += (size_t)_Str[_Idx];
}
return (_Val);
}
inline size_t hash_value(const wchar_t *_Str)
{ // hash NTWCS to size_t value
typedef size_t _Strsize;
size_t _Val = _HASH_SEED;
_Strsize _Size = ::wcslen(_Str);
if (0 < _Size)
{ // add one or more elements
_Strsize _Stride = (_Size / 16) + 1;
_Size -= _Stride; // protect against _Size near _Str.max_size()
for(_Strsize _Idx = 0; _Idx <= _Size; _Idx += _Stride)
_Val += (size_t)_Str[_Idx];
}
return (_Val);
}
不知道大家对这个有什么看法 |
|