Class MultiSortedBasePipe<T>

  • Type Parameters:
    T - The data type of the items this pipe works with
    All Implemented Interfaces:
    Closeable, AutoCloseable, BasePipe, Pipe<T>
    Direct Known Subclasses:
    SortedIntersectionPipe, SortedSubtractionPipe, SortedUnionPipe

    public abstract class MultiSortedBasePipe<T>
    extends Object
    implements Pipe<T>
    A base class for pipes performing some set operation on a collection of sorted input pipes. The input pipes must have the same item data type, and must all be ordered by the same order relation. The order relation (comparator) must be consistent with equality (java's Object.equals): a equals b if and only if compare(a,b) == 0. Input pipes may have item repetitions, but they are always treated as sets, where repetitions make no difference. The output of the pipe is always sorted and includes no duplicates.
    Author:
    Eyal Schneider
    • Constructor Detail

      • MultiSortedBasePipe

        public MultiSortedBasePipe​(List<? extends Pipe<T>> inputPipes,
                                   Comparator<? super T> comparator)
        Constructor
        Parameters:
        inputPipes - The input pipes. Each of them must be sorted by the order defined in the provided comparator, and items may have duplicates. The order in which the pipes collection is given is used in the shouldOutput(..) method.
        comparator - The total order relation to use when merging the data from the different input pipes. Should be consistent with the ordering of the input pipes. IMPORTANT NOTE: comparator must be consistent with equals: a.equals(b) if and only if comparator.compare(a,b)==0.
      • MultiSortedBasePipe

        @SafeVarargs
        public MultiSortedBasePipe​(Comparator<? super T> comparator,
                                   Pipe<T>... inputPipes)
        Constructor
        Parameters:
        comparator - The total order relation to use when merging the data from the different input pipes. Should be consistent with the ordering of the input pipes. IMPORTANT NOTE: comparator must be consistent with equals: a.equals(b) if and only if comparator.compare(a,b)==0.
        inputPipes - The input pipes. Each of them must be sorted by the order defined in the provided comparator, and items may have duplicates. The order in which the pipes collection is given is used in the shouldOutput(..) method.
    • Method Detail

      • start

        public void start()
                   throws PipeException,
                          InterruptedException
        Description copied from interface: BasePipe
        Performs pre-processing prior to item flow throw the pipe. Implementations must call the same method for all their input pipes before accessing their items. This is typically done here.
        Specified by:
        start in interface BasePipe
        Throws:
        PipeException - In case of pipe errors in this pipe or somewhere up-stream.
        InterruptedException - In case that the operation has been interrupted by another thread.
      • next

        public T next()
               throws PipeException,
                      InterruptedException
        Specified by:
        next in interface Pipe<T>
        Returns:
        The next item in this pipe output, or null if the output end has been reached. May be a blocking operation.
        Throws:
        PipeException - In case of pipe errors in this pipe or somewhere up-stream while trying to prepare next item to return.
        InterruptedException - In case that the operation has been interrupted by another thread.
      • peek

        public T peek()
        Specified by:
        peek in interface Pipe<T>
        Returns:
        The next item in the pipe's output. Does not remove it, so next call to next() will return it.
      • shouldOutput

        protected abstract boolean shouldOutput​(T item,
                                                BitSet pipesSeen)
        The method to override for determining whether a visited value should be part of the output or not
        Parameters:
        item - The item
        pipesSeen - A bitset specifying the different input pipes in which the item is found. Index #i corresponds to pipe #i in the pipes list provided in the ctor. Note that it is guaranteed that at least one bit is turned on.
        Returns:
        true for outputting this item, false for skipping
      • canTerminate

        protected abstract boolean canTerminate​(BitSet activePipes)
        Called whenever one of the input pipes terminated. Allows implementations to decide whether to exit early if this there's no point in continuing with the remaining active pipes.
        Parameters:
        activePipes - A bitset describing the currently active pipes. Index #i corresponds to pipe #i in the pipes list provided in the ctor.
        Returns:
        true for terminating the output, false for continuing.
      • getInputPipes

        protected List<? extends Pipe<T>> getInputPipes()
        Returns:
        The input pipes