FNV是 Glenn Fowler, Landon Curt Noll, and Phong Vo 三人的缩写。

FNV-1 哈希算法的核心思想如下:

 hash = offset_basis
 for each octet_of_data to be hashed    hash = hash * FNV_prime    hash = hash xor octet_of_data   return hash

实现源码

uint32_t fnv_hash(char const *str, int len)
    {
        unsigned long hash = 2166136261; //offset_basis
//FNV prime for 32 bit is 16777619
//#define FNV_OP()  hash = (hash*16777619)^*str++
#define FNV_OP()  hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);\
          hash ^=*str++;
        for (; len >= 8; len -= 8) {
            FNV_OP(); //1
            FNV_OP(); //2
            FNV_OP(); //3
            FNV_OP(); //4
            FNV_OP(); //5
            FNV_OP(); //6
            FNV_OP(); //7
            FNV_OP(); //8
       }
       switch (len) {
           case 7: FNV_OP(); /* fallthrough... */
           case 6: FNV_OP(); /* fallthrough... */
           case 5: FNV_OP(); /* fallthrough... */
           case 4: FNV_OP(); /* fallthrough... */
           case 3: FNV_OP(); /* fallthrough... */
           case 2: FNV_OP(); /* fallthrough... */
           case 1: FNV_OP(); break;
           case 0: break;
       }
       return hash;
   }

c++ 0x 标准 tr1中的实现 (gcc代码)

  

// Dummy generic implementation (for sizeof(size_t) != 4, 8).
  template<std::size_t = sizeof(std::size_t)>
    struct Fnv_hash
    {
      static std::size_t
      hash(const char* first, std::size_t length)
      {
        std::size_t result = 0;
        for (; length > 0; --length)
          result = (result * 131) + *first++;
        return result;
      }
    };

template<>
    struct Fnv_hash<4>
    {
      static std::size_t
      hash(const char* first, std::size_t length)
      {
        std::size_t result = static_cast<std::size_t>(2166136261UL);
        for (; length > 0; --length)
          {
            result ^= (std::size_t)*first++;
            result *= 16777619UL;
          }
        return result;
      }
    };

template<>
    struct Fnv_hash<8>
    {
      static std::size_t
      hash(const char* first, std::size_t length)
      {
        std::size_t result = static_cast<std::size_t>(14695981039346656037ULL);
        for (; length > 0; --length)
          {
            result ^= (std::size_t)*first++;
            result *= 1099511628211ULL;
          }
        return result;
      }
    };

FNV哈希 通用性很好,time33只针对英文文本比较合适。

FNV哈希应用于很多地方:

calc
Domain Name Servers
mdbm key/value data lookup functions
Database indexing hashes
major web search / indexing engines
high performance EMail servers
Netnews history file Message-ID lookup functions
Anti-spam filters
NFS implementations (e.g., FreeBSD 4.3, IRIX, Linux (NFS v4))
Cohesia MASS project server collision avoidance
spellchecker programmed in Ada 95
flatassembler's open source x86 assembler - user-defined symbol hashtree
PowerBASIC inline assembly routine
text based referenced resources for video games on the PS2, Gamecube and XBOX
non-cryptographic file fingerprints
FRET - a tool to identify file data structures / helps to understand file formats
used to in the process of computing Unique IDs in DASM (DTN Applications for Symbian Mobile-phones)
Used by Microsoft in their hash_map implementation for VC++ 2005
Used in an implementation of libketama for use in items such as memcache.
Used in the realpath cache in PHP 5.x (php-5.2.3/TSRM/tsrm_virtual_cwd.c).
Used to improve the fragment cache at twitter (see slide 31).

转载于:https://www.cnblogs.com/napoleon_liu/archive/2010/12/26/1917396.html

C++ 下一代标准库 tr1中默认的哈希 FNV hash相关推荐

  1. python中wraps_python 装饰器及标准库functools中的wraps

    最近在看 flask的视图装饰器 时,忽然想起预(复)习一下python的装饰器. 这里有一篇比较好的讲解装饰器的书写的 Python装饰器学习(九步入门) . 这里不单独记录装饰器的书写格式了,重点 ...

  2. Python输入一个字符串,输出其中每个字符的出现次数。要求使用标准库collotections中的Counter类...

    一.题目: 1.输入一个字符串,输出其中每个字符的出现次数.要求使用标准库collotections中的Counter类. 2.输入一个字符串,输出其中只出现了一次的字符及其下标. 3.输入一个字符串 ...

  3. Python标准库collections中与字典有关的类

    Python标准库中提供了很多扩展功能,大幅度提高了开发效率.这里主要介绍OrderedDict类.defaultdict类和Counter类. (1)OrderedDict类 Python内置字典d ...

  4. Python3.9标准库math中的函数汇总介绍(53个函数和5个常数)

    为了更好的用计算机帮助我们运算,首先要了解自己使用的库中有什么方法,否则就会做会多费力不讨好的重复工作. 本篇博文我们来了解Python标准库的math函数. 参考资料: Python3.9的math ...

  5. python标准库random中函数的作用_Python随机函数库random的使用方法详解

    Python随机函数库random的使用方法详解 前言 众所周知,python拥有丰富的内置库,还支持众多的第三方库,被称为胶水语言,随机函数库random,就是python自带的标准库,他的用法极为 ...

  6. pythonturtle是标准库_Python中turtle库的使用

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. Turtle图形库 Turtle库是Python内置的图形化模块,属于标准库之一,位于Python ...

  7. STM32标准库工程中移植TencentOS-tiny

    一.移植前的准备 1. 引言 因为项目的原因需要将原有的标准库工程升级为基于TencentOS-tiny的RTOS工程,所以花费一点时间记录移植过程,提供一份向基于STM32标准库的老工程中移植Ten ...

  8. Python使用标准库itertools中count类求解数苹果问题

    问题描述:有一箱苹果, 4 个 4 个地数最后余下 1 个, 5 个 5 个地数最后余下 2 个, 9 个 9 个地数最后余下 7 个.编写程序计算这箱苹果至少有多少个. 解题思路:先确定除以 9 余 ...

  9. 区块链中哈希算法c语言代码,解析区块链中的核心技术哈希(Hash)算法

    原标题:解析区块链中的核心技术哈希(Hash)算法 作者:崔利民 区块链是分布式数据存储.点对点传输.共识机制.加密算法等计算机技术的新型应用模式.区块链的关键技术组成主要为:P2P网络协议.共识机制 ...

最新文章

  1. 《让系统发生重大宕机事故的15个方法》
  2. STM32固件库的安装与介绍
  3. dns被自动修改_部分 DNS 查询延迟的原因与解决方案
  4. 队列的链式存储和实现(C语言)【队列】(8)
  5. IE盒模型和标准盒模型
  6. classpath: spring 中的查找方式
  7. iOS 计算文字宽度的一个细节
  8. 斯坦福计算机录取难吗,申请斯坦福究竟有多难? 什么样条件的人才能被斯坦福录取?斯坦福大学直播!...
  9. echarts同一页面四个图表切换的js数据交互
  10. 计算机类科技论文发表,计算机类科技论文
  11. Plugin For KanColleViewer – Provissy Tools V1.0
  12. matlab绘制xy色度图函数
  13. Windows API——SHFileOperation——文件操作
  14. 微信打不开拼多多提示服务器吃撑,微信拼多多助力打不开
  15. 巨额商誉,谁来买单?
  16. 深圳学校积分计算机,深圳积分入户初级计算机证书,居然也能积这么多分!
  17. GStreamer修改解码器默认优先级
  18. 1371: 三位数求解
  19. 重庆邮电大学计算机学院蓝桥杯,第七届蓝桥杯大赛个人赛(软件类)-重庆赛区获奖名单...
  20. html里c3动画是什么,C3动画+H5知识点使用总结

热门文章

  1. 3 .6 .5 优化Ad-Hoc工作负载
  2. 服务器返回值 解释 ajax提交方式 后台数据刷进前端
  3. 六十万的成长_我的EA策略分析和实现
  4. [MySQL 5.6优化] --order by limit x,x 优化
  5. 挖掘经典:几乎被人遗忘的HTML七种用法 (转)
  6. 大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(十二)多语言支持...
  7. 全球最囧的爱情测试.....
  8. 北向资金进行股票、期货指数、基金策略
  9. Android应用开发—浅谈MVX模式
  10. Spring AOP 代理模式