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

FNV是 Glenn FowlerLandon 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).

posted @ 2010-12-26 21:56  napoleon_liu  阅读(2981)  评论(1编辑  收藏  举报