Skip to content

Commit

Permalink
use 137 as seed2 to reduce hash collisions, fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
zenozeng committed Mar 2, 2015
1 parent 155ef10 commit 7f0bb7a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "color-hash",
"version": "1.0.0",
"version": "1.0.1",
"homepage": "https://github.com/zenozeng/color-hash",
"authors": [
"Zeno Zeng <[email protected]>"
Expand Down
6 changes: 4 additions & 2 deletions dist/color-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
*/
var BKDRHash = function(str) {
var seed = 131;
var seed2 = 137;
var hash = 0;
var MAX_SAFE_INTEGER = parseInt(9007199254740991 / seed / seed); // Number.MAX_SAFE_INTEGER equals 9007199254740991
// Note: Number.MAX_SAFE_INTEGER equals 9007199254740991
var MAX_SAFE_INTEGER = parseInt(9007199254740991 / seed2);
for(var i = 0; i < str.length; i++) {
if(hash > MAX_SAFE_INTEGER) {
hash = parseInt(hash / seed);
hash = parseInt(hash / seed2);
}
hash = hash * seed + str.charCodeAt(i);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/bkdr-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/
var BKDRHash = function(str) {
var seed = 131;
var seed2 = 137;
var hash = 0;
var MAX_SAFE_INTEGER = parseInt(9007199254740991 / seed / seed); // Number.MAX_SAFE_INTEGER equals 9007199254740991
// Note: Number.MAX_SAFE_INTEGER equals 9007199254740991
var MAX_SAFE_INTEGER = parseInt(9007199254740991 / seed2);
for(var i = 0; i < str.length; i++) {
if(hash > MAX_SAFE_INTEGER) {
hash = parseInt(hash / seed);
hash = parseInt(hash / seed2);
}
hash = hash * seed + str.charCodeAt(i);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "color-hash",
"version": "1.0.0",
"version": "1.0.1",
"description": "Generate color based on the given string (using HSL color space and BKDRHash).",
"main": "lib/color-hash.js",
"scripts": {
Expand Down

0 comments on commit 7f0bb7a

Please sign in to comment.