Top Description Fields Constructors Methods
com.googlecode.javaewah

public final Class EWAHCompressedBitmap

extends Object
implements Cloneable, Externalizable, Iterable<Integer>, BitmapStorage, LogicalElement<EWAHCompressedBitmap>
Class Inheritance
All Implemented Interfaces
com.googlecode.javaewah.LogicalElement, com.googlecode.javaewah.BitmapStorage, java.lang.Iterable, java.io.Externalizable, java.io.Serializable, java.lang.Cloneable
Imports
com.googlecode.javaewah.symmetric.RunningBitmapMerge, .ThresholdFuncBitmap, java.io.*, java.nio.ByteBuffer, .IntBuffer, .LongBuffer, java.util.ArrayList, .Iterator, .List

This implements the patent-free(1) EWAH scheme. Roughly speaking, it is a 64-bit variant of the BBC compression scheme used by Oracle for its bitmap indexes.

The objective of this compression type is to provide some compression, while reducing as much as possible the CPU cycle usage.

Once constructed, the bitmap is essentially immutable (unless you call the "set" or "add" methods). Thus, it can be safely used in multi-threaded programs.

This implementation being 64-bit, it assumes a 64-bit CPU together with a 64-bit Java Virtual Machine. This same code on a 32-bit machine may not be as fast. There is also a 32-bit version of this code in the class javaewah32.EWAHCompressedBitmap32.

Here is a code sample to illustrate usage:

EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0, 2, 55, 64,
        1 << 30);
EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1, 3, 64,
        1 << 30);
EWAHCompressedBitmap ewahBitmap3 = EWAHCompressedBitmap
        .bitmapOf(5, 55, 1 << 30);
EWAHCompressedBitmap ewahBitmap4 = EWAHCompressedBitmap
        .bitmapOf(4, 66, 1 << 30);
EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2);
EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2);
EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2);
andbitmap = EWAHCompressedBitmap.and(ewahBitmap1, ewahBitmap2, ewahBitmap3,
        ewahBitmap4);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bos);
ewahBitmap1.writeExternal(oo);
oo.close();
ewahBitmap1 = null;
ewahBitmap1 = new EWAHCompressedBitmap();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ewahBitmap1.readExternal(new ObjectInputStream(bis));
EWAHCompressedBitmap threshold2 = EWAHCompressedBitmap.threshold(2,
        ewahBitmap1, ewahBitmap2, ewahBitmap3, ewahBitmap4);

For more details, see the following papers:

A 32-bit version of the compressed format was described by Wu et al. and named WBC:

Probably, the best prior art is the Oracle bitmap compression scheme (BBC):

1- The authors do not know of any patent infringed by the following implementation. However, similar schemes, like WAH are covered by patents.

Since
0.1.0
See Also
EWAHCompressedBitmap32

Field Summary

Modifier and TypeField and Description
public static final boolean
ADJUST_CONTAINER_SIZE_WHEN_AGGREGATING

whether we adjust after some aggregation by adding in zeroes *

pack-priv final Buffer
buffer

The buffer

private RunningLengthWord
rlw

The current (last) running length word.

pack-priv static final long
private int
sizeInBits

sizeInBits: number of bits in the (uncompressed) bitmap.

public static final int
WORD_IN_BITS

The Constant WORD_IN_BITS represents the number of bits in a long.

Constructor Summary

AccessConstructor and Description
public
EWAHCompressedBitmap()

Creates an empty bitmap (no bit set to true).

public
EWAHCompressedBitmap(int
number of 64-bit words reserved when the object is created)
bufferSize
)

Sets explicitly the buffer size (in 64-bit words).

public
EWAHCompressedBitmap(ByteBuffer
data source
buffer
)

Creates a bitmap with the specified ByteBuffer backend.

public
EWAHCompressedBitmap(LongBuffer
data source
buffer
)

Creates a bitmap with the specified java.nio.LongBuffer backend.

private

Method Summary

Modifier and TypeMethod and Description
public void
add(final long
the word
newData
)
Deprecated use addWord() instead.

public void
add(final long
the word
newData
,
final int
the number of significant bits (by default it should be 64)
bitsThatMatter
)
Deprecated use addWord() instead.

public void
addLiteralWord(final long
the word
newData
)

Implements com.googlecode.javaewah.BitmapStorage.addLiteralWord.

Adding literal word directly to the bitmap (for expert use).

public void
addStreamOfEmptyWords(final boolean
the boolean value
v
,
long
the number
number
)

Implements com.googlecode.javaewah.BitmapStorage.addStreamOfEmptyWords.

For experts: You want to add many zeroes or ones?

public void
addStreamOfLiteralWords(final Buffer
the buffer wrapping the literal words
buffer
,
final int
the starting point in the array
start
,
final int
the number of literal words to add
number
)

Implements com.googlecode.javaewah.BitmapStorage.addStreamOfLiteralWords.

if you have several literal words to copy over, this might be faster.

public void
addStreamOfNegatedLiteralWords(final Buffer
the buffer wrapping the literal words
buffer
,
final int
the starting point in the array
start
,
final int
the number of literal words to add
number
)

Implements com.googlecode.javaewah.BitmapStorage.addStreamOfNegatedLiteralWords.

Same as addStreamOfLiteralWords, but the words are negated.

public void
addWord(final long
the word
newData
)

Implements com.googlecode.javaewah.BitmapStorage.addWord.

Adding words directly to the bitmap (for expert use).

public void
addWord(final long
the word
newData
,
final int
the number of significant bits (by default it should be 64)
bitsThatMatter
)

Adding words directly to the bitmap (for expert use).

public EWAHCompressedBitmap

Returns:

the EWAH compressed bitmap
and
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Implements com.googlecode.javaewah.LogicalElement.and.

Returns a new compressed bitmap containing the bitwise AND values of the current bitmap with some other bitmap.

public static EWAHCompressedBitmap

Returns:

result of the AND
and
(final EWAHCompressedBitmap...
bitmaps to AND together
bitmaps
)

Implements com.googlecode.javaewah.LogicalElement.and.

Returns a new compressed bitmap containing the bitwise AND values of the provided bitmaps.

public int

Returns:

the cardinality
andCardinality
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Returns the cardinality of the result of a bitwise AND of the values of the current bitmap with some other bitmap.

public static int

Returns:

the cardinality
andCardinality
(final EWAHCompressedBitmap...
bitmaps to AND
bitmaps
)

Returns the cardinality of the result of a bitwise AND of the values of the provided bitmaps.

public EWAHCompressedBitmap

Returns:

the EWAH compressed bitmap
andNot
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Implements com.googlecode.javaewah.LogicalElement.andNot.

Returns a new compressed bitmap containing the bitwise AND NOT values of the current bitmap with some other bitmap.

public int

Returns:

the cardinality
andNotCardinality
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Returns the cardinality of the result of a bitwise AND NOT of the values of the current bitmap with some other bitmap.

public void
andNotToContainer(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
,
final BitmapStorage
where to store the result
container
)

Returns a new compressed bitmap containing the bitwise AND NOT values of the current bitmap with some other bitmap.

public void
andToContainer(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
,
final BitmapStorage
where we store the result
container
)

Computes new compressed bitmap containing the bitwise AND values of the current bitmap with some other bitmap.

public static void
andWithContainer(final BitmapStorage
where the result is stored
container
,
final EWAHCompressedBitmap...
bitmaps to AND
bitmaps
)

For internal use.

public static EWAHCompressedBitmap

Returns:

the bitmap
bitmapOf
(int...
list of set bit positions
setBits
)

Return a bitmap with the bit set to true at the given positions.

private static int
public int

Returns:

the number of bits set to true
cardinality
()

reports the number of bits set to true.

public ChunkIterator

Returns:

the chunk iterator
chunkIterator
()

Iterator over the chunk of bits.

public void
clear()

Implements com.googlecode.javaewah.BitmapStorage.clear.

Clear any set bits and set size in bits back to 0

public boolean

Returns:

true if the value was unset
clear
(final int
the index
i
)

Set the bit at position i to false.

public IntIterator

Returns:

the int iterator
clearIntIterator
()

Iterator over the clear bits.

public EWAHCompressedBitmap
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public EWAHCompressedBitmap

Returns:

the EWAH compressed bitmap
compose
(EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Implements com.googlecode.javaewah.LogicalElement.compose.

Returns a new compressed bitmap containing the composition of the current bitmap with some other bitmap.

public void
composeToContainer(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
,
final EWAHCompressedBitmap
where we store the result
container
)

Computes a new compressed bitmap containing the composition of the current bitmap with some other bitmap.

public void
deserialize(DataInput
the DataInput stream
in
)

Deserialize.

private int
distanceInWords(int
the index
i
)

For internal use.

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Check to see whether the two compressed bitmaps contain the same set bits.

private void
extendAndSet(int
the index
i
,
boolean
the value
value
)

For internal use.

private void
fastaddStreamOfEmptyWords(final boolean
the boolean value
v
,
long
the number (must be greater than 0)
number
)

For experts: You want to add many zeroes or ones faster?

public boolean

Returns:

whether the bit is set to true
get
(final int
the bit we are interested in
i
)

Query the value of a single bit.

public EWAHIterator

Returns:

the EWAHIterator
getEWAHIterator
()

Gets an EWAHIterator over the data.

public int

Returns:

location of the first set bit or -1
getFirstSetBit
()

getFirstSetBit is a light-weight method that returns the location of the set bit (=1) or -1 if there is none.

public IteratingRLW

Returns:

the IteratingRLW iterator corresponding to this bitmap
getIteratingRLW
()

Gets an IteratingRLW to iterate over the data.

public List<Integer>

Returns:

a list
getPositions
()
Deprecated use toList() instead.

private ReverseEWAHIterator

Returns:

the ReverseEWAHIterator
getReverseEWAHIterator
()

Gets a ReverseEWAHIterator over the data.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a customized hash code (based on Karp-Rabin).

private void
insertEmptyWord(final boolean
the boolean value
v
)

For internal use.

private void
insertLiteralWord(final long
the literal word
newData
)

For internal use.

public boolean

Returns:

whether they intersect
intersects
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Return true if the two EWAHCompressedBitmap have both at least one true bit in the same position.

public IntIterator

Returns:

the int iterator
intIterator
()

Iterator over the set bits (this is what most people will want to use to browse the content if they want an iterator).

public boolean

Returns:

true if no bit is set
isEmpty
()

Checks whether this bitmap is empty (has a cardinality of zero).

public Iterator<Integer>

Returns:

the iterator
iterator
()

Implements java.lang.Iterable.iterator.

Iterates over the positions of the true values.

private void
locateAndSet(int
the index
i
,
boolean
the value
value
)

For internal use.

pack-priv static int
private boolean
mergeLiteralWordInCurrentRunningLength(boolean value, boolean rb, long rl, int wordPosition)

private boolean
mergeLiteralWordInNextRunningLength(boolean value, long lw, int pos, int wordPosition)

public void
not()

Implements com.googlecode.javaewah.LogicalElement.not.

Negate (bitwise) the current bitmap.

public EWAHCompressedBitmap

Returns:

the EWAH compressed bitmap
or
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Implements com.googlecode.javaewah.LogicalElement.or.

Returns a new compressed bitmap containing the bitwise OR values of the current bitmap with some other bitmap.

public static EWAHCompressedBitmap

Returns:

result of the OR
or
(final EWAHCompressedBitmap...
bitmaps to OR together
bitmaps
)

Implements com.googlecode.javaewah.LogicalElement.or.

Returns a new compressed bitmap containing the bitwise OR values of the provided bitmaps.

public int

Returns:

the cardinality
orCardinality
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Returns the cardinality of the result of a bitwise OR of the values of the current bitmap with some other bitmap.

public static int

Returns:

the cardinality
orCardinality
(final EWAHCompressedBitmap...
bitmaps to OR
bitmaps
)

Returns the cardinality of the result of a bitwise OR of the values of the provided bitmaps.

public void
orToContainer(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
,
final BitmapStorage
where we store the result
container
)

Computes the bitwise or between the current bitmap and the bitmap "a".

public static void
orWithContainer(final BitmapStorage
where the aggregate is written.
container
,
final EWAHCompressedBitmap...
to be aggregated
bitmaps
)

Uses an adaptive technique to compute the logical OR.

public void
readExternal(ObjectInput
the stream to read data from in order to restore the object
in
)

Implements java.io.Externalizable.readExternal.

The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.

public IntIterator

Returns:

the int iterator
reverseIntIterator
()

Iterator over the set bits in reverse order.

public void
serialize(DataOutput
the DataOutput stream
out
)

Serialize.

public int

Returns:

the size in bytes
serializedSizeInBytes
()

Report the number of bytes required to serialize this bitmap The current bitmap is not modified.

public boolean

Returns:

true if the value was set
set
(final int
the index
i
)

Set the bit at position i to true.

private boolean
set(int
the index
i
,
boolean
the value
value
)

For internal use.

private void
setInLiteralWords(boolean value, int i, int nbits, int pos, long rl, boolean rb, long lw)

private void
setInRunningLength(boolean value, int i, int nbits, int pos, long rl, boolean rb, long lw)

private void
setRLWInfo(int pos, boolean rb, long rl, long lw)

public boolean

Returns:

true if the update was possible
setSizeInBits
(final int
the size in bits
size
,
final boolean
the default boolean value
defaultValue
)

Change the reported size in bits of the *uncompressed* bitmap represented by this compressed bitmap.

public void
setSizeInBitsWithinLastWord(final int
the size in bits
size
)

Implements com.googlecode.javaewah.BitmapStorage.setSizeInBitsWithinLastWord.

Sets the size in bits of the bitmap as an *uncompressed* bitmap.

public EWAHCompressedBitmap

Returns:

new shifted bitmap
shift
(final int
number of bits
b
)

Generates a new bitmap shifted by "b" bits.

public int

Returns:

the size in bits
sizeInBits
()

Implements com.googlecode.javaewah.LogicalElement.sizeInBits.

Returns the size in bits of the *uncompressed* bitmap represented by this compressed bitmap.

public int

Returns:

the size in bytes
sizeInBytes
()

Implements com.googlecode.javaewah.LogicalElement.sizeInBytes.

Report the *compressed* size of the bitmap (equivalent to memory usage, after accounting for some overhead).

public void
swap(final EWAHCompressedBitmap
bitmap to swap with
other
)

Swap the content of the bitmap with another.

public static EWAHCompressedBitmap

Returns:

the aggregated bitmap
threshold
(final int
the threshold
t
,
final EWAHCompressedBitmap...
input data
bitmaps
)

Compute a Boolean threshold function: bits are true where at least t bitmaps have a true bit.

public static void
thresholdWithContainer(final BitmapStorage
where we write the aggregated bitmap
container
,
final int
the threshold
t
,
final EWAHCompressedBitmap...
input data
bitmaps
)

Compute a Boolean threshold function: bits are true where at least T bitmaps have a true bit.

public int[]

Returns:

the array containing the location of the set bits
toArray
()

Populate an array of (sorted integers) corresponding to the location of the set bits.

public String

Returns:

the string
toDebugString
()

A more detailed string describing the bitmap (useful for debugging).

public List<Integer>

Returns:

the positions in a list
toList
()

Gets the locations of the true values as one list.

public String

Returns:

the string
toString
()

Overrides java.lang.Object.toString.

A string describing the bitmap.

public void
trim()

Reduce the internal buffer to its minimal allowable size.

public void
writeExternal(ObjectOutput
the stream to write the object to
out
)

Implements java.io.Externalizable.writeExternal.

The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

public EWAHCompressedBitmap

Returns:

the EWAH compressed bitmap
xor
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Implements com.googlecode.javaewah.LogicalElement.xor.

Returns a new compressed bitmap containing the bitwise XOR values of the current bitmap with some other bitmap.

public static EWAHCompressedBitmap

Returns:

result of the XOR
xor
(final EWAHCompressedBitmap...
bitmaps to XOR together
bitmaps
)

Implements com.googlecode.javaewah.LogicalElement.xor.

Returns a new compressed bitmap containing the bitwise XOR values of the provided bitmaps.

public int

Returns:

the cardinality
xorCardinality
(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
)

Returns the cardinality of the result of a bitwise XOR of the values of the current bitmap with some other bitmap.

public void
xorToContainer(final EWAHCompressedBitmap
the other bitmap (it will not be modified)
a
,
final BitmapStorage
where we store the result
container
)

Computes a new compressed bitmap containing the bitwise XOR values of the current bitmap with some other bitmap.

public static void
xorWithContainer(final BitmapStorage
where the aggregate is written.
container
,
final EWAHCompressedBitmap...
to be aggregated
bitmaps
)

Uses an adaptive technique to compute the logical XOR.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait