This commit is contained in:
krahets
2023-08-13 19:36:03 +08:00
parent 059a849167
commit 971624c291
31 changed files with 1750 additions and 156 deletions

View File

@@ -231,25 +231,89 @@ index = hash(key) % capacity
=== "JS"
```javascript title="simple_hash.js"
[class]{}-[func]{addHash}
/* 加法哈希 */
function addHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
[class]{}-[func]{mulHash}
/* 乘法哈希 */
function mulHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (31 * hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
[class]{}-[func]{xorHash}
/* 异或哈希 */
function xorHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash & MODULUS;
}
[class]{}-[func]{rotHash}
/* 旋转哈希 */
function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;
}
return hash;
}
```
=== "TS"
```typescript title="simple_hash.ts"
[class]{}-[func]{addHash}
/* 加法哈希 */
function addHash(key: string): number {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
[class]{}-[func]{mulHash}
/* 乘法哈希 */
function mulHash(key: string): number {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (31 * hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
[class]{}-[func]{xorHash}
/* 异或哈希 */
function xorHash(key: string): number {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash & MODULUS;
}
[class]{}-[func]{rotHash}
/* 旋转哈希 */
function rotHash(key: string): number {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;
}
return hash;
}
```
=== "C"