当前位置: 首页>>代码示例>>Java>>正文


Java BitWriter类代码示例

本文整理汇总了Java中com.tomgibara.bits.BitWriter的典型用法代码示例。如果您正苦于以下问题:Java BitWriter类的具体用法?Java BitWriter怎么用?Java BitWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


BitWriter类属于com.tomgibara.bits包,在下文中一共展示了BitWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: encodeFloat

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
/**
 * Writes a float. NaN and infinite values are not supported.
 *
 * @param writer
 *            the writer that will store the encoded value
 * @param value
 *            a float, not NaN or +/- infinity
 * @return the number of bits written
 * @throws BitStreamException
 *             if there was a problem writing bits to the stream
 * @throws IllegalArgumentException
 *             if the supplied value is infinite or NaN.
 */

public int encodeFloat(BitWriter writer, float value) {
	if (Float.isNaN(value) || Float.isInfinite(value)) throw new IllegalArgumentException();
	int bits = Float.floatToIntBits(value);
	int sign = bits & 0x80000000;
	if (sign == bits) return coding.unsafeEncodePositiveInt(writer, sign == 0 ? 0 : 1);

	int mantissa = bits & 0x007fffff;
	if (sign == 0) {
		mantissa = (mantissa << 1) + 2;
	} else {
		mantissa = (mantissa << 1) + 3;
	}
	int exponent = ((bits & 0x7f800000) >> 23) - 127;
	return coding.unsafeEncodePositiveInt(writer, mantissa) + encodeInt(writer, exponent);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:30,代码来源:ExtendedCoding.java

示例2: encodeDouble

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
/**
 * Writes a double. NaN and infinite values are not supported.
 *
 * @param writer
 *            the writer that will store the encoded value
 * @param value
 *            a double, not NaN or +/- infinity
 * @return the number of bits written
 * @throws BitStreamException
 *             if there was a problem writing bits to the stream
 * @throws IllegalArgumentException
 *             if the supplied value is infinite or NaN.
 */

public int encodeDouble(BitWriter writer, double value) {
	if (Double.isNaN(value) || Double.isInfinite(value)) throw new IllegalArgumentException();
	long bits = Double.doubleToLongBits(value);
	long sign = bits & 0x8000000000000000L;
	if (sign == bits) return coding.unsafeEncodePositiveInt(writer, sign == 0L ? 0 : 1);

	long mantissa = bits & 0x000fffffffffffffL;
	if (sign == 0) {
		mantissa = (mantissa << 1) + 2L;
	} else {
		mantissa = (mantissa << 1) + 3L;
	}
	int exponent = (int) ((bits & 0x7ff0000000000000L) >> 52) - 1023;
	return coding.unsafeEncodePositiveLong(writer, mantissa) + encodeInt(writer, exponent);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:30,代码来源:ExtendedCoding.java

示例3: testDictionary

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testDictionary() {
	HuffmanCoding coding = new HuffmanCoding(new UnorderedFrequencies(9L, 16L, 25L, 36L));
	int[] sequence = {0,1,2,3,2,1,0,3,2,1,0};
	byte[] bytes = new byte[11];
	BitWriter writer = Bits.writerTo(bytes);
	for (int i = 0; i < sequence.length; i++) {
		coding.encodePositiveInt(writer, sequence[i]);
	}
	writer.flush();
	Dictionary dictionary = coding.getDictionary();
	coding = new HuffmanCoding(dictionary);
	BitReader reader = Bits.readerFrom(bytes);
	for (int i = 0; i < sequence.length; i++) {
		assertEquals("mismatch at index " + i, sequence[i], coding.decodePositiveInt(reader));
	}
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:17,代码来源:HuffmanCodingTest.java

示例4: testDecode

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
private static BitReader testDecode(HuffmanCoding.Frequencies frequencies) {
	HuffmanCoding huffman = new HuffmanCoding(frequencies);
	int[] memory = new int[1000];
	int count = frequencies.getCorrespondence().getCount();
	BitWriter w = Bits.writerTo(memory);
	for (int i = 0; i < count; i++) {
		huffman.encodePositiveInt(w, i);
	}
	w.flush();

	BitReader r = Bits.readerFrom(memory);
	for (int i = 0; i < count; i++) {
		assertEquals(i, huffman.decodePositiveInt(r));
	}

	r.setPosition(0);
	return r;
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:19,代码来源:HuffmanCodingTest.java

示例5: testInt

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testInt() {
	int[] ints = new int[1000];
	BitWriter writer = Bits.writerTo(ints);
	BitReader reader = Bits.readerFrom(ints);
	for (int i = -10000; i < 10000; i++) {
		checkInt(writer, reader, i);
	}

	// tests that fit into int manipulation
	checkInt(writer, reader, 1-(1 << 30));
	checkInt(writer, reader, -(1 << 30));
	checkInt(writer, reader, 1 - (1 << 30));
	checkInt(writer, reader, (1 << 30));
	checkInt(writer, reader, 1 + (1 << 30));

	//tests that exceed int manipulation
	checkInt(writer, reader, -(1 << 30) - 1);

	Random r = new Random(0L);
	for (int i = 0; i < 1000000; i++) {
		checkInt(writer, reader, r.nextInt());
	}

}
 
开发者ID:tomgibara,项目名称:coding,代码行数:25,代码来源:ExtendedCodingTest.java

示例6: testBigInt

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testBigInt() {
	int bits = 4096;
	int[] ints = new int[bits / 32];
	BitWriter writer = Bits.writerTo(ints);
	BitReader reader = Bits.readerFrom(ints);

	for (long i = 0; i < 100L; i++) {
		checkPositiveBigInt(writer, reader, BigInteger.valueOf(i));
	}

	for (long i = 0; i < 10000000000L; i+=1000000L) {
  			checkPositiveBigInt(writer, reader, BigInteger.valueOf(i));
	}

	Random r = new Random(0L);
	for (int i = 0; i < 10000; i++) {
		BigInteger value = new BigInteger(r.nextInt(bits/4), r);
		checkBigInt(writer, reader, value);
	}

}
 
开发者ID:tomgibara,项目名称:coding,代码行数:22,代码来源:ExtendedCodingTest.java

示例7: testFloat

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testFloat() {
	for (C coding : getCodings()) {
		if (isEncodableValueLimited(coding)) return;
		int bytes = 16;
		int[] memory = new int[bytes];
		BitWriter writer = Bits.writerTo(memory, bytes * 8);
		BitReader reader = Bits.readerFrom(memory, bytes * 8);
		checkFloat(writer, reader, 0.0f);
		checkFloat(writer, reader, -0.0f);
		checkFloat(writer, reader, 1.0f);
		checkFloat(writer, reader, 2.0f);
		checkFloat(writer, reader, 3.0f);
		checkFloat(writer, reader, 4.0f);

		for (float d = -100.0f; d < 100.0f; d += 0.1f) {
			checkFloat(writer, reader, d);
		}

		Random r = new Random(0L);
		for (int i = 0; i < 10000; i++) {
			float f = Float.intBitsToFloat(r.nextInt());
			if (Float.isNaN(f) || Float.isInfinite(f)) continue;
			checkFloat(writer, reader, f);
		}
	}
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:27,代码来源:ExtendedCodingTest.java

示例8: testEqualsGolomb

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testEqualsGolomb() {
	Random r = new Random();
	for (int i = 0; i < 10000; i++) {
		int bits = r.nextInt(8);
		int value = r.nextInt(10 << bits);
		RiceCoding rc = new RiceCoding(bits);
		GolombCoding gc = new GolombCoding(1 << bits);
		BitWriter rw = Bits.writerToNothing();
		rc.encodePositiveInt(rw, value);
		BitWriter gw = Bits.writerToNothing();
		gc.encodePositiveInt(gw, value);
		assertEquals(rw.getPosition(), gw.getPosition());
		BitVector v = new BitVector((int) rw.getPosition());
		rc.encodePositiveInt(v.openWriter(), value);
		rc.encodePositiveInt(v.xor().openWriter(0, v.size()), value);
		assertTrue(v.zeros().isAll());
	}
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:19,代码来源:RiceCodingTest.java

示例9: testCorrectness

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testCorrectness() {
		for (ExtendedCoding coding : getCodings()) {
			int[] memory = new int[1];
//			IntArrayBitWriter writer = new IntArrayBitWriter(memory, 32);
//			IntArrayBitReader reader = new IntArrayBitReader(memory, 32);
			BitWriter writer = Bits.writerTo(memory, 32);
			BitReader reader = Bits.readerFrom(memory, 32);
			for (int i = 1; i <= 10; i++) {
				writer.setPosition(0);
				coding.encodePositiveInt(writer, i);
				writer.flush();
				reader.setPosition(0);
				int j = coding.decodePositiveInt(reader);
				assertEquals(i, j);
			}
		}
	}
 
开发者ID:tomgibara,项目名称:coding,代码行数:18,代码来源:EliasDeltaCodingTest.java

示例10: testPass

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
private void testPass(int size, long seed) {
	BitWriter writer = newBitWriter(size * 32);
	ArrayList<Point> list = new ArrayList<Point>(size);

	Random r = new Random(seed);
	for (int i = 0; i < size; i++) {
		int x = r.nextInt(33);
		int y = r.nextInt() & ((1 << x) - 1);
		writer.write(y, x);
		list.add( new Point(x, y) );
	}
	long pos = writer.getPosition();
	writer.flush();
	assertEquals(0, writer.padToBoundary(getBoundary()));

	BitReader reader = bitReaderFor(writer);
	for (int i = 0; i < size; i++) {
		Point pt = list.get(i);
		int v = reader.read(pt.x);
		if (pt.y != v) throw new RuntimeException("Failed at " + i + ": " + v + " is not " + pt.y);
	}
	if (reader.getPosition() != pos) throw new RuntimeException();
}
 
开发者ID:tomgibara,项目名称:bits,代码行数:24,代码来源:AbstractBitWriterTest.java

示例11: testLength

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
public void testLength() {
	Random random = new Random(0);
	for (int i = 0; i < 100; i++) {
		int length = random.nextInt(1000);
		BitWriter writer = newBitWriter(length);
		for (int j = 0; j < length; j++) {
			writer.writeBoolean(true);
		}
		writer.flush();
		BitReader reader = bitReaderFor(writer);
		for (int j = 0; j < length; j++) {
			assertTrue(reader.readBoolean());
		}
		try {
			reader.read(getBoundary().mask + 1);
			fail();
		} catch (EndOfBitStreamException e) {
			/* expected */
		}
	}
}
 
开发者ID:tomgibara,项目名称:bits,代码行数:22,代码来源:AbstractBitWriterTest.java

示例12: unsafeEncodePositiveInt

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
@Override
int unsafeEncodePositiveInt(BitWriter writer, int value) {
	int q = value / divisor;
	int r = value - q * divisor;
	return
	UnaryCoding.zeroTerminated.unsafeEncodePositiveInt(writer, q)
	+ truncated.encodeInt(writer, r);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:9,代码来源:GolombCoding.java

示例13: unsafeEncodePositiveLong

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
@Override
int unsafeEncodePositiveLong(BitWriter writer, long value) {
	long q = value / divisor;
	long r = value - q * divisor;
	return
	UnaryCoding.zeroTerminated.unsafeEncodePositiveLong(writer, q)
	+ truncated.encodeLong(writer, r);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:9,代码来源:GolombCoding.java

示例14: unsafeEncodePositiveBigInt

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
@Override
int unsafeEncodePositiveBigInt(BitWriter writer, BigInteger value) {
	BigInteger[] qr = value.divideAndRemainder(bigDivisor);
	return
	UnaryCoding.zeroTerminated.unsafeEncodePositiveBigInt(writer, qr[0])
	+ truncated.encodeBigInt(writer, qr[1]);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:8,代码来源:GolombCoding.java

示例15: encodePositiveInt

import com.tomgibara.bits.BitWriter; //导入依赖的package包/类
@Override
public int encodePositiveInt(BitWriter writer, int value) {
	if (writer == null) throw new IllegalArgumentException("null writer");
	if (value < 0) throw new IllegalArgumentException("negative value");
	if (intSize != 0) {
		if (value >= intSize) throw new IllegalArgumentException("invalid value");
		return encodeInt(writer, value);
	}
	return longSize == 0L ? encodeBigInt(writer, BigInteger.valueOf(value)) : encodeLong(writer, value);
}
 
开发者ID:tomgibara,项目名称:coding,代码行数:11,代码来源:TruncatedBinaryCoding.java


注:本文中的com.tomgibara.bits.BitWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。