字串hash函式(hashCode的生成)
非常好的外文網站!!!:
General Purpose Hash Function Algorithms
最終結果:
1. BKDRHash
2. Blizzard hash
**************
Java 版:
今天根據自己的理解重新整理了一下幾個字串hash函式,使用了模板,使其支援寬字串,程式碼如下:
- /// @brief BKDR Hash Function
-
/// @detail 本演算法由於在Brian Kernighan與Dennis Ritchie的《The C Programming Language》一書被展示而得名,是一種簡單快捷的hash演算法,
- template<class T>
- size_t BKDRHash(const T *str)
- {
- registersize_t hash = 0;
- while (size_t ch = (size_t)*str++)
- {
- hash = hash * 131 + ch; // 也可以乘以31、131、1313、13131、131313..
-
// 有人說將乘法分解為位運算及加減法可以提高效率,如將上式表達為:hash = hash << 7 + hash << 1 + hash + ch;
- // 但其實在Intel平臺上,CPU內部對二者的處理效率都是差不多的,
- // 我分別進行了100億次的上述兩種運算,發現二者時間差距基本為0(如果是Debug版,分解成位運算後的耗時還要高1/3);
- // 在ARM這類RISC系統上沒有測試過,由於ARM內部使用Booth's Algorithm來模擬32位整數乘法運算,它的效率與乘數有關:
- // 當乘數8-31位都為1或0時,需要1個時鐘週期
- // 當乘數16-31位都為1或0時,需要2個時鐘週期
- // 當乘數24-31位都為1或0時,需要3個時鐘週期
-
// 否則,需要4個時鐘週期
- // 因此,雖然我沒有實際測試,但是我依然認為二者效率上差別不大
- }
- return hash;
- }
- /// @brief SDBM Hash Function
- /// @detail 本演算法是由於在開源專案SDBM(一種簡單的資料庫引擎)中被應用而得名,它與BKDRHash思想一致,只是種子不同而已。
- template<class T>
- size_t SDBMHash(const T *str)
- {
- registersize_t hash = 0;
- while (size_t ch = (size_t)*str++)
- {
- hash = 65599 * hash + ch;
- //hash = (size_t)ch + (hash << 6) + (hash << 16) - hash;
- }
- return hash;
- }
- /// @brief RS Hash Function
- /// @detail 因Robert Sedgwicks在其《Algorithms in C》一書中展示而得名。
- template<class T>
- size_t RSHash(const T *str)
- {
- registersize_t hash = 0;
- size_t magic = 63689;
- while (size_t ch = (size_t)*str++)
- {
- hash = hash * magic + ch;
- magic *= 378551;
- }
- return hash;
- }
- /// @brief AP Hash Function
- /// @detail 由Arash Partow發明的一種hash演算法。
- template<class T>
- size_t APHash(const T *str)
- {
- registersize_t hash = 0;
- size_t ch;
- for (long i = 0; ch = (size_t)*str++; i++)
- {
- if ((i & 1) == 0)
- {
- hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
- }
- else
- {
- hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
- }
- }
- return hash;
- }
- /// @brief JS Hash Function
- /// 由Justin Sobel發明的一種hash演算法。
- template<class T>
- size_t JSHash(const T *str)
- {
- if(!*str) // 這是由本人新增,以保證空字串返回雜湊值0
- return 0;
- registersize_t hash = 1315423911;
- while (size_t ch = (size_t)*str++)
- {
- hash ^= ((hash << 5) + ch + (hash >> 2));
- }
- return hash;
- }
- /// @brief DEK Function
- /// @detail 本演算法是由於Donald E. Knuth在《Art Of Computer Programming Volume 3》中展示而得名。
- template<class T>
- size_t DEKHash(const T* str)
- {
- if(!*str) // 這是由本人新增,以保證空字串返回雜湊值0
- return 0;
- registersize_t hash = 1315423911;
- while (size_t ch = (size_t)*str++)
- {
- hash = ((hash << 5) ^ (hash >> 27)) ^ ch;
- }
- return hash;
- }
- /// @brief FNV Hash Function
- /// @detail Unix system系統中使用的一種著名hash演算法,後來微軟也在其hash_map中實現。
- template<class T>
- size_t FNVHash(const T* str)
- {
- if(!*str) // 這是由本人新增,以保證空字串返回雜湊值0
- return 0;
- registersize_t hash = 2166136261;
- while (size_t ch = (size_t)*str++)
-
相關推薦
字串hash函式(hashCode的生成)
非常好的外文網站!!!: General Purpose Hash Function Algorithms 最終結果: 1. BKDRHash 2. Blizzard hash ************** Java 版:
hash函式(SHA-256)的C++實現
//SHA-256 /*理解演算法最重要,最好自己動手實現試試看,可以使用MFC寫一個簡單的互動介面*/ #include <iostream> #include <cstdio> #include <cstdlib> using
C/C++ 指標小結——指標與其它資料型別(陣列、字串、函式、結構體)的關係
一、指標與陣列和字串 1、指標與陣列 當宣告數時,編譯器在連續的記憶體空間分配基本地址和足夠的儲存空間,以容納陣列的所有元素。基本地址是陣列第一個元素(索引為0)的儲存位置。編譯器還把陣列名定義為指向第一個元素的常量指標。 元素的地址是通過索引和資料型別的比例因子來計算的;例如: x[3
斐波那契+n的k次方+整數各位之和+字串反向排列(逆置)+實現strlen函式+n的階乘+列印整數的每一位
用兩種方法求斐波那契數列指定數值 #include <stdio.h> #include <windows.h> //用遞迴實現斐波那契數列 int fib(int n) { if (n == 1 || n == 2) { return 1; } re
URAL - 1989 Subpalindromes (字串hash + 線段樹區間合併)
題目連結:http://acm.timus.ru/problem.aspx?space=1&num=1989 題目大意:給出一個長度為n的字串S,接下來進行n次操作。操作分為修改和查詢兩種,每次修改操作給出一個整數 i 和一個字元c,表示將第 i 位的字元變成字元c;每次查詢操作給出兩個
mysql資料庫時間字串轉化的函式(面試題)
如題,今天去奧鵬教育面試,第一道題就是這個,mysql字串,時間轉化函式,結果如下: date_format(date,'%Y-%m-%d') -------------->ora
CODE39和CODE128編碼生成函式(VBA版)
Public Function StrToCode39(str As String) As String StrToCode39 = "*" & str & "*" End Function Function StrToCode128(s
C語言的字串處理函式(更)
1、strcpy 2、strcat 3、strncat 4、strcmp 5、strlen 6、bzreo: eg:bzero(ptr,sizeof(char) * 100) : 從ptr
實現字串連線函式(strcat)
在字串的操作中strcat函式的使用是頻繁的,那麼下面我們來自己實現strcat函式的功能。自定義一個函式將要連線的兩個字串作為引數傳入,然後將str1賦值給臨時變數p,然後p一直向後指,直到str1的結尾,然後將str2的值給p,記住最後需要讓*p='\0',做為新的字串的
字串hash補充(快速冪模板)
題目: 字串的雜湊就是通過某些對映關係,將字串對映到數字上去方便進行比較。 比如二進位制數110110,我們知道這個數的十進位制是 54 基於同樣思路,我們可以定義這樣的一個雜湊函式: 雜湊函式
字串處理函式(2)replace&enumerate
replace(old,new) 返回值為用new替換原字串中的old生成新字串所以一定要用自動變數去接好處是沒有old它就不替換也不會報錯enumerate()在需要遍歷索引又要遍歷元素時可以避免麻煩,不用巢狀listList_demo = [’D’,’E’,’M’,’O’
6-2 函式實現字串逆序(10 分)
本題要求實現一個字串逆序的簡單函式。 函式介面定義: void f( char *p ); 函式f對p指向的字串進行逆序操作。要求函式f中不能定義任何陣列,不能呼叫任何字串處理函式。 裁判測試程式樣例: 本題要求實現一個字串逆序的簡單函式。 函式
vb.net 字串操作函式(數字轉字串去掉空格的方法)
Len Len(string|varname) 返回字串內字元的數目,或是儲存一變數所需的位元組數。 Trim Trim(string) 將字串前後的空格去掉 Ltrim Ltrim(string) 將字串前面的空格去掉 Rtrim Rtrim(string)
hash slot(虛擬桶)
系統 crc 移動 是把 如何 for tails html 問題 在分布式集群中,如何保證相同請求落到相同的機器上,並且後面的集群機器可以盡可能的均分請求,並且當擴容或down機的情況下能對原有集群影響最小。 round robin算法:是把數據mod後直接映射到真實節
tomcat配置https自簽名證書(keytool生成)
pri list tin led str orm unit lock pass tomcat配置https自簽名證書(keytool生成) 生成keystore keytool -genkeypair -alias "server" -keyalg &
字串相減 (簡易版)
#include #include<string.h> using namespace std; const int Maxsize=100; class String { char ch[Maxsize]; int len; public: String(const
sincerit 母函式(組合問題)
大佬程式碼: https://blog.csdn.net/yu121380/article/details/79914529 https://blog.csdn.net/xiaofei_it/article/details/17042651?utm_source=blogxgwz0 有1克、
c語言 陣列中字串的旋轉(左旋)(右旋)
在陣列中定義一個字串,該字串由“ abcdef ” 組成,所謂左旋即是讓左邊的第一個字元旋轉到右邊去,左旋一個字元即是產生“ bcdefa ”這樣的字串,右旋與之相反。 為了控制左(右)旋的字元數,需要製作一個可以改
【練習題】第十六章--類和函式(Think Python)
class Time: hour=0 minute=0 second=0 def print_time(t): print("%.2d:%.2d:%.2d"%(t.hour,t.minute,t.second)) def is_after(t1,t2):