samchon / tstl

TypeScript-STL (Standard Template Library, migrated from C++)
MIT License
602 stars 49 forks source link

New method, `UniqueMap.take()` #97

Closed samchon closed 2 years ago

samchon commented 2 years ago
export class HashMap<Key, T>
{
    public take(key: Key, generator: () => T): T
    {
        const it = this.find(key);
        return it.equals(this.end())
            ? this.emplace(key, generator()).first.second
            : it.second;
    }
}