package accrue.analysis.readwritesets;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import accrue.analysis.interprocanalysis.AbstractLocation;
import accrue.analysis.interprocanalysis.Ordered;

/**
 * Sets of locations that are read and written 
 */
public class ReadWriteSet implements Ordered<ReadWriteSet> {
    static private final Map<Set<AbstractLocation>, Set<AbstractLocation>> MEMOIZED_SETS =
            new HashMap<Set<AbstractLocation>, Set<AbstractLocation>>();

    /**
     * Empty read and write sets
     */
    public static final ReadWriteSet EMPTY =
            new ReadWriteSet(Collections.<AbstractLocation> emptySet(),
                             Collections.<AbstractLocation> emptySet());
    /**
     * Set of locations that were read
     */
    private final Set<AbstractLocation> readLocs;
    /**
     * Set of locations that were written
     */
    private final Set<AbstractLocation> writeLocs;

    /**
     * Construct from the raw sets
     * 
     * @param readSet 
     *          Set of locations that were read
     * @param writeSet 
     *          Set of locations that were written
     */
    public ReadWriteSet(Set<AbstractLocation> readSet,
            Set<AbstractLocation> writeSet) {
        if (readSet == null || writeSet == null) {
            throw new IllegalArgumentException("Sets should be non null");
        }

        // memoize the sets to save memory
        this.readLocs = memoize(readSet);
        this.writeLocs = memoize(writeSet);
    }

    private static synchronized Set<AbstractLocation> memoize(
            Set<AbstractLocation> s) {
        // Handle null specially
        if (s == null) {
            return null;
        }
        Set<AbstractLocation> t = MEMOIZED_SETS.get(s);
        if (t == null) {
            MEMOIZED_SETS.put(s, s);
            t = s;
        }
        return t;
    }

    @Override
    public boolean leq(ReadWriteSet o) {
        if (this.readLocs == o.readLocs && this.writeLocs == o.writeLocs) {
            return true;
        }
        return o.readLocs.containsAll(this.readLocs)
                && o.writeLocs.containsAll(this.writeLocs);
    }

    @Override
    public ReadWriteSet upperBound(ReadWriteSet o) {
        if (o == EMPTY || o == null) {
            return this;
        }
        if (this == EMPTY) {
            return o;
        }

        Set<AbstractLocation> newRead =
                new HashSet<AbstractLocation>(this.readLocs);
        newRead.addAll(o.readLocs);

        Set<AbstractLocation> newWrite =
                new HashSet<AbstractLocation>(this.writeLocs);
        newWrite.addAll(o.writeLocs);

        if (this.readLocs.size() == newRead.size()
                && this.writeLocs.size() == newWrite.size()) {
            // we are the upper bound.
            return this;
        }
        if (o.readLocs.size() == newRead.size()
                && o.writeLocs.size() == newWrite.size()) {
            // o is the upper bound.
            return o;
        }
        return new ReadWriteSet(newRead, newWrite);
    }

    @Override
    public ReadWriteSet widen(ReadWriteSet o) {
        return upperBound(o);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result =
                prime * result + ((readLocs == null) ? 0 : readLocs.hashCode());
        result =
                prime * result
                        + ((writeLocs == null) ? 0 : writeLocs.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        ReadWriteSet other = (ReadWriteSet) obj;
        if (readLocs == null) {
            if (other.readLocs != null) return false;
        }
        else if (!readLocs.equals(other.readLocs)) return false;
        if (writeLocs == null) {
            if (other.writeLocs != null) return false;
        }
        else if (!writeLocs.equals(other.writeLocs)) return false;
        return true;
    }

    /**
     * get the set of read locations
     * 
     * @return read set
     */
    public Set<AbstractLocation> readSet() {
        return readLocs;
    }

    /**
     * Get the set of written locations
     * 
     * @return write set
     */
    public Set<AbstractLocation> writeSet() {
        return writeLocs;
    }

    @Override
    public String toString() {
        return "{" + readLocs.size() + " reads, " + writeLocs.size()
                + " writes}";
    }

}
