Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(maps): implement clear method for hashMap and treeMap
  • Loading branch information
amejiarosario committed May 23, 2020
commit 924c9a76db91f7df44f7cb38caba3aa9c3f0497f
11 changes: 10 additions & 1 deletion src/data-structures/maps/hash-maps/hash-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -282,6 +284,13 @@ class HashMap {
get length() {
return this.size;
}

/**
* Removes all key/value pairs from the Map object.
*/
clear() {
this.reset();
}
}

// Aliases
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/maps/hash-maps/hash-map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
14 changes: 14 additions & 0 deletions src/data-structures/maps/map.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
7 changes: 7 additions & 0 deletions src/data-structures/maps/tree-maps/tree-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down