zoukankan      html  css  js  c++  java
  • js 定义hash类

    // JavaScript Document
    function HashTable(){
        this._hash={};
        this._count=0;
        
        
        /**
        *添加或者更新key
        */
        this.put=function(key,value){
            if(this._hash.hasOwnProperty(key)){
                this._hash[key]=value;
                return true;
            }else{
                    this._hash[key]=value;
                    this._count++;
                    return true;
                }
        }
        
        
        /**
        *获取key指定的值
        */
        this.get=function(key){
            if(this.containsKey(key))
                return this._hash[key];
        }    
        
        
        /**
        *获取元素个数
        */    
        this.size=function(){
            return this._count;
        }
        
        
        /**
        *检查是否为空
        */
        this.isEmpty=function(){
            if(this._count<=0)
                return true;
            else return false;
        }
        
        
        /**
        *检查是否包含指定的key
        */
        this.containsKey=function(){
                return this._hash.hasOwnPropetry(key);
        }
        
        
        /**
        *检查是否包含指定的value
        */
        this.containsValue=function(){
            for(var strKey in this._hash)
                if(this._hash[strKey]==value)
                    return true;                
            return false;
        }
        
        
        /**
        *删除一个key
        */
        this.remove=function(key){
                delete this._hash[key];
                this._count--;
        }
        
        
        /**
        *删除所有的key
        */
        this.clear=function(){
            this._hash={};
            this._count=0;
        }
        
        
        /**
        *从HashTable中获取Key的集合,以数组的形式返回
        */
        this.keySet=function(){
            var arrKeySet= new Array();
            var index=0;
            for(var strKey in this._hash){
                arrKeySet[index++]=strKey;
            }
            return (arrKeySet.length==0)?null:arrKeySet;
        }
        
        
        /**
        *从HashTable中获取Key的集合,以数组的形式返回
        */
        this.values=function(){
            var arrValues= new Array();
            var index=0;
            for(var strKey in this._hash){
                arrValues[index++]=this._hash[strKey];
            }
            return (arrValues.length==0)?null:arrValues;
        }
    }

  • 相关阅读:
    php中常用的4种运行方式
    vue前后端分离项目,使用宝塔面板解决跨域问题,设置Nginx反向代理
    通过 Nginx 代理转发配置实现跨域(API 代理转发)
    ajax跨域,这应该是最全的解决方案了
    vue项目打包之后怎么在本地运行
    webpack打包vue项目之后生成的dist文件该怎么启动运行
    PHP7 windows增加自定义扩展和编译PHP源代码
    编写php自定义扩展
    PHP 扩展开发初探
    php实现伪静态以及定义原理
  • 原文地址:https://www.cnblogs.com/hqu-ye/p/3322155.html
Copyright © 2011-2022 走看看