okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

collapseDuplicates({Iterable,Iterator}, Equivalence) #1464

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Would be nice to have a static method {{Iterators.unique(Iterator<T>)}}, which 
filter out duplicates in a sorted iterator.

Original issue reported on code.google.com by egor@technoparkcorp.com on 1 Jul 2013 at 9:24

GoogleCodeExporter commented 9 years ago
I could have sworn there was another issue requesting something like this, but 
I couldn't find it.

Original comment by cgdecker@google.com on 2 Jul 2013 at 3:49

GoogleCodeExporter commented 9 years ago
Internally we've called this collapseDuplicates, but I don't see an issue under 
that name, either.

Original comment by cpov...@google.com on 2 Jul 2013 at 4:06

GoogleCodeExporter commented 9 years ago
maybe this is related: 
https://code.google.com/p/guava-libraries/issues/detail?id=524

Original comment by egor@technoparkcorp.com on 9 Aug 2013 at 7:41

GoogleCodeExporter commented 9 years ago
In my case an Iterable is of unlimited size. That's why I can't save it 
anywhere and then remove duplicates. I really need to filter them out on the 
fly. That's why iterables are for, right? To be optionally unlimited in size

Original comment by egor@technoparkcorp.com on 9 Aug 2013 at 7:58

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
This is how I implemented it in my project:

public final class Distinct<T> implements Iterator<T> {
    private final transient Iterator<T> origin;
    private final transient Set<T> seen = new LinkedHashSet<T>(0);
    private final transient AtomicReference<T> recent =
        new AtomicReference<T>();
    public Distinct(final Iterator<T> iterator) {
        this.origin = iterator;
    }
    @Override
    public boolean hasNext() {
        while (this.recent.get() == null && this.origin.hasNext()) {
            final T next = this.origin.next();
            if (!this.seen.contains(next)) {
                this.seen.add(next);
                this.recent.set(next);
            }
        }
        return this.recent.get() != null;
    }
    @Override
    public T next() {
        if (!this.hasNext()) {
            throw new NoSuchElementException();
        }
        return this.recent.getAndSet(null);
    }
    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

Original comment by egor@technoparkcorp.com on 9 Aug 2013 at 9:23

GoogleCodeExporter commented 9 years ago
I'm going to rename this bug to be specifically about sorted iterators, as the 
original post requested. Thanks for digging up issue 524 about unsorted inputs. 
I'll follow up more there about that case.

Original comment by cpov...@google.com on 9 Aug 2013 at 7:15

GoogleCodeExporter commented 9 years ago
My use case wants a way to take the union of several potentially large subsets 
of a common space, presented as sorted iterators.  mergeSorted and 
collapseDuplicates would do that nicely.

Original comment by phil.h.s...@gmail.com on 26 Apr 2014 at 6:46

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08