@Override
 public int prevSetBit(int i) {
   assert i >= 0;
   final int i4096 = i >>> 12;
   final long index = indices[i4096];
   final long[] bitArray = this.bits[i4096];
   int i64 = i >>> 6;
   final long indexBits = index & ((1L << i64) - 1);
   final int o = Long.bitCount(indexBits);
   if ((index & (1L << i64)) != 0) {
     // There is at least one bit that is set in the same long, check if there
     // is one bit that is set that is lower than i
     final long bits = bitArray[o] & ((1L << i << 1) - 1);
     if (bits != 0) {
       return (i64 << 6) | (63 - Long.numberOfLeadingZeros(bits));
     }
   }
   if (indexBits == 0) {
     // no more bits are set in this block, go find the last bit in the
     // previous block
     return lastDoc(i4096 - 1);
   }
   // go to the previous long
   i64 = 63 - Long.numberOfLeadingZeros(indexBits);
   final long bits = bitArray[o - 1];
   return (i4096 << 12) | (i64 << 6) | (63 - Long.numberOfLeadingZeros(bits));
 }
 private void initHCI() {
   // LHC, NI, ...
   long maxHcAddr = ~((-1L) << dims);
   int nSetFilterBits = Long.bitCount(maskLower | ((~maskUpper) & maxHcAddr));
   // nPossibleMatch = (2^k-x)
   long nPossibleMatch = 1L << (dims - nSetFilterBits);
   if (isNI) {
     int nChild = node.ntGetSize();
     int logNChild = Long.SIZE - Long.numberOfLeadingZeros(nChild);
     // the following will overflow for k=60
     boolean useHcIncrementer = (nChild > nPossibleMatch * (double) logNChild * 2);
     // DIM < 60 as safeguard against overflow of (nPossibleMatch*logNChild)
     if (useHcIncrementer && PhTree11.HCI_ENABLED && dims < 50) {
       useNiHcIncrementer = true;
     } else {
       useNiHcIncrementer = false;
     }
   } else if (PhTree11.HCI_ENABLED) {
     if (isHC) {
       // nPossibleMatch < 2^k?
       // PAPER
       useHcIncrementer = nPossibleMatch * 2 <= maxHcAddr;
     } else {
       int logNPost = Long.SIZE - Long.numberOfLeadingZeros(nMaxEntry) + 1 + 1;
       useHcIncrementer = (nMaxEntry >= 2 * nPossibleMatch * (double) logNPost);
     }
   }
 }
 /** Return the last document that occurs on or before the provided block index. */
 private int lastDoc(int i4096) {
   long index;
   while (i4096 >= 0) {
     index = indices[i4096];
     if (index != 0) {
       final int i64 = 63 - Long.numberOfLeadingZeros(index);
       final long bits = this.bits[i4096][Long.bitCount(index) - 1];
       return (i4096 << 12) | (i64 << 6) | (63 - Long.numberOfLeadingZeros(bits));
     }
     i4096 -= 1;
   }
   return -1;
 }
 /**
  * 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;
 }
  public BytesBytesMultiHashMap(int initialCapacity, float loadFactor, int wbSize, long memUsage) {
    if (loadFactor < 0 || loadFactor > 1) {
      throw new AssertionError("Load factor must be between (0, 1].");
    }
    assert initialCapacity > 0;
    initialCapacity =
        (Long.bitCount(initialCapacity) == 1)
            ? initialCapacity
            : nextHighestPowerOfTwo(initialCapacity);
    // 8 bytes per long in the refs, assume data will be empty. This is just a sanity check.
    int maxCapacity =
        (memUsage <= 0)
            ? DEFAULT_MAX_CAPACITY
            : (int) Math.min((long) DEFAULT_MAX_CAPACITY, memUsage / 8);
    if (maxCapacity < initialCapacity || initialCapacity <= 0) {
      // Either initialCapacity is too large, or nextHighestPowerOfTwo overflows
      initialCapacity =
          (Long.bitCount(maxCapacity) == 1) ? maxCapacity : nextLowestPowerOfTwo(maxCapacity);
    }

    validateCapacity(initialCapacity);
    startingHashBitCount = 63 - Long.numberOfLeadingZeros(initialCapacity);
    this.loadFactor = loadFactor;
    refs = new long[initialCapacity];
    writeBuffers = new WriteBuffers(wbSize, MAX_WB_SIZE);
    resizeThreshold = (int) (initialCapacity * this.loadFactor);
  }
Example #6
0
 // Shifts long bits as double, if the digits is positive, left-shift; if
 // not, shift to right and calculate its carry.
 private static long shiftLongBits(long bits, long digits) {
   if (digits > 0) {
     return bits << digits;
   }
   // change it to positive
   long absdigits = -digits;
   if (!(Long.numberOfLeadingZeros(bits & ~DOUBLE_SIGN_MASK) <= (64 - absdigits))) {
     return 0;
   }
   long ret = bits >> absdigits;
   boolean halfbit = ((bits >> (absdigits - 1)) & 0x1) == 1;
   if (halfbit) {
     // some bits will remain after shifting, calculates its carry
     // subnormal
     if (Long.numberOfTrailingZeros(bits) < (absdigits - 1)) {
       ret = ret + 1;
     }
     if (Long.numberOfTrailingZeros(bits) == (absdigits - 1)) {
       if ((ret & 0x1) == 1) {
         ret = ret + 1;
       }
     }
   }
   return ret;
 }
  /** Writes a signed integral to {@code out}. */
  public static void writeSignedIntegralValue(ByteOutput out, int type, long value) {
    /*
     * Figure out how many bits are needed to represent the value,
     * including a sign bit: The bit count is subtracted from 65
     * and not 64 to account for the sign bit. The xor operation
     * has the effect of leaving non-negative values alone and
     * unary complementing negative values (so that a leading zero
     * count always returns a useful number for our present
     * purpose).
     */
    int requiredBits = 65 - Long.numberOfLeadingZeros(value ^ (value >> 63));

    // Round up the requiredBits to a number of bytes.
    int requiredBytes = (requiredBits + 0x07) >> 3;

    /*
     * Write the header byte, which includes the type and
     * requiredBytes - 1.
     */
    out.writeByte(type | ((requiredBytes - 1) << 5));

    // Write the value, per se.
    while (requiredBytes > 0) {
      out.writeByte((byte) value);
      value >>= 8;
      requiredBytes--;
    }
  }
 public static String formatSize(long value) {
   if (value < 1024) {
     return value + " B";
   }
   int z = (63 - Long.numberOfLeadingZeros(value)) / 10;
   return String.format("%.1f %siB", (double) value / (1L << (z * 10)), " KMGTPE".charAt(z));
 }
Example #9
0
 @Test(timeout = 5000L)
 public void powExactLongIntExact() {
   for (long base : LONGS) {
     for (int power : INTS) {
       if (power < 0) continue;
       boolean overflow =
           BigInteger.valueOf(63 - Long.numberOfLeadingZeros(Math.abs(base)))
                   .multiply(BigInteger.valueOf(power))
                   .compareTo(BigInteger.valueOf(64))
               > 0;
       BigInteger expected = overflow ? null : BigInteger.valueOf(base).pow(power);
       if (expected != null && expected.bitLength() <= 63) {
         long value = powExact(base, power);
         assertEquals(base + " ^ " + power, expected.longValue(), value);
         if (value < 10000000000000000L)
           assertEquals(
               base + " ^ " + power, power == 1 ? base : (long) Math.pow(base, power), value);
         assertEquals(base + " ^ " + power, pow(base, power), value);
       } else {
         try {
           powExact(base, power);
           fail("Should overflow: " + base + " ^ " + power);
         } catch (ArithmeticException e) {
         }
       }
     }
   }
 }
Example #10
0
 static String longToBinaryString(long L) {
   StringBuffer sb = new StringBuffer();
   for (int i = 0; i < Long.numberOfLeadingZeros(L); i++) {
     sb.append('0');
   }
   sb.append(Long.toBinaryString(L));
   return sb.toString();
 }
 @Override
 public long previousClearBit(long fromIndex) {
   if (fromIndex < 0) {
     if (fromIndex == NOT_FOUND) return NOT_FOUND;
     throw new IndexOutOfBoundsException();
   }
   long fromLongIndex = fromIndex >> 6;
   if (fromLongIndex >= longLength) {
     fromLongIndex = longLength - 1;
     fromIndex = size() - 1;
   }
   long l = (~bytes.readLong(fromLongIndex << 3)) << ~fromIndex;
   if (l != 0) return fromIndex - Long.numberOfLeadingZeros(l);
   for (long i = fromLongIndex - 1; i >= 0; i--) {
     l = ~bytes.readLong(i << 3);
     if (l != 0) return (i << 6) + 63 - Long.numberOfLeadingZeros(l);
   }
   return NOT_FOUND;
 }
Example #12
0
 final void forEachValue(Consumer<? super V> action) {
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0) action.accept(this.<V>readValue(allocIndex));
   }
 }
Example #13
0
  /**
   * Answers a double value of d * 2^scaleFactor, the result may be rounded.
   *
   * @param d the base number
   * @param scaleFactor the power number
   * @return d * 2^scaleFactor
   * @since 1.6
   */
  @SuppressWarnings("boxing")
  public static double scalb(double d, int scaleFactor) {
    if (Double.isNaN(d) || Double.isInfinite(d) || 0 == d) {
      return d;
    }
    // change double to long for calculation
    long bits = Double.doubleToLongBits(d);
    // the sign of the results must be the same of given d
    long sign = bits & DOUBLE_SIGN_MASK;
    // calculates the factor of the result
    long factor =
        ((bits & DOUBLE_EXPONENT_MASK) >> DOUBLE_MANTISSA_BITS)
            - DOUBLE_EXPONENT_BIAS
            + scaleFactor;

    // calcutes the factor of sub-normal values
    int subNormalFactor =
        Long.numberOfLeadingZeros(bits & ~DOUBLE_SIGN_MASK) - DOUBLE_NON_MANTISSA_BITS;
    if (subNormalFactor < 0) {
      // not sub-normal values
      subNormalFactor = 0;
    } else {
      factor = factor - subNormalFactor;
    }
    if (factor > Double.MAX_EXPONENT) {
      return (d > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY);
    }

    long result;
    // if result is a sub-normal
    if (factor <= -DOUBLE_EXPONENT_BIAS) {
      // the number of digits that shifts
      long digits = factor + DOUBLE_EXPONENT_BIAS + subNormalFactor;
      if (Math.abs(d) < Double.MIN_NORMAL) {
        // origin d is already sub-normal
        result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK, digits);
      } else {
        // origin d is not sub-normal, change mantissa to sub-normal
        result = shiftLongBits(bits & DOUBLE_MANTISSA_MASK | 0x0010000000000000L, digits - 1);
      }
    } else {
      if (Math.abs(d) >= Double.MIN_NORMAL) {
        // common situation
        result =
            ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
                | (bits & DOUBLE_MANTISSA_MASK);
      } else {
        // origin d is sub-normal, change mantissa to normal style
        result =
            ((factor + DOUBLE_EXPONENT_BIAS) << DOUBLE_MANTISSA_BITS)
                | ((bits << (subNormalFactor + 1)) & DOUBLE_MANTISSA_MASK);
      }
    }
    return Double.longBitsToDouble(result | sign);
  }
Example #14
0
 /**
  * Calculate the number of levels needed for a specific precision. Quadtree cells will not exceed
  * the specified size (diagonal) of the precision.
  *
  * @param meters Maximum size of cells in meters (must greater than zero)
  * @return levels need to achieve precision
  */
 public static int quadTreeLevelsForPrecision(double meters) {
   assert meters >= 0;
   if (meters == 0) {
     return QuadPrefixTree.MAX_LEVELS_POSSIBLE;
   } else {
     final double ratio = 1 + (EARTH_POLAR_DISTANCE / EARTH_EQUATOR); // cell ratio
     final double width = Math.sqrt((meters * meters) / (ratio * ratio)); // convert to cell width
     final long part = Math.round(Math.ceil(EARTH_EQUATOR / width));
     final int level = Long.SIZE - Long.numberOfLeadingZeros(part) - 1; // (log_2)
     return (part <= (1l << level)) ? level : (level + 1); // adjust level
   }
 }
 @Override
 public long previousSetBit(long fromIndex) {
   if (fromIndex < 0) {
     if (fromIndex == NOT_FOUND) return NOT_FOUND;
     throw new IndexOutOfBoundsException();
   }
   long fromLongIndex = fromIndex >> 6;
   if (fromLongIndex >= longLength) {
     // the same policy for this "index out of bounds" situation
     // as in j.u.BitSet
     fromLongIndex = longLength - 1;
     fromIndex = size() - 1;
   }
   // << ~fromIndex === << (63 - (fromIndex & 0x3F))
   long l = bytes.readLong(fromLongIndex << 3) << ~fromIndex;
   if (l != 0) return fromIndex - Long.numberOfLeadingZeros(l);
   for (long i = fromLongIndex - 1; i >= 0; i--) {
     l = bytes.readLong(i << 3);
     if (l != 0) return (i << 6) + 63 - Long.numberOfLeadingZeros(l);
   }
   return NOT_FOUND;
 }
Example #16
0
 final void writeAllEntries(ObjectOutputStream s) throws IOException {
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0) {
       s.writeObject(readKey(allocIndex));
       s.writeObject(readValue(allocIndex));
     }
   }
 }
Example #17
0
 final void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0) {
       writeValue(
           allocIndex, function.apply(this.<K>readKey(allocIndex), this.<V>readValue(allocIndex)));
     }
   }
 }
Example #18
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 #19
0
  /**
   * Returns the index of the nearest bit that is set to {@code true} that occurs on or before the
   * specified starting index. If no such bit exists, or if {@code -1} is given as the starting
   * index, then {@code -1} is returned.
   *
   * @param from the index to start checking from (inclusive)
   * @return the index of the previous set bit, or {@code -1} if there is no such bit
   * @throws IndexOutOfBoundsException if the specified index is less than {@code -1}
   */
  public int previousSetBit(int from) {
    if (from < 0 || from >= size) throw new IndexOutOfBoundsException("index: " + from);

    int u = wordIndex(from);

    long word = words[u] & (WORD_MASK >>> -(from + 1));

    while (true) {
      if (word != 0) return (u + 1) * BITS_PER_WORD - 1 - Long.numberOfLeadingZeros(word);
      if (u-- == 0) return -1;
      word = words[u];
    }
  }
Example #20
0
 final boolean forEachWhile(BiPredicate<? super K, ? super V> predicate) {
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0
         && !predicate.test(this.<K>readKey(allocIndex), this.<V>readValue(allocIndex))) {
       return false;
     }
   }
   return true;
 }
Example #21
0
 private static boolean isCompressible(byte[] data) {
   int len = data.length;
   int[] sum = new int[16];
   for (int i = 0; i < len; i++) {
     int x = (data[i] & 255) >> 4;
     sum[x]++;
   }
   int r = 0;
   for (int x : sum) {
     long v = ((long) x << 32) / len;
     r += 63 - Long.numberOfLeadingZeros(v + 1);
   }
   return len * r < len * 120;
 }
Example #22
0
 @Override
 public final int hashCode() {
   int h = 0;
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0) {
       h += Objects.hashCode(readKey(allocIndex)) ^ Objects.hashCode(readValue(allocIndex));
     }
   }
   return h;
 }
 private void or(final int i4096, final long index, long[] bits, int nonZeroLongCount) {
   assert Long.bitCount(index) == nonZeroLongCount;
   final long currentIndex = indices[i4096];
   if (currentIndex == 0) {
     // fast path: if we currently have nothing in the block, just copy the data
     // this especially happens all the time if you call OR on an empty set
     indices[i4096] = index;
     this.bits[i4096] = Arrays.copyOf(bits, nonZeroLongCount);
     this.nonZeroLongCount += nonZeroLongCount;
     return;
   }
   final long[] currentBits = this.bits[i4096];
   final long[] newBits;
   final long newIndex = currentIndex | index;
   final int requiredCapacity = Long.bitCount(newIndex);
   if (currentBits.length >= requiredCapacity) {
     newBits = currentBits;
   } else {
     newBits = new long[oversize(requiredCapacity)];
   }
   // we iterate backwards in order to not override data we might need on the next iteration if the
   // array is reused
   for (int i = Long.numberOfLeadingZeros(newIndex), newO = Long.bitCount(newIndex) - 1;
       i < 64;
       i += 1 + Long.numberOfLeadingZeros(newIndex << (i + 1)), newO -= 1) {
     // bitIndex is the index of a bit which is set in newIndex and newO is the number of 1 bits on
     // its right
     final int bitIndex = 63 - i;
     assert newO == Long.bitCount(newIndex & ((1L << bitIndex) - 1));
     newBits[newO] =
         longBits(currentIndex, currentBits, bitIndex) | longBits(index, bits, bitIndex);
   }
   indices[i4096] = newIndex;
   this.bits[i4096] = newBits;
   this.nonZeroLongCount += nonZeroLongCount - Long.bitCount(currentIndex & index);
 }
Example #24
0
 final boolean containsValue(SmoothieMap<K, V> map, V value) {
   V v;
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0) {
       if (((v = readValue(allocIndex)) == value
           || (value != null && map.valuesEqual(value, v)))) {
         return true;
       }
     }
   }
   return false;
 }
Example #25
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;
  }
  /** Writes an unsigned integral to {@code out}. */
  public static void writeUnsignedIntegralValue(ByteOutput out, int type, long value) {
    // Figure out how many bits are needed to represent the value.
    int requiredBits = 64 - Long.numberOfLeadingZeros(value);
    if (requiredBits == 0) {
      requiredBits = 1;
    }

    // Round up the requiredBits to a number of bytes.
    int requiredBytes = (requiredBits + 0x07) >> 3;

    /*
     * Write the header byte, which includes the type and
     * requiredBytes - 1.
     */
    out.writeByte(type | ((requiredBytes - 1) << 5));

    // Write the value, per se.
    while (requiredBytes > 0) {
      out.writeByte((byte) value);
      value >>= 8;
      requiredBytes--;
    }
  }
Example #27
0
 @Test(timeout = 5000L)
 public void powExactLongIntRandom() {
   for (int i = 0; i < 100; i++) {
     long base = RANDOM.nextLong();
     for (int j = 0; j < 100; j++) {
       int power = RANDOM.nextInt(Integer.MAX_VALUE);
       boolean overflow =
           BigInteger.valueOf(63 - Long.numberOfLeadingZeros(Math.abs(base)))
                   .multiply(BigInteger.valueOf(power))
                   .compareTo(BigInteger.valueOf(64))
               > 0;
       BigInteger expected = overflow ? null : BigInteger.valueOf(base).pow(power);
       if (expected != null && expected.bitLength() <= 63)
         assertEquals(base + " ^ " + power, expected.longValue(), powExact(base, power));
       else {
         try {
           powExact(base, power);
           fail("Should overflow: " + base + " ^ " + power);
         } catch (ArithmeticException e) {
         }
       }
     }
   }
 }