Java Code Examples for it.unimi.dsi.bits.Fast#mostSignificantBit()

The following examples show how to use it.unimi.dsi.bits.Fast#mostSignificantBit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/** Writes a long natural number in skewed Golomb coding.
 * 
 * <P>This method implements also the case in which <code>b</code> is 0: in this case,
 * the argument <code>x</code> may only be zero, and nothing will be written. 
 *
 * @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 #writeSkewedGolomb(int, int)
 */

public long writeLongSkewedGolomb( final long x, final long b ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( b < 0 ) throw new IllegalArgumentException( "The modulus " + b + " is negative" );
	if ( b == 0 ) {
		if ( x != 0 ) throw new IllegalArgumentException( "The modulus is 0, but the argument is " + x );
		return 0;
	}

	final long i = Fast.mostSignificantBit( x / b + 1 );
	final long l = writeLongUnary( i );
	final long M = ( ( 1 << i + 1 ) - 1 ) * b;
	final long m = ( M / ( 2 * b ) ) * b;

	return l + writeLongMinimalBinary( x - m, M - m );
}
 
Example 2
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/** Writes a natural number in skewed Golomb coding.
 *
 * <P>This method implements also the case in which <code>b</code> is 0: in this case,
 * the argument <code>x</code> may only be zero, and nothing will be written. 
 *
 * @param x a 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.
 */

public int writeSkewedGolomb( final int x, final int b ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( b < 0 ) throw new IllegalArgumentException( "The modulus " + b + " is negative" );
	if ( b == 0 ) {
		if ( x != 0 ) throw new IllegalArgumentException( "The modulus is 0, but the argument is " + x );
		return 0;
	}

	final int i = Fast.mostSignificantBit( x / b + 1 );
	final int l = writeUnary( i );
	final int M = ( ( 1 << i + 1 ) - 1 ) * b;
	final int m = ( M / ( 2 * b ) ) * b;

	return l + writeMinimalBinary( x - m, M - m );
}
 
Example 3
Source File: FenwickTree.java    From fasten with Apache License 2.0 6 votes vote down vote up
public int find(int k) {
	int p, q, m;
	p = 0;
	q = 1 << Fast.mostSignificantBit(n);
	while (q != 0) {
		if (p + q <= n) {
			m = cumCount[p + q];
			if (k >= m) {
				p += q;
				k -= m;
			}
		}
		q >>= 1;
	}
	return p;
}
 
Example 4
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a long natural number in shifted &gamma; coding.
 *
 * @param x a natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 * @see #writeShiftedGamma(int)
 */

public int writeLongShiftedGamma( long x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( x < MAX_PRECOMPUTED ) return writeInt( SHIFTED_GAMMA[ (int)x ], SHIFTED_GAMMA[ (int)x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( x );
	final int l = writeUnary( msb + 1 );
	return l + ( msb > 0 ? writeLong( x, msb ) : 0 );
}
 
Example 5
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a long natural number in variable-length nibble coding.
 *
 * @param x a long natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 * @see #writeNibble(int)
 */

public int writeLongNibble( final long x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );

	if ( x == 0 ) return writeInt( 8, 4 );
	final int msb = Fast.mostSignificantBit( x );
	int h = msb / 3;
	do {
		writeBit( h == 0 );
		writeInt( (int)( x >> h * 3 ) , 3 );
	} while( h-- != 0 );
	return ( ( msb / 3 ) + 1 ) << 2;
}
 
Example 6
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a natural number in variable-length nibble coding.
 *
 * <P>Variable-length nibble coding records a natural number by padding its binary
 * representation to the left using zeroes, until its length is a multiple of three.
 * Then, the resulting string is
    * broken in blocks of 3 bits, and each block is prefixed with a bit, which is
    * zero for all blocks except for the last one. 
 * @param x a natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 */

public int writeNibble( final int x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );

	if ( x == 0 ) return writeInt( 8, 4 );
	final int msb = Fast.mostSignificantBit( x );
	int h = msb / 3;
	do {
		writeBit( h == 0 );
		writeInt( x >> h * 3 , 3 );
	} while( h-- != 0 );
	return ( ( msb / 3 ) + 1 ) << 2;
}
 
Example 7
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a long natural number in &zeta; coding.
 *
 * @param x a long natural number.
 * @param k the shrinking factor.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number or use a nonpositive shrinking factor.
 * @see #writeZeta(int, int)
 */

public int writeLongZeta( long x, final int k ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( k < 1 ) throw new IllegalArgumentException( "The shrinking factor " + k + " is not positive" );
	if ( k == 3 && x < MAX_PRECOMPUTED ) return writeInt( ZETA_3[ (int)x ], ZETA_3[ (int)x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( ++x );
	final int h = msb / k;
	final int l = writeUnary( h );
	final long left = 1 << h * k;
	return l + ( x - left < left 
		? writeLong( x - left, h * k + k - 1 ) 
		: writeLong( x, h * k + k ) );
}
 
Example 8
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a long natural number in &delta; coding.
 *
 * @param x a long natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 * @see #writeDelta(int)
 */

public int writeLongDelta( long x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( x < MAX_PRECOMPUTED ) return writeInt( DELTA[ (int)x ], DELTA[ (int)x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( ++x );
	final int l = writeGamma( msb );
	return l + ( msb != 0 ? writeLong( x, msb ) : 0 );
}
 
Example 9
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/** Writes a long natural number in &gamma; coding.
 *
 * @param x a long natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 * @see #writeGamma(int)
 */

public int writeLongGamma( long x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( x < MAX_PRECOMPUTED ) return writeInt( GAMMA[ (int)x ], GAMMA[ (int)x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( ++x );
	final int l = writeUnary( msb );
	return l + ( msb != 0 ? writeLong( x, msb ) : 0 );
}
 
Example 10
Source File: ConcurrentCountingMap.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
/** Creates a new concurrent counting map.
 *
 * @param concurrencyLevel the number of stripes (it will be {@linkplain Integer#highestOneBit(int) forced to be a power of two}).
 */
public ConcurrentCountingMap(final int concurrencyLevel) {
	stripe = new Stripe[Math.max(2, Integer.highestOneBit(concurrencyLevel))];
	lock = new ReentrantReadWriteLock[stripe.length];
	for(int i = stripe.length; i-- != 0;) {
		stripe[i] = new Stripe();
		lock[i] = new ReentrantReadWriteLock();
	}
	shift = 64 - Fast.mostSignificantBit(stripe.length);
}
 
Example 11
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/** Writes a natural number in shifted &gamma; coding.
 *
 * The shifted &gamma; coding of 0 is 1. The coding of a positive number 
 * of <var>k</var> bits is
 * obtained writing <var>k</var> in unary, followed by the lower
 * <var>k</var>-1 bits of the number (equivalently, by writing
 * <var>k</var> zeroes followed by the number).
 * 
 * @param x a natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 */

public int writeShiftedGamma( int x ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( x < MAX_PRECOMPUTED ) return writeInt( SHIFTED_GAMMA[ x ], SHIFTED_GAMMA[ x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( x );
	final int l = writeUnary( msb + 1 );
	return l + ( msb > 0 ? writeInt( x, msb ) : 0 );
}
 
Example 12
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/** Writes a natural number in &delta; coding.
 *
 * The &delta; coding of a positive number of <var>k</var> bits is
 * obtained writing <var>k</var>-1 in &gamma; coding, followed by the
 * lower <var>k</var>-1 bits of the number. The coding of a natural
 * number is obtained by adding one and coding.
 *
 * @param x a natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 */

public int writeDelta( int x ) throws IOException {
	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 );
}
 
Example 13
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/** Writes a natural number in &gamma; coding.
 *
 * <P>The &gamma; coding of a positive number of <var>k</var> bits is
 * obtained writing <var>k</var>-1 in unary, followed by the lower
 * <var>k</var>-1 bits of the number. The coding of a natural number is
 * obtained by adding one and coding.
 *
 * @param x a natural number.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number.
 */

public int writeGamma( int x ) throws IOException {
	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 );
	// TODO: Can't we eliminate this variable? Is it advantageous?
	final int l = writeUnary( msb );
	return l + ( msb != 0 ? writeInt( x, msb ) : 0 );
}
 
Example 14
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/** Writes a natural number in &zeta; coding.
 *
 * <P>&zeta; coding (with modulo <var>k</var>) records positive numbers in
 * the intervals
 * [1,2<sup><var>k</var></sup>-1],[2<sup><var>k</var></sup>,2<sup><var>k</var>+1</sup>-1],&hellip;,[2<sup><var>hk</var></sup>,2<sup>(<var>h</var>+1)<var>k</var></sup>-1]
 * by coding <var>h</var> in unary, followed by a minimal binary coding of
 * the offset in the interval.  The coding of a natural number is obtained
 * by adding one and coding.
 * 
 * <P>&zeta; codes were defined by 
 * Paolo Boldi and Sebastiano Vigna in 
 * &ldquo;<a href="http://vigna.dsi.unimi.it/papers.php#BoVCWWW">Codes for the World&minus;Wide Web</a>&rdquo;,
	 * <i>Internet Math.</i>, 2(4):405-427, 2005. The paper contains also a detailed analysis.
 * 
 * @param x a natural number.
 * @param k the shrinking factor.
 * @return the number of bits written.
 * @throws IllegalArgumentException if you try to write a negative number or use a nonpositive shrinking factor.
 */

public int writeZeta( int x, final int k ) throws IOException {
	if ( x < 0 ) throw new IllegalArgumentException( "The argument " + x + " is negative" );
	if ( k < 1 ) throw new IllegalArgumentException( "The shrinking factor " + k + " is not positive" );
	if ( k == 3 && x < MAX_PRECOMPUTED ) return writeInt( ZETA_3[ x ], ZETA_3[ x ] >>> 26 );

	final int msb = Fast.mostSignificantBit( ++x );
	final int h = msb / k;
	final int l = writeUnary( h );
	final int left = 1 << h * k;
	return l + ( x - left < left 
		? writeInt( x - left, h * k + k - 1 ) 
		: writeInt( x, h * k + k ) );
}
 
Example 15
Source File: BloomFilter.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** 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 );
}
 
Example 16
Source File: BloomFilter2.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** 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 BloomFilter2( final int n ) {
    this( n, Fast.mostSignificantBit( n ) + 1 );
}
 
Example 17
Source File: Util.java    From BUbiNG with Apache License 2.0 2 votes vote down vote up
/** Returns the length of the vByte encoding of a natural number.
 *
 * @param x a natural number.
 * @return the length of the vByte encoding of {@code x}.
 */
public static final int vByteLength(final int x) {
	return Fast.mostSignificantBit(x) / 7 + 1;
}