diff --git a/CHANGELOG.md b/CHANGELOG.md index a4cfc114..b601c75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +# [1.8.0](https://github.com/amejiarosario/dsa.js/compare/1.7.1...1.8.0) (2020-05-23) + + +### Bug Fixes + +* **book/map:** fix typo on maps space complexity ([2f24f57](https://github.com/amejiarosario/dsa.js/commit/2f24f57f989bb97f198bb32f6daa477d6075dc31)) +* **hashmap:** improve methods documentation ([f6b47b5](https://github.com/amejiarosario/dsa.js/commit/f6b47b5a6c53126bebbad72ac92da1a594042232)) + + +### Features + +* **maps:** implement clear method for hashMap and treeMap ([924c9a7](https://github.com/amejiarosario/dsa.js/commit/924c9a76db91f7df44f7cb38caba3aa9c3f0497f)) + ## [1.7.1](https://github.com/amejiarosario/dsa.js/compare/1.7.0...1.7.1) (2020-05-20) diff --git a/book/content/part03/hashmap.asc b/book/content/part03/hashmap.asc index 8ae810ab..8bf36ede 100644 --- a/book/content/part03/hashmap.asc +++ b/book/content/part03/hashmap.asc @@ -301,7 +301,7 @@ Hash Map it’s very optimal for searching values by key in constant time *O(1)* .2+.^s| Data Structure 2+^s| Searching By .2+^.^s| Insert .2+^.^s| Delete .2+^.^s| Space Complexity ^|_Index/Key_ ^|_Value_ | Hash Map (naïve) ^|O(n) ^|O(n) ^|O(n) ^|O(n) ^|O(n) -| Hash Map (optimized) ^|O(1)* ^|O(n) ^|O(1)* ^|O(1)* ^|O(1)* +| Hash Map (optimized) ^|O(1) ^|O(n) ^|O(1)* ^|O(1) ^|O(n) |=== {empty}* = Amortized run time. E.g. rehashing might affect run time. // end::table[] diff --git a/package-lock.json b/package-lock.json index 112761ba..1ad35ece 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "1.7.1", + "version": "1.8.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d51fc20b..4295ba82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "1.7.1", + "version": "1.8.0", "description": "Data Structures & Algorithms in JS", "author": "Adrian Mejia (https://adrianmejia.com)", "homepage": "https://github.com/amejiarosario/dsa.js", diff --git a/src/data-structures/maps/hash-maps/hash-map.js b/src/data-structures/maps/hash-maps/hash-map.js index d5ea7382..00a26bea 100644 --- a/src/data-structures/maps/hash-maps/hash-map.js +++ b/src/data-structures/maps/hash-maps/hash-map.js @@ -30,6 +30,17 @@ class HashMap { this.reset(); } + /** + * 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. + * @param {number} collisions - The number of collisions. + * @param {array} keysTrackerArray - The array of keys in insertion order + * @param {number} keysTrackerIndex - The last index of keysTrackerArray + */ reset( buckets = new Array(this.initialCapacity), size = 0, @@ -273,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