Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 924c9a7

Browse files
feat(maps): implement clear method for hashMap and treeMap
1 parent f6b47b5 commit 924c9a7

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class HashMap {
3131
}
3232

3333
/**
34-
* Reset or reinitialize all values on the hashmap. Used for rehashing.
34+
* Reset or reinitialize all values on the hashmap.
35+
*
36+
* Used for rehashing, clear and initializing the map.
3537
*
3638
* @param {array} buckets - New bucket.
3739
* @param {number} size - The new size of the hashmap.
@@ -282,6 +284,13 @@ class HashMap {
282284
get length() {
283285
return this.size;
284286
}
287+
288+
/**
289+
* Removes all key/value pairs from the Map object.
290+
*/
291+
clear() {
292+
this.reset();
293+
}
285294
}
286295

287296
// Aliases

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /