union, intersect, subtract

This commit is contained in:
Tony Garnock-Jones 2021-01-09 16:33:56 +01:00
parent b0ed7e914b
commit 3f62d68bab
2 changed files with 22 additions and 4 deletions

View File

@ -97,7 +97,7 @@ export class FlexMap<K, V> implements Map<K, V> {
return this.items.values();
}
[Symbol.toStringTag] = 'FlexMap';
get [Symbol.toStringTag]() { return 'FlexMap'; }
equals(other: any, eqv: Equivalence<V> = (v1, v2) => v1 === v2): boolean {
if (!('size' in other && 'has' in other && 'get' in other)) return false;
@ -206,7 +206,7 @@ export class FlexSet<V> implements Set<V> {
return this.items.values();
}
[Symbol.toStringTag] = 'FlexSet';
get [Symbol.toStringTag]() { return 'FlexSet'; }
equals(other: any): boolean {
if (!('size' in other && 'has' in other)) return false;
@ -220,4 +220,22 @@ export class FlexSet<V> implements Set<V> {
canonicalValues(): IterableIterator<string> {
return this.items.keys();
}
union(other: Set<V>): FlexSet<V> {
const result = new FlexSet(this.canonicalizer, this);
for (let k of other) result.add(k);
return result;
}
intersect(other: Set<V>): FlexSet<V> {
const result = new FlexSet(this.canonicalizer);
for (let k of this) if (other.has(k)) result.add(k);
return result;
}
subtract(other: Set<V>): FlexSet<V> {
const result = new FlexSet(this.canonicalizer);
for (let k of this) if (!other.has(k)) result.add(k);
return result;
}
}

View File

@ -598,7 +598,7 @@ export class Dictionary<T> extends FlexMap<Value, T> {
return this.asPreservesText();
}
[Symbol.toStringTag] = 'Dictionary';
get [Symbol.toStringTag]() { return 'Dictionary'; }
[PreserveOn](encoder: Encoder) {
if (encoder.canonical) {
@ -643,7 +643,7 @@ export class Set extends FlexSet<Value> {
return new Set(this);
}
[Symbol.toStringTag] = 'Set';
get [Symbol.toStringTag]() { return 'Set'; }
[PreserveOn](encoder: Encoder) {
if (encoder.canonical) {