blob: 6da0da456fa25938223bc181c42c52787772f0d2 [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) {
super();
this.initfn = fn;
}
get(key: K): V {
if (!this.has(key)) {
this.set(key, this.initfn());
}
return super.get(key)!;
}
}