jdk/src/share/classes/com/sun/java/util/jar/pack/Coding.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7192 445c518364c4
child 7816 55a18147b4bf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7192
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7192
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    28
import java.io.IOException;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    29
import java.io.InputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    30
import java.io.OutputStream;
445c518364c4 7003227: (pack200) intermittent failures compiling pack200
ksrini
parents: 5506
diff changeset
    31
import java.util.HashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Define the conversions between sequences of small integers and raw bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This is a schema of encodings which incorporates varying lengths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * varying degrees of length variability, and varying amounts of signed-ness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class Coding implements Constants, Comparable, CodingMethod, Histogram.BitMetric {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
      Coding schema for single integers, parameterized by (B,H,S):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
      Let B in [1,5], H in [1,256], S in [0,3].
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
      (S limit is arbitrary.  B follows the 32-bit limit.  H is byte size.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
      A given (B,H,S) code varies in length from 1 to B bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
      The 256 values a byte may take on are divided into L=(256-H) and H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
      values, with all the H values larger than the L values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
      (That is, the L values are [0,L) and the H are [L,256).)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
      The last byte is always either the B-th byte, a byte with "L value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
      (<L), or both.  There is no other byte that satisfies these conditions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
      All bytes before the last always have "H values" (>=L).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
      Therefore, if L==0, the code always has the full length of B bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      The coding then becomes a classic B-byte little-endian unsigned integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      (Also, if L==128, the high bit of each byte acts signals the presence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      of a following byte, up to the maximum length.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      In the unsigned case (S==0), the coding is compact and monotonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      in the ordering of byte sequences defined by appending zero bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      to pad them to a common length B, reversing them, and ordering them
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      lexicographically.  (This agrees with "little-endian" byte order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      Therefore, the unsigned value of a byte sequence may be defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        U(b0)           == b0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                           in [0..L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                           or [0..256) if B==1 (**)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        U(b0,b1)        == b0 + b1*H
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                           in [L..L*(1+H))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                           or [L..L*(1+H) + H^2) if B==2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        U(b0,b1,b2)     == b0 + b1*H + b2*H^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                           in [L*(1+H)..L*(1+H+H^2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                           or [L*(1+H)..L*(1+H+H^2) + H^3) if B==3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        U(b[i]: i<n)    == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                           up to  L*Sum[i<n]( H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                           or to  L*Sum[i<n]( H^i ) + H^n if n==B
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      (**) If B==1, the values H,L play no role in the coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      As a convention, we require that any (1,H,S) code must always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      encode values less than H.  Thus, a simple unsigned byte is coded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      specifically by the code (1,256,0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      (Properly speaking, the unsigned case should be parameterized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      S==Infinity.  If the schema were regular, the case S==0 would really
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      denote a numbering in which all coded values are negative.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      If S>0, the unsigned value of a byte sequence is regarded as a binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      integer.  If any of the S low-order bits are zero, the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      signed value will be non-negative.  If all of the S low-order bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      (S>0) are one, the the corresponding signed value will be negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      The non-negative signed values are compact and monotonically increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      (from 0) in the ordering of the corresponding unsigned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      The negative signed values are compact and monotonically decreasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      (from -1) in the ordering of the corresponding unsigned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      In essence, the low-order S bits function as a collective sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      for negative signed numbers, and as a low-order base-(2^S-1) digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      for non-negative signed numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      Therefore, the signed value corresponding to an unsigned value is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Sgn(x)  == x                               if S==0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Sgn(x)  == (x / 2^S)*(2^S-1) + (x % 2^S),  if S>0, (x % 2^S) < 2^S-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Sgn(x)  == -(x / 2^S)-1,                   if S>0, (x % 2^S) == 2^S-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      Finally, the value of a byte sequence, given the coding parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      (B,H,S), is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        V(b[i]: i<n)  == Sgn(U(b[i]: i<n))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      The extremal positive and negative signed value for a given range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      of unsigned values may be found by sign-encoding the largest unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      value which is not 2^S-1 mod 2^S, and that which is, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
      Because B,H,S are variable, this is not a single coding but a schema
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      of codings.  For optimal compression, it is necessary to adaptively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      select specific codings to the data being compressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      For example, if a sequence of values happens never to be negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      S==0 is the best choice.  If the values are equally balanced between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      negative and positive, S==1.  If negative values are rare, then S>1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      is more appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      A (B,H,S) encoding is called a "subrange" if it does not encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      the largest 32-bit value, and if the number R of values it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      encode can be expressed as a positive 32-bit value.  (Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      B=1 implies R<=256, B=2 implies R<=65536, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      A delta version of a given (B,H,S) coding encodes an array of integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      by writing their successive differences in the (B,H,S) coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      The original integers themselves may be recovered by making a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      running accumulation of sum of the differences as they are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
      As a special case, if a (B,H,S) encoding is a subrange, its delta
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      version will only encode arrays of numbers in the coding's unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      range, [0..R-1].  The coding of deltas is still in the normal signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      range, if S!=0.  During delta encoding, all subtraction results are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      reduced to the signed range, by adding multiples of R.  Likewise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
.     during encoding, all addition results are reduced to the unsigned range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      This special case for subranges allows the benefits of wraparound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      when encoding correlated sequences of very small positive numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // Code-specific limits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static int saturate32(long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (x > Integer.MAX_VALUE)   return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (x < Integer.MIN_VALUE)   return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return (int)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static long codeRangeLong(int B, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return codeRangeLong(B, H, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static long codeRangeLong(int B, int H, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Code range for a all (B,H) codes of length <=nMax (<=B).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // n < B:   L*Sum[i<n]( H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // n == B:  L*Sum[i<B]( H^i ) + H^B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        assert(nMax >= 0 && nMax <= B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        assert(B >= 1 && B <= 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        assert(H >= 1 && H <= 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (nMax == 0)  return 0;  // no codes of zero length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (B == 1)     return H;  // special case; see (**) above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int n = 1; n <= nMax; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            sum += H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        sum *= L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (nMax == B)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            sum += H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** Largest int representable by (B,H,S) in up to nMax bytes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static int codeMax(int B, int H, int S, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        //assert(S >= 0 && S <= S_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        long range = codeRangeLong(B, H, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (range == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return -1;  // degenerate max value for empty set of codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (S == 0 || range >= (long)1<<32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return saturate32(range-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        long maxPos = range-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        while (isNegativeCode(maxPos, S))  --maxPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (maxPos < 0)  return -1;  // No positive codings at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int smax = decodeSign32(maxPos, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // check for 32-bit wraparound:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (smax < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return smax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /** Smallest int representable by (B,H,S) in up to nMax bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Returns Integer.MIN_VALUE if 32-bit wraparound covers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        the entire negative range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static int codeMin(int B, int H, int S, int nMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //assert(S >= 0 && S <= S_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        long range = codeRangeLong(B, H, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (range >= (long)1<<32 && nMax == B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // Can code negative values via 32-bit wraparound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int Smask = (1<<S)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        long maxNeg = range-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        while (!isNegativeCode(maxNeg, S))  --maxNeg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (maxNeg < 0)  return 0;  // No negative codings at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return decodeSign32(maxNeg, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Some of the arithmetic below is on unsigned 32-bit integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // These must be represented in Java as longs in the range [0..2^32-1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // The conversion to a signed int is just the Java cast (int), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // the conversion to an unsigned int is the following little method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private static long toUnsigned32(int sx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return ((long)sx << 32) >>> 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    // Sign encoding:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static boolean isNegativeCode(long ux, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        assert(S > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        assert(ux >= -1);  // can be out of 32-bit range; who cares
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int Smask = (1<<S)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return (((int)ux+1) & Smask) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private static boolean hasNegativeCode(int sx, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        assert(S > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        // If S>=2 very low negatives are coded by 32-bit-wrapped positives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // The lowest negative representable by a negative coding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // ~(umax32 >> S), and the next lower number is coded by wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // the highest positive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        //    CodePos(umax32-1)  ->  (umax32-1)-((umax32-1)>>S)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // which simplifies to ~(umax32 >> S)-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (0 > sx) && (sx >= ~(-1>>>S));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static int decodeSign32(long ux, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        assert(ux == toUnsigned32((int)ux))  // must be unsigned 32-bit number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            : (Long.toHexString(ux));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return (int) ux;  // cast to signed int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (isNegativeCode(ux, S)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // Sgn(x)  == -(x / 2^S)-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            sx = ~((int)ux >>> S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // Sgn(x)  == (x / 2^S)*(2^S-1) + (x % 2^S)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            sx = (int)ux - ((int)ux >>> S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Assert special case of S==1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        assert(!(S == 1) || sx == (((int)ux >>> 1) ^ -((int)ux & 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return sx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static long encodeSign32(int sx, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (S == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return toUnsigned32(sx);  // unsigned 32-bit int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int Smask = (1<<S)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        long ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (!hasNegativeCode(sx, S)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // InvSgn(sx) = (sx / (2^S-1))*2^S + (sx % (2^S-1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            ux = sx + (toUnsigned32(sx) / Smask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // InvSgn(sx) = (-sx-1)*2^S + (2^S-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            ux = (-sx << S) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        ux = toUnsigned32((int)ux);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        assert(sx == decodeSign32(ux, S))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            : (Long.toHexString(ux)+" -> "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
               Integer.toHexString(sx)+" != "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
               Integer.toHexString(decodeSign32(ux, S)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // Top-level coding of single integers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public static void writeInt(byte[] out, int[] outpos, int sx, int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        long ux = encodeSign32(sx, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        assert(ux == toUnsigned32((int)ux));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        assert(ux < codeRangeLong(B, H))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            : Long.toHexString(ux);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        long sum = ux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int pos = outpos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int i = 0; i < B-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (sum < L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            sum -= L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            int b_i = (int)( L + (sum % H) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            sum /= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            out[pos++] = (byte)b_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        out[pos++] = (byte)sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // Report number of bytes written by updating outpos[0]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        outpos[0] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // Check right away for mis-coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        //assert(sx == readInt(out, new int[1], B, H, S));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public static int readInt(byte[] in, int[] inpos, int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int pos = inpos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (int i = 0; i < B; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            int b_i = in[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            sum += b_i*H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (b_i < L)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        //assert(sum >= 0 && sum < codeRangeLong(B, H));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // Report number of bytes read by updating inpos[0]:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        inpos[0] = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return decodeSign32(sum, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // The Stream version doesn't fetch a byte unless it is needed for coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public static int readIntFrom(InputStream in, int B, int H, int S) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // U(b[i]: i<n) == Sum[i<n]( b[i] * H^i )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        long sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        long H_i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        for (int i = 0; i < B; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int b_i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (b_i < 0)  throw new RuntimeException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            sum += b_i*H_i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            H_i *= H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (b_i < L)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        assert(sum >= 0 && sum < codeRangeLong(B, H));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return decodeSign32(sum, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public static final int B_MAX = 5;    /* B: [1,5] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public static final int H_MAX = 256;  /* H: [1,256] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public static final int S_MAX = 2;    /* S: [0,2] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // END OF STATICS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private final int B; /*1..5*/       // # bytes (1..5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private final int H; /*1..256*/     // # codes requiring a higher byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private final int L; /*0..255*/     // # codes requiring a higher byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private final int S; /*0..3*/       // # low-order bits representing sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private final int del; /*0..2*/     // type of delta encoding (0 == none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private final int min;              // smallest representable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private final int max;              // largest representable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private final int umin;             // smallest representable uns. value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private final int umax;             // largest representable uns. value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private final int[] byteMin;        // smallest repr. value, given # bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private final int[] byteMax;        // largest repr. value, given # bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private Coding(int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        this(B, H, S, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private Coding(int B, int H, int S, int del) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        this.B = B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        this.H = H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        this.L = 256-H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        this.S = S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        this.del = del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        this.min = codeMin(B, H, S, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        this.max = codeMax(B, H, S, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        this.umin = codeMin(B, H, 0, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        this.umax = codeMax(B, H, 0, B);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        this.byteMin = new int[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        this.byteMax = new int[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        for (int nMax = 1; nMax <= B; nMax++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            byteMin[nMax-1] = codeMin(B, H, S, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            byteMax[nMax-1] = codeMax(B, H, S, nMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public boolean equals(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (!(x instanceof Coding))  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        Coding that = (Coding) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (this.B != that.B)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (this.H != that.H)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (this.S != that.S)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (this.del != that.del)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return (del<<14)+(S<<11)+(B<<8)+(H<<0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    private static HashMap codeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private static synchronized Coding of(int B, int H, int S, int del) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (codeMap == null)  codeMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Coding x0 = new Coding(B, H, S, del);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        Coding x1 = (Coding) codeMap.get(x0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (x1 == null)  codeMap.put(x0, x1 = x0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static Coding of(int B, int H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return of(B, H, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public static Coding of(int B, int H, int S) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return of(B, H, S, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public boolean canRepresentValue(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return canRepresentUnsigned(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return canRepresentSigned(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /** Can this coding represent a single value, possibly a delta?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *  This ignores the D property.  That is, for delta codings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *  this tests whether a delta value of 'x' can be coded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *  For signed delta codings which produce unsigned end values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *  use canRepresentUnsigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public boolean canRepresentSigned(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return (x >= min && x <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /** Can this coding, apart from its S property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *  represent a single value?  (Negative values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *  can only be represented via 32-bit overflow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *  so this returns true for negative values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *  if isFullRange is true.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public boolean canRepresentUnsigned(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return (x >= umin && x <= umax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    // object-oriented code/decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public int readFrom(byte[] in, int[] inpos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return readInt(in, inpos, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public void writeTo(byte[] out, int[] outpos, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        writeInt(out, outpos, x, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    // Stream versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public int readFrom(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return readIntFrom(in, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public void writeTo(OutputStream out, int x) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        byte[] buf = new byte[B];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int[] pos = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        writeInt(buf, pos, x, B, H, S);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        out.write(buf, 0, pos[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // Stream/array versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // %%% use byte[] buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            a[i] = readFrom(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        for (int dstep = 0; dstep < del; dstep++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            long state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                state += a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // Reduce array values to the required range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    state = reduceToUnsignedRange(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                a[i] = (int) state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (end <= start)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        for (int dstep = 0; dstep < del; dstep++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            int[] deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (!isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                deltas = makeDeltas(a, start, end, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                deltas = makeDeltas(a, start, end, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            a = deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            end = deltas.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // The following code is a buffered version of this loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        //    for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        //        writeTo(out, a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        byte[] buf = new byte[1<<8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        final int bufmax = buf.length-B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        int[] pos = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        for (int i = start; i < end; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            while (pos[0] <= bufmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                writeTo(buf, pos, a[i++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                if (i >= end)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            out.write(buf, 0, pos[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            pos[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /** Tell if the range of this coding (number of distinct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *  representable values) can be expressed in 32 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    boolean isSubrange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return max < Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            && ((long)max - (long)min + 1) <= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /** Tell if this coding can represent all 32-bit values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *  Note:  Some codings, such as unsigned ones, can be neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *  subranges nor full-range codings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    boolean isFullRange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return max == Integer.MAX_VALUE && min == Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /** Return the number of values this coding (a subrange) can represent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    int getRange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        assert(isSubrange());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return (max - min) + 1;  // range includes both min & max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    Coding setB(int B) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    Coding setH(int H) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    Coding setS(int S) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    Coding setL(int L) { return setH(256-L); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    Coding setD(int del) { return Coding.of(B, H, S, del); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    Coding getDeltaCoding() { return setD(del+1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /** Return a coding suitable for representing summed, modulo-reduced values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    Coding getValueCoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (isDelta())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return Coding.of(B, H, 0, del-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /** Reduce the given value to be within this coding's unsigned range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *  by adding or subtracting a multiple of (max-min+1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    int reduceToUnsignedRange(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (value == (int)value && canRepresentUnsigned((int)value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // already in unsigned range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        int range = getRange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        assert(range > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        value %= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (value < 0)  value += range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        assert(canRepresentUnsigned((int)value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    int reduceToSignedRange(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (canRepresentSigned(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // already in signed range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return reduceToSignedRange(value, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    static int reduceToSignedRange(int value, int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int range = (max-min+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        assert(range > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int value0 = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        value -= min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (value < 0 && value0 >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // 32-bit overflow, but the next '%=' op needs to be unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            value -= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            assert(value >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        value %= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (value < 0)  value += range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        value += min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        assert(min <= value && value <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /** Does this coding support at least one negative value?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        Includes codings that can do so via 32-bit wraparound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    boolean isSigned() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return min < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /** Does this coding code arrays by making successive differences? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    boolean isDelta() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return del != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public int B() { return B; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public int H() { return H; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public int L() { return L; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public int S() { return S; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public int del() { return del; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public int min() { return min; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public int max() { return max; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public int umin() { return umin; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public int umax() { return umax; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public int byteMin(int b) { return byteMin[b-1]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public int byteMax(int b) { return byteMax[b-1]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public int compareTo(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        Coding that = (Coding) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        int dkey = this.del - that.del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            dkey = this.B - that.B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            dkey = this.H - that.H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (dkey == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            dkey = this.S - that.S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return dkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /** Heuristic measure of the difference between two codings. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public int distanceFrom(Coding that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        int diffdel = this.del - that.del;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (diffdel < 0)  diffdel = -diffdel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int diffS = this.S - that.S;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (diffS < 0)  diffS = -diffS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int diffB = this.B - that.B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (diffB < 0)  diffB = -diffB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        int diffHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (this.H == that.H) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            diffHL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // Distance in log space of H (<=128) and L (<128).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            int thisHL = this.getHL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            int thatHL = that.getHL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            // Double the accuracy of the log:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            thisHL *= thisHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            thatHL *= thatHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            if (thisHL > thatHL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                diffHL = ceil_lg2(1+(thisHL-1)/thatHL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                diffHL = ceil_lg2(1+(thatHL-1)/thisHL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        int norm = 5*(diffdel + diffS + diffB) + diffHL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        assert(norm != 0 || this.compareTo(that) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return norm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    private int getHL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // Follow H in log space by the multiplicative inverse of L.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (H <= 128)  return H;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (L >= 1)    return 128*128/L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return 128*256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /** ceiling(log[2](x)): {1->0, 2->1, 3->2, 4->2, ...} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    static int ceil_lg2(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        assert(x-1 >= 0);  // x in range (int.MIN_VALUE -> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        x -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        int lg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        while (x != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            lg++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            x >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return lg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    static private final byte[] byteBitWidths = new byte[0x100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        for (int b = 0; b < byteBitWidths.length; b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            byteBitWidths[b] = (byte) ceil_lg2(b + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        for (int i = 10; i >= 0; i = (i << 1) - (i >> 3)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            assert(bitWidth(i) == ceil_lg2(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /** Number of significant bits in i, not counting sign bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *  For positive i, it is ceil_lg2(i + 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    static int bitWidth(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (i < 0)  i = ~i;  // change sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        int w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        int lo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (lo < byteBitWidths.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return byteBitWidths[lo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        hi = (lo >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (hi != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            lo = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            w += 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        hi = (lo >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (hi != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            lo = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            w += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        w += byteBitWidths[lo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        //assert(w == ceil_lg2(i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /** Create an array of successive differences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *  If min==max, accept any and all 32-bit overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *  Otherwise, avoid 32-bit overflow, and reduce all differences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *  to a value in the given range, by adding or subtracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *  multiples of the range cardinality (max-min+1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *  Also, the values are assumed to be in the range [0..(max-min)].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    static int[] makeDeltas(int[] values, int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        assert(max >= min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        int count = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        int[] deltas = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        int state = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (min == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                deltas[i] = value - state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                state = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                assert(value >= 0 && value+min <= max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                int delta = value - state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                assert(delta == (long)value - (long)state); // no overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                state = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                // Reduce delta values to the required range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                delta = reduceToSignedRange(delta, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                deltas[i] = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    boolean canRepresent(int minValue, int maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        assert(minValue <= maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (del > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                // We will force the values to reduce to the right subrange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return canRepresentUnsigned(maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    && canRepresentUnsigned(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                // Huge range; delta values must assume full 32-bit range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                return isFullRange();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            // final values must be representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            return canRepresentSigned(maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                && canRepresentSigned(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    boolean canRepresent(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (len == 0)       return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (isFullRange())  return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // Calculate max, min:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        int max = values[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        int min = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        for (int i = 1; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (max < value)  max = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (min > value)  min = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return canRepresent(min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public double getBitLength(int value) {  // implements BitMetric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        return (double) getLength(value) * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /** How many bytes are in the coding of this value?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *  Returns Integer.MAX_VALUE if the value has no coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *  The coding must not be a delta coding, since there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *  definite size for a single value apart from its context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    public int getLength(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (isDelta() && isSubrange()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            if (!canRepresentUnsigned(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            value = reduceToSignedRange(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            for (int n = 0; n < B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                if (value <= byteMax[n])  return n+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            for (int n = 0; n < B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (value >= byteMin[n])  return n+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public int getLength(int[] values, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int len = end-start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (B == 1)  return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (L == 0)  return len * B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (isDelta()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            int[] deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (!isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                deltas = makeDeltas(values, start, end, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                deltas = makeDeltas(values, start, end, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            //return Coding.of(B, H, S).getLength(deltas, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            values = deltas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            end = values.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        int sum = len;  // at least 1 byte per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // add extra bytes for extra-long values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        for (int n = 1; n <= B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            // what is the coding interval [min..max] for n bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            int max = byteMax[n-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            int min = byteMin[n-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            int longer = 0;  // count of guys longer than n bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                int value = values[start+i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    if (value > max)  longer++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    if (value < min)  longer++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (longer == 0)  break;  // no more passes needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            if (n == B)  return Integer.MAX_VALUE;  // cannot represent!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            sum += longer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (dflt == this)  return new byte[]{ (byte) _meta_default };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        int canonicalIndex = BandStructure.indexOf(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (canonicalIndex > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return new byte[]{ (byte) canonicalIndex };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return new byte[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            (byte)_meta_arb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            (byte)(del + 2*S + 8*(B-1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            (byte)(H-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (_meta_canon_min <= op && op <= _meta_canon_max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            Coding c = BandStructure.codingForIndex(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            assert(c != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            res[0] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (op == _meta_arb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            int dsb = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            int H_1 = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            int del = dsb % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            int S = (dsb / 2) % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            int B = (dsb / 8)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            int H = H_1+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (!((1 <= B && B <= B_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                  (0 <= S && S <= S_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                  (1 <= H && H <= H_MAX) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                  (0 <= del && del <= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                || (B == 1 && H != 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                || (B == 5 && H == 256)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                throw new RuntimeException("Bad arb. coding: ("+B+","+H+","+S+","+del);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            res[0] = Coding.of(B, H, S, del);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return pos-1;  // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public String keyString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return "("+B+","+H+","+S+","+del+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        String str = "Coding"+keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        // If -ea, print out more informative strings!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        //assert((str = stringForDebug()) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    static boolean verboseStringForDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    String stringForDebug() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        String minS = (min == Integer.MIN_VALUE ? "min" : ""+min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        String maxS = (max == Integer.MAX_VALUE ? "max" : ""+max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        String str = keyString()+" L="+L+" r=["+minS+","+maxS+"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (isSubrange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            str += " subrange";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        else if (!isFullRange())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            str += " MIDRANGE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (verboseStringForDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            str += " {";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            int prev_range = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            for (int n = 1; n <= B; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                int range_n = saturate32((long)byteMax[n-1] - byteMin[n-1] + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                assert(range_n == saturate32(codeRangeLong(B, H, n)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                range_n -= prev_range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                prev_range = range_n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                String rngS = (range_n == Integer.MAX_VALUE ? "max" : ""+range_n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                str += " #"+n+"="+rngS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            str += " }";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
}