You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2014/12/12 08:12:48 UTC

[3/4] incubator-calcite git commit: ImmutableBitSet: convert to and from word arrays

ImmutableBitSet: convert to and from word arrays


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/b75a3572
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/b75a3572
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/b75a3572

Branch: refs/heads/master
Commit: b75a3572f3509bfedf73b909dfb10fe573ac2a2b
Parents: 67492c8
Author: Julian Hyde <jh...@apache.org>
Authored: Thu Dec 11 22:33:44 2014 -0800
Committer: Julian Hyde <jh...@apache.org>
Committed: Thu Dec 11 22:36:05 2014 -0800

----------------------------------------------------------------------
 .../apache/calcite/util/ImmutableBitSet.java    | 60 ++++++++++++++++++++
 .../calcite/util/ImmutableBitSetTest.java       | 29 ++++++++++
 2 files changed, 89 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/b75a3572/core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java b/core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java
index ab101b7..e424f9f 100644
--- a/core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java
+++ b/core/src/main/java/org/apache/calcite/util/ImmutableBitSet.java
@@ -26,6 +26,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 import java.io.Serializable;
+import java.nio.LongBuffer;
 import java.util.AbstractList;
 import java.util.Arrays;
 import java.util.BitSet;
@@ -142,6 +143,55 @@ public class ImmutableBitSet
   }
 
   /**
+   * Returns a new immutable bit set containing all the bits in the given long
+   * array.
+   *
+   * <p>More precisely,
+   *
+   * <blockquote>{@code ImmutableBitSet.valueOf(longs).get(n)
+   *   == ((longs[n/64] & (1L<<(n%64))) != 0)}</blockquote>
+   *
+   * <p>for all {@code n < 64 * longs.length}.
+   *
+   * <p>This method is equivalent to
+   * {@code ImmutableBitSet.valueOf(LongBuffer.wrap(longs))}.
+   *
+   * @param longs a long array containing a little-endian representation
+   *        of a sequence of bits to be used as the initial bits of the
+   *        new bit set
+   * @return a {@code ImmutableBitSet} containing all the bits in the long
+   *         array
+   */
+  public static ImmutableBitSet valueOf(long... longs) {
+    int n = longs.length;
+    while (n > 0 && longs[n - 1] == 0) {
+      --n;
+    }
+    if (n == 0) {
+      return EMPTY;
+    }
+    return new ImmutableBitSet(Arrays.copyOf(longs, n));
+  }
+
+  /**
+   * Returns a new immutable bit set containing all the bits in the given long
+   * buffer.
+   */
+  public static ImmutableBitSet valueOf(LongBuffer longs) {
+    longs = longs.slice();
+    int n = longs.remaining();
+    while (n > 0 && longs.get(n - 1) == 0) {
+      --n;
+    }
+    if (n == 0) {
+      return EMPTY;
+    }
+    long[] words = new long[n];
+    longs.get(words);
+    return new ImmutableBitSet(words);
+  }
+
+  /**
    * Creates an ImmutableBitSet with bits from {@code fromIndex} (inclusive) to
    * specified {@code toIndex} (exclusive) set to {@code true}.
    *
@@ -524,6 +574,9 @@ public class ImmutableBitSet
   /**
    * Converts this bit set to an array.
    *
+   * <p>Each entry of the array is the ordinal of a set bit. The array is
+   * sorted.
+   *
    * @return Array of set bits
    */
   public int[] toArray() {
@@ -535,6 +588,13 @@ public class ImmutableBitSet
     return integers;
   }
 
+  /**
+   * Converts this bit set to an array of little-endian words.
+   */
+  public long[] toLongArray() {
+    return words.length == 0 ? words : words.clone();
+  }
+
   /** Returns the union of this bit set with another. */
   public ImmutableBitSet union(ImmutableBitSet other) {
     return builder(this)

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/b75a3572/core/src/test/java/org/apache/calcite/util/ImmutableBitSetTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/util/ImmutableBitSetTest.java b/core/src/test/java/org/apache/calcite/util/ImmutableBitSetTest.java
index 29b6aba..7f2b984 100644
--- a/core/src/test/java/org/apache/calcite/util/ImmutableBitSetTest.java
+++ b/core/src/test/java/org/apache/calcite/util/ImmutableBitSetTest.java
@@ -23,6 +23,7 @@ import com.google.common.collect.Maps;
 
 import org.junit.Test;
 
+import java.nio.LongBuffer;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -396,6 +397,34 @@ public class ImmutableBitSetTest {
     final ImmutableBitSet seventeen = ImmutableBitSet.range(3, 20);
     assertThat(Iterables.size(seventeen.powerSet()), equalTo(131072));
   }
+
+  @Test public void testCreateLongs() {
+    assertThat(ImmutableBitSet.valueOf(0L), equalTo(ImmutableBitSet.of()));
+    assertThat(ImmutableBitSet.valueOf(0xAL),
+        equalTo(ImmutableBitSet.of(1, 3)));
+    assertThat(ImmutableBitSet.valueOf(0xAL, 0, 0),
+        equalTo(ImmutableBitSet.of(1, 3)));
+    assertThat(ImmutableBitSet.valueOf(0, 0, 0xAL, 0),
+        equalTo(ImmutableBitSet.of(129, 131)));
+  }
+
+  @Test public void testCreateLongBuffer() {
+    assertThat(ImmutableBitSet.valueOf(LongBuffer.wrap(new long[] {})),
+        equalTo(ImmutableBitSet.of()));
+    assertThat(ImmutableBitSet.valueOf(LongBuffer.wrap(new long[] {0xAL})),
+        equalTo(ImmutableBitSet.of(1, 3)));
+    assertThat(
+        ImmutableBitSet.valueOf(LongBuffer.wrap(new long[] {0, 0, 0xAL, 0})),
+        equalTo(ImmutableBitSet.of(129, 131)));
+  }
+
+  @Test public void testToLongArray() {
+    final ImmutableBitSet bitSet = ImmutableBitSet.of(29, 4, 1969);
+    assertThat(ImmutableBitSet.valueOf(bitSet.toLongArray()),
+        equalTo(bitSet));
+    assertThat(ImmutableBitSet.valueOf(LongBuffer.wrap(bitSet.toLongArray())),
+        equalTo(bitSet));
+  }
 }
 
 // End ImmutableBitSetTest.java