Java Code Examples for kodkod.util.ints.SparseSequence#get()

The following examples show how to use kodkod.util.ints.SparseSequence#get() . 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 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 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 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 4
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 5
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 6
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;
}