UNPKG

s2-tools

Version:

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.

60 lines 1.57 kB
import { compare, toCell } from '../../dataStructures/uint64'; /** A local multimap key-value store */ export class MultiMap { #store; #count = 0; /** Builds a new MultiMap */ constructor() { this.#store = new Map(); } /** @returns - the length of the map */ get length() { return this.#count; } /** * Adds a value to the list of values associated with a key * @param key - the key * @param value - the value to store */ set(key, value) { const list = this.get(key); if (list === undefined) { this.#store.set(key, [value]); } else { list.push(value); } this.#count++; } /** * Gets the list of values associated with a key * @param key - the key * @returns the list of values if the map contains values for the key */ get(key) { return this.#store.get(key); } /** * iterate through the values * @yields - The values in the store */ async *entries() { const entries = Array.from(this.#store.entries()).map(([id, value]) => [toCell(id), value]); entries.sort((a, b) => compare(a[0], b[0])); for (const [key, value] of entries) { yield { key, value }; } } /** * iterate through the values * @returns - an iterator */ [Symbol.asyncIterator]() { return this.entries(); } /** Closes the store */ close() { this.#store.clear(); } } //# sourceMappingURL=index.js.map