js实现HashMap功能代码

来源:互联网 发布:国家旅游局2015年数据 编辑:程序博客网 时间:2024/05/16 08:14
/*** * js 实现hashmap  功能   * 常用功能实现 * @return */function HashMap(){this.length = 0;this.maxLength = Number.MAX_VALUE;this.container = {};}HashMap.prototype.put = function(objName,objValue){try{if(this.length >= this.maxLength)throw new Error("[Error HashMap] : Map Datas' count overflow !");if(objName != ""){for(var p in this.container){if(p == objName){this.container[objName] = objValue;return ;}}this.container[objName] = objValue;this.length ++ ;}}catch(e){return e;}};HashMap.prototype.get = function(objName){try{if(this.container[objName])return this.container[objName];}catch(e){return e;}};HashMap.prototype.contains = function(objValue){try{for(var p in this.container){if(this.container[p] === objValue)return true;}return false;}catch(e){return e;}};HashMap.prototype.remove = function(objName){try{if(this.container[objName]){delete this.container[objName];this.length -- ;}}catch(e){return e;}};HashMap.prototype.pop = function(objName){try{var ov = this.container[objName];if(ov){delete this.container[objName];this.length -- ;return ov;}return null;}catch(e){return e;}};HashMap.prototype.removeAll = function(){try{this.clear();}catch(e){return e;}};HashMap.prototype.clear = function(){try{delete this.container;this.container = {};this.length = 0;}catch(e){return e;}};HashMap.prototype.isEmpty = function(){if(this.length === 0)return true;elsereturn false;};HashMap.prototype.runIn = function(fun){try{if(!fun)throw new Error("[Error HashMap] : The paramer is null !");for(var p in this.container){var ov = this.container[p];fun(ov);}}catch(e){return e;}};
原创粉丝点击