-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
510 additions
and
26 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
roaringbitmap/src/main/java/org/roaringbitmap/ArrayBatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.roaringbitmap; | ||
|
||
import static org.roaringbitmap.Util.toIntUnsigned; | ||
|
||
public class ArrayBatchIterator implements ContainerBatchIterator { | ||
|
||
private int index = 0; | ||
private final ArrayContainer array; | ||
|
||
public ArrayBatchIterator(ArrayContainer array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public int next(int key, int[] buffer) { | ||
int consumed = 0; | ||
short[] data = array.content; | ||
while (consumed < buffer.length && index < array.getCardinality()) { | ||
buffer[consumed++] = key | toIntUnsigned(data[index++]); | ||
} | ||
assert consumed <= buffer.length; | ||
return consumed; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return index < array.getCardinality(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
roaringbitmap/src/main/java/org/roaringbitmap/BatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.roaringbitmap; | ||
|
||
public interface BatchIterator { | ||
|
||
/** | ||
* Writes the next batch of integers onto the buffer, | ||
* and returns how many were written. Aims to fill | ||
* the buffer. | ||
* @param buffer - the target to write onto | ||
* @return how many values were written during the call. | ||
*/ | ||
int nextBatch(int[] buffer); | ||
|
||
/** | ||
* Returns true is there are more values to get. | ||
* @return whether the iterator is exhaused or not. | ||
*/ | ||
boolean hasNext(); | ||
} |
37 changes: 37 additions & 0 deletions
37
roaringbitmap/src/main/java/org/roaringbitmap/BitmapBatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.roaringbitmap; | ||
|
||
import static java.lang.Long.numberOfTrailingZeros; | ||
|
||
public class BitmapBatchIterator implements ContainerBatchIterator { | ||
|
||
private int wordIndex = 0; | ||
private long word; | ||
private final BitmapContainer bitmap; | ||
|
||
public BitmapBatchIterator(BitmapContainer bitmap) { | ||
this.bitmap = bitmap; | ||
word = bitmap.bitmap[0]; | ||
} | ||
|
||
@Override | ||
public int next(int key, int[] buffer) { | ||
int consumed = 0; | ||
while (consumed < buffer.length) { | ||
while (word == 0) { | ||
++wordIndex; | ||
if (wordIndex == 1024) { | ||
return consumed; | ||
} | ||
word = bitmap.bitmap[wordIndex]; | ||
} | ||
buffer[consumed++] = key + (64 * wordIndex) + numberOfTrailingZeros(word); | ||
word &= (word - 1); | ||
} | ||
return consumed; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return wordIndex < 1024; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
roaringbitmap/src/main/java/org/roaringbitmap/ContainerBatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.roaringbitmap; | ||
|
||
public interface ContainerBatchIterator { | ||
|
||
/** | ||
* Fills the buffer with values prefixed by the key, | ||
* and returns how much of the buffer was used. | ||
* @param key the prefix of the values | ||
* @param buffer the buffer to write values onto | ||
* @return how many values were written. | ||
*/ | ||
int next(int key, int[] buffer); | ||
|
||
/** | ||
* Whether the underlying container is exhausted or not | ||
* @return true if there is data remaining | ||
*/ | ||
boolean hasNext(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
roaringbitmap/src/main/java/org/roaringbitmap/RoaringBatchIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.roaringbitmap; | ||
|
||
public class RoaringBatchIterator implements BatchIterator { | ||
|
||
private final RoaringArray highLowContainer; | ||
int index = 0; | ||
int key; | ||
ContainerBatchIterator iterator; | ||
|
||
public RoaringBatchIterator(RoaringArray highLowContainer) { | ||
this.highLowContainer = highLowContainer; | ||
nextIterator(); | ||
} | ||
|
||
@Override | ||
public int nextBatch(int[] buffer) { | ||
int consumed = 0; | ||
if (iterator.hasNext()) { | ||
consumed += iterator.next(key, buffer); | ||
} else { | ||
++index; | ||
nextIterator(); | ||
if (null != iterator) { | ||
return nextBatch(buffer); | ||
} | ||
} | ||
assert consumed <= buffer.length; | ||
return consumed; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return null != iterator; | ||
} | ||
|
||
private void nextIterator() { | ||
if (index < highLowContainer.size()) { | ||
iterator = highLowContainer.getContainerAtIndex(index).getBatchIterator(); | ||
key = highLowContainer.getKeyAtIndex(index) << 16; | ||
} else { | ||
iterator = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.