/**
  * Find the index of the previous set bit less than or equal to i, returns -1 if none found.
  *
  * @param i starting index
  * @return index of the previous set bit
  */
 public int prevSetBit(final int i) {
   int x = i >> 6; // i / 64 with sign extension
   long w = bitmap[x];
   w <<= 64 - i - 1;
   if (w != 0) {
     return i - Long.numberOfLeadingZeros(w);
   }
   for (--x; x >= 0; --x) {
     if (bitmap[x] != 0) {
       return x * 64 + 63 - Long.numberOfLeadingZeros(bitmap[x]);
     }
   }
   return -1;
 }
Example #2
0
  /**
   * Get the encoded length if an integer is stored in a variable-length format
   *
   * @return the encoded length
   */
  public static int getVIntSize(long i) {
    if (i >= -112 && i <= 127) {
      return 1;
    }

    if (i < 0) {
      i ^= -1L; // take one's complement'
    }
    // find the number of bytes with non-leading zeros
    int dataBits = Long.SIZE - Long.numberOfLeadingZeros(i);
    // find the number of data bytes + length byte
    return (dataBits + 7) / 8 + 1;
  }
Example #3
0
  private Node makeSiblings(Node node, Node sibling) {
    int parentLevel = MAX_BITS - Long.numberOfLeadingZeros(node.bits ^ sibling.bits);

    Node parent = createNode(node.bits, parentLevel, 0);

    // the branch is given by the bit at the level one below parent
    long branch = sibling.bits & parent.getBranchMask();
    if (branch == 0) {
      parent.left = sibling;
      parent.right = node;
    } else {
      parent.left = node;
      parent.right = sibling;
    }

    return parent;
  }