/** * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import { iterableToArray } from '../util'; // TODO: rename to "linear map" and just do key value mapping from index? /** Immutable by convention IntMap */ interface IntMap { has(key: number): boolean, keys(): IterableIterator, values(): IterableIterator, get(key: number): T, readonly size: number } namespace IntMap { export const Empty: IntMap = new Map(); export interface Mutable extends IntMap { set(key: number, value: T): void; } export function keyArray(map: IntMap): number[] { return iterableToArray(map.keys()); } export function Mutable(): Mutable { return new Map() as Mutable; } export function asImmutable(map: IntMap): IntMap { return map; } export function copy(map: IntMap): Mutable { const ret = Mutable(); const it = map.keys(); while (true) { const { done, value } = it.next(); if (done) break; ret.set(value, map.get(value)); } return ret; } export function addFrom(map: Mutable, src: IntMap) { const it = src.keys(); while (true) { const { done, value } = it.next(); if (done) break; map.set(value, src.get(value)); } return map; } } export { IntMap };