zoukankan      html  css  js  c++  java
  • 关于float /double、string类型的hash函数/hash表实现(转)

    #include <ext/hash_map>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    
    
    #define FLT_EPSILON 1.192093e-007
    #define DBL_EPSILON 2.2204460492503131e-016
    #define FLOAT_EPSILON(a,b) ( a > b ? fabs(a) * FLT_EPSILON : fabs(b) * FLT_EPSILON)
    #define DOUBLE_EPSILON(a,b) ( a > b ? fabs(a) * FLT_EPSILON : fabs(b) * DBL_EPSILON)
    #define float_equal(a, b) (fabs((a)-(b)) <= FLOAT_EPSILON(a,b))
    #define double_equal(a, b) (fabs((a)-(b)) <= DOUBLE_EPSILON(a,b))
    #define INT64_MAX 0x7fffffffffffffffLL
    
    typedef struct  
    {  
        size_t operator()(const double & dValue) const  
        {     
            int e = 0 ;  
            double tmp = dValue;  
            if (dValue<0)  
            {  
                tmp = -dValue;  
            }  
            e = ceil (log (dValue));  
            return size_t(( INT64_MAX+ 1.0) * tmp * exp (-e));  
        }  
    } hash_double; 
    
    typedef struct  
    {  
        bool operator()(const double &value1,const double &value2) const  
        {  
            return double_equal(value1,value2);  
        }  
    } hash_double_cmp;  
    
    
    typedef struct  
    {  
        size_t operator()(const string & str) const  
        {  
            size_t h=0;for(size_t i=0;i<str.length();++i)  
            {  
                h = ( h<<5 ) - h + str[i];  
            }  
            return h;  
            //return __stl_hash_string(str.c_str());  
        }  
    } hash_string;  
    
    typedef struct  
    {  
        bool operator()(const string &str1,const string &str2) const  
        {  
            return str1.compare(str2) == 0;  
        }  
    } hash_str_cmp;  

    http://blog.csdn.net/templarzq/article/details/7702910

  • 相关阅读:
    or具体点vc
    异常 中断 实现
    int 0x80 系统调用实现
    方便查看 linux/kernel/sched.c
    第五周课堂笔记1th
    第四周课堂笔记4th
    第四周课堂笔记3th
    第四周课堂笔记2th
    第四周课堂笔记1th
    第三周课堂笔记4thand5th
  • 原文地址:https://www.cnblogs.com/bizhu/p/4134634.html
Copyright © 2011-2022 走看看