-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.js
34 lines (31 loc) · 898 Bytes
/
hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Hash {
constructor(M, P) {
this.M = M;
this.P = P;
}
/**
* Converts a string into an integer hash value
* @param {String} data data to convert the hash value for (string)
* @returns {Integer} returns an Int of the corresponding hash value for the string (data)
*/
get_str_hash(data)
{
data = String(data);
var hash_Int = 0;
for(var i = data.length - 1; i >= 0; i--)
{
hash_Int += (hash_Int*this.X + data.charCodeAt(i))%this.P;
}
return parseInt(hash_Int);
}
/**
* Sets the index multiplication for primal value for str hash function.
* Need to do this before calling {get_str_hash}
* @param {Integer} X the primal function for string based hash function
*/
setPrimeString(X)
{
this.X = X;
}
}
module.exports = Hash;