question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multirator : an iterator that iterates over other iterators

See original GitHub issue

Original issue created by thierryler on 2014-04-15 at 11:29 AM


Hello,

Would you like to add a multirator in Guava ? A multirator (named choosen in 5 minutes) is an iterator that encapsulates other iterators. I gives the next element based on the next elements of the other iterators.

Here is what i looks like :

public interface Multirator<E> extends Iterator<E>, Iterable<E> {     void add(final Iterator<E>… iterators);     void add(final Iterable<E>… iterables); }

I wrote a first implementation who choose the next element as the smallest available :

public class SortedPeekingMultirator<E extends Comparable<E>> implements Multirator<E> {

private final List<PeekingIterator<E>> iterators = new ArrayList<>();

/* Constructors */

@SafeVarargs
public SortedPeekingMultirator(final Iterator<E>... iterators) {
    add(iterators);
}

@SafeVarargs
public SortedPeekingMultirator(final Iterable<E>... iterables) {
    add(iterables);
}

/* From Multirator.java */

@SuppressWarnings("unchecked")
@Override
public void add(final Iterator<E>... iterators) {
    for (final Iterator<E> iter : iterators) {
        final PeekingIterator<E> pi = Iterators.peekingIterator(iter);
        this.iterators.add(pi);
    }
}

@SuppressWarnings("unchecked")
@Override
public void add(final Iterable<E>... iterables) {
    for (final Iterable<E> iterable : iterables) {
        add(iterable.iterator());
    }
}

/* From Iterator.java */


@Override
public boolean hasNext() {

    for (final PeekingIterator<E> pi : iterators) {
        if (pi.hasNext()) {
            return true;
        }
    }

    return false;
}


@Override
public E next() {

    if (!hasNext()) {
        throw new NoSuchElementException();
    }

    E smallest = null;
    int pos = 0;
    int i = 0;

    for (final PeekingIterator<E> pi : iterators) {

        if (!pi.hasNext()) {
            i++;
            continue;
        }

        final E elt = pi.peek();

        if (smallest == null) {
            smallest = elt;
            pos = i++;
            continue;
        }

        if (smallest.compareTo(elt) > 0) {
            smallest = elt;
            pos = i++;
        }
    }

    // next
    iterators.get(pos).next();

    return smallest;
}

}

Please have a look at the attached files for complete source code and tests.

What do you think about this multirator ?

Thierry

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gissuebotcommented, Nov 1, 2014

Original comment posted by thierryler on 2014-04-15 at 02:00 PM


Well I was not aware of the method “concat”. It looks great.

But I would say that it does not provide exactly the same thing. With concat, you can not specify the order for example. And it works with copies.

0reactions
ronshapirocommented, May 7, 2019

Closing this in favor of the alternatives presented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

From An Iterator of Iterators to Cantor's Paradise: A Deep Dive ...
A deep dive into flattening an iterator of iterators and a segue into set ... Note how the two iterators are iterating over...
Read more >
Python Basics: Iteration, Iterables, Iterators, and Looping
An iterable is something you can loop over. An iterator is an object representing a stream of data. It does the iterating over...
Read more >
Python Iterators (__iter__ and __next__) - Programiz
A more elegant way of automatically iterating is by using the for loop. Using this, we can iterate over any object that can...
Read more >
4. Iterators and Generators - Python Cookbook, 3rd ... - O'Reilly
Iterators and Generators Iteration is one of Python's strongest features. ... If all you are doing is iterating over the contents of another...
Read more >
Iterators in Python - GeeksforGeeks
Iterator in Python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found