|
| 1 | +package it.unive.lisa.analysis; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +/** |
| 7 | + * A generic inverse set lattice containing a set of elements. Lattice |
| 8 | + * operations are the opposite of the {@link SetLattice} ones, namely: |
| 9 | + * <ul> |
| 10 | + * <li>the lub is the set intersection</li> |
| 11 | + * <li>the ≤ is the inverse set inclusion</li> |
| 12 | + * </ul> |
| 13 | + * Widening on instances of this lattice depends on the cardinality of the |
| 14 | + * domain of the underlying elements. The provided implementation behave as the |
| 15 | + * domain is <b>finite</b>, thus invoking the lub. Inverse set lattices defined |
| 16 | + * on infinite domains must implement a coherent widening logic. |
| 17 | + * |
| 18 | + * @author <a href="mailto:[email protected]">Luca Negrini</a> |
| 19 | + * |
| 20 | + * @param <S> the concrete instance of {@link InverseSetLattice} |
| 21 | + * @param <E> the type of elements of the domain of this lattice |
| 22 | + */ |
| 23 | +public abstract class InverseSetLattice<S extends InverseSetLattice<S, E>, E> extends BaseLattice<S> { |
| 24 | + |
| 25 | + /** |
| 26 | + * The set of elements contained in the lattice. |
| 27 | + */ |
| 28 | + protected Set<E> elements; |
| 29 | + |
| 30 | + /** |
| 31 | + * Builds the lattice. |
| 32 | + * |
| 33 | + * @param elements the elements that are contained in the lattice |
| 34 | + */ |
| 35 | + protected InverseSetLattice(Set<E> elements) { |
| 36 | + this.elements = elements; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Utility for creating a concrete instance of {@link InverseSetLattice} |
| 41 | + * given a set. This decouples the instance of set used during computation |
| 42 | + * of the elements to put in the lattice from the actual type of set |
| 43 | + * underlying the lattice. |
| 44 | + * |
| 45 | + * @param set the set containing the elements that must be included in the |
| 46 | + * lattice instance |
| 47 | + * |
| 48 | + * @return a new concrete instance of {@link InverseSetLattice} containing |
| 49 | + * the elements of the given set |
| 50 | + */ |
| 51 | + protected abstract S mk(Set<E> set); |
| 52 | + |
| 53 | + @Override |
| 54 | + protected final S lubAux(S other) throws SemanticException { |
| 55 | + Set<E> lub = new HashSet<>(elements); |
| 56 | + lub.retainAll(other.elements); |
| 57 | + return mk(lub); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected S wideningAux(S other) throws SemanticException { |
| 62 | + return lubAux(other); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected final boolean lessOrEqualAux(S other) throws SemanticException { |
| 67 | + return elements.containsAll(other.elements); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int hashCode() { |
| 72 | + final int prime = 31; |
| 73 | + int result = 1; |
| 74 | + result = prime * result + ((elements == null) ? 0 : elements.hashCode()); |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean equals(Object obj) { |
| 80 | + if (this == obj) |
| 81 | + return true; |
| 82 | + if (obj == null) |
| 83 | + return false; |
| 84 | + if (getClass() != obj.getClass()) |
| 85 | + return false; |
| 86 | + InverseSetLattice<?, ?> other = (InverseSetLattice<?, ?>) obj; |
| 87 | + if (elements == null) { |
| 88 | + if (other.elements != null) |
| 89 | + return false; |
| 90 | + } else if (!elements.equals(other.elements)) |
| 91 | + return false; |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public final String toString() { |
| 97 | + return isTop() ? "TOP" : isBottom() ? "BOTTOM" : elements.toString(); |
| 98 | + } |
| 99 | +} |
0 commit comments