UNPKG

3.73 kBMarkdownView Raw
1# 哈希表 HashTable\<T>
2哈希表是根据键直接访问在内存存储位置的数据结构,它通过计算一个关于键值的函数,将所查询的数据映射到表中的一个位置来访问记录,加快访问速度,这种映射函数称为散列函数,存放记录的数组称作散列表。
3
4![HashTable](https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=249bc83ec45c10383073c690d378f876/c9fcc3cec3fdfc035f8e2b9cd63f8794a4c22624.jpg)
5
6## 基本操作的API及示例
7
8### 设置默认槽大小
9##### [s] void setDefaultTableSize(number size)
10``` text
11实例:
12HashTable.setDefaultTableSize(50);
13//哈希槽大小此时为50
14const hashTable = new HashTable();
15const tableSize = hashTable.TableSize;
16console.log(tableSize);
17// 50
18```
19
20
21### Constructor
22##### new HashTable(number size = HashTable.DEFAULT_TABLE_SIZE)
23``` text
24实例:
25const hashTable = new HashTable();
26
27const array = Array(50).fill(0);
28const hashTable2 = new HashTable(array.length);
29```
30
31### 插入 put
32##### HashTable put(any key, T value)
33``` text
34实例:
35const hashTable = new HashTable();
36hashTable.put("1", 1);
37hashTable.put(2, 2);
38hashTable.put({key: 3}, 3);
39hashTable.put(function(){}, 4);
40描述:
41对象类型通过JSON序列化为字符串
42```
43
44### 获取 get
45##### T get(any key)
46``` text
47实例:
48const hashTable = new HashTable();
49hashTable.put("1", 1);
50hashTable.put(2, 2);
51hashTable.put({key: 3}, 3);
52hashTable.put(function(){}, 4);
53
54hashTable.get("1");
55hashTable.get(2);
56hashTable.get({key: 3});
57hashTable.get(function(){});
58```
59### 删除 remove
60##### bollean remove(any key)
61``` text
62实例:
63const hashTable = new HashTable();
64hashTable.put("1", 1);
65hashTable.put(2, 2);
66hashTable.put({key: 3}, 3);
67hashTable.put(function(){}, 4);
68
69hashTable.remove("1");
70hashTable.remove(2);
71hashTable.remove({key: 3});
72hashTable.remove(function(){});
73```
74
75### 是否包含指定的key contains
76##### bollean contains(any key)
77``` text
78实例:
79const hashTable = new HashTable();
80hashTable.put("1", 1);
81hashTable.put(2, 2);
82hashTable.put({key: 3}, 3);
83hashTable.put(function(){}, 4);
84
85hashTable.contains("1");
86hashTable.contains(2);
87hashTable.contains({key: 3});
88hashTable.contains(function(){});
89```
90
91### 获取未hash之前的key getKeys
92##### string[] getKeys()
93``` text
94实例:
95const hashTable = new HashTable();
96hashTable.put("1", 1);
97hashTable.put(2, 2);
98hashTable.put({key: 3}, 3);
99hashTable.put(function(){}, 4);
100
101const keys = hashTable.getKeys();
102// ["1", "2", "{\"key\":3}","function(){}"]
103```
104
105### 获取未序列化之前的key getOrignalKeys
106##### string[] getOrignalKeys()
107``` text
108实例:
109const hashTable = new HashTable();
110hashTable.put("1", 1);
111hashTable.put(2, 2);
112hashTable.put({key: 3}, 3);
113hashTable.put(function(){}, 4);
114
115const keys = hashTable.getOrignalKeys();
116// ["1", 2, {key:3},function(){}]
117```
118
119### 获取所有的值 values
120##### T[] values()
121``` text
122实例:
123const hashTable = new HashTable();
124hashTable.put("1", 1);
125hashTable.put(2, 2);
126hashTable.put({key: 3}, 3);
127hashTable.put(function(){}, 4);
128
129const keys = hashTable.values();
130// [1, 2, 3, 4]
131```
132
133### 清空 clear
134##### void clear()
135``` text
136实例:
137const hashTable = new HashTable();
138hashTable.put("1", 1);
139hashTable.put(2, 2);
140hashTable.put({key: 3}, 3);
141hashTable.put(function(){}, 4);
142
143hashTable.clear();
144console.log(hashTable.Count);
145// 0
146```
147
148### 获取hash计算后的key getHashKey
149##### string getHashKey(any key)
150``` text
151实例:
152const hashTable = new HashTable();
153hashTable.put("1", 1);
154hashTable.put(2, 2);
155hashTable.put({key: 3}, 3);
156hashTable.put(function(){}, 4);
157
158hashTable.getHashKey("1");
159hashTable.getHashKey(2);
160hashTable.getHashKey({key: 3});
161hashTable.getHashKey(function(){});
162// 0
163```
\No newline at end of file