Skip to content

Commit af0f159

Browse files
author
Wakidur Rahaman
committed
hash table implement
1 parent 2d1825e commit af0f159

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var HashTableMain = /** @class */ (function () {
2+
function HashTableMain(size) {
3+
this.data = [];
4+
this.data = new Array(size);
5+
}
6+
HashTableMain.prototype.hash = function (key) {
7+
var hash = 0;
8+
for (var i = 0; i < key.length; i++) {
9+
hash = (hash + key.charCodeAt(i) * i) % this.data.length;
10+
}
11+
return hash;
12+
};
13+
/**
14+
* set
15+
*/
16+
HashTableMain.prototype.set = function (key, value) {
17+
var address = this.hash(key);
18+
if (!this.data[address]) {
19+
this.data[address] = [];
20+
}
21+
this.data[address].push([key, value]);
22+
return this.data;
23+
};
24+
/**
25+
* Get
26+
*/
27+
HashTableMain.prototype.get = function (key) {
28+
var getAddress = this.hash(key);
29+
var currentBucket = this.data[getAddress];
30+
if (currentBucket) {
31+
for (var i = 0; i < currentBucket.length; i++) {
32+
if (currentBucket[i][0] === key) {
33+
return currentBucket[i][1];
34+
}
35+
}
36+
}
37+
return undefined;
38+
};
39+
return HashTableMain;
40+
})();
41+
// Example
42+
var myHashTable01 = new HashTableMain(50);
43+
myHashTable01.set('grapes', 10000);
44+
myHashTable01.get('grapes');
45+
myHashTable01.set('apples', 9);
46+
myHashTable01.get('apples');
47+
myHashTable01.set('banana', 'Nice foots');
48+
myHashTable01.get('banana');
49+
50+
console.log(myHashTable01.data);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class HashTableMain {
2+
private data: [] | [string, number | string][][] = [];
3+
constructor(size: number) {
4+
this.data = new Array(size);
5+
}
6+
7+
private hash(key: string): number {
8+
let hash: number = 0;
9+
for (let i = 0; i < key.length; i++) {
10+
hash = (hash + key.charCodeAt(i) * i) % this.data.length;
11+
}
12+
return hash;
13+
}
14+
15+
/**
16+
* set
17+
*/
18+
public set(key: string, value: number | string) {
19+
let address = this.hash(key);
20+
if (!this.data[address]) {
21+
this.data[address] = [];
22+
}
23+
this.data[address].push([key, value]);
24+
return this.data;
25+
}
26+
27+
/**
28+
* Get
29+
*/
30+
public get(key: string): undefined | string | number {
31+
const getAddress = this.hash(key);
32+
const currentBucket = this.data[getAddress];
33+
34+
if (currentBucket) {
35+
for (let i = 0; i < currentBucket.length; i++) {
36+
if (currentBucket[i][0] === key) {
37+
return currentBucket[i][1];
38+
}
39+
}
40+
}
41+
return undefined;
42+
}
43+
}
44+
45+
// Example
46+
const myHashTable01 = new HashTableMain(50);
47+
myHashTable01.set('grapes', 10000);
48+
myHashTable01.get('grapes');
49+
myHashTable01.set('apples', 9);
50+
myHashTable01.get('apples');
51+
myHashTable01.set('banana', 'Nice foots');
52+
myHashTable01.get('banana');

0 commit comments

Comments
 (0)