Browse Source

some more helpers for iterators / predicates [with Steffen Maercker]

git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@12120 bbc10eb1-c90d-0410-af57-cb519fbb1720
master
Joachim Klein 9 years ago
parent
commit
61701b9f1b
  1. 35
      prism/src/common/functions/primitive/PairPredicateInt.java
  2. 164
      prism/src/common/iterable/FilteringIterable.java
  3. 463
      prism/src/common/iterable/MappingIterator.java
  4. 70
      prism/src/common/iterable/SingletonIterable.java

35
prism/src/common/functions/primitive/PairPredicateInt.java

@ -0,0 +1,35 @@
//==============================================================================
//
// Copyright (c) 2016-
// Authors:
// * Steffen Maercker <maercker@tcs.inf.tu-dresden.de> (TU Dresden)
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden)
//
//------------------------------------------------------------------------------
//
// This file is part of PRISM.
//
// PRISM is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PRISM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PRISM; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//==============================================================================
package common.functions.primitive;
/** Functional interface for a predicate (int, int) -> boolean */
@FunctionalInterface
public interface PairPredicateInt
{
public abstract boolean test(int element1, int element2);
}

164
prism/src/common/iterable/FilteringIterable.java

@ -0,0 +1,164 @@
//==============================================================================
//
// Copyright (c) 2016-
// Authors:
// * Steffen Maercker <maercker@tcs.inf.tu-dresden.de> (TU Dresden)
// * Joachim Klein <klein@tcs.inf.tu-dresden.de> (TU Dresden)
//
//------------------------------------------------------------------------------
//
// This file is part of PRISM.
//
// PRISM is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PRISM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PRISM; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//==============================================================================
package common.iterable;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.function.DoublePredicate;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
/**
* Base class for Iterables around FilteringIterators,
* static constructors for deduplicating entries. */
public abstract class FilteringIterable<T> implements Iterable<T>
{
protected final Iterable<T> iterable;
public FilteringIterable(final Iterable<T> iterable)
{
this.iterable = iterable;
}
public static class Of<T> extends FilteringIterable<T>
{
private Predicate<? super T> predicate;
public Of(Iterable<T> iterable, Predicate<? super T> predicate)
{
super(iterable);
this.predicate = predicate;
}
@Override
public Iterator<T> iterator()
{
return new FilteringIterator.Of<>(iterable, predicate);
}
}
public static class OfInt extends FilteringIterable<Integer> implements IterableInt
{
private IntPredicate predicate;
public OfInt(IterableInt iterable, IntPredicate predicate)
{
super(iterable);
this.predicate = predicate;
}
@Override
public PrimitiveIterator.OfInt iterator()
{
return new FilteringIterator.OfInt((IterableInt) iterable, predicate);
}
}
public static class OfLong extends FilteringIterable<Long> implements IterableLong
{
private LongPredicate predicate;
public OfLong(IterableLong iterable, LongPredicate predicate)
{
super(iterable);
this.predicate = predicate;
}
@Override
public PrimitiveIterator.OfLong iterator()
{
return new FilteringIterator.OfLong((IterableLong) iterable, predicate);
}
}
public static class OfDouble extends FilteringIterable<Double> implements IterableDouble
{
private DoublePredicate predicate;
public OfDouble(IterableDouble iterable, DoublePredicate predicate)
{
super(iterable);
this.predicate = predicate;
}
@Override
public PrimitiveIterator.OfDouble iterator()
{
return new FilteringIterator.OfDouble((IterableDouble) iterable, predicate);
}
}
public static IterableInt dedupe(IterableInt iterable)
{
return new IterableInt()
{
@Override
public PrimitiveIterator.OfInt iterator()
{
return FilteringIterator.dedupe(iterable.iterator());
}
};
}
public static IterableLong dedupe(IterableLong iterable)
{
return new IterableLong()
{
@Override
public PrimitiveIterator.OfLong iterator()
{
return FilteringIterator.dedupe(iterable.iterator());
}
};
}
public static IterableDouble dedupe(IterableDouble iterable)
{
return new IterableDouble()
{
@Override
public PrimitiveIterator.OfDouble iterator()
{
return FilteringIterator.dedupe(iterable.iterator());
}
};
}
public static <T> Iterable<T> dedupe(Iterable<T> iterable)
{
return new Iterable<T>()
{
@Override
public Iterator<T> iterator()
{
return FilteringIterator.dedupe(iterable.iterator());
}
};
}
}

463
prism/src/common/iterable/MappingIterator.java

@ -0,0 +1,463 @@
package common.iterable;
import java.util.Iterator;
import java.util.PrimitiveIterator.OfDouble;
import java.util.PrimitiveIterator.OfInt;
import java.util.PrimitiveIterator.OfLong;
import java.util.function.DoubleFunction;
import java.util.function.DoubleToIntFunction;
import java.util.function.DoubleToLongFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.LongFunction;
import java.util.function.LongToDoubleFunction;
import java.util.function.LongToIntFunction;
import java.util.function.LongUnaryOperator;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
/** Helpers for mapping Iterators to another Iterator, performing some mapping on the elements */
public abstract class MappingIterator<S, T> implements Iterator<T>
{
protected final Iterator<S> iterator;
public MappingIterator(Iterable<S> iterable)
{
this(iterable.iterator());
}
public MappingIterator(Iterator<S> iterator)
{
this.iterator = iterator;
}
@Override
public boolean hasNext()
{
return iterator.hasNext();
}
@Override
public void remove()
{
iterator.remove();
}
/** Map an Iterator<S> to an Iterator<T> using the given mapping function */
public static class From<S, T> extends MappingIterator<S, T>
{
protected final Function<? super S, T> function;
public From(Iterable<S> iterable, Function<? super S, T> function)
{
this(iterable.iterator(), function);
}
public From(Iterator<S> iterator, Function<? super S, T> function)
{
super(iterator);
this.function = function;
}
@Override
public T next()
{
return function.apply(iterator.next());
}
}
/** Map an Iterable<Integer> to a PrimitiveIterator.OfInt */
public static OfInt toInt(Iterable<Integer> iterable)
{
return toInt(iterable.iterator());
}
/** Map an Iterator<Integer> to a PrimitiveIterator.OfInt */
public static OfInt toInt(Iterator<Integer> iterator)
{
if (iterator instanceof OfInt) {
return (OfInt) iterator;
}
return new ToInt<>(iterator, Integer::intValue);
}
/** Map an Iterator<S> to a PrimitiveIterator.OfInt using the given mapping function */
public static class ToInt<S> extends MappingIterator<S, Integer> implements OfInt
{
protected ToIntFunction<? super S> function;
public ToInt(Iterable<S> iterable, ToIntFunction<? super S> function)
{
this(iterable.iterator(), function);
}
public ToInt(Iterator<S> iterator, ToIntFunction<? super S> function)
{
super(iterator);
this.function = function;
}
@Override
public int nextInt()
{
return function.applyAsInt(iterator.next());
}
}
/** Map an Iterable<Double> to a PrimitiveIterator.OfDouble */
public static OfDouble toDouble(Iterable<Double> iterable)
{
return toDouble(iterable.iterator());
}
/** Map an Iterator<Double> to a PrimitiveIterator.OfDouble */
public static OfDouble toDouble(Iterator<Double> iterator)
{
if (iterator instanceof OfDouble) {
return (OfDouble) iterator;
}
return new ToDouble<>(iterator, Double::doubleValue);
}
/** Map an Iterator<S> to a PrimitiveIterator.OfDouble using the given mapping function */
public static class ToDouble<S> extends MappingIterator<S, Double> implements OfDouble
{
protected ToDoubleFunction<? super S> function;
public ToDouble(Iterable<S> iterable, ToDoubleFunction<? super S> function)
{
this(iterable.iterator(), function);
}
public ToDouble(Iterator<S> iterator, ToDoubleFunction<? super S> function)
{
super(iterator);
this.function = function;
}
@Override
public double nextDouble()
{
return function.applyAsDouble(iterator.next());
}
}
/** Map an Iterable<Long> to a PrimitiveIterator.OfLong */
public static OfLong toLong(Iterable<Long> iterable)
{
return toLong(iterable.iterator());
}
/** Map an Iterator<Long> to a PrimitiveIterator.OfLong */
public static OfLong toLong(Iterator<Long> iterator)
{
if (iterator instanceof OfLong) {
return (OfLong) iterator;
}
return new ToLong<>(iterator, Long::longValue);
}
/** Map an Iterator<S> to a PrimitiveIterator.OfLong using the given mapping function */
public static class ToLong<S> extends MappingIterator<S, Long> implements OfLong
{
protected ToLongFunction<? super S> function;
public ToLong(Iterable<S> iterable, ToLongFunction<? super S> function)
{
this(iterable.iterator(), function);
}
public ToLong(Iterator<S> iterator, ToLongFunction<? super S> function)
{
super(iterator);
this.function = function;
}
@Override
public long nextLong()
{
return function.applyAsLong(iterator.next());
}
}
/** Map an iterator over integers to an iterator over T */
public static class FromInt<T> extends MappingIterator<Integer, T>
{
protected IntFunction<T> function;
public FromInt(IterableInt iterable, IntFunction<T> function)
{
this(iterable.iterator(), function);
}
public FromInt(OfInt iterator, IntFunction<T> function)
{
super(iterator);
this.function = function;
}
@Override
public T next()
{
return function.apply(((OfInt) iterator).nextInt());
}
}
/** Map an iterator over integers to another iterator over integers */
public static class FromIntToInt extends MappingIterator<Integer, Integer> implements OfInt
{
protected IntUnaryOperator function;
public FromIntToInt(IterableInt iterable, IntUnaryOperator function)
{
this(iterable.iterator(), function);
}
public FromIntToInt(OfInt iterator, IntUnaryOperator function)
{
super(iterator);
this.function = function;
}
@Override
public int nextInt()
{
return function.applyAsInt(((OfInt) iterator).nextInt());
}
}
/** Map an iterator over integers to iterator over doubles */
public static class FromIntToDouble extends MappingIterator<Integer, Double> implements OfDouble
{
protected IntToDoubleFunction function;
public FromIntToDouble(IterableInt iterable, IntToDoubleFunction function)
{
this(iterable.iterator(), function);
}
public FromIntToDouble(OfInt iterator, IntToDoubleFunction function)
{
super(iterator);
this.function = function;
}
@Override
public double nextDouble()
{
return function.applyAsDouble(((OfInt) iterator).nextInt());
}
}
/** Map an iterator over integers to iterator over longs */
public static class FromIntToLong extends MappingIterator<Integer, Long> implements OfLong
{
protected IntToLongFunction function;
public FromIntToLong(IterableInt iterable, IntToLongFunction function)
{
this(iterable.iterator(), function);
}
public FromIntToLong(OfInt iterator, IntToLongFunction function)
{
super(iterator);
this.function = function;
}
@Override
public long nextLong()
{
return function.applyAsLong(((OfInt) iterator).nextInt());
}
}
/** Map an iterator over doubles to an iterator over T */
public static class FromDouble<T> extends MappingIterator<Double, T>
{
protected DoubleFunction<T> function;
public FromDouble(IterableDouble iterable, DoubleFunction<T> function)
{
this(iterable.iterator(), function);
}
public FromDouble(OfDouble iterator, DoubleFunction<T> function)
{
super(iterator);
this.function = function;
}
@Override
public T next()
{
return function.apply(((OfDouble) iterator).nextDouble());
}
}
/** Map an iterator over doubles to an iterator over integers */
public static class FromDoubleToInt extends MappingIterator<Double, Integer> implements OfInt
{
protected DoubleToIntFunction function;
public FromDoubleToInt(IterableDouble iterable, DoubleToIntFunction function)
{
this(iterable.iterator(), function);
}
public FromDoubleToInt(OfDouble iterator, DoubleToIntFunction function)
{
super(iterator);
this.function = function;
}
@Override
public int nextInt()
{
return function.applyAsInt(((OfDouble) iterator).nextDouble());
}
}
/** Map an iterator over doubles to another iterator over doubles */
public static class FromDoubleToDouble extends MappingIterator<Double, Double> implements OfDouble
{
protected DoubleUnaryOperator function;
public FromDoubleToDouble(IterableDouble iterable, DoubleUnaryOperator function)
{
this(iterable.iterator(), function);
}
public FromDoubleToDouble(OfDouble iterator, DoubleUnaryOperator function)
{
super(iterator);
this.function = function;
}
@Override
public double nextDouble()
{
return function.applyAsDouble(((OfDouble) iterator).nextDouble());
}
}
/** Map an iterator over doubles to an iterator over longs */
public static class FromDoubleToLong extends MappingIterator<Double, Long> implements OfLong
{
protected DoubleToLongFunction function;
public FromDoubleToLong(IterableDouble iterable, DoubleToLongFunction function)
{
this(iterable.iterator(), function);
}
public FromDoubleToLong(OfDouble iterator, DoubleToLongFunction function)
{
super(iterator);
this.function = function;
}
@Override
public long nextLong()
{
return function.applyAsLong(((OfDouble) iterator).nextDouble());
}
}
/** Map an iterator over longs to an iterator over T */
public static class FromLong<T> extends MappingIterator<Long, T>
{
protected LongFunction<T> function;
public FromLong(IterableLong iterable, LongFunction<T> function)
{
this(iterable.iterator(), function);
}
public FromLong(OfLong iterator, LongFunction<T> function)
{
super(iterator);
this.function = function;
}
@Override
public T next()
{
return function.apply(((OfLong) iterator).nextLong());
}
}
/** Map an iterator over longs to an iterator over integers */
public static class FromLongToInt extends MappingIterator<Long, Integer> implements OfInt
{
protected LongToIntFunction function;
public FromLongToInt(IterableLong iterable, LongToIntFunction function)
{
this(iterable.iterator(), function);
}
public FromLongToInt(OfLong iterator, LongToIntFunction function)
{
super(iterator);
this.function = function;
}
@Override
public int nextInt()
{
return function.applyAsInt(((OfLong) iterator).nextLong());
}
}
/** Map an iterator over longs to an iterator over doubles */
public static class FromLongToDouble extends MappingIterator<Long, Double> implements OfDouble
{
protected LongToDoubleFunction function;
public FromLongToDouble(IterableLong iterable, LongToDoubleFunction function)
{
this(iterable.iterator(), function);
}
public FromLongToDouble(OfLong iterator, LongToDoubleFunction function)
{
super(iterator);
this.function = function;
}
@Override
public double nextDouble()
{
return function.applyAsDouble(((OfLong) iterator).nextLong());
}
}
/** Map an iterator over longs to another iterator over longs */
public static class FromLongToLong extends MappingIterator<Long, Long> implements OfLong
{
protected LongUnaryOperator function;
public FromLongToLong(IterableLong iterable, LongUnaryOperator function)
{
this(iterable.iterator(), function);
}
public FromLongToLong(OfLong iterator, LongUnaryOperator function)
{
super(iterator);
this.function = function;
}
@Override
public long nextLong()
{
return function.applyAsLong(((OfLong) iterator).nextLong());
}
}
}

70
prism/src/common/iterable/SingletonIterable.java

@ -0,0 +1,70 @@
package common.iterable;
import java.util.Iterator;
public abstract class SingletonIterable<T> implements Iterable<T>
{
public static class Of<T> extends SingletonIterable<T>
{
final T element;
public Of(T theElement)
{
element = theElement;
}
@Override
public Iterator<T> iterator()
{
return new SingletonIterator.Of<>(element);
}
}
public static class OfInt extends SingletonIterable<Integer> implements IterableInt
{
final int element;
public OfInt(int theElement)
{
element = theElement;
}
@Override
public SingletonIterator.OfInt iterator()
{
return new SingletonIterator.OfInt(element);
}
}
public static class OfLong extends SingletonIterable<Long> implements IterableLong
{
final long element;
public OfLong(long theElement)
{
element = theElement;
}
@Override
public SingletonIterator.OfLong iterator()
{
return new SingletonIterator.OfLong(element);
}
}
public static class OfDouble extends SingletonIterable<Double> implements IterableDouble
{
final double element;
public OfDouble(double theElement)
{
element = theElement;
}
@Override
public SingletonIterator.OfDouble iterator()
{
return new SingletonIterator.OfDouble(element);
}
}
}
Loading…
Cancel
Save