Skip to content

Instantly share code, notes, and snippets.

@PadCesar
Forked from dtx/HashMap.js
Last active December 14, 2018 09:35
Show Gist options
  • Select an option

  • Save PadCesar/5b88aa75ab2f2e869e4055c98cc72c4d to your computer and use it in GitHub Desktop.

Select an option

Save PadCesar/5b88aa75ab2f2e869e4055c98cc72c4d to your computer and use it in GitHub Desktop.

Revisions

  1. PadCesar revised this gist Dec 14, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion HashMap.js
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ HashMap.prototype = {

    keys: function(){
    var keys = [];
    for(var keys in this._map){
    for(var key in this._map){
    if(this._map.hasOwnProperty(key)){
    keys.push(key);
    }
  2. PadCesar revised this gist Dec 14, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion HashMap.js
    Original file line number Diff line number Diff line change
    @@ -59,12 +59,13 @@ HashMap.prototype = {
    },

    values: function(){
    var values[];
    var values = [];
    for(var key in this._map){
    if(this._map.hasOwnProperty(key)){
    values.push(this._map[key]);
    }
    }
    return values;
    },

    size: function(){
  3. @dtx dtx created this gist Sep 27, 2012.
    74 changes: 74 additions & 0 deletions HashMap.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    var HashMap = function(){
    this._size = 0;
    this._map = {};
    }


    HashMap.prototype = {
    put: function(key, value){
    if(!this.containsKey(key)){
    this._size++;
    }
    this._map[key] = value;
    },

    remove:function(key){
    if(this.containsKey(key)){
    this._size--;
    var value = this._map[key];
    delete this._map[key];
    return value;
    }
    else{
    return null;
    }
    },

    containsKey: function(key){
    return this._map.hasOwnProperty(key);
    },

    containsValue: function(value){
    for(var key in this._map){
    if(this._map.hasOwnProperty(key)){
    if(this._map[key] === value){
    return true;
    }
    }
    }
    return false;
    },

    get: function(key){
    return this.containsKey(key) ? this._map[key] : null;
    },

    clear: function(key){
    this.size = 0;
    this._map= {};
    },

    keys: function(){
    var keys = [];
    for(var keys in this._map){
    if(this._map.hasOwnProperty(key)){
    keys.push(key);
    }
    }
    return keys;
    },

    values: function(){
    var values[];
    for(var key in this._map){
    if(this._map.hasOwnProperty(key)){
    values.push(this._map[key]);
    }
    }
    },

    size: function(){
    return this._size;
    }
    };