/** Returns the least significant bit of a long. * * <p>This method computes the LSB of <var>x</var> by computing the MSB * of <var>x</var> & −<var>x</var>. * * @param x a long. * @return the least significant bit of <code>x</code>, of <code>x</code> is nonzero; −1, otherwise. */ private static int msbBasedLeastSignificantBit( long x ) { return mostSignificantBit( x & -x ); }
/** Computes the ceiling of the base-two logarithm of the argument. * * <p>This method relies of {@link #mostSignificantBit(int)}, and thus is pretty fast. * * @param x an integer. * @return the ceiling of the base-two logarithm of the argument, or -1 if <code>x</code> is zero. */ public static int ceilLog2( final int x ) { if ( x == 0 ) return -1; return mostSignificantBit( x - 1 ) + 1; }
/** Returns the number of bits that are necessary to encode the argument. * * @param x an integer. * @return the number of bits that are necessary to encode <code>x</code>. */ public static int length( final int x ) { return x == 0 ? 1 : mostSignificantBit( x ) + 1; }
/** Returns the number of bits that are necessary to encode the argument. * * @param x a long. * @return the number of bits that are necessary to encode <code>x</code>. */ public static int length( final long x ) { return x == 0 ? 1 : mostSignificantBit( x ) + 1; }
/** Returns the number of bits that are necessary to encode the argument. * * @param x an integer. * @return the number of bits that are necessary to encode <code>x</code>. */ public static int length(final int x) { return x == 0 ? 1 : mostSignificantBit(x) + 1; }
/** Computes the ceiling of the base-two logarithm of the argument. * * <p>This method relies of {@link #mostSignificantBit(long)}, and thus is pretty fast. * * @param x an integer. * @return the ceiling of the base-two logarithm of the argument, or -1 if <code>x</code> is zero. */ public static int ceilLog2( final long x ) { if ( x == 0 ) return -1; return mostSignificantBit( x - 1 ) + 1; }
/** Creates a new high-precision Bloom filter a given expected number of elements. * * <p>This constructor uses a number of hash functions that is logarithmic in the number * of expected elements. This usually results in no false positives at all. * * @param n the expected number of elements. */ public BloomFilter( final int n ) { this( n, Fast.mostSignificantBit( n ) + 1 ); }
/** Returns the least significant bit of a long. * * <p>This method computes the LSB of <var>x</var> by computing the MSB * of <var>x</var> & −<var>x</var>. * * @param x a long. * @return the least significant bit of <code>x</code>, of <code>x</code> is nonzero; −1, otherwise. */ private static int msbBasedLeastSignificantBit( long x ) { return mostSignificantBit( x & -x ); }
/** Returns the number of bits that are necessary to encode the argument. * * @param x a long. * @return the number of bits that are necessary to encode <code>x</code>. */ public static int length(final long x) { return x == 0 ? 1 : mostSignificantBit(x) + 1; }
@Override public long previousOne(final long index) { if (index == 0) return -1; final long[] bits = this.bits; final int from = word(index - 1); final long mask = 1L << index - 1; final long maskedFirstWord = bits[from] & (mask | mask - 1); if (maskedFirstWord != 0) return from * (long)BITS_PER_WORD + Fast.mostSignificantBit(maskedFirstWord); for (int i = from; i-- != 0;) if (bits[i] != 0) return i * (long)BITS_PER_WORD + Fast.mostSignificantBit(bits[i]); return -1; }
/** Writes a long natural number in Golomb coding. * * @param x a long natural number. * @param b the modulus for the coding. * @return the number of bits written. * @throws IllegalArgumentException if you try to write a negative number or use a negative modulus. * @see #writeGolomb(int, int) */ public long writeLongGolomb( final long x, final long b ) throws IOException { return writeLongGolomb( x, b, Fast.mostSignificantBit( b ) ); }
public long previousOne( final long index ) { if ( index == 0 ) return -1; final long[] bits = this.bits; final int from = word( index - 1 ); final long mask = 1L << bit( index - 1 ); final long maskedFirstWord = bits[ from ] & ( mask | mask - 1 ); if ( maskedFirstWord != 0 ) return from * BITS_PER_WORD + Fast.mostSignificantBit( maskedFirstWord ); for ( int i = from; i-- != 0; ) if ( bits[ i ] != 0 ) return i * BITS_PER_WORD + Fast.mostSignificantBit( bits[ i ] ); return -1; }
/** Reads a natural number in a limited range using a minimal binary coding. * * @param b a strict upper bound. * @return the next minimally binary encoded natural number. * @throws IllegalArgumentException if you try to read a negative number or use a nonpositive base. * @see OutputBitStream#writeMinimalBinary(int, int) */ public int readMinimalBinary( final int b ) throws IOException { return readMinimalBinary( b, Fast.mostSignificantBit( b ) ); }
/** Writes a long natural number in a limited range using a minimal binary coding. * * @param x a natural number. * @param b a strict upper bound for <code>x</code>. * @return the number of bits written. * @throws IllegalArgumentException if you try to write a negative number or use a nonpositive base. * @see #writeMinimalBinary(int, int) */ public int writeLongMinimalBinary( final long x, final long b ) throws IOException { if ( b < 1 ) throw new IllegalArgumentException( "The bound " + b + " is not positive" ); return writeLongMinimalBinary( x, b, Fast.mostSignificantBit( b ) ); }
@Override public long encode(long symbol) { symbol++; final int msb = Fast.mostSignificantBit(symbol); return Long.reverse(symbol) >>> 63 - 2 * msb; }
/** Writes a long natural number in a limited range using a minimal binary coding. * * @param x a natural number. * @param b a strict upper bound for <code>x</code>. * @return the number of bits written. * @throws IllegalArgumentException if you try to write a negative number or use a nonpositive base. * @see #writeMinimalBinary(int, int) */ public int writeLongMinimalBinary(final long x, final long b) throws IOException { if (b < 1) throw new IllegalArgumentException("The bound " + b + " is not positive"); return writeLongMinimalBinary(x, b, Fast.mostSignificantBit(b)); }
/** Writes a long natural number in Golomb coding. * * @param x a long natural number. * @param b the modulus for the coding. * @return the number of bits written. * @throws IllegalArgumentException if you try to write a negative number or use a negative modulus. * @see #writeGolomb(int, int) */ public long writeLongGolomb(final long x, final long b) throws IOException { return writeLongGolomb(x, b, Fast.mostSignificantBit(b)); }
public int writeNonZeroGamma( long value ) throws IOException { if ( value <= 0 ) throw new IllegalArgumentException( "The argument " + value + " is not strictly positive." ); final int msb = Fast.mostSignificantBit( value ); final long unary = 1L << msb; append( unary, msb + 1 ); append( value ^ unary, msb ); return 2 * msb + 1; }
private int writeGamma( int x ) { if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" ); if ( x < MAX_PRECOMPUTED ) return writeInt( GAMMA[ x ], GAMMA[ x ] >>> 26 ); final int msb = Fast.mostSignificantBit( ++x ); final int l = writeUnary( msb ); return l + ( msb != 0 ? writeInt( x, msb ) : 0 ); }
private int writeDelta( int x ) { if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" ); if ( x < MAX_PRECOMPUTED ) return writeInt( DELTA[ x ], DELTA[ x ] >>> 26 ); final int msb = Fast.mostSignificantBit( ++x ); final int l = writeGamma( msb ); return l + ( msb != 0 ? writeInt( x, msb ) : 0 ); }