锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
/*
name:聽聽聽 Map.js
author:聽 WindDC
date:聽聽聽 2006-10-27
content: 鏈▼搴忕敤JS瀹炵幇綾繪嫙JAVA涓璏AP瀵瑰儚鐨勫姛鑳?br />*/
function Node(key,value){//閿煎瀵硅薄
聽聽聽 this.key=key;
聽聽聽 this.value=value;
}
function Map(){//Map綾?br />聽聽聽 this.nodes=new Array();
}
Map.prototype.put=function(key,value){//寰瀹瑰櫒涓姞鍏ヤ竴涓敭鍊煎
聽聽聽聽聽聽聽 for(var i=0;i<this.nodes.length;i++)
聽聽聽聽聽聽聽聽聽聽 if(this.nodes[i].key==key){//濡傛灉閿煎凡瀛樺湪錛屽垯put鏂規硶涓烘洿鏂板凡鏈夋暟鎹?br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽 this.nodes[i].value=value;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 return;
聽聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽 var node=new Node(key,value);
聽聽聽聽聽聽聽 this.nodes.push(node);
聽聽聽聽聽聽聽 return;
}//put
聽聽
Map.prototype.get=function(key){//鑾峰彇鎸囧畾閿殑鍊?br />聽聽聽聽聽聽聽 for(var i=0;i<this.nodes.length;i++)
聽聽聽聽聽聽聽聽聽聽 if(this.nodes[i].key==key)
聽聽聽聽聽聽聽聽聽聽聽聽聽 return this.nodes[i].value;
聽聽聽聽聽聽聽 return null;
}//get
聽聽聽聽
Map.prototype.size=function(){//鑾峰彇瀹瑰櫒涓璞$殑涓暟
聽聽聽 聽return this.nodes.length;
}//size
聽聽聽 聽聽聽聽
Map.prototype.clear=function(){//娓呯┖瀹瑰櫒
聽聽聽 聽while(this.nodes.length>0)
聽聽聽 聽聽聽 this.nodes.pop();聽聽聽聽聽
}//clear
聽
Map.prototype.remove=function(key){//鍒犻櫎鎸囧畾鍊?br />聽聽聽 聽for(var i=0;i<this.nodes.length;i++)
聽聽聽 聽聽聽 if(this.nodes[i].key==key){
聽聽聽 聽聽聽 聽聽 if(i>0)
聽聽聽 聽聽聽聽聽聽聽聽聽 var nodes1=this.nodes.concat(this.nodes.slice(0,i-1),this.nodes.slice(i+1));
聽聽聽 聽聽聽聽聽聽 else//鍒犻櫎鐨勬槸絎竴涓厓绱?br />聽聽聽 聽聽聽聽聽聽 聽 var nodes1=nodes.slice(1);
聽聽聽 聽聽聽聽聽聽 this.nodes=nodes1;
聽聽聽 聽聽聽 }
}//remove
聽聽聽
Map.prototype.isEmpty=function(){//鏄惁涓虹┖
聽聽聽 聽if(this.nodes.length==0)
聽聽聽 聽聽 return true;
聽聽聽 聽else
聽聽聽 聽聽 return false;
}//isEmpty
聽聽聽
Map.prototype.toString=function(){
聽聽聽聽 var str="[";
聽聽聽聽 for(var i=0;i<this.nodes.length;i++){
聽聽聽聽聽聽聽 if(i<this.nodes.length-1)
聽聽聽聽聽聽聽聽聽聽 str=str+this.nodes[i].key+",";
聽聽聽聽聽聽 else
聽聽聽聽聽聽聽聽聽聽 str=str+this.nodes[i].key;聽聽聽聽
聽聽 聽}
聽聽聽 str=str+"]";
聽聽聽聽return str;
}