it.unimi.dsi.bits.Fast Java Examples

The following examples show how to use it.unimi.dsi.bits.Fast. 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: 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 #2
Source File: FastTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testLeastMostSignificantBit() {
	for( int i = 0; i < 32; i++ ) {
		assertEquals( i, Fast.leastSignificantBit( 1 << i ) );
		assertEquals( i, Fast.mostSignificantBit( 1 << i ) );
		assertEquals( 0, Fast.leastSignificantBit( 1 << i | 1 ) );
		assertEquals( i, Fast.mostSignificantBit( 1 << i | 1 ) );
		assertEquals( i, Fast.leastSignificantBit( 1L << i ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i ) );
		assertEquals( 0, Fast.leastSignificantBit( 1L << i | 1 ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i | 1 ) );
	}
	for( int i = 32; i < 64; i++ ) {
		assertEquals( i, Fast.leastSignificantBit( 1L << i ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i ) );
		assertEquals( 0, Fast.leastSignificantBit( 1L << i | 1 ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i | 1 ) );
	}
}
 
Example #3
Source File: FastTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void testNat2Int() {
	assertEquals( Integer.MIN_VALUE / 2, Fast.nat2int( Integer.MAX_VALUE ) );
	assertEquals( Integer.MIN_VALUE / 2 + 1, Fast.nat2int( Integer.MAX_VALUE - 2 ) );
	assertEquals( Integer.MAX_VALUE / 2, Fast.nat2int( Integer.MAX_VALUE - 1 ) );

	for( int i = 0; i < 16; i++ ) assertEquals( i, Fast.nat2int( 2*i ) );
	for( int i = -16; i < 0; i++ ) assertEquals( i, Fast.nat2int( -2*i-1 ) );

	assertEquals( Long.MIN_VALUE / 2 + 1, Fast.nat2int( Long.MAX_VALUE - 2 ) );
	assertEquals( Long.MIN_VALUE / 2, Fast.nat2int( Long.MAX_VALUE ) );
	assertEquals( Long.MAX_VALUE / 2, Fast.nat2int( Long.MAX_VALUE - 1 ) );

	for( int i = 0; i < 16; i++ ) assertEquals( i, Fast.nat2int( 2L*i ) );
	for( int i = -16; i < 0; i++ ) assertEquals( i, Fast.nat2int( -2L*i-1 ) );

}
 
Example #4
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 #5
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 #6
Source File: LayeredLabelPropagation.java    From fasten with Apache License 2.0 5 votes vote down vote up
public void clear(final int size) {
	if (mask + 1 < (1 << (Fast.ceilLog2(size) + 1))) {
		mask = (1 << (Fast.ceilLog2(size) + 1)) - 1;
		count = new int[mask + 1];
		key = new int[mask + 1];
		location = new int[mask + 1];
	}
	else while (n-- != 0) count[location[n]] = 0;
	n = 0;
}
 
Example #7
Source File: FastTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testInt2Nat() {
	assertEquals( Integer.MAX_VALUE, Fast.int2nat( Integer.MIN_VALUE / 2 ) );
	assertEquals( Integer.MAX_VALUE - 2, Fast.int2nat( Integer.MIN_VALUE / 2 + 1 ) );
	assertEquals( Integer.MAX_VALUE - 1, Fast.int2nat( Integer.MAX_VALUE / 2 ) );

	for( int i = 0; i < 16; i++ ) assertEquals( 2*i, Fast.int2nat( i ) );
	for( int i = -16; i < 0; i++ ) assertEquals( -2*i-1, Fast.int2nat( i ) );

	assertEquals( Long.MAX_VALUE, Fast.int2nat( Long.MIN_VALUE / 2 ) );
	assertEquals( Long.MAX_VALUE - 2, Fast.int2nat( Long.MIN_VALUE / 2 + 1 ) );
	assertEquals( Long.MAX_VALUE - 1, Fast.int2nat( Long.MAX_VALUE / 2 ) );

	for( int i = 0; i < 16; i++ ) assertEquals( 2L*i, Fast.int2nat( i ) );
	for( int i = -16; i < 0; i++ ) assertEquals( -2L*i-1, Fast.int2nat( i ) );
}
 
Example #8
Source File: FastTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testCount() {
	assertEquals( 0, Fast.count( 0 ) );
	assertEquals( 1, Fast.count( 1 ) );
	assertEquals( 64, Fast.count( 0xFFFFFFFFFFFFFFFFL ) );
	assertEquals( 32, Fast.count( 0xFFFFFFFFL ) );
	assertEquals( 32, Fast.count( 0xAAAAAAAAAAAAAAAAL ) );
}
 
Example #9
Source File: FastTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testLeastSignificantBit() {
	assertEquals( -1, Fast.leastSignificantBit( 0 ) );
	for( int i = 0; i < 64; i++ ) {
		assertEquals( i, Fast.leastSignificantBit( 1L << i ) );
		assertEquals( 0, Fast.leastSignificantBit( 1L << i | 1 ) );
		assertEquals( i, Fast.leastSignificantBit( 1L << i ) );
		assertEquals( 0, Fast.leastSignificantBit( 1L << i | 1 ) );
	}
	
	for( long i = 1; i < ( 1L << 62 ); i += 10000000000000L )
		assertEquals( Long.toString( i ), Long.numberOfTrailingZeros( i ), Fast.leastSignificantBit( i ) );
}
 
Example #10
Source File: FastTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void testMostSignificantBit() {
	assertEquals( -1, Fast.mostSignificantBit( 0 ) );
	for( int i = 0; i < 64; i++ ) {
		assertEquals( i, Fast.mostSignificantBit( 1L << i ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i | 1 ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i ) );
		assertEquals( i, Fast.mostSignificantBit( 1L << i | 1 ) );
	}
	
	for( long i = 1; i < ( 1L << 62 ); i += 10000000000000L )
		assertEquals( Long.toString( i ), 63 - Long.numberOfLeadingZeros( i ), Fast.mostSignificantBit( i ) );
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: LayeredLabelPropagation.java    From fasten with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
	final ImmutableGraph symGraph = this.symGraph;
	final int numNodes = LayeredLabelPropagation.this.n;
	final long numArcs = LayeredLabelPropagation.this.symGraph.numArcs();
	final int[] perm = this.perm;
	int[] permutedSuccessors = new int[32];
	int[] successors;
	final long granularity = Math.max(1024, numArcs >>> 9);
	int start, end;

	double gapCost = 0;
	for (;;) {

		// Try to get another piece of work.
		synchronized(LayeredLabelPropagation.this.cumulativeOutdegrees) {
			if (nextNode == numNodes) {
				LayeredLabelPropagation.this.gapCost.add(gapCost);
				break;
			}
			start = nextNode;
			final long target = nextArcs + granularity;
			if (target >= numArcs) nextNode = numNodes;
			else {
				nextArcs = cumulativeOutdegrees.skipTo(target);
				nextNode = cumulativeOutdegrees.currentIndex();
			}
			end = nextNode;
		}

		final NodeIterator nodeIterator = symGraph.nodeIterator(start);
		for (int i = start; i < end; i++) {
			nodeIterator.nextInt();
			final int node = perm[i];
			final int outdegree = nodeIterator.outdegree();
			if (outdegree > 0) {
				successors = nodeIterator.successorArray();
				permutedSuccessors = IntArrays.grow(permutedSuccessors, outdegree);
				for (int j = outdegree; j-- != 0;)
					permutedSuccessors[j] = perm[successors[j]];
				IntArrays.quickSort(permutedSuccessors, 0, outdegree);
				int prev = node;
				for (int j = 0; j < outdegree; j++) {
					gapCost += Fast.ceilLog2(Math.abs(prev - permutedSuccessors[j]));
					prev = permutedSuccessors[j];
				}
			}
		}
	}
}
 
Example #19
Source File: FastTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testSelect() {
	assertEquals( 0, Fast.select( 1, 0 ) );
	for( int i = 0; i < 64; i++ ) assertEquals( i, Fast.select( 0xFFFFFFFFFFFFFFFFL, i ) );
	for( int i = 1; i < 32; i++ ) assertEquals( 2 * i + 1, Fast.select( 0xAAAAAAAAAAAAAAAAL, i ) );
}
 
Example #20
Source File: FastTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testLength() {
	assertEquals( 1, Fast.length( 0 ) );
	assertEquals( 1, Fast.length( 0L ) );
	for( int i = 1; i < 100; i++ ) assertEquals( Fast.mostSignificantBit( i ) + 1, Fast.length( i ) ); 
	for( long i = 1; i < 100; i++ ) assertEquals( Fast.mostSignificantBit( i ) + 1, Fast.length( i ) ); 
}
 
Example #21
Source File: FastTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testCeilLog2() {
	for( int i = 1; i < 1000; i++ ) assertEquals( (int)Math.ceil( Math.log( i ) / Math.log( 2 ) ), Fast.ceilLog2( i ) );
}
 
Example #22
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 #23
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 #24
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 #25
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 #26
Source File: InputBitStream.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** Reads a long natural number in Golomb coding.
 *
 * <P>This method implements also the case in which <code>b</code> is 0: in this case,
 * nothing will be read, and 0 will be returned. 
 *
 * @param b the modulus for the coding.
 * @return the next Golomb-encoded long natural number.
 * @throws IllegalArgumentException if you use a nonpositive modulus.
 * @see OutputBitStream#writeGolomb(int, int)
 */

public long readLongGolomb( final long b ) throws IOException {
	return readLongGolomb( b, Fast.mostSignificantBit( b ) );
}
 
Example #27
Source File: InputBitStream.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** Reads a natural number in Golomb coding.
 *
 * <P>This method implements also the case in which <code>b</code> is 0: in this case,
 * nothing will be read, and 0 will be returned. 
 *
 * @param b the modulus for the coding.
 * @return the next Golomb-encoded natural number.
 * @throws IllegalArgumentException if you use a nonpositive modulus.
 * @see OutputBitStream#writeGolomb(int, int)
 */

public int readGolomb( final int b ) throws IOException {
	return readGolomb( b, Fast.mostSignificantBit( b ) );
}
 
Example #28
Source File: InputBitStream.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** Reads a long natural number in a limited range using a minimal binary coding.
 *
 * @param b a strict upper bound.
 * @return the next minimally binary encoded long natural number.
 * @throws IllegalArgumentException if you try to read a negative number or use a nonpositive base.
 * @see OutputBitStream#writeMinimalBinary(int, int)
 */

public long readLongMinimalBinary( final long b ) throws IOException {
	return readLongMinimalBinary( b, Fast.mostSignificantBit( b ) );
}
 
Example #29
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** 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 ) );
}
 
Example #30
Source File: OutputBitStream.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** Writes a natural number in Golomb coding.
 * 
 * <p>Golomb coding with modulo <var>b</var> writes a natural number <var>x</var> as the quotient of
 * the division of <var>x</var> and <var>b</var> in {@linkplain #writeUnary(int) unary}, 
 * followed by the remainder in {@linkplain #writeMinimalBinary(int, int) minimal binary code}.
 *
 * <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 writeGolomb( final int x, final int b ) throws IOException {
	return writeGolomb( x, b, Fast.mostSignificantBit( b ) );
}