2021-12-01 16:24:29 +00:00
|
|
|
/// SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-26 13:38:38 +00:00
|
|
|
/// SPDX-FileCopyrightText: Copyright © 2016-2022 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
2018-11-01 14:55:45 +00:00
|
|
|
|
2021-01-11 22:35:36 +00:00
|
|
|
// Utilities for Maps of Sets
|
2019-06-14 14:33:02 +00:00
|
|
|
|
2021-05-17 14:26:01 +00:00
|
|
|
import { FlexSet, FlexMap, Canonicalizer } from '@preserves/core';
|
2018-09-09 17:13:57 +00:00
|
|
|
|
2021-01-11 22:35:36 +00:00
|
|
|
export function add<K,V>(m: FlexMap<K, FlexSet<V>>, k: K, v: V, c: Canonicalizer<V>) {
|
|
|
|
let s = m.get(k);
|
|
|
|
if (!s) {
|
|
|
|
s = new FlexSet(c);
|
|
|
|
m.set(k, s);
|
|
|
|
}
|
|
|
|
s.add(v);
|
2018-09-09 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 22:35:36 +00:00
|
|
|
export function del<K,V>(m: FlexMap<K, FlexSet<V>>, k: K, v: V) {
|
|
|
|
const s = m.get(k);
|
|
|
|
if (!s) return;
|
|
|
|
s.delete(v);
|
|
|
|
if (s.size === 0) m.delete(k);
|
|
|
|
}
|