forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java and C++ code for the section hash algorithm (krahets#560)
- Loading branch information
Showing
6 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* File: built_in_hash.cpp | ||
* Created Time: 2023-06-21 | ||
* Author: Krahets ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.hpp" | ||
|
||
/* Driver Code */ | ||
int main() { | ||
int num = 3; | ||
size_t hashNum = hash<int>()(num); | ||
cout << "整数 " << num << " 的哈希值为 " << hashNum << "\n"; | ||
|
||
bool bol = true; | ||
size_t hashBol = hash<bool>()(bol); | ||
cout << "布尔量 " << bol << " 的哈希值为 " << hashBol << "\n"; | ||
|
||
double dec = 3.14159; | ||
size_t hashDec = hash<double>()(dec); | ||
cout << "小数 " << dec << " 的哈希值为 " << hashDec << "\n"; | ||
|
||
string str = "Hello 算法"; | ||
size_t hashStr = hash<string>()(str); | ||
cout << "字符串 " << str << " 的哈希值为 " << hashStr << "\n"; | ||
|
||
// 在 C++ 中,内置 std:hash() 仅提供基本数据类型的哈希值计算 | ||
// 数组、对象的哈希值计算需要自行实现 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* File: simple_hash.cpp | ||
* Created Time: 2023-06-21 | ||
* Author: Krahets ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.hpp" | ||
|
||
/* 加法哈希 */ | ||
int addHash(string key) { | ||
long long hash = 0; | ||
const int MODULUS = 1000000007; | ||
for (unsigned char c : key) { | ||
hash = (hash + (int)c) % MODULUS; | ||
} | ||
return (int)hash; | ||
} | ||
|
||
/* 乘法哈希 */ | ||
int mulHash(string key) { | ||
long long hash = 0; | ||
const int MODULUS = 1000000007; | ||
for (unsigned char c : key) { | ||
hash = (31 * hash + (int)c) % MODULUS; | ||
} | ||
return (int)hash; | ||
} | ||
|
||
/* 异或哈希 */ | ||
int xorHash(string key) { | ||
int hash = 0; | ||
const int MODULUS = 1000000007; | ||
for (unsigned char c : key) { | ||
cout<<(int)c<<endl; | ||
hash ^= (int)c; | ||
} | ||
return hash & MODULUS; | ||
} | ||
|
||
/* 旋转哈希 */ | ||
int rotHash(string key) { | ||
long long hash = 0; | ||
const int MODULUS = 1000000007; | ||
for (unsigned char c : key) { | ||
hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS; | ||
} | ||
return (int)hash; | ||
} | ||
|
||
/* Driver Code */ | ||
int main() { | ||
string key = "Hello dsad3241241dsa算123法"; | ||
|
||
int hash = addHash(key); | ||
cout << "加法哈希值为 " << hash << endl; | ||
|
||
hash = mulHash(key); | ||
cout << "乘法哈希值为 " << hash << endl; | ||
|
||
hash = xorHash(key); | ||
cout << "异或哈希值为 " << hash << endl; | ||
|
||
hash = rotHash(key); | ||
cout << "旋转哈希值为 " << hash << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* File: built_in_hash.java | ||
* Created Time: 2023-06-21 | ||
* Author: Krahets ([email protected]) | ||
*/ | ||
|
||
package chapter_hashing; | ||
|
||
import utils.*; | ||
import java.util.*; | ||
|
||
public class built_in_hash { | ||
public static void main(String[] args) { | ||
int num = 3; | ||
int hashNum = Integer.hashCode(num); | ||
System.out.println("整数 " + num + " 的哈希值为 " + hashNum); | ||
|
||
boolean bol = true; | ||
int hashBol = Boolean.hashCode(bol); | ||
System.out.println("布尔量 " + bol + " 的哈希值为 " + hashBol); | ||
|
||
double dec = 3.14159; | ||
int hashDec = Double.hashCode(dec); | ||
System.out.println("小数 " + dec + " 的哈希值为 " + hashDec); | ||
|
||
String str = "Hello 算法"; | ||
int hashStr = str.hashCode(); | ||
System.out.println("字符串 " + str + " 的哈希值为 " + hashStr); | ||
|
||
Object[] arr = { 12836, "小哈" }; | ||
int hashTup = Arrays.hashCode(arr); | ||
System.out.println("数组 " + Arrays.toString(arr) + " 的哈希值为 " + hashTup); | ||
|
||
ListNode obj = new ListNode(0); | ||
int hashObj = obj.hashCode(); | ||
System.out.println("节点对象 " + obj + " 的哈希值为 " + hashObj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* File: simple_hash.java | ||
* Created Time: 2023-06-21 | ||
* Author: Krahets ([email protected]) | ||
*/ | ||
|
||
package chapter_hashing; | ||
|
||
public class simple_hash { | ||
/* 加法哈希 */ | ||
static int addHash(String key) { | ||
long hash = 0; | ||
final int MODULUS = 1000000007; | ||
for (char c : key.toCharArray()) { | ||
hash = (hash + (int) c) % MODULUS; | ||
} | ||
return (int) hash; | ||
} | ||
|
||
/* 乘法哈希 */ | ||
static int mulHash(String key) { | ||
long hash = 0; | ||
final int MODULUS = 1000000007; | ||
for (char c : key.toCharArray()) { | ||
hash = (31 * hash + (int) c) % MODULUS; | ||
} | ||
return (int) hash; | ||
} | ||
|
||
/* 异或哈希 */ | ||
static int xorHash(String key) { | ||
int hash = 0; | ||
final int MODULUS = 1000000007; | ||
for (char c : key.toCharArray()) { | ||
System.out.println((int)c); | ||
hash ^= (int) c; | ||
} | ||
return hash & MODULUS; | ||
} | ||
|
||
/* 旋转哈希 */ | ||
static int rotHash(String key) { | ||
long hash = 0; | ||
final int MODULUS = 1000000007; | ||
for (char c : key.toCharArray()) { | ||
hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS; | ||
} | ||
return (int) hash; | ||
} | ||
|
||
public static void main(String[] args) { | ||
String key = "Hello 算法"; | ||
|
||
int hash = addHash(key); | ||
System.out.println("加法哈希值为 " + hash); | ||
|
||
hash = mulHash(key); | ||
System.out.println("乘法哈希值为 " + hash); | ||
|
||
hash = xorHash(key); | ||
System.out.println("异或哈希值为 " + hash); | ||
|
||
hash = rotHash(key); | ||
System.out.println("旋转哈希值为 " + hash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters