kodkod.util.ints.SparseSequence Java Examples

The following examples show how to use kodkod.util.ints.SparseSequence. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BooleanMatrix.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a new matrix such that an entry in the returned matrix represents a 
 * combination of the corresponding entries in this and other matrix.  The effect 
 * of this method is the same as calling this.compose(ExprOperator.Binary.OR, other).
 * 
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions && m.factory = this.factory &&
 *                              all i: [0..m.dimensions.capacity) | 
 *                               m.elements[i] = this.elements[i] OR other.elements[i] }
 * @throws NullPointerException  other = null 
 * @throws IllegalArgumentException  !other.dimensions.equals(this.dimensions) || this.factory != other.factory
 */
public final BooleanMatrix or(BooleanMatrix  other) {
	checkFactory(this.factory, other.factory); 
	checkDimensions(this.dims, other.dims);
	if (this.cells.isEmpty())
		return other.clone();
	else if (other.cells.isEmpty())
		return this.clone();
	final BooleanMatrix ret = new BooleanMatrix(dims, factory, cells, other.cells);
	final SparseSequence<BooleanValue> retSeq = ret.cells;
	for(IndexedEntry<BooleanValue> e0 : cells) {
		BooleanValue v1 = other.cells.get(e0.index());
		if (v1==null)
			retSeq.put(e0.index(), e0.value());
		else
			retSeq.put(e0.index(), factory.or(e0.value(), v1));
	}
	for(IndexedEntry<BooleanValue> e1 : other.cells) {
		if (!cells.containsIndex(e1.index()))
			retSeq.put(e1.index(), e1.value());
	}

	return ret;
}
 
Example #2
Source File: BooleanMatrix.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a boolean matrix m such that m = this if the given condition evaluates
 * to TRUE and m = other otherwise.
 * 
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions &&
    *                              all i: [0..m.dimensions.capacity) | 
    *                                 m.elements[i] = condition => this.elements[i], other.elements[i] }
    * @throws NullPointerException  other = null || condition = null
    * @throws IllegalArgumentException  !other.dimensions.equals(this.dimensions) || this.factory != other.factory
 */
public final BooleanMatrix choice(BooleanValue condition, BooleanMatrix other) {
	checkFactory(this.factory, other.factory); 
	checkDimensions(this.dims, other.dims);
	if (condition==TRUE) return this.clone();
	else if (condition==FALSE) return other.clone();

	final BooleanMatrix ret =  new BooleanMatrix(dims, factory);
	final SparseSequence<BooleanValue> otherCells = other.cells;
	for(IndexedEntry<BooleanValue> e0 : cells) {
		BooleanValue v1 = otherCells.get(e0.index());
		if (v1==null)
			ret.fastSet(e0.index(), factory.and(condition, e0.value()));
		else
			ret.fastSet(e0.index(), factory.ite(condition, e0.value(), v1));
	}
	for(IndexedEntry<BooleanValue> e1 : other.cells) {
		if (!cells.containsIndex(e1.index()))
			ret.fastSet(e1.index(), factory.and(condition.negation(), e1.value()));
	}
	return ret;
}
 
Example #3
Source File: BooleanMatrix.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new matrix such that an entry in the returned matrix represents a
 * conjunction of the corresponding entries in this and other matrix. The effect
 * of this method is the same as calling this.compose(ExprOperator.Binary.AND,
 * other).
 *
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions && m.factory =
 *         this.factory && all i: [0..m.dimensions.capacity) | m.elements[i] =
 *         this.elements[i] AND other.elements[i] }
 * @throws NullPointerException other = null
 * @throws IllegalArgumentException !other.dimensions.equals(this.dimensions) ||
 *             this.factory != other.factory
 */
public final BooleanMatrix and(BooleanMatrix other) {
    checkFactory(this.factory, other.factory);
    checkDimensions(this.dims, other.dims);

    final BooleanMatrix ret = new BooleanMatrix(dims, factory, cells, other.cells);
    ret.mergeDefConds(this, other);

    final SparseSequence<BooleanValue> s1 = other.cells;
    if (cells.isEmpty() || s1.isEmpty())
        return ret;
    for (IndexedEntry<BooleanValue> e0 : cells) {
        BooleanValue v1 = s1.get(e0.index());
        if (v1 != null)
            ret.fastSet(e0.index(), factory.and(e0.value(), v1));
    }
    return ret;
}
 
Example #4
Source File: BooleanMatrix.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new matrix such that an entry in the returned matrix represents a
 * combination of the corresponding entries in this and other matrix. The effect
 * of this method is the same as calling this.compose(ExprOperator.Binary.OR,
 * other).
 *
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions && m.factory =
 *         this.factory && all i: [0..m.dimensions.capacity) | m.elements[i] =
 *         this.elements[i] OR other.elements[i] }
 * @throws NullPointerException other = null
 * @throws IllegalArgumentException !other.dimensions.equals(this.dimensions) ||
 *             this.factory != other.factory
 */
public final BooleanMatrix or(BooleanMatrix other) {
    checkFactory(this.factory, other.factory);
    checkDimensions(this.dims, other.dims);
    if (this.cells.isEmpty())
        return other.clone();
    else if (other.cells.isEmpty())
        return this.clone();

    final BooleanMatrix ret = new BooleanMatrix(dims, factory, cells, other.cells);
    ret.mergeDefConds(this, other);

    final SparseSequence<BooleanValue> retSeq = ret.cells;
    for (IndexedEntry<BooleanValue> e0 : cells) {
        BooleanValue v1 = other.cells.get(e0.index());
        if (v1 == null)
            retSeq.put(e0.index(), e0.value());
        else
            retSeq.put(e0.index(), factory.or(e0.value(), v1));
    }
    for (IndexedEntry<BooleanValue> e1 : other.cells) {
        if (!cells.containsIndex(e1.index()))
            retSeq.put(e1.index(), e1.value());
    }
    return ret;
}
 
Example #5
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Constructs a new simplifier that will use the given collection 
 * of empty expressions and integer constants.  The empties sequence must map each of its indices
 * to an empty expression of the same arity as the index.  The constants sequence must map each of
 * its indices to a Kodkod constant with the same value as the index.
 */
@SuppressWarnings("unchecked")
Simplifier(SparseSequence<Expression> empties, SparseSequence<IntConstant> constants) {
	super(Collections.EMPTY_SET);
	this.empties = empties;
	this.constants = constants;
}
 
Example #6
Source File: BooleanMatrix.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a new matrix such that an entry in the returned matrix represents a 
 * conjunction of the corresponding entries in this and other matrix.  The effect 
 * of this method is the same as calling this.compose(ExprOperator.Binary.AND, other).
 * 
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions && m.factory = this.factory &&
 *                              all i: [0..m.dimensions.capacity) | 
 *                               m.elements[i] = this.elements[i] AND other.elements[i] }
 * @throws NullPointerException  other = null
 * @throws IllegalArgumentException  !other.dimensions.equals(this.dimensions) || this.factory != other.factory
 */
public final BooleanMatrix and(BooleanMatrix  other) {
	checkFactory(this.factory, other.factory); 
	checkDimensions(this.dims, other.dims);
	final BooleanMatrix ret = new BooleanMatrix(dims, factory, cells, other.cells);
	final SparseSequence<BooleanValue> s1 = other.cells;
	if (cells.isEmpty() || s1.isEmpty()) return ret;
	for(IndexedEntry<BooleanValue> e0 : cells) {
		BooleanValue v1 = s1.get(e0.index());
		if (v1!=null)
			ret.fastSet(e0.index(), factory.and(e0.value(), v1));
	}
	return ret;
}
 
Example #7
Source File: Bounds.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a Bounds object with the given factory and mappings.
 */
private Bounds(TupleFactory factory, Map<Relation,TupleSet> lower, Map<Relation,TupleSet> upper, SparseSequence<TupleSet> intbounds) {
    this.factory = factory;
    this.lowers = lower;
    this.uppers = upper;
    this.intbounds = intbounds;
    this.relations = relations(lowers, uppers);
    this.atom2rel = new HashMap<Object,Relation>();
    for (Entry<Relation,TupleSet> e : uppers.entrySet()) {
        addAtomRel(e.getKey(), e.getValue());
    }
}
 
Example #8
Source File: Bounds.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a Bounds object with the given factory and mappings.
 */
private Bounds(TupleFactory factory, Map<Relation, TupleSet> lower, Map<Relation, TupleSet> upper, SparseSequence<TupleSet> intbounds) {
	this.factory = factory;
	this.lowers = lower;
	this.uppers = upper;
	this.intbounds = intbounds;
	this.relations = relations(lowers, uppers);
}
 
Example #9
Source File: LeafInterpreter.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new LeafInterpreter using the given values.
 * @requires lowers.keySet() = uppers.keySet()
 * @ensures this.universe' = universe && this.relations' = lowers.keySet() &&
 * this.ints' = ints.indices && this.factory' = factory && 
 * this.ubounds' = uppers && this.lbounds' = lowers && 
 * this.ibounds' = ints
 */
private LeafInterpreter(Universe universe, Map<Relation, TupleSet> lowers, Map<Relation, TupleSet> uppers, 
		SparseSequence<TupleSet> ints, BooleanFactory factory, Map<Relation, IntRange> vars) {
	this.universe = universe;
	this.lowers = lowers;
	this.uppers = uppers;
	this.ints = ints;
	this.factory = factory;
	this.vars = vars;
}
 
Example #10
Source File: BooleanMatrix.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a boolean matrix m such that m = this if the given condition
 * evaluates to TRUE and m = other otherwise.
 *
 * @return { m: BooleanMatrix | m.dimensions = this.dimensions && all i:
 *         [0..m.dimensions.capacity) | m.elements[i] = condition =>
 *         this.elements[i], other.elements[i] }
 * @throws NullPointerException other = null || condition = null
 * @throws IllegalArgumentException !other.dimensions.equals(this.dimensions) ||
 *             this.factory != other.factory
 */
public final BooleanMatrix choice(BooleanValue condition, BooleanMatrix other) {
    checkFactory(this.factory, other.factory);
    checkDimensions(this.dims, other.dims);
    if (condition == TRUE)
        return this.clone();
    else if (condition == FALSE)
        return other.clone();

    final BooleanMatrix ret = new BooleanMatrix(dims, factory);
    final SparseSequence<BooleanValue> otherCells = other.cells;
    for (IndexedEntry<BooleanValue> e0 : cells) {
        BooleanValue v1 = otherCells.get(e0.index());
        if (v1 == null)
            ret.fastSet(e0.index(), factory.and(condition, e0.value()));
        else
            ret.fastSet(e0.index(), factory.ite(condition, e0.value(), v1));
    }
    for (IndexedEntry<BooleanValue> e1 : other.cells) {
        if (!cells.containsIndex(e1.index()))
            ret.fastSet(e1.index(), factory.and(condition.negation(), e1.value()));
    }

    BooleanValue of = factory.ite(condition, defCond().getOverflow(), other.defCond().getOverflow());
    BooleanValue accumOF = factory.ite(condition, defCond().getAccumOverflow(), other.defCond().getAccumOverflow());
    ret.defCond().setOverflows(of, accumOF);
    return ret;
}
 
Example #11
Source File: Propagator.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Constructs a new propagator.
 */
private Propagator(Set<Formula> conjuncts, SparseSequence<Expression> empties, SparseSequence<IntConstant> constants) {
	super(empties, constants);
	this.conjuncts = conjuncts;
	this.negs = new LinkedHashSet<Formula>();
	for(Formula f: conjuncts) { 
		if (f instanceof NotFormula)
			negs.add(((NotFormula)f).formula());
	}
}
 
Example #12
Source File: Instance.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private Instance(Universe u, Map<Relation,TupleSet> tuples, SparseSequence<TupleSet> ints) {
    if (u == null)
        throw new NullPointerException("universe=null");
    this.universe = u;
    this.tuples = tuples;
    this.ints = ints;
}
 
Example #13
Source File: BooleanMatrix.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the dot product of this and other matrix, using conjunction instead
 * of multiplication and disjunction instead of addition.
 *
 * @return { m: BooleanMatrix | m = this*other }
 * @throws NullPointerException other = null
 * @throws IllegalArgumentException this.factory != other.factory
 * @throws IllegalArgumentException dimensions incompatible for multiplication
 */
public final BooleanMatrix dot(final BooleanMatrix other) {
    checkFactory(this.factory, other.factory);

    final BooleanMatrix ret = new BooleanMatrix(dims.dot(other.dims), factory, cells, other.cells);
    ret.mergeDefConds(this, other);

    if (cells.isEmpty() || other.cells.isEmpty())
        return ret;

    final SparseSequence<BooleanValue> mutableCells = ret.clone().cells;
    final int b = other.dims.dimension(0);
    final int c = other.dims.capacity() / b;

    for (IndexedEntry<BooleanValue> e0 : cells) {
        int i = e0.index();
        BooleanValue iVal = e0.value();
        int rowHead = (i % b) * c, rowTail = rowHead + c - 1;
        for (Iterator<IndexedEntry<BooleanValue>> iter1 = other.cells.iterator(rowHead, rowTail); iter1.hasNext();) {
            IndexedEntry<BooleanValue> e1 = iter1.next();
            BooleanValue retVal = factory.and(iVal, e1.value());
            if (retVal != FALSE) {
                int k = (i / b) * c + e1.index() % c;
                if (retVal == TRUE)
                    mutableCells.put(k, TRUE);
                else {
                    BooleanValue kVal = mutableCells.get(k);
                    if (kVal != TRUE) {
                        if (kVal == null) {
                            kVal = BooleanAccumulator.treeGate(OR);
                            mutableCells.put(k, kVal);
                        }
                        ((BooleanAccumulator) kVal).add(retVal);
                    }
                }
            }
        }
    }

    // make mutable gates immutable
    for (IndexedEntry<BooleanValue> e : mutableCells) {
        if (e.value() != TRUE) {
            ret.fastSet(e.index(), factory.accumulate((BooleanAccumulator) e.value()));
        } else {
            ret.fastSet(e.index(), TRUE);
        }
    }

    return ret;
}
 
Example #14
Source File: LeafInterpreter.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Returns an empty interpreter which cannot be {@linkplain #extend(Set, Map, Map) extended} with new mappings.
 * @return some l: LeafInterpreter | l.universe = universe && no l.relations  && 
 *           no l.ints && l.factory = BooleanFactory.constantFactory(options)  
 */
@SuppressWarnings("unchecked")
static final LeafInterpreter empty(Universe universe, Options options) {
	return new LeafInterpreter(universe, (Map<Relation, TupleSet>)Collections.EMPTY_MAP, 
			(SparseSequence<TupleSet>)Ints.EMPTY_SEQUENCE, options);
}
 
Example #15
Source File: Instance.java    From kodkod with MIT License 4 votes vote down vote up
private Instance(Universe u, Map<Relation, TupleSet> tuples, SparseSequence<TupleSet> ints) {
	this.universe = u;
	this.tuples = tuples;
	this.ints = ints;
}
 
Example #16
Source File: BooleanMatrix.java    From kodkod with MIT License 4 votes vote down vote up
/**
    * Returns the dot product of this and other matrix, using conjunction instead of 
    * multiplication and disjunction instead of addition.
    * 
    * @return { m: BooleanMatrix | m = this*other }
    * @throws NullPointerException  other = null
    * @throws IllegalArgumentException  this.factory != other.factory
    * @throws IllegalArgumentException  dimensions incompatible for multiplication
    */
public final BooleanMatrix dot(final BooleanMatrix other) {  
	checkFactory(this.factory, other.factory);
	
	final BooleanMatrix ret =  new BooleanMatrix(dims.dot(other.dims), factory, cells, other.cells);
	if (cells.isEmpty() || other.cells.isEmpty()) return ret;
	
	final SparseSequence<BooleanValue> mutableCells = ret.clone().cells;
	final int b = other.dims.dimension(0); 
	final int c = other.dims.capacity() / b; 
	
	for(IndexedEntry<BooleanValue> e0 : cells) {
		int i = e0.index();
		BooleanValue iVal = e0.value();
		int rowHead = (i % b)*c, rowTail = rowHead + c - 1;
		for(Iterator<IndexedEntry<BooleanValue>> iter1 = other.cells.iterator(rowHead, rowTail); iter1.hasNext();) {
			IndexedEntry<BooleanValue> e1 = iter1.next();
			BooleanValue retVal = factory.and(iVal, e1.value());
			if (retVal != FALSE) {
				int k = (i / b)*c + e1.index()%c;
				if (retVal==TRUE) mutableCells.put(k, TRUE);
				else {
					BooleanValue kVal = mutableCells.get(k);
					if (kVal != TRUE) {
						if (kVal==null) {
							kVal = BooleanAccumulator.treeGate(OR);
							mutableCells.put(k, kVal);
						} 
						((BooleanAccumulator) kVal).add(retVal);
					}
				}
			}
		}		
	}
	
	// make mutable gates immutable
	for(IndexedEntry<BooleanValue> e : mutableCells) {
		if (e.value()!=TRUE) {
			ret.fastSet(e.index(), factory.accumulate((BooleanAccumulator) e.value()));
		} else {
			ret.fastSet(e.index(), TRUE);
		}
	}
	return ret;
}
 
Example #17
Source File: LeafInterpreter.java    From org.alloytools.alloy with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a new LeafInterpreter using the given values.
 *
 * @requires lowers.keySet() = uppers.keySet()
 * @ensures this.universe' = universe && this.relations' = lowers.keySet() &&
 *          this.ints' = ints.indices && this.factory' = factory &&
 *          this.ubounds' = uppers && this.lbounds' = lowers && this.ibounds' =
 *          ints
 */
private LeafInterpreter(Universe universe, Map<Relation,TupleSet> lowers, Map<Relation,TupleSet> uppers, SparseSequence<TupleSet> ints, BooleanFactory factory, Map<Relation,IntRange> vars) {
    this.universe = universe;
    this.lowers = lowers;
    this.uppers = uppers;
    this.ints = ints;
    this.factory = factory;
    this.vars = vars;
}
 
Example #18
Source File: LeafInterpreter.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Constructs a new LeafInterpreter using the given values.
 * @requires lowers.keySet() = uppers.keySet()
 * @ensures this.universe' = universe && this.relations' = lowers.keySet() &&
 * this.ints' = ints.indices && this.factory' = factory && 
 * this.ubounds' = uppers && this.lbounds' = lowers && 
 * this.ibounds' = ints
 */
@SuppressWarnings("unchecked")
private LeafInterpreter(Universe universe, Map<Relation, TupleSet> rbound, SparseSequence<TupleSet> ints, Options options) {
	this(universe, rbound, rbound, ints, BooleanFactory.constantFactory(options), Collections.EMPTY_MAP);
}
 
Example #19
Source File: Bounds.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Returns a sparse sequence view of this.intBound.  The returned sequence
 * is not modifiable.
 * @return a sparse sequence view of this.intBound
 */
public SparseSequence<TupleSet> intBounds() {
	return unmodifiableSequence(intbounds);
}
 
Example #20
Source File: BooleanMatrix.java    From kodkod with MIT License 2 votes vote down vote up
/**  
 * Constructs a new matrix with the given dimensions, factory, and entries.
 * 
 * @requires dimensions != null && factory != null && seq != null
 * @requires seq.indices() in [0..dimensions.capacity)
 * @ensures this.dimensions' = dimensions && this.factory' = factory && 
 *          this.elements' = [0..dimensions.capacity)->one FALSE 
 */
private BooleanMatrix(Dimensions dimensions, BooleanFactory factory, SparseSequence<BooleanValue> seq) {
	this.dims = dimensions;
	this.factory = factory;
	this.cells = seq;
}
 
Example #21
Source File: Instance.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Returns a sparse sequence view of int<:this.tuples.  The returned sequence is unmodifiable.
 * @return a sparse sequence view of int<:this.tuples.
 */
public SparseSequence<TupleSet> intTuples() {
	return Ints.unmodifiableSequence(ints);
}
 
Example #22
Source File: LeafInterpreter.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an empty interpreter which cannot be
 * {@linkplain #extend(Set, Map, Map) extended} with new mappings.
 *
 * @return some l: LeafInterpreter | l.universe = universe && no l.relations &&
 *         no l.ints && l.factory = BooleanFactory.constantFactory(options)
 */
@SuppressWarnings("unchecked" )
static final LeafInterpreter empty(Universe universe, Options options) {
    return new LeafInterpreter(universe, (Map<Relation,TupleSet>) Collections.EMPTY_MAP, (SparseSequence<TupleSet>) Ints.EMPTY_SEQUENCE, options);
}
 
Example #23
Source File: LeafInterpreter.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new LeafInterpreter using the given values.
 *
 * @requires lowers.keySet() = uppers.keySet()
 * @ensures this.universe' = universe && this.relations' = lowers.keySet() &&
 *          this.ints' = ints.indices && this.factory' = factory &&
 *          this.ubounds' = uppers && this.lbounds' = lowers && this.ibounds' =
 *          ints
 */
@SuppressWarnings("unchecked" )
private LeafInterpreter(Universe universe, Map<Relation,TupleSet> rbound, SparseSequence<TupleSet> ints, Options options) {
    this(universe, rbound, rbound, ints, BooleanFactory.constantFactory(options), Collections.EMPTY_MAP);
}
 
Example #24
Source File: BooleanMatrix.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new matrix with the given dimensions, factory, and entries.
 *
 * @requires dimensions != null && factory != null && seq != null
 * @requires seq.indices() in [0..dimensions.capacity)
 * @ensures this.dimensions' = dimensions && this.factory' = factory &&
 *          this.elements' = [0..dimensions.capacity)->one FALSE
 */
private BooleanMatrix(Dimensions dimensions, BooleanFactory factory, SparseSequence<BooleanValue> seq) {
    this.dims = dimensions;
    this.factory = factory;
    this.cells = seq;
}
 
Example #25
Source File: Instance.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a sparse sequence view of int<:this.tuples. The returned sequence is
 * unmodifiable.
 *
 * @return a sparse sequence view of int<:this.tuples.
 */
public SparseSequence<TupleSet> intTuples() {
    return Ints.unmodifiableSequence(ints);
}
 
Example #26
Source File: Bounds.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a sparse sequence view of this.intBound. The returned sequence is not
 * modifiable.
 *
 * @return a sparse sequence view of this.intBound
 */
public SparseSequence<TupleSet> intBounds() {
    return unmodifiableSequence(intbounds);
}