Skip to content

Commit b5c7426

Browse files
Merge pull request #58 from amejiarosario/feature/hashmap
Feature/hashmap
2 parents 8ea61ce + 2f24f57 commit b5c7426

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

book/content/part03/hashmap.asc

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ Hash Map it’s very optimal for searching values by key in constant time *O(1)*
301301
.2+.^s| Data Structure 2+^s| Searching By .2+^.^s| Insert .2+^.^s| Delete .2+^.^s| Space Complexity
302302
^|_Index/Key_ ^|_Value_
303303
| Hash Map (naïve) ^|O(n) ^|O(n) ^|O(n) ^|O(n) ^|O(n)
304-
| Hash Map (optimized) ^|O(1)* ^|O(n) ^|O(1)* ^|O(1)* ^|O(1)*
304+
| Hash Map (optimized) ^|O(1) ^|O(n) ^|O(1)* ^|O(1) ^|O(n)
305305
|===
306306
{empty}* = Amortized run time. E.g. rehashing might affect run time.
307307
// end::table[]

src/data-structures/maps/hash-maps/hash-map.js

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ class HashMap {
3030
this.reset();
3131
}
3232

33+
/**
34+
* Reset or reinitialize all values on the hashmap.
35+
*
36+
* Used for rehashing, clear and initializing the map.
37+
*
38+
* @param {array} buckets - New bucket.
39+
* @param {number} size - The new size of the hashmap.
40+
* @param {number} collisions - The number of collisions.
41+
* @param {array} keysTrackerArray - The array of keys in insertion order
42+
* @param {number} keysTrackerIndex - The last index of keysTrackerArray
43+
*/
3344
reset(
3445
buckets = new Array(this.initialCapacity),
3546
size = 0,
@@ -273,6 +284,13 @@ class HashMap {
273284
get length() {
274285
return this.size;
275286
}
287+
288+
/**
289+
* Removes all key/value pairs from the Map object.
290+
*/
291+
clear() {
292+
this.reset();
293+
}
276294
}
277295

278296
// Aliases

src/data-structures/maps/hash-maps/hash-map.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe('HashMap Tests', () => {
265265
hashMap.delete('All About That Bass');
266266
hashMap.set('All About That Bass', 'Meghan Trainor');
267267
expect(hashMap.keysTrackerIndex).toBe(12);
268-
// should hava a hole
268+
// should have a hole
269269
expect(hashMap.keysTrackerArray).toEqual(['Pineapple', 'Despacito', 'Bailando', 'Dura', 'Lean On', 'Hello',
270270
undefined,
271271
'Wake Me Up', 'Brother', 'Faded', 'The Spectre', 'All About That Bass']);

src/data-structures/maps/map.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,19 @@ mapImplementations.forEach((MapImplementation) => {
138138
]);
139139
});
140140
});
141+
142+
describe('#clear', () => {
143+
beforeEach(() => {
144+
map.set(1, 2);
145+
map.set(2, 'dos');
146+
map.set(3, 3);
147+
});
148+
149+
it('should work', () => {
150+
expect(map.size).toBe(3);
151+
expect(map.clear()).toEqual();
152+
expect(map.size).toBe(0);
153+
});
154+
});
141155
});
142156
});

src/data-structures/maps/tree-maps/tree-map.js

+7
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ class TreeMap {
133133
yield [node.value, node.data()];
134134
}
135135
}
136+
137+
/**
138+
* Removes all key/value pairs from the Map object.
139+
*/
140+
clear() {
141+
this.tree = new Tree();
142+
}
136143
}
137144

138145
// Aliases

0 commit comments

Comments
 (0)