Javascript String hashCode()

Description

Javascript String hashCode()


String.prototype.hashCode = function() {
  for(var ret = 0, i = 0, len = this.length; i < len; i++) {
    ret = (31 * ret + this.charCodeAt(i)) << 0;
  }/*  w w  w .  ja va  2 s .c  o  m*/
  return Math.abs(ret);
};

Javascript String hashCode()

String.prototype.hashCode = function () {
  return this.split("").reduce(function (a,b) {
    a=((a<<5)-a)+b.charCodeAt(0);/* w  ww  .jav a  2s.  c o  m*/
    return a&a
  }, 0); 
};

Javascript String hashCode()

String.prototype.hashCode = function(){
  var hash = 0;//from   w  w  w. j ava 2s .c  om
  for (var i = 0; i < this.length; i++) {
    var code = this.charCodeAt(i);
    hash = ((hash<<5)-hash)+code;
    hash = hash & hash; // Convert to 32bit integer
  }
  return hash;
}

Javascript String hashCode()

String.prototype.hashCode = function() {
    var hash = 0, i, chr, len;
    if (this.length == 0) return hash;
    for (i = 0, len = this.length; i < len; i++) {
        chr   = this.charCodeAt(i);/*w  w  w  . ja v  a2  s.c  o  m*/
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};

Javascript String hashCode()

String.prototype.hashCode = function hashCode() {
  if (this.length <= 0) {
    return 0;/*  w ww  .jav a2 s  . c  om*/
  }

  let hash = 0;
  for (let i = 0, l = this.length; i < l; i++) {
    hash = ((hash << 5) - hash) + this.charCodeAt(i);
    hash |= 0;
  }
  return hash;
};

Javascript String hashCode()

String.prototype.hashCode = function(){
  let hash = 0, i = 0;
  if (!this.length) return hash;
  for (; i < this.length; i++) {
    let char = this.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash &= hash; // convert hash to 32 bit integer
  }/* ww w  .  j  a  va 2 s.  co m*/
  return hash;
}

console.log('abcasdfasdfasdiuasjwepriuwepoiu'.hashCode());

Javascript String hashCode()

/**/*from   ww  w.j  av  a2 s  .  c  o m*/
 * This function is copy & pasted from this answer:
 *
 * http://stackoverflow.com/a/15710692/3036625
 */
String.prototype.hashCode = function () {
    if (this.length == 0) return 0;
    return this.split("").reduce(function (a, b) {
        a = ((a << 5) - a) + b.charCodeAt(0);
        return a & a
    }, 0);
};

JSON.hashCode = function (obj) {
    return JSON.stringify(obj).hashCode();
};

Javascript String hashCode()

/**/*  w w w . ja v a 2  s.c  o m*/
 * @see http://stackoverflow.com/q/7616461/940217
 * @return {number}
 */
 
String.prototype.hashCode = function(){
    var hash = 0;
    if (this.length === 0) return hash;
    if (Array.prototype.reduce){
     hash = this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); 
    }
    else 
    for (var i = 0; i < this.length; i++) {
        hash  = ((hash<<5)-hash) + this.charCodeAt(i);
        hash = hash & hash; // Convert to 32bit integer
    }
 return hash&65535;
};

Javascript String hashCode()

function HashMap(capacity){
 this.capacity = capacity;/*w  ww .ja  v  a 2 s.  co m*/
 this.table = []
}

String.prototype.hashCode = function()
{
 var hash = 0;
 if (this.length == 0) {
  return hash
 }
 for (var i =  0; i < this.length; i++) {
  char = this.charCodeAt(i);
  hash = ((hash<<5)-hash)+char;
  hash &= hash;
 }
 return hash;
}

function mod (input, div) {
 return (input % div + div)%div;
}

// var myIndx = mod(myHashCode, arrSize);

console.log('Hello World');

Javascript String hashCode()

var crypto = require('crypto');

function hashString(str) {
  return crypto.createHash('md5').update(str || '').digest('hex');
}

String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length === 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);/*ww w. ja  va2  s. c o  m*/
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};

module.exports = hashString;

Javascript String hashCode()

function HashMap(capacity){
  this.capacity = capacity;/*  w  w  w .  j  a v a 2  s  .com*/
  this.table = [];
}

String.prototype.hashCode = function(){
  var hash = 0;
  if(this.length == 0) return hash;
  for(i = 0; i < this.length; i++){
    char = this.charCodeAt(i);
    hash = ((hash<<5)-hash)+char;
    hash &= hash;
  }
  return hash
}

function mod(input, div){
  return (input % div + div) % div;
}

var listMap = new HashMap(100)

Javascript String hashCode()

/* eslint-disable no-bitwise*/
/* eslint-disable no-extend-native */
/* eslint-disable prefer-rest-params */

"use strict";//  w w  w  .  j a v a  2 s. c o  m

String.prototype.hashCode = function hashCode() {
  let i;
  let len;
  let ret;
  for (ret = 0, i = 0, len = this.length; i < len; i++) {
    ret = ((31 * ret) + this.charCodeAt(i)) << 0;
  }
  return ret;
};

Javascript String hashCode()

/*jslint bitwise: true*/
function hashFnv32a(str, asString) {
    "use strict";
    var i, l, hval = 0x811c9dc5;

    for (i = 0, l = str.length; i < l; i += 1) {
        hval ^= str.charCodeAt(i);/*from  w  w w . ja  va 2  s  .c  o m*/
        hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
    }
    if (asString) {
        // Convert to 8 digit hex string
        return ("0000000" + (hval >>> 0).toString(16)).substr(-8);
    }
    return hval >>> 0;
}

String.prototype.hashCode = function () {
    "use strict";
    return hashFnv32a(this, true);
};



PreviousNext

Related