use of com.sri.ai.util.collect.IntegerIterator in project aic-expresso by aic-sri-international.
the class IntegerInterval method iterator.
/**
* If type is bounded, iterates over its elements.
* If it is unbounded on the positive side but bounded on the negative side,
* provides an iterator from the least element that never stops.
* If it is unbounded on the negative side but bounded on the positive side,
* provides a backward iterator from the maximum element that never stops.
* If it is unbounded on both sides, iterates over 0, 1, -1, 2, -2, indefinitely.
*/
@Override
public Iterator<Expression> iterator() {
Iterator<Integer> integerIterator;
if (noLowerBound()) {
if (noUpperBound()) {
integerIterator = new // alternates between the iterators given
BreadthFirstIterator<Integer>(// natural numbers
IntegerIterator.fromThisValueOnForever(0), // backwards from -1
new IntegerIterator(-1, 1, /* upper bound, never reached */
-1));
} else {
myAssert(() -> isNumber(nonStrictUpperBound), () -> "Cannot iterate over elements of undefined interval " + this);
// interval is -infinity..nonStrictUpperBound, start from the latter backwards forever
integerIterator = new IntegerIterator(nonStrictUpperBound.intValue(), nonStrictUpperBound.intValue() + 1, /* never reached */
-1);
}
} else {
if (noUpperBound()) {
myAssert(() -> isNumber(nonStrictLowerBound), () -> "Cannot iterate over elements of undefined interval " + this);
// go from integer after strict lower bound, forward forever
integerIterator = IntegerIterator.fromThisValueOnForever(nonStrictLowerBound.intValue());
} else {
myAssert(() -> isNumber(nonStrictLowerBound) && isNumber(nonStrictUpperBound), () -> "Cannot iterate over elements of undefined interval " + this);
// just regular interval; note that IntegerIterator takes lower bound inclusive, upper bound exclusive, hence the +1's
integerIterator = new IntegerIterator(nonStrictLowerBound.intValue(), nonStrictUpperBound.intValue() + 1);
}
}
return functionIterator(integerIterator, i -> makeSymbol(i.intValue()));
}
use of com.sri.ai.util.collect.IntegerIterator in project aic-expresso by aic-sri-international.
the class Expressions method makeSingleIndexPathsIteratorFromTo.
/**
* Makes iterator over paths with a single index, starting from <code>start</code> to <code>end</code> exclusive.
*/
public static FunctionIterator<Integer, List<Integer>> makeSingleIndexPathsIteratorFromTo(int start, int end) {
IntegerIterator integerIterator = new IntegerIterator(start, end);
FunctionIterator<Integer, List<Integer>> result = new FunctionIterator<Integer, List<Integer>>(integerIterator, INTEGER_SINGLETON_LIST_MAKER);
return result;
}
Aggregations