amejiarosario/dsa.js-data-structures-algorithms-javascript

Better hash function ?

nuintun opened this issue · 2 comments

企业微信截图_20200520151838

// TextEncoder
const encoder = new TextEncoder();

/**
 * @description Polynomial hash codes are used to hash String typed keys.
 * It uses FVN-1a hashing algorithm for 32 bits
 * @see http://bit.ly/fvn-1a
 * @param {any} key
 * @return {integer} bucket index
 */
hashFunction(key) {
  const bytes = encoder.encode(key);

  // FNV_offset_basis (32 bit)
  let hash = 2166136261;

  const { length } = bytes;

  for (let i = 0; i < length; ) {
    // XOR
    hash ^= bytes[i++];
    // 32 bit FNV_prime
    hash *= 16777619;
  }

  return (hash >>> 0) % this.buckets.length;
}

New function support key out of ASCII.

Yes, this one looks better! If you want you can create PR with this change and I'll accept it.

🎉 This issue has been resolved in version 1.7.1 🎉

The release is available on:

Your semantic-release bot 📦🚀