日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

哈希函数原理及实现

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈希函数原理及实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

哈希解決沖突

1000以內的素數

一般的hash實現已經總結出一些比較重要的素數:

static unsigned int table_size[] = {7,13,31,        61,         127,        251,       509,        1021,2039,      4093,       8191,       16381,     32749,      65521,131071,    262143,     524287,     1048575,   2097151,    4194303,8388607,   16777211,   33554431,   67108863,  134217727,  268435455,536870911, 1073741823, 2147483647, 0
};

--------------以上出自《算法精解? C語言描述》


下面這個hash函數是一個能夠較好的處理字符串的哈希函數,它通過一系列的位操作將健強制轉換為整數。此函數改編自《compilers principles techniques and tools》

也就是《編譯原理》--龍書

int hashpjw(const void *key,size_t PRIME_TBLSIZ) {const char         *ptr;int                val;/*****************************************************************************
*                                                                            *
*  Hash the key by performing a number of bit operations on it.              *
*                                                                            *
*****************************************************************************/val = 0;
ptr = key;while (*ptr != '\0') {int tmp;val = (val << 4) + (*ptr);if (tmp = (val & 0xf0000000)) {val = val ^ (tmp >> 24);val = val ^ tmp;}ptr++;}/*****************************************************************************
*                                                                            *
*  In practice, replace PRIME_TBLSIZ with the actual table size.             *
*                                                                            *
*****************************************************************************/return val % PRIME_TBLSIZ;}

《算法精解? C語言描述》源碼地址:https://github.com/Caloni/MasteringCAlgorithms

http://technologyx.org/sourcecode/candcplusplus/c/hashpjw-c/


下面這個哈希函數來自MIT:http://web.mit.edu/majapw/MacData/afs/sipb/user/ssen/src/coreutils-5.2.1/lib/hash-pjw.c

#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)/* A hash function for NUL-terminated char* strings usingthe method described by Bruno Haible.See http://www.haible.de/bruno/hashfunc.html.? */size_t
hash_pjw (const void *x, size_t tablesize)
{const char *s;size_t h = 0;for (s = x; *s; s++)h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));return h % tablesize;
}

MIT那個項目還有一個hash函數的完整實現:http://web.mit.edu/majapw/MacData/afs/sipb/user/ssen/src/coreutils-5.2.1/lib/hash.c



總結

以上是生活随笔為你收集整理的哈希函数原理及实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。