Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const sha256 = require('js-sha256');
- class KeyValuePair {
- constructor(key, value) {
- this.key = key;
- this.value = value;
- this.next = null;
- }
- }
- class HashTable {
- constructor(numBuckets = 4) {
- this.count = 0; //Number of elements in array
- this.capacity = numBuckets; //How many items the table can hold
- this.data = new Array(numBuckets) //Create array to hold values
- //Fill array with null values
- for(let i = 0; i < this.data.length; i++){
- this.data[i] = null;
- }
- }
- hash(key) {
- let rawValue = sha256(key); //Generate sha256 hash for key
- let firstEight = rawValue.slice(0,8) //Remove the first eight hex elements
- let returnValue = parseInt(firstEight, 16); //Convert the hex elements into int
- return returnValue; //Return int hash
- }
- hashMod(key) {
- let returnValue = this.hash(key) % this.capacity; //Make sure the return index is in array
- return returnValue;
- }
- insertNoCollisions(key, value) {
- const index = this.hashMod(key); //Generate a key to place the value
- let keyToAdd = new KeyValuePair(key,value);
- //If the index already has an element, throw error, if not add it
- if(this.data[index] !== null){
- throw new Error("hash collision or same key/value pair already exists!")
- }
- else{
- this.data[index] = keyToAdd;
- this.count++;
- }
- }
- insertWithHashCollisions(key, value) {
- const index = this.hashMod(key);
- let keyToAdd = new KeyValuePair(key, value);
- //This time, if the index is occupied create a linked list
- if(this.data[index] !== null){
- this.head = this.data[index];
- keyToAdd.next = this.head;
- this.data[index] = keyToAdd;
- }
- else{
- this.data[index] = keyToAdd;
- }
- this.count++;
- }
- insert(key, value) {
- const index = this.hashMod(key); //Generate index for node
- if(this.containsTargetKey(key)) { //Check for existence of key
- //If the key exists, start at the head of list and iterate through to find it
- let currentNode = this.data[index];
- while (currentNode) {
- if (currentNode.key === key) {
- currentNode.value = value; // Update the value of the existing key
- return;
- }
- currentNode = currentNode.next;
- }
- }
- else {
- let newNode = new KeyValuePair(key, value);
- //Set the next pointer to the existing head at the index if there is one, null otherwise
- newNode.next = this.data[index];
- this.data[index] = newNode; //Add the new node
- this.count++; //Increase count
- }
- }
- //Check hashmap to see if any objects at the index have a matching key.
- containsTargetKey(key) {
- const index = this.hashMod(key); //The key should have the same index
- let currentNode = this.data[index];
- //Transverse linked list in case of collision to see if any of the keys match
- while (currentNode) {
- if (currentNode.key === key) {
- return true;
- }
- currentNode = currentNode.next;
- }
- return false;
- }
- print() {
- for (let i = 0; i < this.data.length; i++) {
- let currentNode = this.data[i];
- let bucketStr = `Bucket ${i}: `;
- while (currentNode) {
- bucketStr += `{ Key: ${currentNode.key}, Value: ${currentNode.value} } -> `;
- currentNode = currentNode.next;
- }
- console.log(bucketStr + 'null');
- }
- }
- }
- let hashTable = new HashTable(2);
- hashTable.insert("key-1", "val-1");
- hashTable.insert("key-2", "val-2");
- //hashTable.insert("key-3", "val-3");
- hashTable.insert("key-1", "val-100000");
- hashTable.print();
- module.exports = HashTable;
Advertisement
Add Comment
Please, Sign In to add comment