blob: fcd8d734588d6938ce7b31eccfc51ef183fe7115 [file] [log] [blame]
// A map with a default value. Great for counting or binnings things by key.
export class DefaultMap<K, V> extends Map<K, V> {
private initfn: ()=> V;
// Accepts a function that returns the dict's default value, an empty Array for example.
constructor(fn: ()=> V) {
this.initfn = fn;
get(key: K): V {
if (!this.has(key)) {
this.set(key, this.initfn());
return super.get(key)!;