Last active
January 31, 2019 10:37
-
-
Save rxluz/e1827719447265435f7576c7bf17349b to your computer and use it in GitHub Desktop.
JS Data Structures: Hash tables, see more at: https://medium.com/p/e3c229ecaacb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function HashTable() { | |
let table = [] | |
let size = 0 | |
class Utils { | |
static djb2(key) { | |
let hash = 5381 | |
const keyChars = key.split('') | |
keyChars.forEach(chr => (hash = hash * 33 + chr.charCodeAt(0))) | |
return hash % 1013 | |
} | |
static item(key, value) { | |
return { | |
key, | |
value, | |
toString: () => `['${key}', '${value}']`, | |
} | |
} | |
static addComma(content) { | |
return content !== '' ? ', ' : '' | |
} | |
} | |
class PublicHashTable { | |
put(key, value) { | |
let index = Utils.djb2(key) | |
while (table[index] && table[index].key !== key) { | |
index++ | |
} | |
if (!table[index]) { | |
size++ | |
} | |
table[index] = Utils.item(key, value) | |
return true | |
} | |
get(key) { | |
if (this.isEmpty()) return undefined | |
let index = Utils.djb2(key) | |
while (table[index] && table[index].key !== key) { | |
index++ | |
} | |
if (table[index] && table[index].key === key) { | |
return table[index].value | |
} | |
return undefined | |
} | |
remove(key) { | |
if (this.isEmpty()) return false | |
let index = Utils.djb2(key) | |
while (table[index] && table[index].key !== key) { | |
index++ | |
} | |
if (table[index] && table[index].key === key) { | |
delete table[index] | |
size-- | |
return true | |
} | |
return true | |
} | |
has(key) { | |
if (this.isEmpty()) return false | |
let index = Utils.djb2(key) | |
while (table[index] && table[index].key !== key) { | |
index++ | |
} | |
if (table[index] && table[index].key === key) { | |
return true | |
} | |
return false | |
} | |
keys() { | |
let keysList = [] | |
if (this.isEmpty()) return keysList | |
table.forEach(item => keysList.push(item.key)) | |
return keysList | |
} | |
values() { | |
let valuesList = [] | |
if (this.isEmpty()) return valuesList | |
table.forEach(item => valuesList.push(item.value)) | |
return valuesList | |
} | |
toString() { | |
let content = '' | |
table.forEach(item => { | |
const comma = Utils.addComma(content) | |
content += `${comma}${item.toString()}` | |
}) | |
return content | |
} | |
size() { | |
return size | |
} | |
isEmpty() { | |
return this.size() === 0 | |
} | |
clear() { | |
table = [] | |
size = 0 | |
} | |
} | |
return new PublicHashTable() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment