Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dfunction InstantiationException(stackMessage) {
  2.     throw new Error("InstantiationException: "+ stackMessage);
  3. }
  4. var _extends = (this && this._extends) || function (d, b) {
  5.     for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  6.     function _() { this.constructor = d; }
  7.     d.prototype = b === null ? Object.create(b) : (_.prototype = b.prototype, new _());
  8. };
  9. function Cloneable() {
  10.     this.clone = function() {
  11.         return this;
  12.     }
  13. }
  14. /**
  15.  *
  16.  * @param {function} K
  17.  * @param {function} V
  18.  * @typedef {{map: object}}
  19.  */
  20. function HashMap(K,V, preMap = undefined) {
  21.     if (typeof K != 'function' || typeof V != 'function')
  22.         throw new InstantiationException("K and V need to be typeof Function");
  23.     var map = preMap || new Object();
  24.     /**
  25.      * @return {void}
  26.      */
  27.     this.put = function(key,value) {
  28.        map[`${key}`] = value;
  29.     }
  30.     /**
  31.      * @param {object} key
  32.      * @return {any} value
  33.      */
  34.    
  35.     this.get = function(key) {
  36.         return map[key];
  37.     }
  38.     /**
  39.      * @return {Array} values of the map
  40.      */
  41.     this.values = function() {
  42.         return Object.values(map);
  43.     }
  44.     /**
  45.      * @return keys
  46.      */
  47.     this.keySet = function() {
  48.         return Object.keys(map);
  49.     }
  50.     this.forEach = function(callback) {
  51.         console.log(map);
  52.         var iteratorable = [];
  53.         for (var [key,value] in (this.values(),this.keySet())) {
  54.             iteratorable.push([key,value])
  55.         }
  56.         for (var [key,val] of iteratorable) {
  57.             console.log(key);
  58.             console.log(val);
  59.             callback.call(key,val);
  60.         }
  61.     }
  62.     /**
  63.      * Removes the key and the value.
  64.      * @param {any} key
  65.      *
  66.      */
  67.     this.remove = function(key) {
  68.         delete map[key];
  69.         return true;
  70.     }
  71.     /**
  72.      * If the map value is empty, return true
  73.      *
  74.      * (think of it as an entry set being empty)
  75.      * @return {boolean}
  76.      */
  77.     this.isEmpty = function() {
  78.         return !(this.keys().length > 0 && this.values().length > 0);
  79.     }
  80.     /**
  81.      * @param {any} key The key
  82.      * @param {any} value the value to replace the current value inside the key.
  83.      * @typedef {{key key, Object value}}
  84.      * @return {Object} null if the key is not set, return the original value if it is set.
  85.      */
  86.  
  87.     this.replace = function(key, value) {
  88.         if (map[key]) {
  89.             var old = map[key];
  90.             map[key] = value;
  91.             return old;
  92.         }
  93.         return null;
  94.     }
  95.     this.clone = function() {
  96.         return new HashMap(K,V,map) /* ;p */;
  97.     }
  98.  
  99.     // return map;
  100. }
  101. var map = new HashMap(String, Number);
  102. map.put("test", "banana");
  103. map.get("test");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement