亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索

怎樣利用JS自定義哈希表和順序列表

php中世界最好的語(yǔ)言
發(fā)布: 2018-06-04 13:40:56
原創(chuàng)
1681人瀏覽過(guò)

/**哈希表*/
var HashMap = function(){
	this.table={};
 	this.count=0;//長(zhǎng)度

 	/**添加對(duì)象
	* @param key 鍵
	* @param obj 對(duì)象
	*/
	this.put=function(key,obj){
	    var v=this.table[key];
	    if(typeof(v)=="undefined")this.count+=1;
	    this.table[key]=obj;
	};
	/**獲得對(duì)象
	* @param key 鍵
	* @return obj 對(duì)象
	*/
	this.get=function(key){
	    return this.table[key];
	};
	
	this.contains=function(key){
		return (key in this.table);
	};
	/**刪除對(duì)象
	* @param key 鍵
	* @return obj 對(duì)象
	*/
	this.remove=function(key){
	    var obj=this.table[key];
	    if(obj != undefined){
		    delete this.table[key];
		    this.count -= 1;
	    }
	    return obj;
	};
	/**清空所有對(duì)象*/
	this.clear=function(){
	    this.table={};
 	    this.count=0;
	};
	/**當(dāng)前大小*/
	this.size=function(){
		return this.count;
	};
	/**是否為空*/
	this.isEmpty=function(){
		return this.count <= 0;
	};
	/**獲得所有值*/
	this.values=function(){
		var values = new Array();
		for(var prop in this.table){
		 	values.push(this.table[prop]);
		}
		return values;
	};
	/**獲得所有鍵*/
	this.keys=function(){
		var keys = new Array();
		for(var prop in this.table){
		 	keys.push(prop);
		}
		return keys;
	};
};

/**順序鏈表*/
var ArrayList = function(){
	this.count=0;//長(zhǎng)度
 	this.initialCapacity = 10;
	this.list=new Array(this.initialCapacity);

	/**添加對(duì)象
	* @param obj 對(duì)象
	*/
	this.add=function(obj){
        this.list[this.count]=obj;
        this.count+=1;
	};
	/**按索引設(shè)置對(duì)象
	* @param obj 對(duì)象
	*/
	this.set=function(index,obj){
		if(this.list.length<index){
			this.list[index]=obj;
			return true;
		}else return false;
	};
	/**獲得對(duì)象
	* @param index 索引位
	* @return obj 對(duì)象
	*/
	this.get=function(index){
	    return this.list[index];
	};
	/**刪除對(duì)象
	* @param index 索引位
	* @return obj 對(duì)象
	*/
	this.remove=function(index){
	    var obj=null;
	    if(index>=0&&index<this.count){
	  	    obj = this.list[index];
            for (var i = index; i < this.count - 1; i++) {
                this.list[i] = this.list[i + 1];
            }
            this.list[this.count-1] = null;
            this.count-=1; 
	    }
	    return obj;
	};
	/**刪除一組對(duì)象
	* @param index  索引位
	* @param length 長(zhǎng)度
	*/
	this.removeGroup=function(index,length){
	    if(index>=0){
		    var start=index;var end=index+length;
	  	    var counts=0;var lists=[];
            for (var i = 0; i < this.count; i++) {
        	    if(i>=start&&i<end)continue;
        	    lists[lists.length] = this.list[i];
        	    counts++;
            }
            this.list=lists;
            this.count=counts; 
	    }
	};
	/**清空所有對(duì)象*/
	this.clear=function(){
	    this.count=0;   
	    this.list=[];
	};
	/**獲得鏈表大小
	* @return 返回大小
	*/
	this.size=function(){
	    return this.count;
	};
	this.getList=function(){
		return this.list;
	};
	this.setList=function(list){
		this.list=list;
		this.count=list.length;
	};
	/**折半查找
	* @param value 對(duì)比的數(shù)據(jù)
	* @return fun  對(duì)比方法
	* @return 返回位置,-1為沒(méi)找到
	*/
	this.bisearch=function(value,fun){
	   var pos=-1;
       var start = 0;
       var end = this.count-1;
       var current = parseInt(this.count/2);
       for(var i=0;i<current;i++){
       	 if(!fun(value,this.list[start])){
       		start =start+1;
       	 }else{
       	 	pos=start;break;
       	 }
       	 if(!fun(value,this.list[current])){
       		current =current+1;if(current>end)break;
       	 }else{
       	 	pos=current;break;
       	 }
       }
       return pos;
	};
	/**順序查找
	* @param value 對(duì)比的數(shù)據(jù)
	* @return fun  對(duì)比方法
	* @return 返回位置,-1為沒(méi)找到
	*/
	this.search=function(value,fun){
	   var pos=-1;
       for(var i=0;i<this.count;i++){
       	 if(fun(value,this.list[i])){
       	 	pos=i;break;
       	 }
       }
       return pos;
	};
};
登錄后復(fù)制

相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

推薦閱讀:

移動(dòng)端WEB開(kāi)發(fā)中click,touch,tap事件使用詳解

怎樣操作頁(yè)面、可視區(qū)、屏幕等寬高屬性

愛(ài)圖表
愛(ài)圖表

AI驅(qū)動(dòng)的智能化圖表創(chuàng)作平臺(tái)

愛(ài)圖表99
查看詳情 愛(ài)圖表

以上就是怎樣利用JS自定義哈希表和順序列表的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

相關(guān)標(biāo)簽:
最佳 Windows 性能的頂級(jí)免費(fèi)優(yōu)化軟件
最佳 Windows 性能的頂級(jí)免費(fèi)優(yōu)化軟件

每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。

下載
來(lái)源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問(wèn)題
開(kāi)源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線(xiàn)php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)