简体   繁体   English

如何为各种类型的键实现哈希函数?

[英]How to implement the hash function for the various type of key?

I have implemented the hash map in C++. 我已经在C ++中实现了哈希映射。 Everything works fine, except the hash function. 除了哈希函数外,其他所有东西都工作正常。

I have a template class of the element so that I can use various variable types for the hash map. 我有一个元素的模板类,以便可以将各种变量类型用于哈希映射。 Here is my code for the element. 这是我的元素代码。

template <class KeyType, class ValType>
class MapElem
{
public:
    typedef KeyType ktype;
    typedef ValType vtype;

    KeyType key;
    ValType val;

    MapElem* link;  // singly linked list
};

And the hashfunction code. 和哈希函数代码。

template <class HashMapElemType>
unsigned int 
HashMap<HashMapElemType>::hashfunction(const KeyType k)
{
    unsigned int hashIndex = 0;



    if (typeid(KeyType).name() == typeid(std::string).name())
    {
        unsigned int hashIndex = 0;

        const char* c = k.c_str();

        unsigned int i = 0;
        int index = 0;
        int shift = 0;

        while (c[index] != '\0')
        {
            if (shift == 32)
                shift = 0;
            i += ((int) c[index++]) << shift;
            shift += 8;
        }

        hashIndex = i;
    }
    else if (typeid(KeyType).name() == typeid(float).name())
    {   
        float f = k;
        hashIndex = (unsigned int) f;
    }
    else if (typeid(KeyType).name() == typeid(int).name())
    {
        int i = k;
        hashIndex = (unsigned int) i;
    }
    else
    {
        hashIndex = k;
    }

    hashIndex = hashIndex % divisor;

    return hashIndex;
}

And there is a compile error for type casting in the hashfunction. 哈希函数中的类型转换存在编译错误。 I understand why the error occurs, but I don't know how to fix it. 我知道为什么会发生错误,但我不知道如何解决。 I wonder how to get a hash value from different types of key value. 我想知道如何从不同类型的键值中获取哈希值。

oh here is the error enter image description here 哦,这是错误,请在此处输入图片说明

Your hash function should be a template function on the key type, implemented outside of your container class. 您的哈希函数应该是密钥类型上的模板函数,应在容器类之外实现。 You can then specialize the template function for each key type you're actually using the hash map with. 然后,您可以为实际上使用哈希映射的每种键类型专门化模板功能。 This turns the type check from run-time to compile-time, making it both faster and safer. 这会将类型检查从运行时转变为编译时,从而使其更快,更安全。

// hash function prototype, no implementation
template<typename T> unsigned int CalculateHash( const T& v );

// hash function specialization for std::string
template<> unsigned int CalculateHash( const std::string& v )
{
  // your hash function for std::string ...
}

Inside your container implementation you can then use the generic hash function to generate a hash value for your key. 然后,在容器实现内部,您可以使用通用哈希函数为键生成哈希值。

template <class HashMapElemType>
unsigned int HashMap<HashMapElemType>::hashfunction(const KeyType& k)
{
  // delegate to global hash function template
  return ::CalculateHash<KeyType>( k ); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM