C++ 下一代标准库 tr1中默认的哈希 FNV hash
FNV是 Glenn Fowler, Landon Curt Noll, and Phong Vo 三人的縮寫。
FNV-1 哈希算法的核心思想如下:
hash = offset_basis for each octet_of_data to be hashedhash = hash * FNV_prime
hash = hash xor octet_of_data
return hash
實現(xiàn)源碼
??? 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 標(biāo)準(zhǔn) tr1中的實現(xiàn) (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哈希應(yīng)用于很多地方:
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).
轉(zhuǎn)載于:https://www.cnblogs.com/napoleon_liu/archive/2010/12/26/1917396.html
總結(jié)
以上是生活随笔為你收集整理的C++ 下一代标准库 tr1中默认的哈希 FNV hash的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于JDBC的宠物管理系统
- 下一篇: 《码出高效》