All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dafny.DafnySet Maven / Gradle / Ivy

The newest version!
// Copyright by the contributors to the Dafny Project
// SPDX-License-Identifier: MIT

package dafny;

import java.util.*;

// A class that is equivalent to the implementation of Set in Dafny
public class DafnySet {
    private Set innerSet;

    public DafnySet() {
        innerSet = new HashSet<>();
    }

    public DafnySet(Set s) {
        assert s != null : "Precondition Violation";
        innerSet = new HashSet<>(s);
    }

    public DafnySet(Collection c) {
        assert c != null : "Precondition Violation";
        innerSet = new HashSet<>(c);
    }

    public DafnySet(DafnySet other) {
        assert other != null : "Precondition Violation";
        innerSet = new HashSet<>(other.innerSet);
    }

    public DafnySet(List l) {
        assert l != null : "Precondition Violation";
        innerSet = new HashSet<>(l);
    }

    @SafeVarargs
    public static  DafnySet of(T ... elements) {
        return new DafnySet(Arrays.asList(elements));
    }

    private static final DafnySet EMPTY = DafnySet.of();

    @SuppressWarnings("unchecked")
    public static  DafnySet empty() {
        // Safe because immutable
        return (DafnySet) EMPTY;
    }

    @SuppressWarnings("unchecked")
    public static  TypeDescriptor> _typeDescriptor(TypeDescriptor elementType) {
        // Fudge the type parameter; it's not great, but it's safe because
        // (for now) type descriptors are only used for default values
        return TypeDescriptor.referenceWithDefault(
                (Class>) (Class) DafnySet.class,
                DafnySet.empty());
    }

    // Determines if the current object is a subset of the DafnySet passed in. Requires that the input DafnySet is not
    // null.
    public boolean isSubsetOf(DafnySet other) {
        assert other != null : "Precondition Violation";
        return other.containsAll(this);
    }

    // Determines if the current object is a proper subset of the DafnySet passed in. Requires that the input DafnySet
    // is not null.
    public boolean isProperSubsetOf(DafnySet other) {
        assert other != null : "Precondition Violation";
        return isSubsetOf(other) && size() < other.size();
    }

    public boolean contains(Object t) {
        assert t != null : "Precondition Violation";
        return innerSet.contains(t);
    }

    public  boolean disjoint(DafnySet other) {
        assert other != null : "Precondition Violation";
        for (U u : other.innerSet) {
            if (contains(u)) return false;
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    public static  DafnySet union(DafnySet th, DafnySet other) {
        assert th != null : "Precondition Violation";
        assert other != null : "Precondition Violation";

        if (th.isEmpty()) {
            return (DafnySet)other;
        } else if (other.isEmpty()) {
            return (DafnySet)th;
        } else {
            DafnySet u = new DafnySet((DafnySet)other);
            u.addAll((DafnySet)th);
            return u;
        }
    }

    //Returns a DafnySet containing elements only found in the current DafnySet
    @SuppressWarnings("unchecked")
    public static  DafnySet difference(DafnySet th, DafnySet other) {
        assert th != null : "Precondition Violation";
        assert other != null : "Precondition Violation";
        DafnySet u = new DafnySet((DafnySet)th);
        u.removeAll((DafnySet)other);
        return u;
    }

    public static  DafnySet intersection(DafnySet th, DafnySet other) {
        assert th != null : "Precondition Violation";
        assert other != null : "Precondition Violation";
        DafnySet u = new DafnySet();
        for (T ele : th.innerSet) {
            if (other.contains(ele)) u.add(ele);
        }
        return u;
    }

    public boolean containsAll(DafnySet other) {
        assert other != null : "Precondition Violation";
        return innerSet.containsAll(other.innerSet);
    }

    public int size() {
        return innerSet.size();
    }

    public int cardinalityInt() {
        return size();
    }

    public boolean isEmpty() {
        return innerSet.isEmpty();
    }

    public boolean add(T t) {
        assert t != null : "Precondition Violation";
        return innerSet.add(t);
    }

    public boolean remove(T t) {
        assert t != null : "Precondition Violation";
        return innerSet.remove(t);
    }

    public boolean removeAll(DafnySet other) {
        assert other != null : "Precondition Violation";
        return innerSet.removeAll(other.innerSet);
    }

    public boolean addAll(DafnySet other) {
        assert other != null : "Precondition Violation";
        return innerSet.addAll(other.innerSet);
    }

    public Collection> AllSubsets(){
        // Start by putting all set elements into a list, but don't include null
        List elmts = new ArrayList<>();
        elmts.addAll(innerSet);
        int n = elmts.size();
        DafnySet s;
        HashSet> r = new HashSet<>();
        for (int i = 0; i < (1<();
            int m = 1; // m is used to check set bit in binary representation.
            // Print current subset
            for (int j = 0; j < n; j++, m = m << 1)
            {
                if ((i & m) > 0)
                {
                    s.add(elmts.get(j));
                }
            }
            r.add(s);
        }
        return r;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        DafnySet o = (DafnySet) obj;
        return containsAll(o) && o.containsAll(this);
    }

    @Override
    public int hashCode() {
        return innerSet.hashCode();
    }

    @Override
    public String toString() {
        String s = "{";
        String sep = "";
        for (T elem : innerSet) {
            s += sep + Helpers.toString(elem);
            sep = ", ";
        }
        return s + "}";
    }

    public DafnyMultiset asDafnyMultiset() {
        return new DafnyMultiset<>(innerSet);
    }

    public Set Elements() {
        return innerSet;
    }
}