Example #1
0
 /** Compare two nodes. Their lengths must be equal. Their links must also compare. */
 public boolean postMortem(PostMortem pm) {
   Node that = (Node) pm;
   if (that == null) {
     JSONzip.log("\nMisalign");
     return false;
   }
   if (this.integer != that.integer) {
     JSONzip.log("\nInteger " + this.integer + " <> " + that.integer);
     return false;
   }
   if (this.next == null) {
     if (that.next == null) {
       return true;
     }
     JSONzip.log("\nNext is null " + this.integer);
     return false;
   }
   for (int i = 0; i < 256; i += 1) {
     Node node = this.next[i];
     if (node != null) {
       if (!node.postMortem(that.next[i])) {
         return false;
       }
     } else if (that.next[i] != null) {
       JSONzip.log("\nMisalign " + i);
       return false;
     }
   }
   return true;
 }
Example #2
0
  /** Compare two Huffman tables. */
  public boolean postMortem(PostMortem pm) {

    // Go through every integer in the domain, generating its bit sequence, and
    // then proving that that bit sequence produces the same integer.

    for (int integer = 0; integer < this.domain; integer += 1) {
      if (!postMortem(integer)) {
        JSONzip.log("\nBad huff ");
        JSONzip.logchar(integer, integer);
        return false;
      }
    }
    return this.table.postMortem(((Huff) pm).table);
  }
Example #3
0
 public int registerOne(Kim kim, int from, int thru) {
   if (this.length < this.capacity) {
     Node node = this.root;
     for (int at = from; at < thru; at += 1) {
       node = node.vet(kim.get(at));
     }
     if (node.integer == none) {
       int integer = this.length;
       node.integer = integer;
       this.uses[integer] = 1;
       this.kims[integer] = kim;
       this.froms[integer] = from;
       this.thrus[integer] = thru;
       if (JSONzip.probe) {
         try {
           JSONzip.log("<<" + integer + " " + new Kim(kim, from, thru) + ">> ");
         } catch (Throwable ignore) {
         }
       }
       this.length += 1;
       return integer;
     }
   }
   return none;
 }
Example #4
0
 /**
  * Write the bits corresponding to a symbol. The weight of the symbol will be incremented.
  *
  * @param value The number of the symbol to write
  * @param bitwriter The destination of the bits.
  * @return this
  * @throws JSONException
  */
 public void write(int value, BitWriter bitwriter) throws JSONException {
   this.width = 0;
   write(this.symbols[value], bitwriter);
   tick(value);
   if (JSONzip.probe) {
     JSONzip.logchar(value, this.width);
   }
 }
Example #5
0
 /**
  * Register a value in the keep. Compact the keep if it is full. The next time this value is
  * encountered, its integer can be sent instead.
  *
  * @param value A value.
  */
 public void register(Object value) {
   if (JSONzip.probe) {
     int integer = find(value);
     if (integer >= 0) {
       JSONzip.log("\nDuplicate key " + value);
     }
   }
   if (this.length >= this.capacity) {
     compact();
   }
   this.list[this.length] = value;
   this.map.put(value, new Integer(this.length));
   this.uses[this.length] = 1;
   if (JSONzip.probe) {
     JSONzip.log("<" + this.length + " " + value + "> ");
   }
   this.length += 1;
 }
Example #6
0
 /**
  * Set a node's link to another node.
  *
  * @param cell An integer between 0 and 255.
  * @param node The new value for the cell.
  */
 public void set(int cell, Node node) {
   if (this.next == null) {
     this.next = new Node[256];
   }
   if (JSONzip.probe) {
     if (node == null || this.next[cell] != null) {
       JSONzip.log("\nUnexpected set.\n");
     }
   }
   this.next[cell] = node;
 }
Example #7
0
 public boolean postMortem(PostMortem pm) {
   boolean result = true;
   TrieKeep that = (TrieKeep) pm;
   if (this.length != that.length) {
     JSONzip.log("\nLength " + this.length + " <> " + that.length);
     return false;
   }
   if (this.capacity != that.capacity) {
     JSONzip.log("\nCapacity " + this.capacity + " <> " + that.capacity);
     return false;
   }
   for (int i = 0; i < this.length; i += 1) {
     Kim thiskim = this.kim(i);
     Kim thatkim = that.kim(i);
     if (!thiskim.equals(thatkim)) {
       JSONzip.log("\n[" + i + "] " + thiskim + " <> " + thatkim);
       result = false;
     }
   }
   return result && this.root.postMortem(that.root);
 }
Example #8
0
 public boolean postMortem(PostMortem pm) {
   MapKeep that = (MapKeep) pm;
   if (this.length != that.length) {
     JSONzip.log(this.length + " <> " + that.length);
     return false;
   }
   for (int i = 0; i < this.length; i += 1) {
     boolean b;
     if (this.list[i] instanceof Kim) {
       b = ((Kim) this.list[i]).equals(that.list[i]);
     } else {
       Object o = this.list[i];
       Object q = that.list[i];
       if (o instanceof Number) {
         o = o.toString();
       }
       if (q instanceof Number) {
         q = q.toString();
       }
       b = o.equals(q);
     }
     if (!b) {
       JSONzip.log(
           "\n["
               + i
               + "]\n "
               + this.list[i]
               + "\n "
               + that.list[i]
               + "\n "
               + this.uses[i]
               + "\n "
               + that.uses[i]);
       return false;
     }
   }
   return true;
 }
Example #9
0
 /**
  * Read bits until a symbol can be identified. The weight of the read symbol will be incremented.
  *
  * @param bitreader The source of bits.
  * @return The integer value of the symbol.
  * @throws JSONException
  */
 public int read(BitReader bitreader) throws JSONException {
   try {
     this.width = 0;
     Symbol symbol = this.table;
     while (symbol.integer == none) {
       this.width += 1;
       symbol = bitreader.bit() ? symbol.one : symbol.zero;
     }
     tick(symbol.integer);
     if (JSONzip.probe) {
       JSONzip.logchar(symbol.integer, this.width);
     }
     return symbol.integer;
   } catch (Throwable e) {
     throw new JSONException(e);
   }
 }
Example #10
0
 public void registerMany(Kim kim) {
   int length = kim.length;
   int limit = this.capacity - this.length;
   if (limit > JSONzip.substringLimit) {
     limit = JSONzip.substringLimit;
   }
   int until = length - (JSONzip.minSubstringLength - 1);
   for (int from = 0; from < until; from += 1) {
     int len = length - from;
     if (len > JSONzip.maxSubstringLength) {
       len = JSONzip.maxSubstringLength;
     }
     len += from;
     Node node = this.root;
     for (int at = from; at < len; at += 1) {
       Node next = node.vet(kim.get(at));
       if (next.integer == none && at - from >= (JSONzip.minSubstringLength - 1)) {
         next.integer = this.length;
         this.uses[this.length] = 1;
         this.kims[this.length] = kim;
         this.froms[this.length] = from;
         this.thrus[this.length] = at + 1;
         if (JSONzip.probe) {
           try {
             JSONzip.log("<<" + this.length + " " + new Kim(kim, from, at + 1) + ">> ");
           } catch (Throwable ignore) {
           }
         }
         this.length += 1;
         limit -= 1;
         if (limit <= 0) {
           return;
         }
       }
       node = next;
     }
   }
 }