乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      javascript 實現(xiàn)java中的Map (高效)

       CevenCheng 2012-05-24
      javascript實現(xiàn)java中的Map,代碼是在國外的一個網(wǎng)站上看到的(http:///questions/368280/javascript-hashmap-equivalent),自己稍作了修改,之前也看到過有人用2個數(shù)組實現(xiàn)了Map,但是我感覺效率比較低,現(xiàn)在這個我感覺效率還可以,自己做了下測試,代碼如下: 
      Map.js 
      Java代碼  收藏代碼
      1. function Map(linkItems) {   
      2.     this.current = undefined;   
      3.     this._size = 0;   
      4.     if(linkItems === false){  
      5.         this.disableLinking();   
      6.     }   
      7. }  
      8. /** 
      9.  * 獲取當前map 
      10.  * @return 當前對象 
      11.  */  
      12. Map.noop = function() {   
      13.     return this;   
      14. };   
      15. /** 
      16.  * 非法操作 
      17.  * @return 
      18.  */  
      19. Map.illegal = function() {   
      20.     throw new Error("非法操作,Map已經(jīng)被禁用");   
      21. };   
      22. /** 
      23.  *  
      24.  * @param obj 
      25.  * @param foreignKeys 
      26.  * @return 
      27.  */  
      28. Map.from = function(obj, foreignKeys) {   
      29.     var map = new Map;   
      30.     for(var prop in obj) {   
      31.         if(foreignKeys || obj.hasOwnProperty(prop)){  
      32.             map.put(prop, obj[prop]);   
      33.         }   
      34.     }   
      35.     return map;   
      36. };   
      37. /** 
      38.  * 禁用map 
      39.  * @return 
      40.  */  
      41. Map.prototype.disableLinking = function() {   
      42.     this.link = Map.noop;   
      43.     this.unlink = Map.noop;   
      44.     this.disableLinking = Map.noop;   
      45.     this.next = Map.illegal;   
      46.     this.key = Map.illegal;   
      47.     this.value = Map.illegal;   
      48.     this.clear = Map.illegal;   
      49.     return this;   
      50. };   
      51. /** 
      52.  * 返回hash值 例如:number 123 
      53.  * @param value key/value 
      54.  * @return 
      55.  */  
      56. Map.prototype.hash = function(value) {   
      57.     return (typeof value) + ' ' + (value instanceof Object ? (value.__hash || (value.__hash = ++arguments.callee.current)) : value.toString());   
      58. };   
      59. /** 
      60.  * 返回map的長度 
      61.  * @return 
      62.  */  
      63. Map.prototype.size = function() {   
      64.     return this._size;  
      65. };   
      66.   
      67. Map.prototype.hash.current = 0;   
      68. /** 
      69.  * 通過key獲取value 
      70.  * @param key 
      71.  * @return 
      72.  */  
      73. Map.prototype.get = function(key) {   
      74.     var item = this[this.hash(key)];   
      75.     return item === undefined ? undefined : item.value;   
      76. };   
      77. /** 
      78.  * 向map中添加數(shù)據(jù) 
      79.  * @param key 鍵 
      80.  * @param value 值 
      81.  * @return 
      82.  */  
      83. Map.prototype.put = function(key, value) {   
      84.     var hash = this.hash(key);   
      85.     if(this[hash] === undefined) {   
      86.         var item = { key : key, value : value };   
      87.         this[hash] = item;   
      88.         this.link(item);   
      89.         ++this._size;   
      90.     }else{  
      91.         this[hash].value = value;  
      92.     }   
      93.     return this;   
      94. };   
      95. /** 
      96.  * 通過key刪除數(shù)據(jù) 
      97.  * @param key 
      98.  * @return 
      99.  */  
      100. Map.prototype.remove = function(key) {   
      101.     var hash = this.hash(key);   
      102.     var item = this[hash];   
      103.     if(item !== undefined) {   
      104.         --this._size;   
      105.         this.unlink(item);   
      106.         delete this[hash];   
      107.     }   
      108.     return this;   
      109. };   
      110. /** 
      111.  * 清除map 
      112.  * @return 
      113.  */  
      114. Map.prototype.clear = function() {   
      115.     while(this._size){  
      116.         this.remove(this.key());   
      117.     }   
      118.     return this;   
      119. };   
      120. /** 
      121.  * 處理隊列 
      122.  * @param item 
      123.  * @return 
      124.  */  
      125. Map.prototype.link = function(item) {   
      126.     if(this._size == 0) {   
      127.         item.prev = item;   
      128.         item.next = item;   
      129.         this.current = item;   
      130.     }else {   
      131.         item.prev = this.current.prev;   
      132.         item.prev.next = item;   
      133.         item.next = this.current;   
      134.         this.current.prev = item;  
      135.     }   
      136. };   
      137. Map.prototype.unlink = function(item) {   
      138.     if(this._size == 0){   
      139.         this.current = undefined;  
      140.     }else {   
      141.         item.prev.next = item.next;   
      142.         item.next.prev = item.prev;   
      143.         if(item === this.current){  
      144.             this.current = item.next;   
      145.         }   
      146.     }   
      147. };   
      148. /** 
      149.  * 獲取下一個 
      150.  * @return 
      151.  */  
      152. Map.prototype.next = function() {   
      153.     this.current = this.current.next;   
      154.     return this;  
      155. };   
      156. /** 
      157.  * 獲取key 
      158.  * @return 
      159.  */  
      160. Map.prototype.key = function() {   
      161.     return this.current.key;   
      162. };   
      163. /** 
      164.  * 獲取value 
      165.  * @return 
      166.  */  
      167. Map.prototype.value = function() {   
      168.     return this.current.value;   
      169. };   

      測試代碼如下: 
      Java代碼  收藏代碼
      1. var l=10000;  
      2.     var map=new Map();  
      3.     var start=new Date().getTime();  
      4.     for(var i=0;i<l;i++){  
      5.         map.put("key_"+i,new Date());  
      6.     }  
      7.     var end=new Date().getTime();  
      8.     document.write("向map中添加了  "+l+" 個Date對象..........");  
      9.     document.write("<br/>");  
      10.     document.write("耗時  "+(end-start)+" 毫秒,map的長度為:"+map.size());  
      11.     document.write("<br/>");  
      12.     document.write("在map中提取全部數(shù)據(jù)..........");  
      13.     document.write("<br/>");  
      14.     start=new Date().getTime();  
      15.     for(var i=0;i<map.size();i++){  
      16.         map.get("key_"+i).getTime();  
      17.     }  
      18.     end=new Date().getTime();  
      19.     document.write("耗時  "+(end-start)+" 毫秒");  
      20.     document.write("<br/>");  
      21.     document.write("清空map..........");  
      22.     document.write("<br/>");  
      23.     start=new Date().getTime();  
      24.     map.clear();  
      25.     end=new Date().getTime();  
      26.     document.write("耗時  "+(end-start)+" 毫秒,map的長度為:"+map.size());  
      27.     document.write("<br/>");  

      測試結(jié)果如下: 
      1.IE7 

       

      2.Firefox 3.6.8 

       

      3.谷歌瀏覽器5.0 

       

      方法next的使用: 
      Java代碼  收藏代碼
      1. var map=new Map();  
      2.     map.put("key_1","value_1");  
      3.     map.put("key_2","value_2");  
      4.     map.put("key_3","value_3");  
      5.     var m=map.next();  
      6.     document.write("map.next:key="+m.key()+" value="+m.value());  
      7.     document.write("<br/>");  
      8.     m=map.next();  
      9.     document.write("map.next:key="+m.key()+" value="+m.value());  

      結(jié)果如下: 
      Java代碼  收藏代碼
      1. map.next:key=key_2 value=value_2  
      2. map.next:key=key_3 value=value_3   

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多