lazy_set: Move set and array properties to the data field.

The data field will be a union type when it is converted to typescript.
This approach allows us to avoid introducing additional type check for
both of the properties.
This commit is contained in:
Priyank Patel
2021-12-26 19:47:19 +00:00
committed by Tim Abbott
parent be486b6138
commit 16a3d444fd

View File

@@ -24,31 +24,38 @@ export class LazySet {
*/ */
constructor(vals) { constructor(vals) {
this.arr = vals; this.data = {
this.set = undefined; arr: vals,
set: undefined,
};
} }
keys() { keys() {
if (this.set !== undefined) { const {data} = this;
return this.set.keys(); if (data.set !== undefined) {
return data.set.keys();
} }
return this.arr.values(); return data.arr.values();
} }
_make_set() { _make_set() {
if (this.set !== undefined) { if (this.data.set !== undefined) {
return; return;
} }
this.set = new Set(this.arr);
this.arr = undefined; this.data = {
arr: undefined,
set: new Set(this.data.arr),
};
} }
get size() { get size() {
if (this.set !== undefined) { const {data} = this;
return this.set.size; if (data.set !== undefined) {
return data.set.size;
} }
return this.arr.length; return data.arr.length;
} }
map(f) { map(f) {
@@ -58,19 +65,19 @@ export class LazySet {
has(v) { has(v) {
this._make_set(); this._make_set();
const val = this._clean(v); const val = this._clean(v);
return this.set.has(val); return this.data.set.has(val);
} }
add(v) { add(v) {
this._make_set(); this._make_set();
const val = this._clean(v); const val = this._clean(v);
this.set.add(val); this.data.set.add(val);
} }
delete(v) { delete(v) {
this._make_set(); this._make_set();
const val = this._clean(v); const val = this._clean(v);
return this.set.delete(val); return this.data.set.delete(val);
} }
_clean(v) { _clean(v) {