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 | 实例:
|
12 | HashTable.setDefaultTableSize(50);
|
13 | //哈希槽大小此时为50
|
14 | const hashTable = new HashTable();
|
15 | const tableSize = hashTable.TableSize;
|
16 | console.log(tableSize);
|
17 | // 50
|
18 | ```
|
19 |
|
20 |
|
21 | ### Constructor
|
22 | ##### new HashTable(number size = HashTable.DEFAULT_TABLE_SIZE)
|
23 | ``` text
|
24 | 实例:
|
25 | const hashTable = new HashTable();
|
26 |
|
27 | const array = Array(50).fill(0);
|
28 | const hashTable2 = new HashTable(array.length);
|
29 | ```
|
30 |
|
31 | ### 插入 put
|
32 | ##### HashTable put(any key, T value)
|
33 | ``` text
|
34 | 实例:
|
35 | const hashTable = new HashTable();
|
36 | hashTable.put("1", 1);
|
37 | hashTable.put(2, 2);
|
38 | hashTable.put({key: 3}, 3);
|
39 | hashTable.put(function(){}, 4);
|
40 | 描述:
|
41 | 对象类型通过JSON序列化为字符串
|
42 | ```
|
43 |
|
44 | ### 获取 get
|
45 | ##### T get(any key)
|
46 | ``` text
|
47 | 实例:
|
48 | const hashTable = new HashTable();
|
49 | hashTable.put("1", 1);
|
50 | hashTable.put(2, 2);
|
51 | hashTable.put({key: 3}, 3);
|
52 | hashTable.put(function(){}, 4);
|
53 |
|
54 | hashTable.get("1");
|
55 | hashTable.get(2);
|
56 | hashTable.get({key: 3});
|
57 | hashTable.get(function(){});
|
58 | ```
|
59 | ### 删除 remove
|
60 | ##### bollean remove(any key)
|
61 | ``` text
|
62 | 实例:
|
63 | const hashTable = new HashTable();
|
64 | hashTable.put("1", 1);
|
65 | hashTable.put(2, 2);
|
66 | hashTable.put({key: 3}, 3);
|
67 | hashTable.put(function(){}, 4);
|
68 |
|
69 | hashTable.remove("1");
|
70 | hashTable.remove(2);
|
71 | hashTable.remove({key: 3});
|
72 | hashTable.remove(function(){});
|
73 | ```
|
74 |
|
75 | ### 是否包含指定的key contains
|
76 | ##### bollean contains(any key)
|
77 | ``` text
|
78 | 实例:
|
79 | const hashTable = new HashTable();
|
80 | hashTable.put("1", 1);
|
81 | hashTable.put(2, 2);
|
82 | hashTable.put({key: 3}, 3);
|
83 | hashTable.put(function(){}, 4);
|
84 |
|
85 | hashTable.contains("1");
|
86 | hashTable.contains(2);
|
87 | hashTable.contains({key: 3});
|
88 | hashTable.contains(function(){});
|
89 | ```
|
90 |
|
91 | ### 获取未hash之前的key getKeys
|
92 | ##### string[] getKeys()
|
93 | ``` text
|
94 | 实例:
|
95 | const hashTable = new HashTable();
|
96 | hashTable.put("1", 1);
|
97 | hashTable.put(2, 2);
|
98 | hashTable.put({key: 3}, 3);
|
99 | hashTable.put(function(){}, 4);
|
100 |
|
101 | const keys = hashTable.getKeys();
|
102 | // ["1", "2", "{\"key\":3}","function(){}"]
|
103 | ```
|
104 |
|
105 | ### 获取未序列化之前的key getOrignalKeys
|
106 | ##### string[] getOrignalKeys()
|
107 | ``` text
|
108 | 实例:
|
109 | const hashTable = new HashTable();
|
110 | hashTable.put("1", 1);
|
111 | hashTable.put(2, 2);
|
112 | hashTable.put({key: 3}, 3);
|
113 | hashTable.put(function(){}, 4);
|
114 |
|
115 | const keys = hashTable.getOrignalKeys();
|
116 | // ["1", 2, {key:3},function(){}]
|
117 | ```
|
118 |
|
119 | ### 获取所有的值 values
|
120 | ##### T[] values()
|
121 | ``` text
|
122 | 实例:
|
123 | const hashTable = new HashTable();
|
124 | hashTable.put("1", 1);
|
125 | hashTable.put(2, 2);
|
126 | hashTable.put({key: 3}, 3);
|
127 | hashTable.put(function(){}, 4);
|
128 |
|
129 | const keys = hashTable.values();
|
130 | // [1, 2, 3, 4]
|
131 | ```
|
132 |
|
133 | ### 清空 clear
|
134 | ##### void clear()
|
135 | ``` text
|
136 | 实例:
|
137 | const hashTable = new HashTable();
|
138 | hashTable.put("1", 1);
|
139 | hashTable.put(2, 2);
|
140 | hashTable.put({key: 3}, 3);
|
141 | hashTable.put(function(){}, 4);
|
142 |
|
143 | hashTable.clear();
|
144 | console.log(hashTable.Count);
|
145 | // 0
|
146 | ```
|
147 |
|
148 | ### 获取hash计算后的key getHashKey
|
149 | ##### string getHashKey(any key)
|
150 | ``` text
|
151 | 实例:
|
152 | const hashTable = new HashTable();
|
153 | hashTable.put("1", 1);
|
154 | hashTable.put(2, 2);
|
155 | hashTable.put({key: 3}, 3);
|
156 | hashTable.put(function(){}, 4);
|
157 |
|
158 | hashTable.getHashKey("1");
|
159 | hashTable.getHashKey(2);
|
160 | hashTable.getHashKey({key: 3});
|
161 | hashTable.getHashKey(function(){});
|
162 | // 0
|
163 | ``` |
\ | No newline at end of file |