Search in sources :

Example 1 with IntegerIterator

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()));
}
Also used : IntegerIterator(com.sri.ai.util.collect.IntegerIterator)

Example 2 with IntegerIterator

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;
}
Also used : FunctionIterator(com.sri.ai.util.collect.FunctionIterator) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IntegerIterator(com.sri.ai.util.collect.IntegerIterator)

Aggregations

IntegerIterator (com.sri.ai.util.collect.IntegerIterator)2 Util.mapIntoList (com.sri.ai.util.Util.mapIntoList)1 FunctionIterator (com.sri.ai.util.collect.FunctionIterator)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1