@Override public int compare(byte[] left, byte[] right) { int l = Varint.readUnsignedVarInt(left); int r = Varint.readUnsignedVarInt(right); return l - r; } }
public static int readSignedVarInt(byte[] bytes) { int raw = readUnsignedVarInt(bytes); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readSignedVarLong(DataInput) */ public static int readSignedVarInt(DataInput in) throws IOException { int raw = readUnsignedVarInt(in); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * Constructor to support instances serialized with the legacy sparse * encoding scheme. * * @param p - the precision value for the normal set * @param sp - the precision value for the sparse set * @param deltaByteSet - a list of varint byte arrays encoded using a delta encoding scheme */ public HyperLogLogPlus(int p, int sp, List<byte[]> deltaByteSet) { this(p, sp); sparseSet = new int[deltaByteSet.size()]; int previousValue = 0; for (int i = 0; i < deltaByteSet.size(); i++) { int nextValue = Varint.readUnsignedVarInt(deltaByteSet.get(i)); sparseSet[i] = nextValue + previousValue; previousValue = sparseSet[i]; } }
private static HyperLogLogPlus decodeBytes(DataInput oi) throws IOException { int p = Varint.readUnsignedVarInt(oi); int sp = Varint.readUnsignedVarInt(oi); int formatType = Varint.readUnsignedVarInt(oi); if (formatType == 0) { int size = Varint.readUnsignedVarInt(oi); byte[] longArrayBytes = new byte[size]; oi.readFully(longArrayBytes); HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, new RegisterSet(1 << p, Bits.getBits(longArrayBytes))); hyperLogLogPlus.format = Format.NORMAL; return hyperLogLogPlus; } else { int[] rehydratedSparseSet = new int[Varint.readUnsignedVarInt(oi)]; int prevDeltaRead = 0; for (int i = 0; i < rehydratedSparseSet.length; i++) { int nextVal = Varint.readUnsignedVarInt(oi) + prevDeltaRead; rehydratedSparseSet[i] = nextVal; prevDeltaRead = nextVal; } HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, rehydratedSparseSet); hyperLogLogPlus.format = Format.SPARSE; return hyperLogLogPlus; } } }
@Override public int compare(byte[] left, byte[] right) { int l = Varint.readUnsignedVarInt(left); int r = Varint.readUnsignedVarInt(right); return l - r; } }
public static int readSignedVarInt(byte[] bytes) { int raw = readUnsignedVarInt(bytes); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readSignedVarLong(DataInput) */ public static int readSignedVarInt(DataInput in) throws IOException { int raw = readUnsignedVarInt(in); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
/** * Constructor to support instances serialized with the legacy sparse * encoding scheme. * * @param p - the precision value for the normal set * @param sp - the precision value for the sparse set * @param deltaByteSet - a list of varint byte arrays encoded using a delta encoding scheme */ public HyperLogLogPlus(int p, int sp, List<byte[]> deltaByteSet) { this(p, sp); sparseSet = new int[deltaByteSet.size()]; int previousValue = 0; for (int i = 0; i < deltaByteSet.size(); i++) { int nextValue = Varint.readUnsignedVarInt(deltaByteSet.get(i)); sparseSet[i] = nextValue + previousValue; previousValue = sparseSet[i]; } }
/** * Constructor to support instances serialized with the legacy sparse * encoding scheme. * * @param p - the precision value for the normal set * @param sp - the precision value for the sparse set * @param deltaByteSet - a list of varint byte arrays encoded using a delta encoding scheme */ public HyperLogLogPlus(int p, int sp, List<byte[]> deltaByteSet) { this(p, sp); sparseSet = new int[deltaByteSet.size()]; int previousValue = 0; for (int i = 0; i < deltaByteSet.size(); i++) { int nextValue = Varint.readUnsignedVarInt(deltaByteSet.get(i)); sparseSet[i] = nextValue + previousValue; previousValue = sparseSet[i]; } }
private static HyperLogLogPlus decodeBytes(DataInput oi) throws IOException { int p = Varint.readUnsignedVarInt(oi); int sp = Varint.readUnsignedVarInt(oi); int formatType = Varint.readUnsignedVarInt(oi); if (formatType == 0) { int size = Varint.readUnsignedVarInt(oi); byte[] longArrayBytes = new byte[size]; oi.readFully(longArrayBytes); HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, new RegisterSet(1 << p, com.clearspring.analytics.util.Bits.getBits(longArrayBytes))); hyperLogLogPlus.format = Format.NORMAL; return hyperLogLogPlus; } else { int[] rehydratedSparseSet = new int[Varint.readUnsignedVarInt(oi)]; int prevDeltaRead = 0; for (int i = 0; i < rehydratedSparseSet.length; i++) { int nextVal = Varint.readUnsignedVarInt(oi) + prevDeltaRead; rehydratedSparseSet[i] = nextVal; prevDeltaRead = nextVal; } HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, rehydratedSparseSet); hyperLogLogPlus.format = Format.SPARSE; return hyperLogLogPlus; } } }
private static HyperLogLogPlus decodeBytes(DataInput oi) throws IOException { int p = Varint.readUnsignedVarInt(oi); int sp = Varint.readUnsignedVarInt(oi); int formatType = Varint.readUnsignedVarInt(oi); if (formatType == 0) { int size = Varint.readUnsignedVarInt(oi); byte[] longArrayBytes = new byte[size]; oi.readFully(longArrayBytes); HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, new RegisterSet((int) Math.pow(2, p), Bits.getBits(longArrayBytes))); hyperLogLogPlus.format = Format.NORMAL; return hyperLogLogPlus; } else { int[] rehydratedSparseSet = new int[Varint.readUnsignedVarInt(oi)]; int prevDeltaRead = 0; for (int i = 0; i < rehydratedSparseSet.length; i++) { int nextVal = Varint.readUnsignedVarInt(oi) + prevDeltaRead; rehydratedSparseSet[i] = nextVal; prevDeltaRead = nextVal; } HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(p, sp, rehydratedSparseSet); hyperLogLogPlus.format = Format.SPARSE; return hyperLogLogPlus; } } }
@Override public void readFields(final DataInput input) throws IOException { final int rasterTileSize = Varint.readUnsignedVarInt(input); final byte[] rasterTileBinary = new byte[rasterTileSize]; input.readFully(rasterTileBinary); rasterTile = new RasterTile(); rasterTile.fromBinary(rasterTileBinary); minX = input.readDouble(); maxX = input.readDouble(); minY = input.readDouble(); maxY = input.readDouble(); int crsStrSize = Varint.readUnsignedVarInt(input); if (crsStrSize > 0) { byte[] crsStrBytes = new byte[crsStrSize]; input.readFully(crsStrBytes); String crsStr = StringUtils.stringFromBinary(crsStrBytes); try { crs = CRS.decode(crsStr); } catch (FactoryException e) { LOGGER.error("Unable to decode " + crsStr + " CRS", e); throw new RuntimeException("Unable to decode " + crsStr + " CRS", e); } } else { crs = GeometryUtils.getDefaultCRS(); } }
private GeometryCollection readGeometryCollection(DataInput input, byte metadata) throws IOException { if ((metadata & TWKBUtils.EMPTY_GEOMETRY) != 0) { return GeometryUtils.GEOMETRY_FACTORY.createGeometryCollection(); } int numGeometries = Varint.readUnsignedVarInt(input); Geometry[] geometries = new Geometry[numGeometries]; for (int i = 0; i < numGeometries; i++) { geometries[i] = read(input); } return GeometryUtils.GEOMETRY_FACTORY.createGeometryCollection(geometries); }
public Coordinate[] readPointArray(DataInput input) throws IOException { int numCoordinates = Varint.readUnsignedVarInt(input); Coordinate[] coordinates = new Coordinate[numCoordinates]; long lastX = 0; long lastY = 0; for (int i = 0; i < numCoordinates; i++) { lastX = Varint.readSignedVarLong(input) + lastX; lastY = Varint.readSignedVarLong(input) + lastY; coordinates[i] = new Coordinate( ((double) lastX) / precisionMultiplier, ((double) lastY) / precisionMultiplier); } return coordinates; } }
private MultiLineString readMultiLineString( PrecisionReader precision, byte metadata, DataInput input) throws IOException { if ((metadata & TWKBUtils.EMPTY_GEOMETRY) != 0) { return GeometryUtils.GEOMETRY_FACTORY.createMultiLineString(); } int numLines = Varint.readUnsignedVarInt(input); LineString[] lines = new LineString[numLines]; for (int i = 0; i < numLines; i++) { lines[i] = GeometryUtils.GEOMETRY_FACTORY.createLineString(precision.readPointArray(input)); } return GeometryUtils.GEOMETRY_FACTORY.createMultiLineString(lines); }
private MultiPolygon readMultiPolygon(PrecisionReader precision, byte metadata, DataInput input) throws IOException { if ((metadata & TWKBUtils.EMPTY_GEOMETRY) != 0) { return GeometryUtils.GEOMETRY_FACTORY.createMultiPolygon(); } int numPolygons = Varint.readUnsignedVarInt(input); Polygon[] polygons = new Polygon[numPolygons]; int numRings; for (int i = 0; i < numPolygons; i++) { numRings = Varint.readUnsignedVarInt(input); if (numRings == 0) { polygons[i] = GeometryUtils.GEOMETRY_FACTORY.createPolygon(); continue; } LinearRing exteriorRing = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(precision.readPointArray(input)); LinearRing[] interiorRings = new LinearRing[numRings - 1]; for (int j = 0; j < numRings - 1; j++) { interiorRings[j] = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(precision.readPointArray(input)); } polygons[i] = GeometryUtils.GEOMETRY_FACTORY.createPolygon(exteriorRing, interiorRings); } return GeometryUtils.GEOMETRY_FACTORY.createMultiPolygon(polygons); }
private Polygon readPolygon(PrecisionReader precision, byte metadata, DataInput input) throws IOException { if ((metadata & TWKBUtils.EMPTY_GEOMETRY) != 0) { return GeometryUtils.GEOMETRY_FACTORY.createPolygon(); } int numRings = Varint.readUnsignedVarInt(input); LinearRing exteriorRing = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(precision.readPointArray(input)); LinearRing[] interiorRings = new LinearRing[numRings - 1]; for (int i = 0; i < numRings - 1; i++) { interiorRings[i] = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(precision.readPointArray(input)); } return GeometryUtils.GEOMETRY_FACTORY.createPolygon(exteriorRing, interiorRings); }
visibility = new byte[0]; final byte[] value = new byte[Varint.readUnsignedVarInt(in)]; final int numDuplicates = in.readUnsignedByte(); if ((dataId.length > 0) && (in.read(dataId) != dataId.length)) {
@Override public Coordinate[] readPointArray(DataInput input) throws IOException { int numCoordinates = Varint.readUnsignedVarInt(input); Coordinate[] coordinates = new Coordinate[numCoordinates]; long lastX = 0; long lastY = 0; long lastZ = 0; long lastM = 0; for (int i = 0; i < numCoordinates; i++) { lastX = Varint.readSignedVarLong(input) + lastX; lastY = Varint.readSignedVarLong(input) + lastY; coordinates[i] = new Coordinate( ((double) lastX) / precisionMultiplier, ((double) lastY) / precisionMultiplier); if (hasZ) { lastZ = Varint.readSignedVarLong(input) + lastZ; coordinates[i].setZ(((double) lastZ) / zPrecisionMultiplier); } if (hasM) { lastM = Varint.readSignedVarLong(input) + lastM; coordinates[i].setM(((double) lastM) / mPrecisionMultiplier); } } return coordinates; } }