This commit is contained in:
Vergil
2020-04-20 09:39:00 +08:00
parent 633374325c
commit 126b272afd

View File

@@ -73,3 +73,66 @@ export function ExecDatabase(num) {
}) })
} }
// end---> // end--->
// 哈希表
export function HashTable() {
let size = 0
// eslint-disable-next-line no-new-object
let entry = new Object()
this.add = function(key, value) {
if (!this.containsKey(key)) {
size++
}
entry[key] = value
}
this.getValue = function(key) {
return this.containsKey(key) ? entry[key] : null
}
this.remove = function(key) {
if (this.containsKey(key) && (delete entry[key])) {
size--
}
}
this.containsKey = function(key) {
return (key in entry)
}
this.containsValue = function(value) {
for (const prop in entry) {
if (entry[prop] === value) {
return true
}
}
return false
}
this.getValues = function() {
// eslint-disable-next-line no-array-constructor
const values = new Array()
for (const prop in entry) {
values.push(entry[prop])
}
return values
}
// this.getKey = function(value) {
// for (const prop in entry) {
// if (entry[key]) {
// return prop
// }
// }
// }
this.getKeys = function() {
// eslint-disable-next-line no-array-constructor
const keys = new Array()
for (const prop in entry) {
keys.push(prop)
}
return keys
}
this.getSize = function() {
return size
}
this.clear = function() {
size = 0
// eslint-disable-next-line no-new-object
entry = new Object()
}
}