UNPKG

@eagleoutice/flowr-dev

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

157 lines 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SetUpperBoundDomain = void 0; const assert_1 = require("../../util/assert"); const set_1 = require("../../util/collections/set"); const logic_1 = require("../../util/logic"); const abstract_domain_1 = require("./abstract-domain"); const lattice_1 = require("./lattice"); const value_abstract_domain_1 = require("./value-abstract-domain"); /** * The set upper bound abstract domain as sets capturing possible values of the concrete set bounded by a `limit` for the maximum number of inferred values. * The Bottom element is defined as the{@link Bottom} and the Top element is defined as {@link Top} symbol. * @template T - Type of the values in the abstract domain * @template Value - Type of the constraint in the abstract domain (Top, Bottom, or an actual value) */ class SetUpperBoundDomain extends abstract_domain_1.AbstractDomain { limit; setType; /** * @param limit - A limit for the maximum number of elements to store in the set * @param newSet - An optional set constructor for the domain elements if the type `T` is not storable in a HashSet */ constructor(value, limit = abstract_domain_1.DEFAULT_INFERENCE_LIMIT, setType = Set) { if (value !== lattice_1.Top && value !== lattice_1.Bottom) { if (Array.isArray(value)) { super((value.length > limit ? lattice_1.Top : new setType(value))); } else { super((value.size > limit ? lattice_1.Top : new setType(value))); } } else { super(value); } this.limit = limit; this.setType = setType; } create(value) { return new SetUpperBoundDomain(value, this.limit, this.setType); } from(...values) { if (values.length === 0) { return this.bottom(); } const sets = values.map(value => new Set(value)); return this.create(sets.reduce((result, set) => result.union(set))); } static top(limit, setType) { return new this(lattice_1.Top, limit, setType); } static bottom(limit, setType) { return new this(lattice_1.Bottom, limit, setType); } static from(values, limit, setType) { values = !Array.isArray(values) ? [values] : values.every(value => value instanceof Set || Array.isArray(value)) ? values : [values]; if (values.length === 0) { return this.bottom(); } const sets = values.map(value => new Set(value)); return new this(sets.reduce((result, set) => result.union(set)), limit, setType); } top() { return this.create(lattice_1.Top); } bottom() { return this.create(lattice_1.Bottom); } equalsValue(other) { return (0, set_1.setEquals)(this.value, other.value); } leqValue(other) { return this.value.isSubsetOf(other.value); } joinValue(other) { return this.create(this.value.union(other.value)); } meetValue(other) { return this.create(this.value.intersection(other.value)); } union(other) { other = other instanceof abstract_domain_1.AbstractDomain ? other : this.create(other); if (this.isBottom() || other.isBottom()) { return this.bottom(); } return this.join(other); } intersect(other) { other = other instanceof abstract_domain_1.AbstractDomain ? other : this.create(other); if (this.isBottom() || other.isBottom()) { return this.bottom(); } return this.meet(other); } subtract(other) { other = other instanceof abstract_domain_1.AbstractDomain ? other : this.create(other); if (this.isBottom() || other.isBottom()) { return this.bottom(); } else if (this.isValue() && other.isValue()) { return this.create(this.value.difference(other.value)); } else if (this.isValue()) { return this.create(this.value); } return this.top(); } satisfies(set, comparator = value_abstract_domain_1.SetComparator.Equal) { if (this.isTop()) { return logic_1.Ternary.Maybe; } const values = [...set]; switch (comparator) { case value_abstract_domain_1.SetComparator.Equal: case value_abstract_domain_1.SetComparator.SubsetOrEqual: { if (this.isValue() && values.length <= this.value.size && values.every(value => this.value.has(value))) { return logic_1.Ternary.Maybe; } return logic_1.Ternary.Never; } case value_abstract_domain_1.SetComparator.Subset: { if (this.isValue() && values.length < this.value.size && values.every(value => this.value.has(value))) { return logic_1.Ternary.Maybe; } return logic_1.Ternary.Never; } case value_abstract_domain_1.SetComparator.Intersect: { if (this.isTop() || this.isValue() && [...set].some(value => this.value.has(value))) { return logic_1.Ternary.Maybe; } return logic_1.Ternary.Never; } case value_abstract_domain_1.SetComparator.NoIntersect: { return logic_1.TernaryLogic.negate(this.satisfies(set, value_abstract_domain_1.SetComparator.Intersect)); } default: { (0, assert_1.assertUnreachable)(comparator); } } } jsonify() { return this.value.values().toArray(); } stringify() { return `{${this.value.values().map(abstract_domain_1.AbstractDomain.toString).toArray().join(', ')}}`; } isTop() { return this.value === lattice_1.Top; } isBottom() { return this.value === lattice_1.Bottom; } isValue() { return this.value !== lattice_1.Top && this.value !== lattice_1.Bottom; } } exports.SetUpperBoundDomain = SetUpperBoundDomain; //# sourceMappingURL=set-upper-bound-domain.js.map