0N/A/*
3261N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.java.util.jar.pack;
0N/A
3063N/Aimport java.io.IOException;
3063N/Aimport java.io.InputStream;
3063N/Aimport java.io.OutputStream;
3063N/Aimport java.util.HashMap;
3315N/Aimport java.util.Map;
3315N/Aimport static com.sun.java.util.jar.pack.Constants.*;
0N/A/**
0N/A * Define the conversions between sequences of small integers and raw bytes.
0N/A * This is a schema of encodings which incorporates varying lengths,
0N/A * varying degrees of length variability, and varying amounts of signed-ness.
0N/A * @author John Rose
0N/A */
3315N/Aclass Coding implements Comparable, CodingMethod, Histogram.BitMetric {
0N/A /*
0N/A Coding schema for single integers, parameterized by (B,H,S):
0N/A
0N/A Let B in [1,5], H in [1,256], S in [0,3].
0N/A (S limit is arbitrary. B follows the 32-bit limit. H is byte size.)
0N/A
0N/A A given (B,H,S) code varies in length from 1 to B bytes.
0N/A
0N/A The 256 values a byte may take on are divided into L=(256-H) and H
0N/A values, with all the H values larger than the L values.
0N/A (That is, the L values are [0,L) and the H are [L,256).)
0N/A
0N/A The last byte is always either the B-th byte, a byte with "L value"
0N/A (<L), or both. There is no other byte that satisfies these conditions.
0N/A All bytes before the last always have "H values" (>=L).
0N/A
0N/A Therefore, if L==0, the code always has the full length of B bytes.
0N/A The coding then becomes a classic B-byte little-endian unsigned integer.
0N/A (Also, if L==128, the high bit of each byte acts signals the presence
0N/A of a following byte, up to the maximum length.)
0N/A
0N/A In the unsigned case (S==0), the coding is compact and monotonic
0N/A in the ordering of byte sequences defined by appending zero bytes
0N/A to pad them to a common length B, reversing them, and ordering them
0N/A lexicographically. (This agrees with "little-endian" byte order.)
0N/A
0N/A Therefore, the unsigned value of a byte sequence may be defined as:
0N/A <pre>
0N/A U(b0) == b0
0N/A in [0..L)
0N/A or [0..256) if B==1 (**)
0N/A
0N/A U(b0,b1) == b0 + b1*H
0N/A in [L..L*(1+H))
0N/A or [L..L*(1+H) + H^2) if B==2
0N/A
0N/A U(b0,b1,b2) == b0 + b1*H + b2*H^2
0N/A in [L*(1+H)..L*(1+H+H^2))
0N/A or [L*(1+H)..L*(1+H+H^2) + H^3) if B==3
0N/A
0N/A U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
0N/A up to L*Sum[i<n]( H^i )
0N/A or to L*Sum[i<n]( H^i ) + H^n if n==B
0N/A </pre>
0N/A
0N/A (**) If B==1, the values H,L play no role in the coding.
0N/A As a convention, we require that any (1,H,S) code must always
0N/A encode values less than H. Thus, a simple unsigned byte is coded
0N/A specifically by the code (1,256,0).
0N/A
0N/A (Properly speaking, the unsigned case should be parameterized as
0N/A S==Infinity. If the schema were regular, the case S==0 would really
0N/A denote a numbering in which all coded values are negative.)
0N/A
0N/A If S>0, the unsigned value of a byte sequence is regarded as a binary
0N/A integer. If any of the S low-order bits are zero, the corresponding
0N/A signed value will be non-negative. If all of the S low-order bits
0N/A (S>0) are one, the the corresponding signed value will be negative.
0N/A
0N/A The non-negative signed values are compact and monotonically increasing
0N/A (from 0) in the ordering of the corresponding unsigned values.
0N/A
0N/A The negative signed values are compact and monotonically decreasing
0N/A (from -1) in the ordering of the corresponding unsigned values.
0N/A
0N/A In essence, the low-order S bits function as a collective sign bit
0N/A for negative signed numbers, and as a low-order base-(2^S-1) digit
0N/A for non-negative signed numbers.
0N/A
0N/A Therefore, the signed value corresponding to an unsigned value is:
0N/A <pre>
0N/A Sgn(x) == x if S==0
0N/A Sgn(x) == (x / 2^S)*(2^S-1) + (x % 2^S), if S>0, (x % 2^S) < 2^S-1
0N/A Sgn(x) == -(x / 2^S)-1, if S>0, (x % 2^S) == 2^S-1
0N/A </pre>
0N/A
0N/A Finally, the value of a byte sequence, given the coding parameters
0N/A (B,H,S), is defined as:
0N/A <pre>
0N/A V(b[i]: i<n) == Sgn(U(b[i]: i<n))
0N/A </pre>
0N/A
0N/A The extremal positive and negative signed value for a given range
0N/A of unsigned values may be found by sign-encoding the largest unsigned
0N/A value which is not 2^S-1 mod 2^S, and that which is, respectively.
0N/A
0N/A Because B,H,S are variable, this is not a single coding but a schema
0N/A of codings. For optimal compression, it is necessary to adaptively
0N/A select specific codings to the data being compressed.
0N/A
0N/A For example, if a sequence of values happens never to be negative,
0N/A S==0 is the best choice. If the values are equally balanced between
0N/A negative and positive, S==1. If negative values are rare, then S>1
0N/A is more appropriate.
0N/A
0N/A A (B,H,S) encoding is called a "subrange" if it does not encode
0N/A the largest 32-bit value, and if the number R of values it does
0N/A encode can be expressed as a positive 32-bit value. (Note that
0N/A B=1 implies R<=256, B=2 implies R<=65536, etc.)
0N/A
0N/A A delta version of a given (B,H,S) coding encodes an array of integers
0N/A by writing their successive differences in the (B,H,S) coding.
0N/A The original integers themselves may be recovered by making a
0N/A running accumulation of sum of the differences as they are read.
0N/A
0N/A As a special case, if a (B,H,S) encoding is a subrange, its delta
0N/A version will only encode arrays of numbers in the coding's unsigned
0N/A range, [0..R-1]. The coding of deltas is still in the normal signed
0N/A range, if S!=0. During delta encoding, all subtraction results are
0N/A reduced to the signed range, by adding multiples of R. Likewise,
0N/A. during encoding, all addition results are reduced to the unsigned range.
0N/A This special case for subranges allows the benefits of wraparound
0N/A when encoding correlated sequences of very small positive numbers.
0N/A */
0N/A
0N/A // Code-specific limits:
0N/A private static int saturate32(long x) {
0N/A if (x > Integer.MAX_VALUE) return Integer.MAX_VALUE;
0N/A if (x < Integer.MIN_VALUE) return Integer.MIN_VALUE;
0N/A return (int)x;
0N/A }
0N/A private static long codeRangeLong(int B, int H) {
0N/A return codeRangeLong(B, H, B);
0N/A }
0N/A private static long codeRangeLong(int B, int H, int nMax) {
0N/A // Code range for a all (B,H) codes of length <=nMax (<=B).
0N/A // n < B: L*Sum[i<n]( H^i )
0N/A // n == B: L*Sum[i<B]( H^i ) + H^B
0N/A assert(nMax >= 0 && nMax <= B);
0N/A assert(B >= 1 && B <= 5);
0N/A assert(H >= 1 && H <= 256);
0N/A if (nMax == 0) return 0; // no codes of zero length
0N/A if (B == 1) return H; // special case; see (**) above
0N/A int L = 256-H;
0N/A long sum = 0;
0N/A long H_i = 1;
0N/A for (int n = 1; n <= nMax; n++) {
0N/A sum += H_i;
0N/A H_i *= H;
0N/A }
0N/A sum *= L;
0N/A if (nMax == B)
0N/A sum += H_i;
0N/A return sum;
0N/A }
0N/A /** Largest int representable by (B,H,S) in up to nMax bytes. */
0N/A public static int codeMax(int B, int H, int S, int nMax) {
0N/A //assert(S >= 0 && S <= S_MAX);
0N/A long range = codeRangeLong(B, H, nMax);
0N/A if (range == 0)
0N/A return -1; // degenerate max value for empty set of codes
0N/A if (S == 0 || range >= (long)1<<32)
0N/A return saturate32(range-1);
0N/A long maxPos = range-1;
3315N/A while (isNegativeCode(maxPos, S)) {
3315N/A --maxPos;
3315N/A }
0N/A if (maxPos < 0) return -1; // No positive codings at all.
0N/A int smax = decodeSign32(maxPos, S);
0N/A // check for 32-bit wraparound:
0N/A if (smax < 0)
0N/A return Integer.MAX_VALUE;
0N/A return smax;
0N/A }
0N/A /** Smallest int representable by (B,H,S) in up to nMax bytes.
0N/A Returns Integer.MIN_VALUE if 32-bit wraparound covers
0N/A the entire negative range.
0N/A */
0N/A public static int codeMin(int B, int H, int S, int nMax) {
0N/A //assert(S >= 0 && S <= S_MAX);
0N/A long range = codeRangeLong(B, H, nMax);
0N/A if (range >= (long)1<<32 && nMax == B) {
0N/A // Can code negative values via 32-bit wraparound.
0N/A return Integer.MIN_VALUE;
0N/A }
0N/A if (S == 0) {
0N/A return 0;
0N/A }
0N/A long maxNeg = range-1;
3315N/A while (!isNegativeCode(maxNeg, S))
3315N/A --maxNeg;
3315N/A
0N/A if (maxNeg < 0) return 0; // No negative codings at all.
0N/A return decodeSign32(maxNeg, S);
0N/A }
0N/A
0N/A // Some of the arithmetic below is on unsigned 32-bit integers.
0N/A // These must be represented in Java as longs in the range [0..2^32-1].
0N/A // The conversion to a signed int is just the Java cast (int), but
0N/A // the conversion to an unsigned int is the following little method:
0N/A private static long toUnsigned32(int sx) {
0N/A return ((long)sx << 32) >>> 32;
0N/A }
0N/A
0N/A // Sign encoding:
0N/A private static boolean isNegativeCode(long ux, int S) {
0N/A assert(S > 0);
0N/A assert(ux >= -1); // can be out of 32-bit range; who cares
0N/A int Smask = (1<<S)-1;
0N/A return (((int)ux+1) & Smask) == 0;
0N/A }
0N/A private static boolean hasNegativeCode(int sx, int S) {
0N/A assert(S > 0);
0N/A // If S>=2 very low negatives are coded by 32-bit-wrapped positives.
0N/A // The lowest negative representable by a negative coding is
0N/A // ~(umax32 >> S), and the next lower number is coded by wrapping
0N/A // the highest positive:
0N/A // CodePos(umax32-1) -> (umax32-1)-((umax32-1)>>S)
0N/A // which simplifies to ~(umax32 >> S)-1.
0N/A return (0 > sx) && (sx >= ~(-1>>>S));
0N/A }
0N/A private static int decodeSign32(long ux, int S) {
0N/A assert(ux == toUnsigned32((int)ux)) // must be unsigned 32-bit number
0N/A : (Long.toHexString(ux));
0N/A if (S == 0) {
0N/A return (int) ux; // cast to signed int
0N/A }
0N/A int sx;
0N/A if (isNegativeCode(ux, S)) {
0N/A // Sgn(x) == -(x / 2^S)-1
0N/A sx = ~((int)ux >>> S);
0N/A } else {
0N/A // Sgn(x) == (x / 2^S)*(2^S-1) + (x % 2^S)
0N/A sx = (int)ux - ((int)ux >>> S);
0N/A }
0N/A // Assert special case of S==1:
0N/A assert(!(S == 1) || sx == (((int)ux >>> 1) ^ -((int)ux & 1)));
0N/A return sx;
0N/A }
0N/A private static long encodeSign32(int sx, int S) {
0N/A if (S == 0) {
0N/A return toUnsigned32(sx); // unsigned 32-bit int
0N/A }
0N/A int Smask = (1<<S)-1;
0N/A long ux;
0N/A if (!hasNegativeCode(sx, S)) {
0N/A // InvSgn(sx) = (sx / (2^S-1))*2^S + (sx % (2^S-1))
0N/A ux = sx + (toUnsigned32(sx) / Smask);
0N/A } else {
0N/A // InvSgn(sx) = (-sx-1)*2^S + (2^S-1)
0N/A ux = (-sx << S) - 1;
0N/A }
0N/A ux = toUnsigned32((int)ux);
0N/A assert(sx == decodeSign32(ux, S))
0N/A : (Long.toHexString(ux)+" -> "+
0N/A Integer.toHexString(sx)+" != "+
0N/A Integer.toHexString(decodeSign32(ux, S)));
0N/A return ux;
0N/A }
0N/A
0N/A // Top-level coding of single integers:
0N/A public static void writeInt(byte[] out, int[] outpos, int sx, int B, int H, int S) {
0N/A long ux = encodeSign32(sx, S);
0N/A assert(ux == toUnsigned32((int)ux));
0N/A assert(ux < codeRangeLong(B, H))
0N/A : Long.toHexString(ux);
0N/A int L = 256-H;
0N/A long sum = ux;
0N/A int pos = outpos[0];
0N/A for (int i = 0; i < B-1; i++) {
0N/A if (sum < L)
0N/A break;
0N/A sum -= L;
0N/A int b_i = (int)( L + (sum % H) );
0N/A sum /= H;
0N/A out[pos++] = (byte)b_i;
0N/A }
0N/A out[pos++] = (byte)sum;
0N/A // Report number of bytes written by updating outpos[0]:
0N/A outpos[0] = pos;
0N/A // Check right away for mis-coding.
0N/A //assert(sx == readInt(out, new int[1], B, H, S));
0N/A }
0N/A public static int readInt(byte[] in, int[] inpos, int B, int H, int S) {
0N/A // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
0N/A int L = 256-H;
0N/A long sum = 0;
0N/A long H_i = 1;
0N/A int pos = inpos[0];
0N/A for (int i = 0; i < B; i++) {
0N/A int b_i = in[pos++] & 0xFF;
0N/A sum += b_i*H_i;
0N/A H_i *= H;
0N/A if (b_i < L) break;
0N/A }
0N/A //assert(sum >= 0 && sum < codeRangeLong(B, H));
0N/A // Report number of bytes read by updating inpos[0]:
0N/A inpos[0] = pos;
0N/A return decodeSign32(sum, S);
0N/A }
0N/A // The Stream version doesn't fetch a byte unless it is needed for coding.
0N/A public static int readIntFrom(InputStream in, int B, int H, int S) throws IOException {
0N/A // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
0N/A int L = 256-H;
0N/A long sum = 0;
0N/A long H_i = 1;
0N/A for (int i = 0; i < B; i++) {
0N/A int b_i = in.read();
0N/A if (b_i < 0) throw new RuntimeException("unexpected EOF");
0N/A sum += b_i*H_i;
0N/A H_i *= H;
0N/A if (b_i < L) break;
0N/A }
0N/A assert(sum >= 0 && sum < codeRangeLong(B, H));
0N/A return decodeSign32(sum, S);
0N/A }
0N/A
0N/A public static final int B_MAX = 5; /* B: [1,5] */
0N/A public static final int H_MAX = 256; /* H: [1,256] */
0N/A public static final int S_MAX = 2; /* S: [0,2] */
0N/A
0N/A // END OF STATICS.
0N/A
0N/A private final int B; /*1..5*/ // # bytes (1..5)
0N/A private final int H; /*1..256*/ // # codes requiring a higher byte
0N/A private final int L; /*0..255*/ // # codes requiring a higher byte
0N/A private final int S; /*0..3*/ // # low-order bits representing sign
0N/A private final int del; /*0..2*/ // type of delta encoding (0 == none)
0N/A private final int min; // smallest representable value
0N/A private final int max; // largest representable value
0N/A private final int umin; // smallest representable uns. value
0N/A private final int umax; // largest representable uns. value
0N/A private final int[] byteMin; // smallest repr. value, given # bytes
0N/A private final int[] byteMax; // largest repr. value, given # bytes
0N/A
0N/A private Coding(int B, int H, int S) {
0N/A this(B, H, S, 0);
0N/A }
0N/A private Coding(int B, int H, int S, int del) {
0N/A this.B = B;
0N/A this.H = H;
0N/A this.L = 256-H;
0N/A this.S = S;
0N/A this.del = del;
0N/A this.min = codeMin(B, H, S, B);
0N/A this.max = codeMax(B, H, S, B);
0N/A this.umin = codeMin(B, H, 0, B);
0N/A this.umax = codeMax(B, H, 0, B);
0N/A this.byteMin = new int[B];
0N/A this.byteMax = new int[B];
0N/A
0N/A for (int nMax = 1; nMax <= B; nMax++) {
0N/A byteMin[nMax-1] = codeMin(B, H, S, nMax);
0N/A byteMax[nMax-1] = codeMax(B, H, S, nMax);
0N/A }
0N/A }
0N/A
0N/A public boolean equals(Object x) {
0N/A if (!(x instanceof Coding)) return false;
0N/A Coding that = (Coding) x;
0N/A if (this.B != that.B) return false;
0N/A if (this.H != that.H) return false;
0N/A if (this.S != that.S) return false;
0N/A if (this.del != that.del) return false;
0N/A return true;
0N/A }
0N/A
0N/A public int hashCode() {
0N/A return (del<<14)+(S<<11)+(B<<8)+(H<<0);
0N/A }
0N/A
3315N/A private static Map<Coding, Coding> codeMap;
0N/A
0N/A private static synchronized Coding of(int B, int H, int S, int del) {
3315N/A if (codeMap == null) codeMap = new HashMap<>();
0N/A Coding x0 = new Coding(B, H, S, del);
3315N/A Coding x1 = codeMap.get(x0);
0N/A if (x1 == null) codeMap.put(x0, x1 = x0);
0N/A return x1;
0N/A }
0N/A
0N/A public static Coding of(int B, int H) {
0N/A return of(B, H, 0, 0);
0N/A }
0N/A
0N/A public static Coding of(int B, int H, int S) {
0N/A return of(B, H, S, 0);
0N/A }
0N/A
0N/A public boolean canRepresentValue(int x) {
0N/A if (isSubrange())
0N/A return canRepresentUnsigned(x);
0N/A else
0N/A return canRepresentSigned(x);
0N/A }
0N/A /** Can this coding represent a single value, possibly a delta?
0N/A * This ignores the D property. That is, for delta codings,
0N/A * this tests whether a delta value of 'x' can be coded.
0N/A * For signed delta codings which produce unsigned end values,
0N/A * use canRepresentUnsigned.
0N/A */
0N/A public boolean canRepresentSigned(int x) {
0N/A return (x >= min && x <= max);
0N/A }
0N/A /** Can this coding, apart from its S property,
0N/A * represent a single value? (Negative values
0N/A * can only be represented via 32-bit overflow,
0N/A * so this returns true for negative values
0N/A * if isFullRange is true.)
0N/A */
0N/A public boolean canRepresentUnsigned(int x) {
0N/A return (x >= umin && x <= umax);
0N/A }
0N/A
0N/A // object-oriented code/decode
0N/A public int readFrom(byte[] in, int[] inpos) {
0N/A return readInt(in, inpos, B, H, S);
0N/A }
0N/A public void writeTo(byte[] out, int[] outpos, int x) {
0N/A writeInt(out, outpos, x, B, H, S);
0N/A }
0N/A
0N/A // Stream versions
0N/A public int readFrom(InputStream in) throws IOException {
0N/A return readIntFrom(in, B, H, S);
0N/A }
0N/A public void writeTo(OutputStream out, int x) throws IOException {
0N/A byte[] buf = new byte[B];
0N/A int[] pos = new int[1];
0N/A writeInt(buf, pos, x, B, H, S);
0N/A out.write(buf, 0, pos[0]);
0N/A }
0N/A
0N/A // Stream/array versions
0N/A public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
0N/A // %%% use byte[] buffer
0N/A for (int i = start; i < end; i++)
0N/A a[i] = readFrom(in);
3315N/A
0N/A for (int dstep = 0; dstep < del; dstep++) {
0N/A long state = 0;
0N/A for (int i = start; i < end; i++) {
0N/A state += a[i];
0N/A // Reduce array values to the required range.
0N/A if (isSubrange()) {
0N/A state = reduceToUnsignedRange(state);
0N/A }
0N/A a[i] = (int) state;
0N/A }
0N/A }
0N/A }
0N/A public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
0N/A if (end <= start) return;
0N/A for (int dstep = 0; dstep < del; dstep++) {
0N/A int[] deltas;
0N/A if (!isSubrange())
0N/A deltas = makeDeltas(a, start, end, 0, 0);
0N/A else
0N/A deltas = makeDeltas(a, start, end, min, max);
0N/A a = deltas;
0N/A start = 0;
0N/A end = deltas.length;
0N/A }
0N/A // The following code is a buffered version of this loop:
0N/A // for (int i = start; i < end; i++)
0N/A // writeTo(out, a[i]);
0N/A byte[] buf = new byte[1<<8];
0N/A final int bufmax = buf.length-B;
0N/A int[] pos = { 0 };
0N/A for (int i = start; i < end; ) {
0N/A while (pos[0] <= bufmax) {
0N/A writeTo(buf, pos, a[i++]);
0N/A if (i >= end) break;
0N/A }
0N/A out.write(buf, 0, pos[0]);
0N/A pos[0] = 0;
0N/A }
0N/A }
0N/A
0N/A /** Tell if the range of this coding (number of distinct
0N/A * representable values) can be expressed in 32 bits.
0N/A */
0N/A boolean isSubrange() {
0N/A return max < Integer.MAX_VALUE
0N/A && ((long)max - (long)min + 1) <= Integer.MAX_VALUE;
0N/A }
0N/A
0N/A /** Tell if this coding can represent all 32-bit values.
0N/A * Note: Some codings, such as unsigned ones, can be neither
0N/A * subranges nor full-range codings.
0N/A */
0N/A boolean isFullRange() {
0N/A return max == Integer.MAX_VALUE && min == Integer.MIN_VALUE;
0N/A }
0N/A
0N/A /** Return the number of values this coding (a subrange) can represent. */
0N/A int getRange() {
0N/A assert(isSubrange());
0N/A return (max - min) + 1; // range includes both min & max
0N/A }
0N/A
0N/A Coding setB(int B) { return Coding.of(B, H, S, del); }
0N/A Coding setH(int H) { return Coding.of(B, H, S, del); }
0N/A Coding setS(int S) { return Coding.of(B, H, S, del); }
0N/A Coding setL(int L) { return setH(256-L); }
0N/A Coding setD(int del) { return Coding.of(B, H, S, del); }
0N/A Coding getDeltaCoding() { return setD(del+1); }
0N/A
0N/A /** Return a coding suitable for representing summed, modulo-reduced values. */
0N/A Coding getValueCoding() {
0N/A if (isDelta())
0N/A return Coding.of(B, H, 0, del-1);
0N/A else
0N/A return this;
0N/A }
0N/A
0N/A /** Reduce the given value to be within this coding's unsigned range,
0N/A * by adding or subtracting a multiple of (max-min+1).
0N/A */
0N/A int reduceToUnsignedRange(long value) {
0N/A if (value == (int)value && canRepresentUnsigned((int)value))
0N/A // already in unsigned range
0N/A return (int)value;
0N/A int range = getRange();
0N/A assert(range > 0);
0N/A value %= range;
0N/A if (value < 0) value += range;
0N/A assert(canRepresentUnsigned((int)value));
0N/A return (int)value;
0N/A }
0N/A
0N/A int reduceToSignedRange(int value) {
0N/A if (canRepresentSigned(value))
0N/A // already in signed range
0N/A return value;
0N/A return reduceToSignedRange(value, min, max);
0N/A }
0N/A static int reduceToSignedRange(int value, int min, int max) {
0N/A int range = (max-min+1);
0N/A assert(range > 0);
0N/A int value0 = value;
0N/A value -= min;
0N/A if (value < 0 && value0 >= 0) {
0N/A // 32-bit overflow, but the next '%=' op needs to be unsigned
0N/A value -= range;
0N/A assert(value >= 0);
0N/A }
0N/A value %= range;
0N/A if (value < 0) value += range;
0N/A value += min;
0N/A assert(min <= value && value <= max);
0N/A return value;
0N/A }
0N/A
0N/A /** Does this coding support at least one negative value?
0N/A Includes codings that can do so via 32-bit wraparound.
0N/A */
0N/A boolean isSigned() {
0N/A return min < 0;
0N/A }
0N/A /** Does this coding code arrays by making successive differences? */
0N/A boolean isDelta() {
0N/A return del != 0;
0N/A }
0N/A
0N/A public int B() { return B; }
0N/A public int H() { return H; }
0N/A public int L() { return L; }
0N/A public int S() { return S; }
0N/A public int del() { return del; }
0N/A public int min() { return min; }
0N/A public int max() { return max; }
0N/A public int umin() { return umin; }
0N/A public int umax() { return umax; }
0N/A public int byteMin(int b) { return byteMin[b-1]; }
0N/A public int byteMax(int b) { return byteMax[b-1]; }
0N/A
0N/A public int compareTo(Object x) {
0N/A Coding that = (Coding) x;
0N/A int dkey = this.del - that.del;
0N/A if (dkey == 0)
0N/A dkey = this.B - that.B;
0N/A if (dkey == 0)
0N/A dkey = this.H - that.H;
0N/A if (dkey == 0)
0N/A dkey = this.S - that.S;
0N/A return dkey;
0N/A }
0N/A
0N/A /** Heuristic measure of the difference between two codings. */
0N/A public int distanceFrom(Coding that) {
0N/A int diffdel = this.del - that.del;
0N/A if (diffdel < 0) diffdel = -diffdel;
0N/A int diffS = this.S - that.S;
0N/A if (diffS < 0) diffS = -diffS;
0N/A int diffB = this.B - that.B;
0N/A if (diffB < 0) diffB = -diffB;
0N/A int diffHL;
0N/A if (this.H == that.H) {
0N/A diffHL = 0;
0N/A } else {
0N/A // Distance in log space of H (<=128) and L (<128).
0N/A int thisHL = this.getHL();
0N/A int thatHL = that.getHL();
0N/A // Double the accuracy of the log:
0N/A thisHL *= thisHL;
0N/A thatHL *= thatHL;
0N/A if (thisHL > thatHL)
0N/A diffHL = ceil_lg2(1+(thisHL-1)/thatHL);
0N/A else
0N/A diffHL = ceil_lg2(1+(thatHL-1)/thisHL);
0N/A }
0N/A int norm = 5*(diffdel + diffS + diffB) + diffHL;
0N/A assert(norm != 0 || this.compareTo(that) == 0);
0N/A return norm;
0N/A }
0N/A private int getHL() {
0N/A // Follow H in log space by the multiplicative inverse of L.
0N/A if (H <= 128) return H;
0N/A if (L >= 1) return 128*128/L;
0N/A return 128*256;
0N/A }
0N/A
0N/A /** ceiling(log[2](x)): {1->0, 2->1, 3->2, 4->2, ...} */
0N/A static int ceil_lg2(int x) {
0N/A assert(x-1 >= 0); // x in range (int.MIN_VALUE -> 32)
0N/A x -= 1;
0N/A int lg = 0;
0N/A while (x != 0) {
0N/A lg++;
0N/A x >>= 1;
0N/A }
0N/A return lg;
0N/A }
0N/A
0N/A static private final byte[] byteBitWidths = new byte[0x100];
0N/A static {
0N/A for (int b = 0; b < byteBitWidths.length; b++) {
0N/A byteBitWidths[b] = (byte) ceil_lg2(b + 1);
0N/A }
0N/A for (int i = 10; i >= 0; i = (i << 1) - (i >> 3)) {
0N/A assert(bitWidth(i) == ceil_lg2(i + 1));
0N/A }
0N/A }
0N/A
0N/A /** Number of significant bits in i, not counting sign bits.
0N/A * For positive i, it is ceil_lg2(i + 1).
0N/A */
0N/A static int bitWidth(int i) {
0N/A if (i < 0) i = ~i; // change sign
0N/A int w = 0;
0N/A int lo = i;
0N/A if (lo < byteBitWidths.length)
0N/A return byteBitWidths[lo];
0N/A int hi;
0N/A hi = (lo >>> 16);
0N/A if (hi != 0) {
0N/A lo = hi;
0N/A w += 16;
0N/A }
0N/A hi = (lo >>> 8);
0N/A if (hi != 0) {
0N/A lo = hi;
0N/A w += 8;
0N/A }
0N/A w += byteBitWidths[lo];
0N/A //assert(w == ceil_lg2(i + 1));
0N/A return w;
0N/A }
0N/A
0N/A /** Create an array of successive differences.
0N/A * If min==max, accept any and all 32-bit overflow.
0N/A * Otherwise, avoid 32-bit overflow, and reduce all differences
0N/A * to a value in the given range, by adding or subtracting
0N/A * multiples of the range cardinality (max-min+1).
0N/A * Also, the values are assumed to be in the range [0..(max-min)].
0N/A */
0N/A static int[] makeDeltas(int[] values, int start, int end,
0N/A int min, int max) {
0N/A assert(max >= min);
0N/A int count = end-start;
0N/A int[] deltas = new int[count];
0N/A int state = 0;
0N/A if (min == max) {
0N/A for (int i = 0; i < count; i++) {
0N/A int value = values[start+i];
0N/A deltas[i] = value - state;
0N/A state = value;
0N/A }
0N/A } else {
0N/A for (int i = 0; i < count; i++) {
0N/A int value = values[start+i];
0N/A assert(value >= 0 && value+min <= max);
0N/A int delta = value - state;
0N/A assert(delta == (long)value - (long)state); // no overflow
0N/A state = value;
0N/A // Reduce delta values to the required range.
0N/A delta = reduceToSignedRange(delta, min, max);
0N/A deltas[i] = delta;
0N/A }
0N/A }
0N/A return deltas;
0N/A }
0N/A
0N/A boolean canRepresent(int minValue, int maxValue) {
0N/A assert(minValue <= maxValue);
0N/A if (del > 0) {
0N/A if (isSubrange()) {
0N/A // We will force the values to reduce to the right subrange.
0N/A return canRepresentUnsigned(maxValue)
0N/A && canRepresentUnsigned(minValue);
0N/A } else {
0N/A // Huge range; delta values must assume full 32-bit range.
0N/A return isFullRange();
0N/A }
0N/A }
0N/A else
0N/A // final values must be representable
0N/A return canRepresentSigned(maxValue)
0N/A && canRepresentSigned(minValue);
0N/A }
0N/A
0N/A boolean canRepresent(int[] values, int start, int end) {
0N/A int len = end-start;
0N/A if (len == 0) return true;
0N/A if (isFullRange()) return true;
0N/A // Calculate max, min:
3315N/A int lmax = values[start];
3315N/A int lmin = lmax;
0N/A for (int i = 1; i < len; i++) {
0N/A int value = values[start+i];
3315N/A if (lmax < value) lmax = value;
3315N/A if (lmin > value) lmin = value;
0N/A }
3315N/A return canRepresent(lmin, lmax);
0N/A }
0N/A
0N/A public double getBitLength(int value) { // implements BitMetric
0N/A return (double) getLength(value) * 8;
0N/A }
0N/A
0N/A /** How many bytes are in the coding of this value?
0N/A * Returns Integer.MAX_VALUE if the value has no coding.
0N/A * The coding must not be a delta coding, since there is no
0N/A * definite size for a single value apart from its context.
0N/A */
0N/A public int getLength(int value) {
0N/A if (isDelta() && isSubrange()) {
0N/A if (!canRepresentUnsigned(value))
0N/A return Integer.MAX_VALUE;
0N/A value = reduceToSignedRange(value);
0N/A }
0N/A if (value >= 0) {
0N/A for (int n = 0; n < B; n++) {
0N/A if (value <= byteMax[n]) return n+1;
0N/A }
0N/A } else {
0N/A for (int n = 0; n < B; n++) {
0N/A if (value >= byteMin[n]) return n+1;
0N/A }
0N/A }
0N/A return Integer.MAX_VALUE;
0N/A }
0N/A
0N/A public int getLength(int[] values, int start, int end) {
0N/A int len = end-start;
0N/A if (B == 1) return len;
0N/A if (L == 0) return len * B;
0N/A if (isDelta()) {
0N/A int[] deltas;
0N/A if (!isSubrange())
0N/A deltas = makeDeltas(values, start, end, 0, 0);
0N/A else
0N/A deltas = makeDeltas(values, start, end, min, max);
0N/A //return Coding.of(B, H, S).getLength(deltas, 0, len);
0N/A values = deltas;
0N/A start = 0;
0N/A }
0N/A int sum = len; // at least 1 byte per
0N/A // add extra bytes for extra-long values
0N/A for (int n = 1; n <= B; n++) {
0N/A // what is the coding interval [min..max] for n bytes?
3315N/A int lmax = byteMax[n-1];
3315N/A int lmin = byteMin[n-1];
0N/A int longer = 0; // count of guys longer than n bytes
0N/A for (int i = 0; i < len; i++) {
0N/A int value = values[start+i];
0N/A if (value >= 0) {
3315N/A if (value > lmax) longer++;
0N/A } else {
3315N/A if (value < lmin) longer++;
0N/A }
0N/A }
0N/A if (longer == 0) break; // no more passes needed
0N/A if (n == B) return Integer.MAX_VALUE; // cannot represent!
0N/A sum += longer;
0N/A }
0N/A return sum;
0N/A }
0N/A
0N/A public byte[] getMetaCoding(Coding dflt) {
0N/A if (dflt == this) return new byte[]{ (byte) _meta_default };
0N/A int canonicalIndex = BandStructure.indexOf(this);
0N/A if (canonicalIndex > 0)
0N/A return new byte[]{ (byte) canonicalIndex };
0N/A return new byte[]{
0N/A (byte)_meta_arb,
0N/A (byte)(del + 2*S + 8*(B-1)),
0N/A (byte)(H-1)
0N/A };
0N/A }
0N/A public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
0N/A int op = bytes[pos++] & 0xFF;
0N/A if (_meta_canon_min <= op && op <= _meta_canon_max) {
0N/A Coding c = BandStructure.codingForIndex(op);
0N/A assert(c != null);
0N/A res[0] = c;
0N/A return pos;
0N/A }
0N/A if (op == _meta_arb) {
0N/A int dsb = bytes[pos++] & 0xFF;
0N/A int H_1 = bytes[pos++] & 0xFF;
0N/A int del = dsb % 2;
0N/A int S = (dsb / 2) % 4;
0N/A int B = (dsb / 8)+1;
0N/A int H = H_1+1;
0N/A if (!((1 <= B && B <= B_MAX) &&
0N/A (0 <= S && S <= S_MAX) &&
0N/A (1 <= H && H <= H_MAX) &&
0N/A (0 <= del && del <= 1))
0N/A || (B == 1 && H != 256)
0N/A || (B == 5 && H == 256)) {
0N/A throw new RuntimeException("Bad arb. coding: ("+B+","+H+","+S+","+del);
0N/A }
0N/A res[0] = Coding.of(B, H, S, del);
0N/A return pos;
0N/A }
0N/A return pos-1; // backup
0N/A }
0N/A
0N/A
0N/A public String keyString() {
0N/A return "("+B+","+H+","+S+","+del+")";
0N/A }
0N/A
0N/A public String toString() {
0N/A String str = "Coding"+keyString();
0N/A // If -ea, print out more informative strings!
0N/A //assert((str = stringForDebug()) != null);
0N/A return str;
0N/A }
0N/A
0N/A static boolean verboseStringForDebug = false;
0N/A String stringForDebug() {
0N/A String minS = (min == Integer.MIN_VALUE ? "min" : ""+min);
0N/A String maxS = (max == Integer.MAX_VALUE ? "max" : ""+max);
0N/A String str = keyString()+" L="+L+" r=["+minS+","+maxS+"]";
0N/A if (isSubrange())
0N/A str += " subrange";
0N/A else if (!isFullRange())
0N/A str += " MIDRANGE";
0N/A if (verboseStringForDebug) {
0N/A str += " {";
0N/A int prev_range = 0;
0N/A for (int n = 1; n <= B; n++) {
0N/A int range_n = saturate32((long)byteMax[n-1] - byteMin[n-1] + 1);
0N/A assert(range_n == saturate32(codeRangeLong(B, H, n)));
0N/A range_n -= prev_range;
0N/A prev_range = range_n;
0N/A String rngS = (range_n == Integer.MAX_VALUE ? "max" : ""+range_n);
0N/A str += " #"+n+"="+rngS;
0N/A }
0N/A str += " }";
0N/A }
0N/A return str;
0N/A }
0N/A}