diff --git a/src/data-structures/maps/hash-maps/hash-map.js b/src/data-structures/maps/hash-maps/hash-map.js index 97d580fb..00a26bea 100644 --- a/src/data-structures/maps/hash-maps/hash-map.js +++ b/src/data-structures/maps/hash-maps/hash-map.js @@ -31,7 +31,9 @@ class HashMap { } /** - * Reset or reinitialize all values on the hashmap. Used for rehashing. + * Reset or reinitialize all values on the hashmap. + * + * Used for rehashing, clear and initializing the map. * * @param {array} buckets - New bucket. * @param {number} size - The new size of the hashmap. @@ -282,6 +284,13 @@ class HashMap { get length() { return this.size; } + + /** + * Removes all key/value pairs from the Map object. + */ + clear() { + this.reset(); + } } // Aliases diff --git a/src/data-structures/maps/hash-maps/hash-map.spec.js b/src/data-structures/maps/hash-maps/hash-map.spec.js index b7ec0d70..daaf93b7 100644 --- a/src/data-structures/maps/hash-maps/hash-map.spec.js +++ b/src/data-structures/maps/hash-maps/hash-map.spec.js @@ -265,7 +265,7 @@ describe('HashMap Tests', () => { hashMap.delete('All About That Bass'); hashMap.set('All About That Bass', 'Meghan Trainor'); expect(hashMap.keysTrackerIndex).toBe(12); - // should hava a hole + // should have a hole expect(hashMap.keysTrackerArray).toEqual(['Pineapple', 'Despacito', 'Bailando', 'Dura', 'Lean On', 'Hello', undefined, 'Wake Me Up', 'Brother', 'Faded', 'The Spectre', 'All About That Bass']); diff --git a/src/data-structures/maps/map.spec.js b/src/data-structures/maps/map.spec.js index 5d8d3749..a69c70e3 100644 --- a/src/data-structures/maps/map.spec.js +++ b/src/data-structures/maps/map.spec.js @@ -138,5 +138,19 @@ mapImplementations.forEach((MapImplementation) => { ]); }); }); + + describe('#clear', () => { + beforeEach(() => { + map.set(1, 2); + map.set(2, 'dos'); + map.set(3, 3); + }); + + it('should work', () => { + expect(map.size).toBe(3); + expect(map.clear()).toEqual(); + expect(map.size).toBe(0); + }); + }); }); }); diff --git a/src/data-structures/maps/tree-maps/tree-map.js b/src/data-structures/maps/tree-maps/tree-map.js index 68562da5..eed8bc78 100644 --- a/src/data-structures/maps/tree-maps/tree-map.js +++ b/src/data-structures/maps/tree-maps/tree-map.js @@ -133,6 +133,13 @@ class TreeMap { yield [node.value, node.data()]; } } + + /** + * Removes all key/value pairs from the Map object. + */ + clear() { + this.tree = new Tree(); + } } // Aliases