JavaScript——HashMap實現


本文版權歸博客園和作者吳雙本人共同所有,轉載和爬蟲請注明原文鏈接博客園蝸牛 cnblogs.com\tdws .

首先提供一種獲取hashCode的方法,是一種比較受歡迎的方式,該方法參照了一位園友的文章,鏈接在尾部給出:

 var djb2Code = function (str) {
        var hash = 5381;
        for (i = 0; i < str.length; i++) {
            char = str.charCodeAt(i);
            hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
        }
        return hash;
   }

接下來我們用js實現hashmap, hashmap是一種鍵值對的數據結構。意味着你可以通過key快速找到你所需要查找的值。我使用數組加上LinkedList來實現hashmap,這種方式也被稱為解決hashcode沖突的分離鏈接法。hashmap通常具備以下幾種方法:put,get,remove。put是寫入和修改數據,在put數據時,首先獲取key的hashcode,作為數組的索引。而數組索引對應的值則是一個linkedlist,並且linkedlist所存儲的節點值,同時包含着所需存儲的key和value。這樣以便解決當hashcode重復沖突時,在鏈表中根據key名稱來get查找值。 關於hashmap更多的原理,我推薦這篇文章 http://www.admin10000.com/document/3322.html 

下面直接給出實現,其中使用到LinkedList數據結構的源碼,在我的這篇分享當中:http://www.cnblogs.com/tdws/p/6033209.html

  1   var djb2Code = function (str) {
  2         var hash = 5381;
  3         for (i = 0; i < str.length; i++) {
  4             char = str.charCodeAt(i);
  5             hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
  6         }
  7         return hash;
  8     }
  9 
 10 
 11 
 12     function HashMap() {
 13         var map = [];
 14         var keyValPair = function (key, value) {
 15             this.key = key;
 16             this.value = value;
 17         }
 18         this.put = function (key, value) {
 19             var position = djb2Code(key);
 20             if (map[position] == undefined) {
 21                 map[position] = new LinkedList();
 22             }
 23             map[position].append(new keyValPair(key, value));
 24         },
 25         this.get = function (key) {
 26             var position = djb2Code(key);
 27             if (map[position] != undefined) {
 28                 var current = map[position].getHead();
 29                 while (current.next) {
 30                     if (current.element.key === key) {  //嚴格判斷
 31                         return current.element.value;
 32                     }
 33                     current = current.next;
 34                 }
 35                 if (current.element.key === key) {//如果只有head節點,則不會進while.  還有尾節點,不會進while,這個判斷必不可少
 36                     return current.element.value;
 37                 }
 38             }
 39             return undefined;
 40         },
 41         this.remove = function (key) {
 42             var position = djb2Code(key);
 43             if (map[position] != undefined) {
 44                 var current = map[position].getHead();
 45                 while (current.next) {
 46                     if (current.element.key === key) {
 47                         map[position].remove(current.element);
 48                         if (map[position].isEmpty()) {
 49                             map[position] == undefined;
 50                         }
 51                         return true;
 52                     }
 53                     current = current.next;
 54                 }
 55                 if (current.element.key === key) {
 56                     map[position].remove(current.element);
 57                     if (map[position].isEmpty()) {
 58                         map[position] == undefined;
 59                     }
 60                     return true;
 61                 }
 62             }
 63         }
 64     }
 65 
 66 
 67 
 68 
 69     //鏈表
 70     function LinkedList() {
 71         var Node = function (element) {        //新元素構造
 72             this.element = element;
 73             this.next = null;
 74         };
 75         var length = 0;
 76         var head = null;
 77 
 78         this.append = function (element) {
 79             var node = new Node(element);        //構造新的元素節點
 80             var current;
 81             if (head === null) {             //頭節點為空時  當前結點作為頭節點
 82                 head = node;
 83             } else {
 84                 current = head;
 85                 while (current.next) {          //遍歷,直到節點的next為null時停止循環,當前節點為尾節點
 86                     current = current.next;
 87                 }
 88                 current.next = node;            //將尾節點指向新的元素,新元素作為尾節點
 89             }
 90             length++;                    //更新鏈表長度
 91         };
 92         this.removeAt = function (position) {
 93             if (position > -1 && position < length) {
 94                 var current = head;
 95                 var index = 0;
 96                 var previous;
 97                 if (position == 0) {
 98                     head = current.next;
 99                 } else {
100                     while (index++ < position) {
101                         previous = current;
102                         current = current.next;
103                     }
104                     previous.next = current.next;
105                 }
106                 length--;
107                 return current.element;
108             } else {
109                 return null;
110             }
111         };
112         this.insert = function (position, element) {
113             if (position > -1 && position <= length) {        //校驗邊界
114                 var node = new Node(element);
115                 current = head;
116                 var index = 0;
117                 var previous;
118                 if (position == 0) {                    //作為頭節點,將新節點的next指向原有的頭節點。
119                     node.next = current;
120                     head = node;                        //新節點賦值給頭節點
121                 } else {
122                     while (index++ < position) {
123                         previous = current;
124                         current = current.next;
125                     }                                //遍歷結束得到當前position所在的current節點,和上一個節點
126                     previous.next = node;                    //上一個節點的next指向新節點  新節點指向當前結點,可以參照上圖來看
127                     node.next = current;
128                 }
129                 length++;
130                 return true;
131             } else {
132                 return false;
133             }
134 
135         };
136         this.toString = function () {
137             var current = head;
138             var string = '';
139             while (current) {
140                 string += ',' + current.element;
141                 current = current.next;
142             }
143             return string;
144         };
145         this.indexOf = function (element) {
146             var current = head;
147             var index = -1;
148             while (current) {
149                 if (element === current.element) {            //從頭節點開始遍歷
150                     return index;
151                 }
152                 index++;
153                 current = current.next;
154             }
155             return -1;
156         };
157         this.getLength = function () {
158             return length;
159         };
160         this.getHead = function () {
161             return head;
162         };
163         this.isEmpty = function () {
164             return length == 0;
165         }
166     }

參考文章:js獲取hashcode  : http://www.cnblogs.com/pigtail/p/3342977.html

如果我的點滴分享對你有點滴幫助,歡迎點擊下方紅色按鈕,我將長期輸出分享。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM