Laophatgia / google-collections

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

Enhancement request - PeekingListIterator #281

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I was using the collections API, and found I needed a ListIterator version
of PeekingIterator, so I wrote one (see attached).  I was looking to do a
k-way merge of some sorted iterators, and wanted to have one of the
iterators be able to add elements.  However, now I see from one of the
other enhancement requests that a collatedIterator will be added - perhaps
that would cover my use case.

Still, here's the rough-cut code, some testing done.  Beyond the normal
ListIterator/PeekingIterator interface, I added the reverse of peek() as
recall().  I'm sure there are some rough edges in
function/optimization/code-readability. 

Original issue reported on code.google.com by blank...@gmail.com on 30 Oct 2009 at 4:39

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks for the contribution. However, I doubt there's much demand for a
PeekingListIterator.

Since neither ListIterators or PeekingIterators are needed that often, a 
combination
of the two would probably be a rare occurrence. Index-based logic usually leads 
to
more readable code than using ListIterators, though it may not work in the case
you're describing.

Original comment by jared.l....@gmail.com on 30 Oct 2009 at 6:21

GoogleCodeExporter commented 8 years ago
Yup, figured so; essentially am using it as:

List<V> kwaymerge (Iterable<Iterable<V>> sources) {

List<V> result;

if (sources.hasNext()) {
 result = Lists.newLinkedList(sources.next());
} else return Lists.newLinkedList();

PeekingListIterator<V> primary;
PeekingIterator<V> from;

while(sources.hasNext()) {
 primary = ListIterators.peekingListIterator(result.listIterator());
 secondary = Iterators.peekingIterator(sources.next());
 // merge logic, which is simplified by being able to add with the ListIterator
}

return result;

}

Original comment by blank...@gmail.com on 30 Oct 2009 at 11:30

GoogleCodeExporter commented 8 years ago
If the need for this starts cropping up more than we expect, we'll reopen this 
issue.

Original comment by kevin...@gmail.com on 30 Oct 2009 at 4:00