Java Code Examples for kodkod.instance.TupleSet#iterator()

The following examples show how to use kodkod.instance.TupleSet#iterator() . 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: SudokuParser.java    From kodkod with MIT License 6 votes vote down vote up
/**
 * Returns a pretty-printed string of the given sudoku solution.
 * @requires solution is a valid sudoku solution
 * @requires some r: int | solution.universe = { i: Integer | 1 <= i.intValue() <= r*r }
 * @return a pretty-printed string of the given sudoku solution
 */
public static final String prettyPrint(TupleSet solution) { 
	final StringBuilder str = new StringBuilder();
	final int n = solution.universe().size();
	final int r = (int)Math.sqrt(n);
	appendDivider(str, r);
	final Iterator<Tuple> psol = solution.iterator();
	for(int i = 1; i <= n; i++) {
		str.append("| ");
		for(int j = 0; j < r; j++) {
			for(int k = 0; k < r; k++) {
				final int atom = (Integer)psol.next().atom(2);
				if (atom<10&&r>3) str.append(" ");
				str.append(atom);
				str.append(" ");
			}
			str.append("| ");
		}
		str.append("\n");
		if (i%r==0)	appendDivider(str, r);		
	}
	return str.toString();
}
 
Example 2
Source File: SudokuParser.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a string representation of the given puzzle.
 * @requires some r: int | puzzle.universe.atoms[int] = { i: Integer | 1 <= i.intValue() <= r*r } 
    * @requires puzzle.arity = 3   
 * @return a string representation of the given puzzle
 */
public static final String toString(TupleSet puzzle) { 
	final StringBuilder str = new StringBuilder();
	final int n = puzzle.universe().size();
	final String sep = (n>9) ? " " : "";
	Iterator<Tuple> itr = puzzle.iterator();
	if (!itr.hasNext()) { 
		str.append(0);
		for(int i = 1, max = n*n; i < max; i++) { 
			str.append(sep+0);
		}
		return str.toString();
	}
	
	int last = 0;
	Tuple tuple = itr.next();
	if ((Integer)tuple.atom(0)==1 && (Integer)tuple.atom(1)==1) { 
		str.append(tuple.atom(2));
	} else {
		str.append(0);
		itr = puzzle.iterator();
	}
	
	while(itr.hasNext()) { 
		tuple = itr.next();
		final int current = n*((Integer)tuple.atom(0)-1) + ((Integer)tuple.atom(1)-1);
		for(int i = last+1; i < current; i++) { 
			str.append(sep+0);
		}
		str.append(sep+tuple.atom(2));
		last = current;
	}

	for(int i = last+1, max = n*n; i < max; i++) { 
		str.append(sep+0);
	}
	return str.toString();
}