package me.hugmanrique.pokedata.compression.huff;

import me.hugmanrique.pokedata.utils.Pair;

/**
 * @author Hugmanrique
 * @since 29/05/2017
 */
public class HuffTreeNode {
    static int maxInPos = 0;
    HuffTreeNode node0, nod1;
    int data = -1; // [-1, 0xFF]

    Pair<Boolean, Integer> getValue(NLinked)
}

@@ -79,18 +79,106 @@
 ata.
-utils.Pair
+compression.HexInputStream;%0Aimport me.hugmanrique.pokedata.utils.Pair;%0A%0Aimport java.io.IOException
 ;%0A%0A/
@@ -312,16 +312,17 @@
 de0, nod
+e
 1;%0A    i
@@ -390,15 +390,2179 @@
 lue(
-NLinked)
+LinkedListNode%3CInteger%3E code) throws Exception %7B%0A        Pair%3CBoolean, Integer%3E out = new Pair%3C%3E();%0A        out.setSecond(data);%0A%0A        if (code == null) %7B%0A            out.setFirst(node0 == null && node1 == null && data %3E= 0);%0A            return out;%0A        %7D%0A%0A        if (code.getValue() %3E 1) %7B%0A            throw new Exception(%22List should be a list of bytes ( %3C 2 ). Got %22 + code.getValue() + %22 instead%22);%0A        %7D%0A%0A        int c = code.getValue();%0A        HuffTreeNode node = (c == 0 ? node0 : node1);%0A%0A        if (node == null) %7B%0A            out.setFirst(false);%0A        %7D%0A%0A        // TODO Check if we need to throw this NPE%0A        return node.getValue(code.getPrevious());%0A    %7D%0A%0A    protected void parseData(HexInputStream stream) throws IOException %7B%0A        /*%0A%09%09 * Tree Table (list of 8bit nodes, starting with the root node) Root%0A%09%09 * Node and Non-Data-Child Nodes are: Bit0-5 Offset to next child node,%0A%09%09 * Next child node0 is at (CurrentAddr AND NOT 1)+Offset*2+2 Next child%0A%09%09 * node1 is at (CurrentAddr AND NOT 1)+Offset*2+2+1 Bit6 Node1 End Flag%0A%09%09 * (1=Next child node is data) Bit7 Node0 End Flag (1=Next child node is%0A%09%09 * data) Data nodes are (when End Flag was set in parent node): Bit0-7%0A%09%09 * Data (upper bits should be zero if Data Size is less than 8)%0A%09%09 */%0A        node0 = new HuffTreeNode();%0A        node1 = new HuffTreeNode();%0A        long pos = stream.getPos();%0A        int b = stream.readU8();%0A        long offset = b & 0x3F;%0A        boolean end0 = (b & 0x80) %3E 0;%0A        boolean end1 = (b & 0x40) %3E 0;%0A%0A        // Parse data for node0 and node1%0A        stream.setPosition((pos - (pos & 1)) + offset * 2 + 2);%0A        parseData(node0, stream, end0, maxInPos);%0A%0A        stream.setPosition((pos - (pos & 1)) + offset * 2 + 2 + 1);%0A        parseData(node1, stream, end1, maxInPos);%0A%0A        stream.setPosition(pos);%0A    %7D%0A%0A    private void parseData(HuffTreeNode node, HexInputStream stream, boolean end, int maxInPos) throws IOException %7B%0A        if (stream.getPos() %3C maxInPos) %7B%0A            if (end) %7B%0A                node.data = stream.readU8();%0A            %7D else %7B%0A                node.parseData(stream);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Optimize up HuffTreeNode