Package org.roaringbitmap.buffer
Class BufferIntIteratorFlyweight
- java.lang.Object
-
- org.roaringbitmap.buffer.BufferIntIteratorFlyweight
-
- All Implemented Interfaces:
java.lang.Cloneable
,IntIterator
,PeekableIntIterator
public class BufferIntIteratorFlyweight extends java.lang.Object implements PeekableIntIterator
Fast iterator minimizing the stress on the garbage collector. You can create one reusable instance of this class and thenwrap(ImmutableRoaringBitmap)
For better performance, consider theImmutableRoaringBitmap.forEach(org.roaringbitmap.IntConsumer)
method.
-
-
Constructor Summary
Constructors Constructor Description BufferIntIteratorFlyweight()
Creates an instance that is not ready for iteration.BufferIntIteratorFlyweight(ImmutableRoaringBitmap r)
Creates an instance that is ready for iteration.
-
Method Summary
Modifier and Type Method Description void
advanceIfNeeded(int minval)
If needed, advance as long as the next value is smaller than minval The advanceIfNeeded method is used for performance reasons, to skip over unnecessary repeated calls to next.PeekableIntIterator
clone()
Creates a copy of the iterator.boolean
hasNext()
int
next()
int
peekNext()
Look at the next value without advancing The peek is useful when working with several iterators at once.void
wrap(ImmutableRoaringBitmap r)
Prepares a bitmap for iteration
-
-
-
Constructor Detail
-
BufferIntIteratorFlyweight
public BufferIntIteratorFlyweight()
Creates an instance that is not ready for iteration. You must first callwrap(ImmutableRoaringBitmap)
.
-
BufferIntIteratorFlyweight
public BufferIntIteratorFlyweight(ImmutableRoaringBitmap r)
Creates an instance that is ready for iteration.- Parameters:
r
- bitmap to be iterated over
-
-
Method Detail
-
clone
public PeekableIntIterator clone()
Description copied from interface:PeekableIntIterator
Creates a copy of the iterator.- Specified by:
clone
in interfaceIntIterator
- Specified by:
clone
in interfacePeekableIntIterator
- Overrides:
clone
in classjava.lang.Object
- Returns:
- a clone of the current iterator
-
hasNext
public boolean hasNext()
- Specified by:
hasNext
in interfaceIntIterator
- Returns:
- whether there is another value
-
next
public int next()
- Specified by:
next
in interfaceIntIterator
- Returns:
- next integer value
-
wrap
public void wrap(ImmutableRoaringBitmap r)
Prepares a bitmap for iteration- Parameters:
r
- bitmap to be iterated over
-
advanceIfNeeded
public void advanceIfNeeded(int minval)
Description copied from interface:PeekableIntIterator
If needed, advance as long as the next value is smaller than minval The advanceIfNeeded method is used for performance reasons, to skip over unnecessary repeated calls to next. Suppose for example that you wish to compute the intersection between an ordered list of integers (e.g., int[] x = {1,4,5}) and a PeekableIntIterator. You might do it as follows...
The benefit of calling advanceIfNeeded is that each such call can be much faster than repeated calls to "next". The underlying implementation can "skip" over some data.PeekableIntIterator j = // get an iterator int val = // first value from my other data structure j.advanceIfNeeded(val); while ( j.hasNext() ) { if(j.next() == val) { // ah! ah! val is in the intersection... // do something here val = // get next value? } j.advanceIfNeeded(val); }
- Specified by:
advanceIfNeeded
in interfacePeekableIntIterator
- Parameters:
minval
- threshold
-
peekNext
public int peekNext()
Description copied from interface:PeekableIntIterator
Look at the next value without advancing The peek is useful when working with several iterators at once. Suppose that you have 100 iterators, and you want to compute their intersections without materializing the result. You might do it as follows...
Notice how the peek method allows you to compare iterators in a way that the next method could not do.PriorityQueue pq = new PriorityQueue(100, new Comparator<PeekableIntIterator>() { public int compare(PeekableIntIterator a, PeekableIntIterator b) { return a.peek() - b.peek(); } }); //... populate pq while(! pq.isEmpty() ) { // get iterator with a smallest value PeekableIntIterator pi = pq.poll(); int x = pi.next(); // advance // do something with x if(pi.hasNext()) pq.add(pi) }
- Specified by:
peekNext
in interfacePeekableIntIterator
- Returns:
- next value
-
-