From b99a977f01428ad9c4ded8c99c81b350e724f47d Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 14:17:07 -0300 Subject: [PATCH 1/2] Add hashmap --- src/hashmap/hashmap.spec.ts | 27 ++++++++++++++++++++ src/hashmap/hashmap.ts | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/hashmap/hashmap.spec.ts create mode 100644 src/hashmap/hashmap.ts diff --git a/src/hashmap/hashmap.spec.ts b/src/hashmap/hashmap.spec.ts new file mode 100644 index 0000000..1ff4d23 --- /dev/null +++ b/src/hashmap/hashmap.spec.ts @@ -0,0 +1,27 @@ +import HashMap from './hashmap'; + +interface TestElement { + n : number +} +describe('HashMaP', () => { + let hashmap: HashMap; + beforeEach(() => { + hashmap = new HashMap (); + }); + + it('should add an element into the Hashmap', () => { + hashmap.set('pepe', { n: 12 }); + expect(hashmap.length).toEqual(1); + }); + it('should return the requested item', () => { + hashmap.set('pepe', { n: 12 }); + expect(hashmap.get('pepe')).toEqual({ key: 'pepe', value: { n: 12 } }); + }); + it('should return undefined when the key not hashed', () => { + expect(hashmap.get('zapallo')).toEqual(undefined); + }); + it('returns undefined when the key collides but is not found', () => { + hashmap.set('pepe', { n: 12 }); + expect(hashmap.get('coco')).toEqual(undefined); + }); +}); diff --git a/src/hashmap/hashmap.ts b/src/hashmap/hashmap.ts new file mode 100644 index 0000000..4a92722 --- /dev/null +++ b/src/hashmap/hashmap.ts @@ -0,0 +1,51 @@ +import BinarySearchTree from '../tree/BST/binarySearchTree'; + +interface HashMapElement{ + key: string, + value?: T +} + +const ARRAY_SIZE = 1000; + +export default class HashMap { + private memory : BinarySearchTree>[] = []; + + public length = 0; + + private compareFunction = (a: { key: string }, + b:HashMapElement): number => a.key.localeCompare(b.key); + + set(key: string, value: T): void { + const placeToAdd = this.hashFunction(key) % ARRAY_SIZE; + if (!this.memory[placeToAdd]) { + this.memory[placeToAdd] = new BinarySearchTree>(this.compareFunction); + } + this.memory[placeToAdd].add({ key, value }); + this.length += 1; + } + + get(key: string): HashMapElement | undefined { + const positionInArray = this.hashFunction(key) % ARRAY_SIZE; + const treeInArray = this.memory[positionInArray]; + if (!treeInArray) { + return undefined; + } + const result = treeInArray.search({ key }); + if (!result) { + return undefined; + } + return result; + } + + private hashFunction(key: string): number { + let hashValue = 0; + const stringKey = key.toString(); + + for (let index = 0; index < stringKey.length; index += 1) { + const charCode = stringKey.charCodeAt(index); + hashValue += charCode; + } + + return hashValue; + } +} From 6444dc2b3082d0493ff6b0ad2a80ac2d78e79579 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 14:51:08 -0300 Subject: [PATCH 2/2] Fix hashmap default type and tests --- src/hashmap/hashmap.spec.ts | 5 ++++- src/hashmap/hashmap.ts | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hashmap/hashmap.spec.ts b/src/hashmap/hashmap.spec.ts index 1ff4d23..07ae9c5 100644 --- a/src/hashmap/hashmap.spec.ts +++ b/src/hashmap/hashmap.spec.ts @@ -13,14 +13,17 @@ describe('HashMaP', () => { hashmap.set('pepe', { n: 12 }); expect(hashmap.length).toEqual(1); }); + it('should return the requested item', () => { hashmap.set('pepe', { n: 12 }); expect(hashmap.get('pepe')).toEqual({ key: 'pepe', value: { n: 12 } }); }); + it('should return undefined when the key not hashed', () => { expect(hashmap.get('zapallo')).toEqual(undefined); }); - it('returns undefined when the key collides but is not found', () => { + + it('shoud return undefined when the key collides but is not found', () => { hashmap.set('pepe', { n: 12 }); expect(hashmap.get('coco')).toEqual(undefined); }); diff --git a/src/hashmap/hashmap.ts b/src/hashmap/hashmap.ts index 4a92722..63a43ac 100644 --- a/src/hashmap/hashmap.ts +++ b/src/hashmap/hashmap.ts @@ -8,23 +8,23 @@ interface HashMapElement{ const ARRAY_SIZE = 1000; export default class HashMap { - private memory : BinarySearchTree>[] = []; + private memory : BinarySearchTree>[] = []; public length = 0; private compareFunction = (a: { key: string }, - b:HashMapElement): number => a.key.localeCompare(b.key); + b:HashMapElement): number => a.key.localeCompare(b.key); set(key: string, value: T): void { const placeToAdd = this.hashFunction(key) % ARRAY_SIZE; if (!this.memory[placeToAdd]) { - this.memory[placeToAdd] = new BinarySearchTree>(this.compareFunction); + this.memory[placeToAdd] = new BinarySearchTree>(this.compareFunction); } this.memory[placeToAdd].add({ key, value }); this.length += 1; } - get(key: string): HashMapElement | undefined { + get(key: string): HashMapElement | undefined { const positionInArray = this.hashFunction(key) % ARRAY_SIZE; const treeInArray = this.memory[positionInArray]; if (!treeInArray) {