Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Unnamed
Wednesday, May 23rd, 2012 at 4:52:01pm MDT 

  1. package TDC.Compression.Arithmetic;
  2.  
  3. /**
  4. * Excluding Adaptive Unigram Model {
  5. * @author Inna Tuzhikova
  6. */
  7. final class ExcludingAdaptiveUnigramModel {
  8.  
  9.     /**
  10.      * Construct an excluding adaptive unigram model.
  11.      */
  12.     public ExcludingAdaptiveUnigramModel() {
  13.         _count = new int[257];
  14.         java.util.Arrays.fill(_count, 1); // counts are non-cumulative
  15.     }
  16.  
  17.     /**
  18.      * Compute the resulting interval to code the specified symbol given the
  19.      * specified excluded bytes.
  20.      *
  21.      * @param symbol Symbol to code.
  22.      * @param result Interval to code the symbol.
  23.      * @param exclusions Bytes to exclude as possible outcomes for interval.
  24.      */
  25.     public void interval(int symbol, int[] result, ByteSet exclusions) {
  26.         if (symbol == ArithCodeModel.EOF) {
  27.             symbol = EOF_INDEX;
  28.         }
  29.         int sum = 0;
  30.         for (int i = 0; i < symbol; ++i) {
  31.             if (!exclusions.contains(i)) {
  32.                 sum += _count[i];
  33.             }
  34.         }
  35.         result[0] = sum;
  36.         sum += _count[symbol];
  37.         result[1] = sum;
  38.         for (int i = symbol + 1; i < _count.length - 1; ++i) {
  39.             if (!exclusions.contains(i)) {
  40.                 sum += _count[i];
  41.             }
  42.         }
  43.         if (symbol != EOF_INDEX) {
  44.             sum += _count[EOF_INDEX];
  45.         }
  46.         result[2] = sum;
  47.         increment(symbol);
  48.     }
  49.  
  50.     /**
  51.      * Return the symbol corresponding to the specified count, given the
  52.      * specified excluded bytes.
  53.      *
  54.      * @param midCount Count of symbol to return.
  55.      * @param exclusions Bytes to exclude from consideration.
  56.      * @return Symbol represented by specified count.
  57.      */
  58.     public int pointToSymbol(int midCount, ByteSet exclusions) {
  59.         int sum = 0;
  60.         for (int mid = 0;; ++mid) {
  61.             if (mid != EOF_INDEX && exclusions.contains(mid)) {
  62.                 continue;
  63.             }
  64.             sum += _count[mid];
  65.             if (sum > midCount) {
  66.                 return (mid == EOF_INDEX) ? ArithCodeModel.EOF : mid;
  67.             }
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Total count for interval given specified set of exclusions.
  73.      *
  74.      * @param exclusions Bytes to exclude as outcomes.
  75.      * @return Total count of all non-excluded outcomes.
  76.      */
  77.     public int totalCount(ByteSet exclusions) {
  78.         int total = 0;
  79.         for (int i = 0; i < _count.length; ++i) {
  80.             if (i == EOF_INDEX || !exclusions.contains(i)) {
  81.                 total += _count[i];
  82.             }
  83.         }
  84.         return total;
  85.     }
  86.  
  87.     /**
  88.      * Increment the count for the given outcome.
  89.      *
  90.      * @param i Outcome to increment
  91.      */
  92.     public void increment(int i) {
  93.         if (++_count[i] > MAX_INDIVIDUAL_COUNT) {
  94.             rescale();
  95.         }
  96.     }
  97.     /**
  98.      * Counts for each outcome. Indices 0 to 255 for the usual counts, 256 for
  99.      * end-of-file, and 257 for total.
  100.      */
  101.     private int[] _count;
  102.  
  103.     /**
  104.      * Rescale the counts by dividing all frequencies by 2, but taking a minimum
  105.      * of 1.
  106.      */
  107.     private void rescale() {
  108.         for (int i = 0; i < _count.length; ++i) {
  109.             _count[i] = (_count[i] + 1) / 2;
  110.         }
  111.     }
  112.     /**
  113.      * Maximum count before rescaling.
  114.      */
  115.     private static final int MAX_INDIVIDUAL_COUNT = 8 * 1024;
  116.     /**
  117.      * Index in the count array for the end-of-file outcome.
  118.      */
  119.     private static final int EOF_INDEX = 256;
  120. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation
fantasy-obligation