it.unimi.dsi.io.OutputBitStream Java Examples

The following examples show how to use it.unimi.dsi.io.OutputBitStream. 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: EliasFanoBitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException {
    int l = 31 - Integer.numberOfLeadingZeros(in[offset + len - 1] / len);
    int mask = (1 << l) - 1;

    if (l < 1) {
        l = 1;
        mask = 1;
    }

    int d = 0;
    obs.writeInt(l, 32);
    for (int i = offset; i < offset + len; i++) {
        final int x = in[i];
        final int hx = x >> l;
        obs.writeUnary(hx - d);
        obs.writeInt(x & mask, l);
        d = hx;
    }
}
 
Example #2
Source File: RiceBitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException {
    long sum = 0;
    for (int i = offset; i < offset + len; i++) {
        sum += in[i];
    }
    int b = (int) (0.69 * sum / (double) len);
    if (b == 0) {
        b = 1;
    }
    int log2b = 31 - Integer.numberOfLeadingZeros(b);

    obs.writeInt(log2b, 32);
    for (int i = offset; i < offset + len; i++) {
        obs.writeUnary(in[i] >> log2b);
        obs.writeInt(in[i] & ((1 << log2b) - 1), log2b);
    }
}
 
Example #3
Source File: BitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public byte[] co(int[] in, int offset, int len) {

    byte[] out = new byte[len * 4 + 1024];
    int writtenBits = 0;
    try (OutputBitStream obs = new OutputBitStream(out)) {
        write(obs, in, offset, len);
        writtenBits = (int) obs.writtenBits();
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }

    out = Arrays.copyOf(out, (writtenBits + 7) / 8);

    add(len * Integer.BYTES, out.length * Byte.BYTES);

    return out;
}
 
Example #4
Source File: CodecTestCase.java    From database with GNU General Public License v2.0 6 votes vote down vote up
protected void checkPrefixCodec( PrefixCodec codec, Random r ) throws IOException {
	int[] symbol = new int[ 100 ];
	BooleanArrayList bits = new BooleanArrayList();
	for( int i = 0; i < symbol.length; i++ ) symbol[ i ] = r.nextInt( codec.size() ); 
	for( int i = 0; i < symbol.length; i++ ) {
		BitVector word = codec.codeWords()[ symbol[ i ] ];
		for( int j = 0; j < word.size(); j++ ) bits.add( word.get( j ) );
	}

	BooleanIterator booleanIterator = bits.iterator();
	Decoder decoder = codec.decoder();
	for( int i = 0; i < symbol.length; i++ ) {
		assertEquals( decoder.decode( booleanIterator ), symbol[ i ] );
	}
	
	FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
	OutputBitStream obs = new OutputBitStream( fbaos, 0 );
	obs.write( bits.iterator() );
	obs.flush();
	InputBitStream ibs = new InputBitStream( fbaos.array );
	
	for( int i = 0; i < symbol.length; i++ ) {
		assertEquals( decoder.decode( ibs ), symbol[ i ] );
	}
}
 
Example #5
Source File: CodeWordCoder.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public int encode( final int symbol, final OutputBitStream obs ) throws IOException {
	final BitVector w = codeWord[ symbol ];
	final int size = w.size();
	for( int i = 0; i < size; i++ ) obs.writeBit( w.getBoolean( i ) );
	return size;
}
 
Example #6
Source File: CanonicalHuffmanRabaCoder.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Write out the optional packed symbol table (symbol2byte). When present,
 * the symbol table is written as a sequence of the in use byte values in
 * the unsigned byte order (this is the order in which the frequency[] was
 * specified).
 * 
 * @param symbol2byte
 *            The symbol table.
 * @param obs
 *            The symbol table is written on this bit stream.
 */
protected void writeSymbolTable(final Symbol2Byte symbol2byte,
        final OutputBitStream obs) throws IOException {

    assert symbol2byte != null;

    assert obs != null;

    final int nsymbols = symbol2byte.getSymbolCount();

    assert nsymbols <= 256;

    // for each symbol, write the byte
    for (int i = 0; i < nsymbols; i++) {

        obs.writeInt(symbol2byte.symbol2byte(i), 8/* len */);

    }

}
 
Example #7
Source File: AbstractKeyArrayIndexProcedure.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
        public void writeExternal(final ObjectOutput out) throws IOException {

            out.writeByte(VERSION);
            
            @SuppressWarnings("resource")
            final OutputBitStream obs = new OutputBitStream((OutputStream) out,
                    0/* unbuffered! */, false/*reflectionTest*/);

            obs.writeNibble(n);
            
//            obs.write(a.iterator());

            for (int i = 0; i < n; i++) {

                obs.writeBit(a[i]);

            }

            obs.flush();
            
        }
 
Example #8
Source File: TestCanonicalHuffmanRabaCoder.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify we can regenerate the {@link Fast64CodeWordCoder} from the code
 * word[]. This is tested by coding and decoding random symbol sequences.
 * For this test we need to reconstruct the {@link Fast64CodeWordCoder}. To
 * do that, we need to use the codeWord[] and create a long[] having the
 * same values as the codeWords, but expressed as 64-bit integers.
 * 
 * @param frequency
 *            The frequency[] should include a reasonable proportion of
 *            symbols with a zero frequency in order to replicate the
 *            expected conditions when coding non-random data such as are
 *            found in the keys of a B+Tree.
 * 
 * @throws IOException
 */
public void doRecoderRoundTripTest(final int frequency[]) throws IOException {
    
    final DecoderInputs decoderInputs = new DecoderInputs();
    
    final HuffmanCodec codec = new HuffmanCodec(frequency, decoderInputs);

    final PrefixCoder expected = codec.coder();
    
    final PrefixCoder actual = new Fast64CodeWordCoder(codec.codeWords());
    
    if (log.isDebugEnabled())
        log.debug(printCodeBook(codec.codeWords()));

    /*
     * First verify that both coders produce the same coded values for a
     * symbol sequence of random length drawn from the full set of symbols
     * of random length [1:nsymbols].
     */
    final int[] value = new int[r.nextInt(frequency.length) + 1];
    for(int i=0; i<value.length; i++) {
        // any of the symbols in [0:nsymbols-1].
        value[i] = r.nextInt(frequency.length);
    }

    /*
     * Now code the symbol sequence using both coders and then compare the
     * coded values. They should be the same.
     */
    final byte[] codedValue;
    {
        final FastByteArrayOutputStream ebaos = new FastByteArrayOutputStream();
        final FastByteArrayOutputStream abaos = new FastByteArrayOutputStream();
        final OutputBitStream eobs = new OutputBitStream(ebaos);
        final OutputBitStream aobs = new OutputBitStream(abaos);
        for (int i = 0; i < value.length; i++) {
            final int symbol = value[i];
            expected.encode(symbol, eobs);
            actual.encode(symbol, aobs);
        }
        eobs.flush();
        aobs.flush();
        assertEquals(0, BytesUtil.compareBytesWithLenAndOffset(0/* aoff */,
                ebaos.length, ebaos.array, 0/* boff */, abaos.length,
                abaos.array));
        codedValue = new byte[abaos.length];
        System.arraycopy(abaos.array/*src*/, 0/*srcPos*/, codedValue/*dest*/, 0/*destPos*/, abaos.length/*len*/);
    }

    /*
     * Now verify that the coded sequence decodes to the original symbol
     * sequence using a Decoder which is reconstructed from the bit length
     * and symbol arrays of the codec.
     */
    final CanonicalFast64CodeWordDecoder actualDecoder = new CanonicalFast64CodeWordDecoder(
            decoderInputs.getLengths(), decoderInputs.getSymbols());

    {

        final InputBitStream ibs = new InputBitStream(codedValue);
        
        for (int i = 0; i < value.length; i++) {

            assertEquals(value[i]/* symbol */, actualDecoder.decode(ibs));

        }
        
    }

}
 
Example #9
Source File: Fast64CodeWordCoder.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int encode( final int symbol, final OutputBitStream obs ) throws IOException {
    return obs.writeLong( longCodeWord[ symbol ], length[ symbol ] );
}
 
Example #10
Source File: ZetaBitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException {
    for (int i = offset; i < offset + len; i++) {
        obs.writeZeta(in[i], k);
    }
}
 
Example #11
Source File: FixedLengthBitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException {
    for (int i = offset; i < offset + len; i++) {
        obs.writeInt(in[i], b);
    }
}
 
Example #12
Source File: GammaBitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException {
    for (int i = offset; i < offset + len; i++) {
        obs.writeGamma(in[i]);
    }
}
 
Example #13
Source File: Coder.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/** Encodes a symbol.
 * 
 * @param symbol a symbol.
 * @param obs the output bit stream where the encoded symbol will be written.
 * @return the number of bits written.
 */
int encode( int symbol, OutputBitStream obs ) throws IOException;
 
Example #14
Source File: BitStreamCODEC.java    From RankSys with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Write integer array to BitStream.
 *
 * @param obs output bit stream
 * @param in input array
 * @param offset offset of array
 * @param len number of bytes to write
 * @throws IOException when IO error
 */
protected abstract void write(OutputBitStream obs, int[] in, int offset, int len) throws IOException;
 
Example #15
Source File: CanonicalHuffmanRabaCoder.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Write out the coded values.
 * 
 * @param coder
 *            The coder.
 * @param raba
 *            The logical byte[][].
 * @param byte2symbol
 *            The mapping from byte values to symbol indices.
 * @param codedValueOffset
 *            An optional array dimensioned to <code>nvalues+1</code>, where
 *            <i>nvalues</i> is #of values in the logical byte[][]. When
 *            given, the array will be populated with the relative bit
 *            offset of the start of each coded value. The offsets are
 *            relative to the start of the first coded value.
 * 
 * @return The #of bits written (sum total of the bit lengths of the coded
 *         values).
 */
protected long writeCodedValues(final PrefixCoder coder,
        final IRaba raba, final Byte2Symbol byte2symbol,
        final long[] codedValueOffset, final OutputBitStream obs)
        throws IOException {

    final int nvalues = raba.size();

    if (codedValueOffset != null) {

        if (codedValueOffset.length != nvalues + 1)

            throw new IllegalArgumentException();

    }

    long bitsWritten = 0L;

    for (int i = 0; i < nvalues; i++) {

        if (codedValueOffset != null)
            codedValueOffset[i] = bitsWritten;

        final byte[] a = raba.get(i);

        if (a != null) {

            for (byte b : a) {

                final int symbol = byte2symbol.byte2symbol(b);

                if (symbol == -1)
                    throw new UnsupportedOperationException(
                            "Can not code value: " + b);

                bitsWritten += coder.encode(symbol, obs);

            }

        }

    }

    if (codedValueOffset != null)
        codedValueOffset[nvalues] = bitsWritten;

    if (log.isDebugEnabled())
        log
                .debug("codedValueOffset[]="
                        + Arrays.toString(codedValueOffset));

    return bitsWritten;

}
 
Example #16
Source File: CodeWordCoder.java    From database with GNU General Public License v2.0 votes vote down vote up
public int flush( final OutputBitStream unused ) { return 0; } 
Example #17
Source File: Coder.java    From database with GNU General Public License v2.0 votes vote down vote up
/** Flushes the coder.
 * 
 * <strong>Warning</strong>: this method will <em>not</em> {@link OutputBitStream#flush() flush} <code>obs</code>.
 *  
 * @param obs the output bit stream where the flushing bits will be written.
 * @return the number of bits written to flush the coder.
 */

int flush( OutputBitStream obs );
 
Example #18
Source File: ByteArrayBuffer.java    From database with GNU General Public License v2.0 votes vote down vote up
/**
 * Return a bit stream which will write on this buffer. The stream will
 * begin at the current {@link #pos()}.
 * 
 * @return The bit stream.
 * 
 * @todo Add OBS(byte[],off,len) ctor for faster operations. However, you
 *       MUST pre-extend the buffer to have sufficient capacity since an
 *       {@link OutputBitStream} wrapping a byte[] will not auto-extend.
 */
public OutputBitStream getOutputBitStream() {
    
    return new OutputBitStream(this, 0 /* unbuffered! */, false/* reflectionTest */);

}