-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Hash table | ||
|
||
A [hash table](https://en.wikipedia.org/wiki/Hash_table) s a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. | ||
A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found. | ||
|
||
A hash table implemented here has following methods: | ||
|
||
- `get(key)` - returns value by provided `key`. Returns `null` if no `key` found. | ||
- `set(key, value)` - adds new key/value pair into hash table. | ||
- `remove(key)` - removes key/value pair from hash table by provided `key` and returns `true`. Return `false` if no `key` found. | ||
- `_hash(key)` - private method that transforms `key` into an index. |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
interface IHashTable { | ||
get(key: string): unknown; | ||
set(key: string, value: unknown): void; | ||
remove(key: string): boolean; | ||
} | ||
|
||
class HashTable implements IHashTable { | ||
private table: (Array<unknown> | undefined)[]; | ||
public size: number; | ||
|
||
constructor(size: number) { | ||
this.table = new Array(size); | ||
this.size = 0; | ||
} | ||
|
||
private hash(key: string): number { | ||
let total = 0; | ||
|
||
for (let i = 0; i < key.length; i++) { | ||
total += key.charCodeAt(i); | ||
} | ||
|
||
return total % this.size; | ||
} | ||
|
||
public get(key: string): unknown { | ||
const index = this.hash(key); | ||
|
||
return this.table[index]?.[1] ?? null; | ||
} | ||
|
||
public set(key: string, value: unknown) { | ||
const index = this.hash(key); | ||
|
||
this.table[index] = [key, value]; | ||
this.size++; | ||
} | ||
|
||
public remove(key: string) { | ||
const index = this.hash(key); | ||
|
||
if (this.table[index]?.length) { | ||
this.table[index] = undefined; | ||
this.size--; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
export default HashTable; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import HashTable from '..'; | ||
|
||
/** Test cases on get() and set() method */ | ||
describe('HashTable:', () => { | ||
describe('get() and set() methods', () => { | ||
test('should get correct values', () => { | ||
const hashTable = new HashTable(3); | ||
|
||
hashTable.set("Canada", 300); | ||
hashTable.set("France", 100); | ||
hashTable.set("Spain", 110); | ||
|
||
expect(hashTable.get("Canada")).toBe(300); | ||
expect(hashTable.get("France")).toBe(100); | ||
expect(hashTable.get("Spain")).toBe(110); | ||
expect(hashTable.get("Germany")).toBe(null); | ||
}); | ||
}); | ||
|
||
describe('remove() methods', () => { | ||
test('should remove correct values', () => { | ||
const hashTable = new HashTable(3); | ||
|
||
hashTable.set("Andrew", 300); | ||
hashTable.set("Florence", 100); | ||
hashTable.set("Paul", 110); | ||
|
||
expect(hashTable.remove("Canada")).toBeTruthy(); | ||
expect(hashTable.remove("Florence")).toBeTruthy(); | ||
expect(hashTable.remove("Paul")).toBeTruthy(); | ||
expect(hashTable.remove("Kate")).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains 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