关于float /double、string类型的hash函数/hash表实现

#include <ext/hash_map>
#include <math.h>
using namespace std;
using namespace __gnu_cxx;

#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;

hash_map<double, vector,hash_double,hash_double_cmp> m_doubleMap;
hash_map<string, vector,hash_string,hash_str_cmp> m_strMap;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值