src.it.unimi.dsi.test.GeneratePrecomputedOutputCodes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsiutils Show documentation
Show all versions of dsiutils Show documentation
The DSI utilities are a mishmash of classes accumulated during the last twenty years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.
package it.unimi.dsi.test;
import it.unimi.dsi.bits.Fast;
import it.unimi.dsi.fastutil.io.BinIO;
import it.unimi.dsi.io.InputBitStream;
import it.unimi.dsi.io.OutputBitStream;
import java.io.IOException;
import java.util.Arrays;
public final class GeneratePrecomputedOutputCodes {
private GeneratePrecomputedOutputCodes() {}
public static int writeUnary(int x, OutputBitStream obs) throws IOException {
for(int i = 0; i < x; i++) obs.writeBit(0);
obs.writeBit(1);
return x + 1;
}
public static int writeGamma(int x, OutputBitStream obs) throws IOException {
final int msb = Fast.mostSignificantBit(++x);
final int l = writeUnary(msb, obs);
return l + (msb != 0 ? obs.writeInt(x, msb) : 0);
}
public static int writeDelta(int x, OutputBitStream obs) throws IOException {
final int msb = Fast.mostSignificantBit(++x);
final int l = writeGamma(msb, obs);
return l + (msb != 0 ? obs.writeInt(x, msb) : 0);
}
public static int writeZeta(int x, final int k, OutputBitStream obs) throws IOException {
final int msb = Fast.mostSignificantBit(++x);
final int h = msb / k;
final int l = writeUnary(h, obs);
final int left = 1 << h * k;
return l + (x - left < left
? obs.writeInt(x - left, h * k + k - 1)
: obs.writeInt(x, h * k + k));
}
public static int writeShiftedGamma(int x, OutputBitStream obs) throws IOException {
final int msb = Fast.mostSignificantBit(x);
final int l = writeUnary(msb + 1, obs);
return l + (msb != -1 ? obs.writeInt(x, msb) : 0);
}
public static void main(final String[] arg) throws IOException {
final int length = Integer.parseInt(arg[0]);
final int size = 1 << length;
final byte[] a = new byte[4];
@SuppressWarnings("resource")
final InputBitStream inputBitStream = new InputBitStream(a);
final OutputBitStream outputBitStream= new OutputBitStream(a);
int l, v;
int[] precomp = new int[size];
for(int i = 0; i < size; i++) {
Arrays.fill(a, (byte)0);
outputBitStream.flush();
outputBitStream.position(0);
l = writeGamma(i, outputBitStream);
outputBitStream.flush();
inputBitStream.flush();
inputBitStream.position(0);
v = inputBitStream.readInt(l);
if (l > 26) throw new IllegalArgumentException();
if (Fast.mostSignificantBit(l) > 5) throw new IllegalArgumentException();
precomp[i] = l << 26 | v;
}
BinIO.storeInts(precomp, "gamma.out." + length);
for(int i = 0; i < size; i++) {
Arrays.fill(a, (byte)0);
outputBitStream.flush();
outputBitStream.position(0);
l = writeDelta(i, outputBitStream);
outputBitStream.flush();
inputBitStream.flush();
inputBitStream.position(0);
v = inputBitStream.readInt(l);
if (l > 26) throw new IllegalArgumentException();
if (Fast.mostSignificantBit(l) > 5) throw new IllegalArgumentException();
precomp[i] = l << 26 | v;
}
BinIO.storeInts(precomp, "delta.out." + length);
for(int i = 0; i < size; i++) {
Arrays.fill(a, (byte)0);
outputBitStream.flush();
outputBitStream.position(0);
l = writeZeta(i, 3, outputBitStream);
outputBitStream.flush();
inputBitStream.flush();
inputBitStream.position(0);
v = inputBitStream.readInt(l);
if (l > 26) throw new IllegalArgumentException();
if (Fast.mostSignificantBit(l) > 5) throw new IllegalArgumentException();
precomp[i] = l << 26 | v;
}
BinIO.storeInts(precomp, "zeta3.out." + length);
for(int i = 0; i < size; i++) {
Arrays.fill(a, (byte)0);
outputBitStream.flush();
outputBitStream.position(0);
l = writeShiftedGamma(i, outputBitStream);
outputBitStream.flush();
inputBitStream.flush();
inputBitStream.position(0);
v = inputBitStream.readInt(l);
if (l > 26) throw new IllegalArgumentException();
if (Fast.mostSignificantBit(l) > 5) throw new IllegalArgumentException();
precomp[i] = l << 26 | v;
}
BinIO.storeInts(precomp, "shiftedgamma.out." + length);
}
}