javascriipt HashTable

也不是我原创的,不过是我注释的,很好用的一个HashTable

 


// 转定义 相当与c++ typedef
var  _UNDEFINED_ = " undefined " ;

// HashTable对象
function  Hashtable() 

    
this.count=0;
    
this.content= new Object(); 
    
    
//判断是否定义
    this.defined=function(p)
    
{
        
return typeof(p)!=_UNDEFINED_;
    }

    
    
//添加HashTable 键值
    this.add = function(key,value)
    

        
if(this.contains(key))    return false//如果该关键字已经存在

        
this.content[key]=value;        //设置键值
        this.count++;                   //增加表长度
        return true;             
    }
 
    
    
//移除HashTable 键值
    this.remove= function(key)
    
{
        
if(!this.contains(key))    return//如果该关键字不存在

        
delete this.content[key];       //清空数据
        this.count--;                   //表长度减1
    }
 
    
    
//得到HashTable 键值
    this.items  = function(key)
    
{
        
if(this.contains(key))    return this.content[key];   //如果关键字存在 返回键值

        
return null;
    }
 
    
    
//判断是否存在
    this.contains= function(key)
    

        
return this.defined(this.content[key]);     //判断是否已经定义
    }
 
    
    
//清空HashTable
    this.clear= function()
    
{
        
for(var k in this.content)    delete this.content[k];     //遍历所有 键值 并清空数据
        this.count=0;                               //重置表长度
    }

    
    
//对象转字符串
    this.toString=function()
    
{
        
var sb=new StringBuilder();             //字符串处理对象
        var hasItem=false;                  //是否为第一键值
        
        
for(var k in this.content)          //遍历所有 键值
        {
            
if(hasItem)
                sb.appendFormat(
"&{0}={1}",k,this.content[k]);      //字符串化
            else
            
{
                sb.appendFormat(
"{0}={1}",k,this.content[k]);      
                hasItem
=true;
            }

        }

        
        
return sb.toString();
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值