Skip to content

Commit

Permalink
feat(maps): implement clear method for hashMap and treeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
amejiarosario committed May 23, 2020
1 parent f6b47b5 commit 924c9a7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/data-structures/maps/hash-maps/hash-map.js
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
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
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
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

0 comments on commit 924c9a7

Please sign in to comment.