Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New version

  • Loading branch information...
commit e443cf5e5d2e33d5a4896564472a50a54eea56f8 1 parent f77cf69
Daniel Lemire authored
Showing with 573 additions and 343 deletions.
  1. +4 −1 CHANGELOG
  2. +1 −1  README.md
  3. +3 −3 src/main/java/com/googlecode/javaewah/BitCounter.java
  4. +1 −1  src/main/java/com/googlecode/javaewah/BitmapStorage.java
  5. +147 −48 src/main/java/com/googlecode/javaewah/EWAHCompressedBitmap.java
  6. +4 −4 src/main/java/com/googlecode/javaewah/FastAggregation.java
  7. +7 −7 src/main/java/com/googlecode/javaewah/IteratorAggregation.java
  8. +2 −2 src/main/java/com/googlecode/javaewah/IteratorUtil.java
  9. +2 −2 src/main/java/com/googlecode/javaewah/NonEmptyVirtualStorage.java
  10. +104 −51 src/main/java/com/googlecode/javaewah/datastructure/BitSet.java
  11. +3 −3 src/main/java/com/googlecode/javaewah/symmetric/ThresholdFuncBitmap.java
  12. +3 −3 src/main/java/com/googlecode/javaewah32/BitCounter32.java
  13. +1 −1  src/main/java/com/googlecode/javaewah32/BitmapStorage32.java
  14. +142 −61 src/main/java/com/googlecode/javaewah32/EWAHCompressedBitmap32.java
  15. +4 −4 src/main/java/com/googlecode/javaewah32/FastAggregation32.java
  16. +7 −7 src/main/java/com/googlecode/javaewah32/IteratorAggregation32.java
  17. +2 −2 src/main/java/com/googlecode/javaewah32/IteratorUtil32.java
  18. +1 −1  src/main/java/com/googlecode/javaewah32/NonEmptyVirtualStorage32.java
  19. +3 −3 src/main/java/com/googlecode/javaewah32/symmetric/ThresholdFuncBitmap32.java
  20. +66 −69 src/test/java/com/googlecode/javaewah/EWAHCompressedBitmapTest.java
  21. +66 −69 src/test/java/com/googlecode/javaewah32/EWAHCompressedBitmap32Test.java
5 CHANGELOG
View
@@ -1,5 +1,8 @@
-version 0.8.3 (upcoming)
+version 0.8.3 (February 20th 2013)
- improved BitSet class
+ - renamed add to addWord (deprecation)
+ - renamed getPositions to toList (deprecation)
+ - clone no longer reports throwing an exception
version 0.8.2
- removed the benchmark code
2  README.md
View
@@ -86,7 +86,7 @@ You can also specify the dependency in the Maven "pom.xml" file:
<dependency>
<groupId>com.googlecode.javaewah</groupId>
<artifactId>JavaEWAH</artifactId>
- <version>0.8.2</version>
+ <version>0.8.3</version>
</dependency>
</dependencies>
6 src/main/java/com/googlecode/javaewah/BitCounter.java
View
@@ -21,7 +21,7 @@
* the word
*/
@Override
- public void add(final long newdata) {
+ public void addWord(final long newdata) {
this.oneBits += Long.bitCount(newdata);
return;
}
@@ -39,7 +39,7 @@ public void add(final long newdata) {
@Override
public void addStreamOfLiteralWords(long[] data, int start, int number) {
for (int i = start; i < start + number; i++) {
- add(data[i]);
+ addWord(data[i]);
}
return;
}
@@ -76,7 +76,7 @@ public void addStreamOfEmptyWords(boolean v, long number) {
public void addStreamOfNegatedLiteralWords(long[] data, int start,
int number) {
for (int i = start; i < start + number; i++) {
- add(~data[i]);
+ addWord(~data[i]);
}
return;
}
2  src/main/java/com/googlecode/javaewah/BitmapStorage.java
View
@@ -22,7 +22,7 @@
* @param newdata
* the word
*/
- public void add(final long newdata);
+ public void addWord(final long newdata);
/**
* if you have several literal words to copy over, this might be faster.
195 src/main/java/com/googlecode/javaewah/EWAHCompressedBitmap.java
View
@@ -131,6 +131,29 @@ public EWAHCompressedBitmap(final int buffersize) {
this.buffer = new long[buffersize];
this.rlw = new RunningLengthWord(this, 0);
}
+
+ /**
+ * @param newdata
+ * the word
+ * @deprecated use addWord() instead.
+ */
+ @Deprecated
+ public void add(final long newdata) {
+ addWord(newdata);
+ }
+ /**
+ * @param newdata
+ * the word
+ * @param bitsthatmatter
+ * the number of significant bits (by default it should
+ * be 64)
+ * @deprecated use addWord() instead.
+ */
+ @Deprecated
+ public void add(final long newdata, final int bitsthatmatter) {
+ addWord(newdata,bitsthatmatter);
+ }
+
/**
* Adding words directly to the bitmap (for expert use).
@@ -140,22 +163,27 @@ public EWAHCompressedBitmap(final int buffersize) {
*
* Most users will want the set method.
*
- * Example: if you add 321, you are have added (in binary notation)
- * 0b101000001, so you have effectively called set(0), set(6), set(8) in
- * sequence.
+ * Example: if you add word 321 to an empty bitmap, you are have
+ * added (in binary notation) 0b101000001, so you have effectively
+ * called set(0), set(6), set(8) in sequence.
*
* Since this modifies the bitmap, this method is not thread-safe.
*
+ * API change: prior to version 0.8.3, this method was called add.
+ *
* @param newdata
* the word
*/
@Override
- public void add(final long newdata) {
- add(newdata, wordinbits);
+ public void addWord(final long newdata) {
+ addWord(newdata, wordinbits);
}
/**
* Adding words directly to the bitmap (for expert use).
+ * Since this modifies the bitmap, this method is not thread-safe.
+ *
+ * API change: prior to version 0.8.3, this method was called add.
*
* @param newdata
* the word
@@ -163,7 +191,7 @@ public void add(final long newdata) {
* the number of significant bits (by default it should
* be 64)
*/
- public void add(final long newdata, final int bitsthatmatter) {
+ public void addWord(final long newdata, final int bitsthatmatter) {
this.sizeinbits += bitsthatmatter;
if (newdata == 0) {
addEmptyWord(false);
@@ -221,6 +249,7 @@ private void addLiteralWord(final long newdata) {
/**
* if you have several literal words to copy over, this might be faster.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
*
* @param data
* the literal words
@@ -256,6 +285,8 @@ public void addStreamOfLiteralWords(final long[] data, final int start,
* For experts: You want to add many zeroes or ones? This is the method
* you use.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
+ *
* @param v
* the boolean value
* @param number
@@ -301,6 +332,8 @@ public void addStreamOfEmptyWords(final boolean v, long number) {
/**
* Same as addStreamOfLiteralWords, but the words are negated.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
+ *
* @param data
* the literal words
* @param start
@@ -341,9 +374,11 @@ public void addStreamOfNegatedLiteralWords(final long[] data,
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @since 0.4.3
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -363,9 +398,11 @@ public EWAHCompressedBitmap and(final EWAHCompressedBitmap a) {
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
+ * The current bitmap is not modified.
+ *
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -407,7 +444,7 @@ public void andToContainer(final EWAHCompressedBitmap a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -425,12 +462,14 @@ public void andToContainer(final EWAHCompressedBitmap a,
/**
* Returns the cardinality of the result of a bitwise AND of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids
+ * allocating an intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int andCardinality(final EWAHCompressedBitmap a) {
@@ -449,8 +488,10 @@ public int andCardinality(final EWAHCompressedBitmap a) {
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -471,9 +512,11 @@ public EWAHCompressedBitmap andNot(final EWAHCompressedBitmap a) {
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
+ * The current bitmap is not modified.
+ *
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where to store the result
*/
@@ -522,7 +565,7 @@ public void andNotToContainer(final EWAHCompressedBitmap a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& (~rlwj.getLiteralWordAt(k)));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -542,12 +585,14 @@ else if (adjustContainerSizeWhenAggregating)
/**
* Returns the cardinality of the result of a bitwise AND NOT of the
- * values of the current bitmap with some other bitmap. Avoids needing
- * to allocate an intermediate bitmap to hold the result of the OR.
+ * values of the current bitmap with some other bitmap. Avoids
+ * allocating an intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int andNotCardinality(final EWAHCompressedBitmap a) {
@@ -596,14 +641,18 @@ public void clear() {
* @see java.lang.Object#clone()
*/
@Override
- public EWAHCompressedBitmap clone()
- throws java.lang.CloneNotSupportedException {
- final EWAHCompressedBitmap clone = (EWAHCompressedBitmap) super
- .clone();
- clone.buffer = this.buffer.clone();
- clone.rlw = new RunningLengthWord(clone, this.rlw.position);
- clone.actualsizeinwords = this.actualsizeinwords;
- clone.sizeinbits = this.sizeinbits;
+ public EWAHCompressedBitmap clone() {
+ EWAHCompressedBitmap clone = null;
+ try {
+ clone = (EWAHCompressedBitmap) super.clone();
+ clone.buffer = this.buffer.clone();
+ clone.rlw = new RunningLengthWord(clone,
+ this.rlw.position);
+ clone.actualsizeinwords = this.actualsizeinwords;
+ clone.sizeinbits = this.sizeinbits;
+ } catch (CloneNotSupportedException e) {
+ e.printStackTrace(); // cannot happen
+ }
return clone;
}
@@ -693,7 +742,9 @@ private void fastaddStreamOfEmptyWords(final boolean v, long number) {
/**
* Gets an EWAHIterator over the data. This is a customized iterator
- * which iterates over run length word. For experts only.
+ * which iterates over run length words. For experts only.
+ *
+ * The current bitmap is not modified.
*
* @return the EWAHIterator
*/
@@ -702,19 +753,36 @@ public EWAHIterator getEWAHIterator() {
}
/**
+ * Gets an IteratingRLW to iterate over the data. For experts only.
+ *
+ * The current bitmap is not modified.
+ *
* @return the IteratingRLW iterator corresponding to this bitmap
*/
public IteratingRLW getIteratingRLW() {
return new IteratingBufferedRunningLengthWord(this);
}
+
+ /**
+ * @return a list
+ * @deprecated use toList() instead.
+ */
+ @Deprecated
+ public List<Integer> getPositions() {
+ return toList();
+ }
/**
- * get the locations of the true values as one vector. (may use more
- * memory than iterator())
+ * Gets the locations of the true values as one list. (May use more
+ * memory than iterator().)
*
- * @return the positions
+ * The current bitmap is not modified.
+ *
+ * API change: prior to version 0.8.3, this method was called getPositions.
+ *
+ * @return the positions in a list
*/
- public List<Integer> getPositions() {
+ public List<Integer> toList() {
final ArrayList<Integer> v = new ArrayList<Integer>();
final EWAHIterator i = this.getEWAHIterator();
int pos = 0;
@@ -749,6 +817,7 @@ public IteratingRLW getIteratingRLW() {
* Returns a customized hash code (based on Karp-Rabin). Naturally, if
* the bitmaps are equal, they will hash to the same value.
*
+ * The current bitmap is not modified.
*/
@Override
public int hashCode() {
@@ -782,9 +851,11 @@ public int hashCode() {
* check whether there is a set bit, but intersects will run faster if
* you don't need the result of the "and" operation.
*
+ * The current bitmap is not modified.
+ *
* @since 0.3.2
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return whether they intersect
*/
public boolean intersects(final EWAHCompressedBitmap a) {
@@ -802,6 +873,8 @@ public boolean intersects(final EWAHCompressedBitmap a) {
* to browse the content if they want an iterator). The location of the
* set bits is returned, in increasing order.
*
+ * The current bitmap is not modified.
+ *
* @return the int iterator
*/
public IntIterator intIterator() {
@@ -809,9 +882,11 @@ public IntIterator intIterator() {
}
/**
- * iterate over the positions of the true values. This is similar to
+ * Iterates over the positions of the true values. This is similar to
* intIterator(), but it uses Java generics.
*
+ * The current bitmap is not modified.
+ *
* @return the iterator
*/
@Override
@@ -876,6 +951,8 @@ else if ((this.actualsizeinwords + number) * 3 / 2 < this.actualsizeinwords
* The running time is proportional to the compressed size (as reported
* by sizeInBytes()).
*
+ * Because this modifies the bitmap, this method is not thread-safe.
+ *
*/
@Override
public void not() {
@@ -924,8 +1001,10 @@ public void not() {
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -940,9 +1019,11 @@ public EWAHCompressedBitmap or(final EWAHCompressedBitmap a) {
* Computes the bitwise or between the current bitmap and the bitmap
* "a". Stores the result in the container.
*
+ * The current bitmap is not modified.
+ *
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -983,7 +1064,7 @@ public void orToContainer(final EWAHCompressedBitmap a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k) {
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
| rlwj.getLiteralWordAt(k));
}
rlwi.discardFirstWords(nbre_literal);
@@ -999,12 +1080,14 @@ public void orToContainer(final EWAHCompressedBitmap a,
/**
* Returns the cardinality of the result of a bitwise OR of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids
+ * allocating an intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int orCardinality(final EWAHCompressedBitmap a) {
@@ -1095,6 +1178,8 @@ private boolean reserve(final int size) {
/**
* Serialize.
*
+ * The current bitmap is not modified.
+ *
* @param out
* the DataOutput stream
* @throws IOException
@@ -1111,6 +1196,8 @@ public void serialize(DataOutput out) throws IOException {
/**
* Report the size required to serialize this bitmap
*
+ * The current bitmap is not modified.
+ *
* @return the size in bytes
*/
public int serializedSizeInBytes() {
@@ -1124,6 +1211,8 @@ public int serializedSizeInBytes() {
*
* (This implementation is based on zhenjl's Go version of JavaEWAH.)
*
+ * The current bitmap is not modified.
+ *
* @param i
* the bit we are interested in
* @return whether the bit is set to true
@@ -1222,6 +1311,8 @@ public void setSizeInBits(final int size) {
* it can be extended. The new bits are set to false or true depending
* on the value of defaultvalue.
*
+ * This method is not thread-safe.
+ *
* @param size
* the size in bits
* @param defaultvalue
@@ -1256,6 +1347,8 @@ public boolean setSizeInBits(final int size, final boolean defaultvalue) {
* this compressed bitmap. Initially, the sizeInBits is zero. It is
* extended automatically when you set bits to true.
*
+ * The current bitmap is not modified.
+ *
* @return the size in bits
*/
@Override
@@ -1398,7 +1491,7 @@ public String toString() {
}
/**
- * swap the content of the bitmap with another.
+ * Swap the content of the bitmap with another.
*
* @param other
* bitmap to swap with
@@ -1448,8 +1541,10 @@ public void writeExternal(ObjectOutput out) throws IOException {
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -1467,9 +1562,11 @@ public EWAHCompressedBitmap xor(final EWAHCompressedBitmap a) {
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
+ * The current bitmap is not modified.
+ *
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -1506,7 +1603,7 @@ public void xorToContainer(final EWAHCompressedBitmap a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
^ rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -1521,12 +1618,14 @@ public void xorToContainer(final EWAHCompressedBitmap a,
/**
* Returns the cardinality of the result of a bitwise XOR of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids
+ * allocating an intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int xorCardinality(final EWAHCompressedBitmap a) {
@@ -1601,7 +1700,7 @@ public static EWAHCompressedBitmap and(
/**
* Returns the cardinality of the result of a bitwise AND of the values
- * of the provided bitmaps. Avoids needing to allocate an intermediate
+ * of the provided bitmaps. Avoids allocating an intermediate
* bitmap to hold the result of the AND.
*
* @since 0.4.3
@@ -1778,7 +1877,7 @@ public static EWAHCompressedBitmap xor(
/**
* Returns the cardinality of the result of a bitwise OR of the values
- * of the provided bitmaps. Avoids needing to allocate an intermediate
+ * of the provided bitmaps. Avoids allocating an intermediate
* bitmap to hold the result of the OR.
*
* @since 0.4.0
8 src/main/java/com/googlecode/javaewah/FastAggregation.java
View
@@ -73,7 +73,7 @@ public static void bufferedandWithContainer(
effective = eff;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
for (IteratingRLW i : al)
if (i.size() == 0) {
al.clear();
@@ -144,7 +144,7 @@ public int compare(EWAHCompressedBitmap a,
maxr = k;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
Arrays.fill(hardbitmap, 0);
}
@@ -213,7 +213,7 @@ public int compare(EWAHCompressedBitmap a,
maxr = k;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
Arrays.fill(hardbitmap, 0);
}
container.setSizeInBits(range);
@@ -494,7 +494,7 @@ public int compare(EWAHCompressedBitmap a,
.getRunningLength());
}
}
- container.add(word);
+ container.addWord(word);
index++;
}
for (int i = 0; i < maxAvailablePos; i++) {
14 src/main/java/com/googlecode/javaewah/IteratorAggregation.java
View
@@ -225,7 +225,7 @@ protected static long discharge(final BitmapStorage container,
if (L + counter > max)
L = max - counter;
for (int k = 0; k < L; ++k) {
- container.add(i.getLiteralWordAt(k));
+ container.addWord(i.getLiteralWordAt(k));
}
counter += L;
i.discardFirstWords(L + L1);
@@ -260,7 +260,7 @@ protected static long dischargeNegated(final BitmapStorage container,
if (L + counter > max)
L = max - counter;
for (int k = 0; k < L; ++k) {
- container.add(~i.getLiteralWordAt(k));
+ container.addWord(~i.getLiteralWordAt(k));
}
counter += L;
i.discardFirstWords(L + L1);
@@ -305,7 +305,7 @@ static void andToContainer(final BitmapStorage container,
if (nbre_literal > 0) {
desiredrlwcount -= nbre_literal;
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -348,7 +348,7 @@ static void andToContainer(final BitmapStorage container,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -408,7 +408,7 @@ public static void xorToContainer(final BitmapStorage container,
if (nbre_literal > 0) {
desiredrlwcount -= nbre_literal;
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
^ rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -592,7 +592,7 @@ public EWAHIterator next() {
i.remove();
}
for (int k = 0; k < effective; ++k) {
- this.buffer.add(this.hardbitmap[k]);
+ this.buffer.addWord(this.hardbitmap[k]);
}
Arrays.fill(this.hardbitmap, 0);
@@ -642,7 +642,7 @@ public EWAHIterator next() {
i.remove();
}
for (int k = 0; k < effective; ++k)
- this.buffer.add(this.hardbitmap[k]);
+ this.buffer.addWord(this.hardbitmap[k]);
Arrays.fill(this.hardbitmap, 0);
return this.buffer.getEWAHIterator();
}
4 src/main/java/com/googlecode/javaewah/IteratorUtil.java
View
@@ -63,7 +63,7 @@ public static void materialize(final IteratingRLW i,
i.getRunningLength());
}
for (int k = 0; k < i.getNumberOfLiteralWords(); ++k)
- c.add(i.getLiteralWordAt(k));
+ c.addWord(i.getLiteralWordAt(k));
if (!i.next())
break;
}
@@ -127,7 +127,7 @@ public static long materialize(final IteratingRLW i,
}
long L = i.getNumberOfLiteralWords();
for (int k = 0; k < L; ++k)
- c.add(i.getLiteralWordAt(k));
+ c.addWord(i.getLiteralWordAt(k));
if (Max > 0) {
if (!i.next())
break;
4 src/main/java/com/googlecode/javaewah/NonEmptyVirtualStorage.java
View
@@ -36,10 +36,10 @@ public synchronized Throwable fillInStackTrace() {
* If the word to be added is non-zero, a NonEmptyException exception is
* thrown.
*
- * @see com.googlecode.javaewah.BitmapStorage#add(long)
+ * @see com.googlecode.javaewah.BitmapStorage#addWord(long)
*/
@Override
- public void add(long newdata) {
+ public void addWord(long newdata) {
if (newdata != 0)
throw nonEmptyException;
return;
155 src/main/java/com/googlecode/javaewah/datastructure/BitSet.java
View
@@ -14,13 +14,10 @@
*
* It differs from the basic Java BitSet class in the following ways:
* <ul>
- * <li>It only aggregate BitSets having the same number of bits. This is
- * the desired behavior in many cases where BitSets are supposed to span
- * the same number of bits and differences are indicative of a programming
- * issue. You can always resize your BitSets.</li>
* <li>You can iterate over set bits using a simpler syntax <code>for(int bs: mybitset)</code>.</li>
* <li>You can compute the cardinality of an intersection and union without writing it out
* or modifying your BitSets (see methods such as andcardinality).</li>
+ * <li>You can recover wasted memory with trim().</li>
* </ul>
*
* @author Daniel Lemire
@@ -40,69 +37,60 @@ public BitSet(final int sizeinbits) {
}
/**
- * Compute bitwise AND, assumes that both bitsets have the same length.
+ * Compute bitwise AND.
*
* @param bs
* other bitset
*/
public void and(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
this.data[k] &= bs.data[k];
}
}
/**
- * Compute cardinality of bitwise AND, assumes that both bitsets have
- * the same length.
+ * Compute cardinality of bitwise AND.
+ *
+ * The current bitmap is modified. Consider calling trim()
+ * to recover wasted memory afterward.
*
* @param bs
* other bitset
* @return cardinality
*/
public int andcardinality(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
int sum = 0;
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
sum += Long.bitCount(this.data[k] & bs.data[k]);
}
return sum;
}
/**
- * Compute bitwise AND NOT, assumes that both bitsets have the same
- * length.
+ * Compute bitwise AND NOT.
+ *
+ * The current bitmap is modified. Consider calling trim()
+ * to recover wasted memory afterward.
*
* @param bs
* other bitset
*/
public void andNot(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
this.data[k] &= ~bs.data[k];
}
}
/**
- * Compute cardinality of bitwise AND NOT, assumes that both bitsets
- * have the same length.
+ * Compute cardinality of bitwise AND NOT.
*
* @param bs
* other bitset
* @return cardinality
*/
public int andNotcardinality(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
int sum = 0;
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
sum += Long.bitCount(this.data[k] & (~bs.data[k]));
}
return sum;
@@ -121,7 +109,8 @@ public int cardinality() {
}
/**
- * Reset all bits to false
+ * Reset all bits to false. This might be wasteful: a better
+ * approach is to create a new empty bitmap.
*/
public void clear() {
Arrays.fill(this.data, 0);
@@ -143,7 +132,25 @@ public BitSet clone() {
public boolean equals(Object o) {
if (!(o instanceof BitSet))
return false;
- return Arrays.equals(this.data, ((BitSet) o).data);
+ BitSet bs = (BitSet)o;
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
+ if(this.data[k] != bs.data[k]) return false;
+ }
+ BitSet longer = bs.size() < this.size() ? this : bs;
+ for (int k = Math.min(this.data.length, bs.data.length); k < Math
+ .max(this.data.length, bs.data.length); ++k) {
+ if(longer.data[k] != 0) return false;
+ }
+ return true;
+ }
+ /**
+ * Check whether a bitset contains a set bit.
+ * @return true if no set bit is found
+ */
+ public boolean empty() {
+ for (long l : this.data)
+ if(l != 0) return false;
+ return true;
}
/**
@@ -157,7 +164,16 @@ public boolean get(final int i) {
@Override
public int hashCode() {
- return Arrays.hashCode(this.data);
+ int B = 31;
+ int hash1 = 0;
+ int hash2 = 0;
+ for (int k = 0; k < this.data.length; ++k) {
+ if (this.data[k] != 0) {
+ hash1 = hash1 * B + (int) this.data[k];
+ hash2 = hash2 * B + (int) (this.data[k] >>> 32);
+ }
+ }
+ return hash1 ^ hash2;
}
/**
@@ -212,6 +228,20 @@ public void remove() {
};
}
+
+
+ /**
+ * Checks whether two bitsets intersect.
+ *
+ * @param bs other bitset
+ * @return true if they have a non-empty intersection (result of AND)
+ */
+ public boolean intersects(BitSet bs) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
+ if((this.data[k] & bs.data[k]) != 0) return true;
+ }
+ return false;
+ }
/**
* Usage: for(int i=bs.nextSetBit(0); i&gt;=0; i=bs.nextSetBit(i+1)) {
@@ -270,36 +300,41 @@ public int nextUnsetBit(final int i) {
}
/**
- * Compute bitwise OR, assumes that both bitsets have the same length.
+ * Compute bitwise OR.
+ *
+ * The current bitmap is modified. Consider calling trim()
+ * to recover wasted memory afterward.
*
* @param bs
* other bitset
*/
public void or(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
+ if(this.size() < bs.size())
+ this.resize(bs.size());
for (int k = 0; k < this.data.length; ++k) {
this.data[k] |= bs.data[k];
}
}
/**
- * Compute cardinality of bitwise OR, assumes that both bitsets have the
- * same length.
+ * Compute cardinality of bitwise OR.
+ *
+ * BitSets are not modified.
*
* @param bs
* other bitset
* @return cardinality
*/
public int orcardinality(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
int sum = 0;
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
sum += Long.bitCount(this.data[k] | bs.data[k]);
}
+ BitSet longer = bs.size() < this.size() ? this : bs;
+ for (int k = Math.min(this.data.length, bs.data.length); k < Math
+ .max(this.data.length, bs.data.length); ++k) {
+ sum += Long.bitCount(longer.data[k]);
+ }
return sum;
}
@@ -353,7 +388,19 @@ public void set(final int i, final boolean b) {
public int size() {
return this.data.length * 64;
}
-
+ /**
+ * Recovers wasted memory
+ *
+ */
+ public void trim() {
+ for(int k = this.data.length - 1; k >= 0; --k)
+ if(this.data[k] != 0) {
+ if(k + 1 < this.data.length)
+ this.data = Arrays.copyOf(this.data, k+1);
+ return;
+ }
+ this.data = new long[0];
+ }
/**
* Set to false
*
@@ -398,36 +445,42 @@ public void writeExternal(ObjectOutput out) throws IOException {
}
/**
- * Compute bitwise XOR, assumes that both bitsets have the same length.
+ * Compute bitwise XOR.
+ *
+ * The current bitmap is modified. Consider calling trim()
+ * to recover wasted memory afterward.
*
* @param bs
* other bitset
*/
public void xor(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
+ if(this.size() < bs.size())
+ this.resize(bs.size());
for (int k = 0; k < this.data.length; ++k) {
this.data[k] ^= bs.data[k];
}
}
/**
- * Compute cardinality of bitwise XOR, assumes that both bitsets have
- * the same length.
+ * Compute cardinality of bitwise XOR.
+ *
+ * BitSets are not modified.
*
* @param bs
* other bitset
* @return cardinality
*/
public int xorcardinality(BitSet bs) {
- if (this.data.length != bs.data.length)
- throw new IllegalArgumentException(
- "incompatible bitsets");
int sum = 0;
- for (int k = 0; k < this.data.length; ++k) {
+ for (int k = 0; k < Math.min(this.data.length,bs.data.length); ++k) {
sum += Long.bitCount(this.data[k] ^ bs.data[k]);
}
+ BitSet longer = bs.size() < this.size() ? this : bs;
+ for (int k = Math.min(this.data.length, bs.data.length); k < Math
+ .max(this.data.length, bs.data.length); ++k) {
+ sum += Long.bitCount(longer.data[k]);
+ }
+
return sum;
}
6 src/main/java/com/googlecode/javaewah/symmetric/ThresholdFuncBitmap.java
View
@@ -120,7 +120,7 @@ private final void orLiterals(final BitmapStorage out,
w |= R.iterator.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(w);
+ out.addWord(w);
}
}
@@ -132,7 +132,7 @@ private final void andLiterals(final BitmapStorage out,
w &= R.iterator.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(w);
+ out.addWord(w);
}
}
@@ -148,7 +148,7 @@ private final void generalLiterals(final int deficit,
.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(threshold4(deficit, this.buffers,
+ out.addWord(threshold4(deficit, this.buffers,
this.bufferUsed));
}
}
6 src/main/java/com/googlecode/javaewah32/BitCounter32.java
View
@@ -22,7 +22,7 @@
*/
// @Override : causes problems with Java 1.5
@Override
- public void add(final int newdata) {
+ public void addWord(final int newdata) {
this.oneBits += Integer.bitCount(newdata);
}
@@ -40,7 +40,7 @@ public void add(final int newdata) {
@Override
public void addStreamOfLiteralWords(int[] data, int start, int number) {
for (int i = start; i < start + number; i++) {
- add(data[i]);
+ addWord(data[i]);
}
}
@@ -76,7 +76,7 @@ public void addStreamOfEmptyWords(boolean v, int number) {
public void addStreamOfNegatedLiteralWords(int[] data, int start,
int number) {
for (int i = start; i < start + number; i++) {
- add(~data[i]);
+ addWord(~data[i]);
}
}
2  src/main/java/com/googlecode/javaewah32/BitmapStorage32.java
View
@@ -22,7 +22,7 @@
* @param newdata
* the word
*/
- public void add(final int newdata);
+ public void addWord(final int newdata);
/**
* if you have several literal words to copy over, this might be faster.
203 src/main/java/com/googlecode/javaewah32/EWAHCompressedBitmap32.java
View
@@ -102,6 +102,28 @@ public EWAHCompressedBitmap32(final int buffersize) {
this.buffer = new int[buffersize];
this.rlw = new RunningLengthWord32(this, 0);
}
+ /**
+ * @param newdata
+ * the word
+ * @deprecated use addWord() instead.
+ */
+ @Deprecated
+ public void add(final int newdata) {
+ addWord(newdata);
+ }
+ /**
+ * @param newdata
+ * the word
+ * @param bitsthatmatter
+ * the number of significant bits (by default it should
+ * be 64)
+ * @deprecated use addWord() instead.
+ */
+ @Deprecated
+ public void add(final int newdata, final int bitsthatmatter) {
+ addWord(newdata,bitsthatmatter);
+ }
+
/**
* Adding words directly to the bitmap (for expert use).
@@ -111,31 +133,35 @@ public EWAHCompressedBitmap32(final int buffersize) {
*
* Most users will want the set method.
*
- * Example: if you add 321, you are have added (in binary notation)
- * 0b101000001, so you have effectively called set(0), set(6), set(8) in
- * sequence.
+ * Example: if you add word 321 to an empty bitmap, you are have
+ * added (in binary notation) 0b101000001, so you have effectively
+ * called set(0), set(6), set(8) in sequence.
*
* Since this modifies the bitmap, this method is not thread-safe.
*
+ * API change: prior to version 0.8.3, this method was called add.
+ *
* @param newdata
* the word
*/
@Override
- public void add(final int newdata) {
- add(newdata, wordinbits);
+ public void addWord(final int newdata) {
+ addWord(newdata, wordinbits);
}
/**
* Adding words directly to the bitmap (for expert use). Since this
* modifies the bitmap, this method is not thread-safe.
*
+ * API change: prior to version 0.8.3, this method was called add.
+ *
* @param newdata
* the word
* @param bitsthatmatter
* the number of significant bits (by default it should
* be 32)
*/
- public void add(final int newdata, final int bitsthatmatter) {
+ public void addWord(final int newdata, final int bitsthatmatter) {
this.sizeinbits += bitsthatmatter;
if (newdata == 0) {
addEmptyWord(false);
@@ -197,6 +223,7 @@ private int addLiteralWord(final int newdata) {
/**
* if you have several literal words to copy over, this might be faster.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
*
* @param data
* the literal words
@@ -232,6 +259,8 @@ public void addStreamOfLiteralWords(final int[] data, final int start,
* For experts: You want to add many zeroes or ones? This is the method
* you use.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
+ *
* @param v
* the boolean value
* @param number
@@ -278,6 +307,8 @@ public void addStreamOfEmptyWords(final boolean v, int number) {
/**
* Same as addStreamOfLiteralWords, but the words are negated.
*
+ * Since this modifies the bitmap, this method is not thread-safe.
+ *
* @param data
* the literal words
* @param start
@@ -310,7 +341,8 @@ public void addStreamOfNegatedLiteralWords(final int[] data,
/**
* Returns a new compressed bitmap containing the bitwise AND values of
- * the current bitmap with some other bitmap.
+ * the current bitmap with some other bitmap. The current bitmap
+ * is not modified.
*
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
@@ -318,8 +350,9 @@ public void addStreamOfNegatedLiteralWords(final int[] data,
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -335,25 +368,14 @@ public EWAHCompressedBitmap32 and(final EWAHCompressedBitmap32 a) {
/**
* Computes new compressed bitmap containing the bitwise AND values of
* the current bitmap with some other bitmap.
- *
- * The running time is proportional to the sum of the compressed sizes
- * (as reported by sizeInBytes()).
- *
- * @param a
- * the other bitmap
- * @param container
- * where we store the result
- */
- /**
- * Computes new compressed bitmap containing the bitwise AND values of
- * the current bitmap with some other bitmap.
+ * The current bitmap is not modified.
*
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
* @since 0.4.0
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -395,7 +417,7 @@ public void andToContainer(final EWAHCompressedBitmap32 a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -413,11 +435,12 @@ public void andToContainer(final EWAHCompressedBitmap32 a,
/**
* Returns the cardinality of the result of a bitwise AND of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids
+ * allocating an intermediate bitmap to hold the result of the OR.
+ * The current bitmap is not modified.
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int andCardinality(final EWAHCompressedBitmap32 a) {
@@ -428,7 +451,8 @@ public int andCardinality(final EWAHCompressedBitmap32 a) {
/**
* Returns a new compressed bitmap containing the bitwise AND NOT values
- * of the current bitmap with some other bitmap.
+ * of the current bitmap with some other bitmap. The current bitmap
+ * is not modified.
*
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
@@ -437,7 +461,7 @@ public int andCardinality(final EWAHCompressedBitmap32 a) {
* call the trim() method to reduce memory usage.
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -452,13 +476,14 @@ public EWAHCompressedBitmap32 andNot(final EWAHCompressedBitmap32 a) {
/**
* Returns a new compressed bitmap containing the bitwise AND NOT values
- * of the current bitmap with some other bitmap.
+ * of the current bitmap with some other bitmap. The current bitmap
+ * is not modified.
*
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -507,7 +532,7 @@ public void andNotToContainer(final EWAHCompressedBitmap32 a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& (~rlwj.getLiteralWordAt(k)));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -528,11 +553,12 @@ else if (adjustContainerSizeWhenAggregating)
/**
* Returns the cardinality of the result of a bitwise AND NOT of the
- * values of the current bitmap with some other bitmap. Avoids needing
- * to allocate an intermediate bitmap to hold the result of the OR.
+ * values of the current bitmap with some other bitmap. Avoids allocating
+ * an intermediate bitmap to hold the result of the OR.
+ * The current bitmap is not modified.
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int andNotCardinality(final EWAHCompressedBitmap32 a) {
@@ -581,13 +607,17 @@ public void clear() {
* @see java.lang.Object#clone()
*/
@Override
- public EWAHCompressedBitmap32 clone()
- throws java.lang.CloneNotSupportedException {
- final EWAHCompressedBitmap32 clone = (EWAHCompressedBitmap32) super
- .clone();
- clone.buffer = this.buffer.clone();
- clone.actualsizeinwords = this.actualsizeinwords;
- clone.sizeinbits = this.sizeinbits;
+ public EWAHCompressedBitmap32 clone() {
+ EWAHCompressedBitmap32 clone = null;
+ try {
+ clone = (EWAHCompressedBitmap32) super
+ .clone();
+ clone.buffer = this.buffer.clone();
+ clone.actualsizeinwords = this.actualsizeinwords;
+ clone.sizeinbits = this.sizeinbits;
+ } catch (CloneNotSupportedException e) {
+ e.printStackTrace(); // cannot happen
+ }
return clone;
}
@@ -676,7 +706,9 @@ private void fastaddStreamOfEmptyWords(final boolean v, int number) {
/**
* Gets an EWAHIterator over the data. This is a customized iterator
- * which iterates over run length word. For experts only.
+ * which iterates over run length words. For experts only.
+ *
+ * The current bitmap is not modified.
*
* @return the EWAHIterator
*/
@@ -685,6 +717,10 @@ public EWAHIterator32 getEWAHIterator() {
}
/**
+ * Gets an IteratingRLW to iterate over the data. For experts only.
+ *
+ * The current bitmap is not modified.
+ *
* @return the IteratingRLW iterator corresponding to this bitmap
*/
public IteratingRLW32 getIteratingRLW() {
@@ -692,12 +728,25 @@ public IteratingRLW32 getIteratingRLW() {
}
/**
- * get the locations of the true values as one vector. (may use more
- * memory than iterator())
+ * @return a list
+ * @deprecated use toList() instead.
+ */
+ @Deprecated
+ public List<Integer> getPositions() {
+ return toList();
+ }
+
+ /**
+ * Gets the locations of the true values as one list. (May use more
+ * memory than iterator().)
+ *
+ * The current bitmap is not modified.
+ *
+ * API change: prior to version 0.8.3, this method was called getPositions.
*
* @return the positions
*/
- public List<Integer> getPositions() {
+ public List<Integer> toList() {
final ArrayList<Integer> v = new ArrayList<Integer>();
final EWAHIterator32 i = this.getEWAHIterator();
int pos = 0;
@@ -732,6 +781,8 @@ public IteratingRLW32 getIteratingRLW() {
* Returns a customized hash code (based on Karp-Rabin). Naturally, if
* the bitmaps are equal, they will hash to the same value.
*
+ * The current bitmap is not modified.
+ *
*/
@Override
public int hashCode() {
@@ -758,8 +809,10 @@ public int hashCode() {
* check whether there is a set bit, but intersects will run faster if
* you don't need the result of the "and" operation.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return whether they intersect
*/
public boolean intersects(final EWAHCompressedBitmap32 a) {
@@ -777,6 +830,8 @@ public boolean intersects(final EWAHCompressedBitmap32 a) {
* to browse the content if they want an iterator). The location of the
* set bits is returned, in increasing order.
*
+ * The current bitmap is not modified.
+ *
* @return the int iterator
*/
public IntIterator intIterator() {
@@ -784,9 +839,11 @@ public IntIterator intIterator() {
}
/**
- * iterate over the positions of the true values. This is similar to
+ * Iterates over the positions of the true values. This is similar to
* intIterator(), but it uses Java generics.
*
+ * The current bitmap is not modified.
+ *
* @return the iterator
*/
@Override
@@ -851,6 +908,8 @@ else if ((this.actualsizeinwords + number) * 3 / 2 < this.actualsizeinwords
* The running time is proportional to the compressed size (as reported
* by sizeInBytes()).
*
+ * Because this method modifies the bitmap, it is not thread-safe.
+ *
*/
@Override
public void not() {
@@ -898,8 +957,10 @@ public void not() {
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -914,8 +975,10 @@ public EWAHCompressedBitmap32 or(final EWAHCompressedBitmap32 a) {
* Computes the bitwise or between the current bitmap and the bitmap
* "a". Stores the result in the container.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -956,7 +1019,7 @@ public void orToContainer(final EWAHCompressedBitmap32 a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k) {
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
| rlwj.getLiteralWordAt(k));
}
rlwi.discardFirstWords(nbre_literal);
@@ -973,11 +1036,13 @@ public void orToContainer(final EWAHCompressedBitmap32 a,
/**
* Returns the cardinality of the result of a bitwise OR of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids allocating
+ * an intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int orCardinality(final EWAHCompressedBitmap32 a) {
@@ -1068,6 +1133,8 @@ private boolean reserve(final int size) {
/**
* Serialize.
*
+ * The current bitmap is not modified.
+ *
* @param out
* the DataOutput stream
* @throws IOException
@@ -1082,7 +1149,9 @@ public void serialize(DataOutput out) throws IOException {
}
/**
- * Report the size required to serialize this bitmap
+ * Report the size required to serialize this bitmap.
+ *
+ * The current bitmap is not modified.
*
* @return the size in bytes
*/
@@ -1097,6 +1166,8 @@ public int serializedSizeInBytes() {
*
* (This implementation is based on zhenjl's Go version of JavaEWAH.)
*
+ * The current bitmap is not modified.
+ *
* @param i
* the bit we are interested in
* @return whether the bit is set to true
@@ -1196,6 +1267,8 @@ public void setSizeInBits(final int size) {
* it can be extended. The new bits are set to false or true depending
* on the value of defaultvalue.
*
+ * This method is not thread-safe.
+ *
* @param size
* the size in bits
* @param defaultvalue
@@ -1230,6 +1303,8 @@ public boolean setSizeInBits(final int size, final boolean defaultvalue) {
* this compressed bitmap. Initially, the sizeInBits is zero. It is
* extended automatically when you set bits to true.
*
+ * The current bitmap is not modified.
+ *
* @return the size in bits
*/
@Override
@@ -1425,8 +1500,10 @@ public void writeExternal(ObjectOutput out) throws IOException {
* If you are not planning on adding to the resulting bitmap, you may
* call the trim() method to reduce memory usage.
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the EWAH compressed bitmap
*/
@Override
@@ -1444,8 +1521,10 @@ public EWAHCompressedBitmap32 xor(final EWAHCompressedBitmap32 a) {
* The running time is proportional to the sum of the compressed sizes
* (as reported by sizeInBytes()).
*
+ * The current bitmap is not modified.
+ *
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @param container
* where we store the result
*/
@@ -1481,7 +1560,7 @@ public void xorToContainer(final EWAHCompressedBitmap32 a,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
^ rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -1496,11 +1575,13 @@ public void xorToContainer(final EWAHCompressedBitmap32 a,
/**
* Returns the cardinality of the result of a bitwise XOR of the values
- * of the current bitmap with some other bitmap. Avoids needing to
- * allocate an intermediate bitmap to hold the result of the OR.
+ * of the current bitmap with some other bitmap. Avoids allocating an
+ * intermediate bitmap to hold the result of the OR.
+ *
+ * The current bitmap is not modified.
*
* @param a
- * the other bitmap
+ * the other bitmap (it will not be modified)
* @return the cardinality
*/
public int xorCardinality(final EWAHCompressedBitmap32 a) {
@@ -1573,7 +1654,7 @@ public static EWAHCompressedBitmap32 and(
/**
* Returns the cardinality of the result of a bitwise AND of the values
- * of the provided bitmaps. Avoids needing to allocate an intermediate
+ * of the provided bitmaps. Avoids allocating an intermediate
* bitmap to hold the result of the AND.
*
* @param bitmaps
@@ -1748,7 +1829,7 @@ public static EWAHCompressedBitmap32 xor(
/**
* Returns the cardinality of the result of a bitwise OR of the values
- * of the provided bitmaps. Avoids needing to allocate an intermediate
+ * of the provided bitmaps. Avoids allocating an intermediate
* bitmap to hold the result of the OR.
*
* @param bitmaps
8 src/main/java/com/googlecode/javaewah32/FastAggregation32.java
View
@@ -68,7 +68,7 @@ public static void bufferedandWithContainer(
effective = eff;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
for (IteratingRLW32 i : al)
if (i.size() == 0) {
al.clear();
@@ -139,7 +139,7 @@ public int compare(EWAHCompressedBitmap32 a,
maxr = k;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
Arrays.fill(hardbitmap, 0);
}
@@ -208,7 +208,7 @@ public int compare(EWAHCompressedBitmap32 a,
maxr = k;
}
for (int k = 0; k < effective; ++k)
- container.add(hardbitmap[k]);
+ container.addWord(hardbitmap[k]);
Arrays.fill(hardbitmap, 0);
}
container.setSizeInBits(range);
@@ -426,7 +426,7 @@ public int compare(EWAHCompressedBitmap32 a,
- rlw.getRunningLength());
}
}
- container.add(word);
+ container.addWord(word);
index++;
}
for (int i = 0; i < maxAvailablePos; i++) {
14 src/main/java/com/googlecode/javaewah32/IteratorAggregation32.java
View
@@ -219,7 +219,7 @@ protected static int discharge(final BitmapStorage32 container,
if (L + counter > max)
L = max - counter;
for (int k = 0; k < L; ++k) {
- container.add(i.getLiteralWordAt(k));
+ container.addWord(i.getLiteralWordAt(k));
}
counter += L;
i.discardFirstWords(L + L1);
@@ -254,7 +254,7 @@ protected static int dischargeNegated(final BitmapStorage32 container,
if (L + counter > max)
L = max - counter;
for (int k = 0; k < L; ++k) {
- container.add(i.getLiteralWordAt(k));
+ container.addWord(i.getLiteralWordAt(k));
}
counter += L;
i.discardFirstWords(L + L1);
@@ -300,7 +300,7 @@ static void andToContainer(final BitmapStorage32 container,
if (nbre_literal > 0) {
desiredrlwcount -= nbre_literal;
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -343,7 +343,7 @@ static void andToContainer(final BitmapStorage32 container,
rlwj.getNumberOfLiteralWords());
if (nbre_literal > 0) {
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
& rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -403,7 +403,7 @@ public static void xorToContainer(final BitmapStorage32 container,
if (nbre_literal > 0) {
desiredrlwcount -= nbre_literal;
for (int k = 0; k < nbre_literal; ++k)
- container.add(rlwi.getLiteralWordAt(k)
+ container.addWord(rlwi.getLiteralWordAt(k)
^ rlwj.getLiteralWordAt(k));
rlwi.discardFirstWords(nbre_literal);
rlwj.discardFirstWords(nbre_literal);
@@ -581,7 +581,7 @@ public EWAHIterator32 next() {
i.remove();
}
for (int k = 0; k < effective; ++k)
- this.buffer.add(this.hardbitmap[k]);
+ this.buffer.addWord(this.hardbitmap[k]);
Arrays.fill(this.hardbitmap, 0);
return this.buffer.getEWAHIterator();
}
@@ -628,7 +628,7 @@ public EWAHIterator32 next() {
i.remove();
}
for (int k = 0; k < effective; ++k)
- this.buffer.add(this.hardbitmap[k]);
+ this.buffer.addWord(this.hardbitmap[k]);
Arrays.fill(this.hardbitmap, 0);
return this.buffer.getEWAHIterator();
}
4 src/main/java/com/googlecode/javaewah32/IteratorUtil32.java
View
@@ -65,7 +65,7 @@ public static void materialize(final IteratingRLW32 i,
i.getRunningLength());
}
for (int k = 0; k < i.getNumberOfLiteralWords(); ++k)
- c.add(i.getLiteralWordAt(k));
+ c.addWord(i.getLiteralWordAt(k));
if (!i.next())
break;
}
@@ -130,7 +130,7 @@ public static long materialize(final IteratingRLW32 i,
}
long L = i.getNumberOfLiteralWords();
for (int k = 0; k < L; ++k)
- c.add(i.getLiteralWordAt(k));
+ c.addWord(i.getLiteralWordAt(k));
if (Max > 0) {
if (!i.next())
break;
2  src/main/java/com/googlecode/javaewah32/NonEmptyVirtualStorage32.java
View
@@ -37,7 +37,7 @@ public synchronized Throwable fillInStackTrace() {
* thrown.
*/
@Override
- public void add(int newdata) {
+ public void addWord(int newdata) {
if (newdata != 0)
throw nonEmptyException;
}
6 src/main/java/com/googlecode/javaewah32/symmetric/ThresholdFuncBitmap32.java
View
@@ -120,7 +120,7 @@ private final void orLiterals(final BitmapStorage32 out,
w |= R.iterator.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(w);
+ out.addWord(w);
}
}
@@ -132,7 +132,7 @@ private final void andLiterals(final BitmapStorage32 out,
w &= R.iterator.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(w);
+ out.addWord(w);
}
}
@@ -148,7 +148,7 @@ private final void generalLiterals(final int deficit,
.getLiteralWordAt(i + runbegin
- R.beginOfRun());
}
- out.add(threshold4(deficit, this.buffers,
+ out.addWord(threshold4(deficit, this.buffers,
this.bufferUsed));
}
}
135 src/test/java/com/googlecode/javaewah/EWAHCompressedBitmapTest.java
View
@@ -167,7 +167,7 @@ public void testDebugSetSizeInBitsTest() {
b.setSizeInBits(6, true);
- List<Integer> positions = b.getPositions();
+ List<Integer> positions = b.toList();
Assert.assertEquals(2, positions.size());
Assert.assertEquals(Integer.valueOf(4), positions.get(0));
@@ -200,7 +200,7 @@ public void EwahIteratorProblem() {
bitmap.set(i);
}
IntIterator iterator = bitmap.intIterator();
- List<Integer> v = bitmap.getPositions();
+ List<Integer> v = bitmap.toList();
int[] array = bitmap.toArray();
for (int k = 0; k < v.size(); ++k) {
Assert.assertTrue(array[k] == v.get(k).intValue());
@@ -261,11 +261,11 @@ public void SsiYanKaiTest() {
39992, 39993, 39994, 39995, 39996, 39997, 39998, 39999,
270000);
LinkedHashSet<Integer> aPositions = new LinkedHashSet<Integer>(
- a.getPositions());
+ a.toList());
int intersection = 0;
EWAHCompressedBitmap inter = new EWAHCompressedBitmap();
LinkedHashSet<Integer> bPositions = new LinkedHashSet<Integer>(
- b.getPositions());
+ b.toList());
for (Integer integer : bPositions) {
if (aPositions.contains(integer)) {
inter.set(integer.intValue());
@@ -306,14 +306,10 @@ public void habermaasTest() {
equal(ab, bitsetab);
EWAHCompressedBitmap bb = aa.or(ab);
EWAHCompressedBitmap bbAnd = aa.and(ab);
- try {
- EWAHCompressedBitmap abnot = ab.clone();
- abnot.not();
- EWAHCompressedBitmap bbAnd2 = aa.andNot(abnot);
- assertEquals(bbAnd2, bbAnd);
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
+ EWAHCompressedBitmap abnot = ab.clone();
+ abnot.not();
+ EWAHCompressedBitmap bbAnd2 = aa.andNot(abnot);
+ assertEquals(bbAnd2, bbAnd);
BitSet bitsetbb = (BitSet) bitsetaa.clone();
bitsetbb.or(bitsetab);
BitSet bitsetbbAnd = (BitSet) bitsetaa.clone();
@@ -361,16 +357,16 @@ public void testClear() {
bitmap.clear();
bitmap.set(7);
Assert.assertTrue(1 == bitmap.cardinality());
- Assert.assertTrue(1 == bitmap.getPositions().size());
+ Assert.assertTrue(1 == bitmap.toList().size());
Assert.assertTrue(1 == bitmap.toArray().length);
- Assert.assertTrue(7 == bitmap.getPositions().get(0).intValue());
+ Assert.assertTrue(7 == bitmap.toList().get(0).intValue());
Assert.assertTrue(7 == bitmap.toArray()[0]);
bitmap.clear();
bitmap.set(5000);
Assert.assertTrue(1 == bitmap.cardinality());
- Assert.assertTrue(1 == bitmap.getPositions().size());
+ Assert.assertTrue(1 == bitmap.toList().size());
Assert.assertTrue(1 == bitmap.toArray().length);
- Assert.assertTrue(5000 == bitmap.getPositions().get(0)
+ Assert.assertTrue(5000 == bitmap.toList().get(0)
.intValue());
bitmap.set(5001);
bitmap.set(5005);
@@ -382,7 +378,7 @@ public void testClear() {
bitmap.set(1000);
bitmap.set(1001);
Assert.assertTrue(4 == bitmap.cardinality());
- List<Integer> positions = bitmap.getPositions();
+ List<Integer> positions = bitmap.toList();
Assert.assertTrue(4 == positions.size());
Assert.assertTrue(5 == positions.get(0).intValue());
Assert.assertTrue(7 == positions.get(1).intValue());
@@ -400,25 +396,25 @@ public void testEWAHCompressedBitmap() {
long specialval = 1l | (1l << 4) | (1l << 63);
long notzero = ~zero;
EWAHCompressedBitmap myarray1 = new EWAHCompressedBitmap();
- myarray1.add(zero);
- myarray1.add(zero);
- myarray1.add(zero);
- myarray1.add(specialval);
- myarray1.add(specialval);
- myarray1.add(notzero);
- myarray1.add(zero);
- Assert.assertEquals(myarray1.getPositions().size(), 6 + 64);
+ myarray1.addWord(zero);
+ myarray1.addWord(zero);
+ myarray1.addWord(zero);
+ myarray1.addWord(specialval);
+ myarray1.addWord(specialval);
+ myarray1.addWord(notzero);
+ myarray1.addWord(zero);
+ Assert.assertEquals(myarray1.toList().size(), 6 + 64);
EWAHCompressedBitmap myarray2 = new EWAHCompressedBitmap();
- myarray2.add(zero);
- myarray2.add(specialval);
- myarray2.add(specialval);
- myarray2.add(notzero);
- myarray2.add(zero);
- myarray2.add(zero);
- myarray2.add(zero);
- Assert.assertEquals(myarray2.getPositions().size(), 6 + 64);
- List<Integer> data1 = myarray1.getPositions();
- List<Integer> data2 = myarray2.getPositions();
+ myarray2.addWord(zero);
+ myarray2.addWord(specialval);
+ myarray2.addWord(specialval);
+ myarray2.addWord(notzero);
+ myarray2.addWord(zero);
+ myarray2.addWord(zero);
+ myarray2.addWord(zero);
+ Assert.assertEquals(myarray2.toList().size(), 6 + 64);
+ List<Integer> data1 = myarray1.toList();
+ List<Integer> data2 = myarray2.toList();
Vector<Integer> logicalor = new Vector<Integer>();
{
HashSet<Integer> tmp = new HashSet<Integer>();
@@ -432,37 +428,37 @@ public void testEWAHCompressedBitmap() {
logicaland.retainAll(data2);
Collections.sort(logicaland);
EWAHCompressedBitmap arrayand = myarray1.and(myarray2);
- Assert.assertTrue(arrayand.getPositions().equals(logicaland));
+ Assert.assertTrue(arrayand.toList().equals(logicaland));
EWAHCompressedBitmap arrayor = myarray1.or(myarray2);
- Assert.assertTrue(arrayor.getPositions().equals(logicalor));
+ Assert.assertTrue(arrayor.toList().equals(logicalor));
EWAHCompressedBitmap arrayandbis = myarray2.and(myarray1);
- Assert.assertTrue(arrayandbis.getPositions().equals(logicaland));
+ Assert.assertTrue(arrayandbis.toList().equals(logicaland));
EWAHCompressedBitmap arrayorbis = myarray2.or(myarray1);
- Assert.assertTrue(arrayorbis.getPositions().equals(logicalor));
+ Assert.assertTrue(arrayorbis.toList().equals(logicalor));
EWAHCompressedBitmap x = new EWAHCompressedBitmap();
- for (Integer i : myarray1.getPositions()) {
+ for (Integer i : myarray1.toList()) {
x.set(i.intValue());
}
- Assert.assertTrue(x.getPositions().equals(
- myarray1.getPositions()));
+ Assert.assertTrue(x.toList().equals(
+ myarray1.toList()));
x = new EWAHCompressedBitmap();
- for (Integer i : myarray2.getPositions()) {
+ for (Integer i : myarray2.toList()) {
x.set(i.intValue());
}
- Assert.assertTrue(x.getPositions().equals(
- myarray2.getPositions()));
+ Assert.assertTrue(x.toList().equals(
+ myarray2.toList()));
x = new EWAHCompressedBitmap();
for (Iterator<Integer> k = myarray1.iterator(); k.hasNext();) {
x.set(extracted(k).intValue());
}
- Assert.assertTrue(x.getPositions().equals(
- myarray1.getPositions()));
+ Assert.assertTrue(x.toList().equals(
+ myarray1.toList()));
x = new EWAHCompressedBitmap();
for (Iterator<Integer> k = myarray2.iterator(); k.hasNext();) {
x.set(extracted(k).intValue());
}
- Assert.assertTrue(x.getPositions().equals(
- myarray2.getPositions()));
+ Assert.assertTrue(x.toList().equals(
+ myarray2.toList()));
}
/**
@@ -488,7 +484,7 @@ public void testExternalization() throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(
bos.toByteArray());
ewcb.readExternal(new ObjectInputStream(bis));
- List<Integer> result = ewcb.getPositions();
+ List<Integer> result = ewcb.toList();
Assert.assertTrue(val.length == result.size());
for (int k = 0; k < val.length; ++k) {
Assert.assertTrue(result.get(k).intValue() == val[k]);
@@ -598,10 +594,10 @@ public void testMassiveAnd() {
for (int k = 1; k < ewah.length; ++k)
answer = answer.and(ewah[k]);
// result should be empty
- if (answer.getPositions().size() != 0)
+ if (answer.toList().size() != 0)
System.out.println(answer.toDebugString());
- Assert.assertTrue(answer.getPositions().size() == 0);
- Assert.assertTrue(EWAHCompressedBitmap.and(ewah).getPositions()
+ Assert.assertTrue(answer.toList().size() == 0);
+ Assert.assertTrue(EWAHCompressedBitmap.and(ewah).toList()
.size() == 0);
}
@@ -623,14 +619,10 @@ public void testMassiveAndNot() {
for (int k = 1; k < ewah.length; ++k) {
answer = answer.andNot(ewah[k]);
EWAHCompressedBitmap copy = null;
- try {
- copy = ewah[k].clone();
- copy.not();
- answer2.and(copy);
- assertEqualsPositions(answer, answer2);
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
+ copy = ewah[k].clone();
+ copy.not();
+ answer2.and(copy);
+ assertEqualsPositions(answer, answer2);
}
}
@@ -884,7 +876,7 @@ public void testSetGet() {
for (int k = 0; k < val.length; ++k) {
ewcb.set(val[k]);
}
- List<Integer> result = ewcb.getPositions();
+ List<Integer> result = ewcb.toList();
Assert.assertTrue(val.length == result.size());
for (int k = 0; k < val.length; ++k) {
Assert.assertEquals(result.get(k).intValue(), val[k]);
@@ -899,6 +891,12 @@ public void testHashCode() {
Assert.assertEquals(EWAHCompressedBitmap.bitmapOf(50), ewcb);
Assert.assertEquals(EWAHCompressedBitmap.bitmapOf(50)
.hashCode(), ewcb.hashCode());
+ ewcb.addWord(~0l);
+ EWAHCompressedBitmap ewcb2 = ewcb.clone();
+ ewcb2.addWord(0);
+ Assert.assertEquals(ewcb
+ .hashCode(), ewcb2.hashCode());
+
}
@Test
@@ -1027,8 +1025,7 @@ public void TayaraTest() {
}
@Test
- public void TestCloneEwahCompressedBitArray()
- throws CloneNotSupportedException {
+ public void TestCloneEwahCompressedBitArray() {
System.out.println("testing EWAH clone");
EWAHCompressedBitmap a = new EWAHCompressedBitmap();
a.set(410018);
@@ -1396,7 +1393,7 @@ static void assertEqualsIterator(BitSet jdkBitmap,
*/
static void assertEqualsPositions(BitSet jdkBitmap,
EWAHCompressedBitmap ewahBitmap) {
- final List<Integer> positions = ewahBitmap.getPositions();
+ final List<Integer> positions = ewahBitmap.toList();
for (int position : positions) {
if (!jdkBitmap.get(position)) {
throw new RuntimeException(
@@ -1440,8 +1437,8 @@ static void assertEqualsPositions(BitSet jdkBitmap,
*/
static void assertEqualsPositions(EWAHCompressedBitmap ewahBitmap1,