Skip to content

Commit 0ed04f1

Browse files
author
Hugo Venega
committed
Fix hashmap class, remove comments, interface implmemented, use a better hash function
1 parent f03ef9d commit 0ed04f1

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ module.exports = {
1313
project: './tsconfig.json'
1414
},
1515
plugins: ['@typescript-eslint'],
16-
rules: {},
16+
rules: {
17+
'class-methods-use-this': 'off',
18+
},
1719
};

src/hashmap/hashmap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('HashMaP', () => {
1515
});
1616
it('should return the requested item', () => {
1717
hashmap.set('pepe', { n: 12 });
18-
expect(hashmap.get('pepe')).toEqual(['pepe', { n: 12 }]);
18+
expect(hashmap.get('pepe')).toEqual({ key: 'pepe', value: { n: 12 } });
1919
});
2020
it('should return undefined when the key not hashed', () => {
2121
expect(hashmap.get('zapallo')).toEqual(undefined);

src/hashmap/hashmap.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
import BinarySearchTree from '../tree/BST/binarySearchTree';
22

3-
const ARRAY_SIZE = 10;
3+
interface HashMapElement<T>{
4+
key: string,
5+
value?: T
6+
}
7+
8+
const ARRAY_SIZE = 1000;
49

5-
export default class HashMap<T> {
6-
private memory : BinarySearchTree<[string, T | null]>[] = [];
10+
export default class HashMap<T = any> {
11+
private memory : BinarySearchTree<HashMapElement<any>>[] = [];
712

813
public length = 0;
914

10-
private compareFunction = (a:[string, T | null],
11-
b:[string, T | null]): number => a[0].localeCompare(b[0]);
15+
private compareFunction = (a: { key: string },
16+
b:HashMapElement<any>): number => a.key.localeCompare(b.key);
1217

1318
set(key: string, value: T): void {
1419
const placeToAdd = this.hashFunction(key) % ARRAY_SIZE;
1520
if (!this.memory[placeToAdd]) {
16-
this.memory[placeToAdd] = new BinarySearchTree<[string, T | null ]>(this.compareFunction);
21+
this.memory[placeToAdd] = new BinarySearchTree<HashMapElement<any>>(this.compareFunction);
1722
}
18-
this.memory[placeToAdd].add([key, value]);
23+
this.memory[placeToAdd].add({ key, value });
1924
this.length += 1;
2025
}
2126

22-
get(key: string): [string, T | null ] | undefined {
27+
get(key: string): HashMapElement<any> | undefined {
2328
const positionInArray = this.hashFunction(key) % ARRAY_SIZE;
2429
const treeInArray = this.memory[positionInArray];
2530
if (!treeInArray) {
2631
return undefined;
2732
}
28-
const result = treeInArray.search([key, null]);
33+
const result = treeInArray.search({ key });
2934
if (!result) {
3035
return undefined;
3136
}
3237
return result;
3338
}
3439

35-
// eslint-disable-next-line class-methods-use-this
36-
private hashFunction(key:string): number {
37-
return key.length;
40+
private hashFunction(key: string): number {
41+
let hashValue = 0;
42+
const stringKey = key.toString();
43+
44+
for (let index = 0; index < stringKey.length; index += 1) {
45+
const charCode = stringKey.charCodeAt(index);
46+
hashValue += charCode;
47+
}
48+
49+
return hashValue;
3850
}
3951
}

0 commit comments

Comments
 (0)