📄 myclass1.jsl
字号:
import java.util.ArrayList;
import java.util.LinkedList;
/** This class is only for Lempel-Ziv decompression. */
public class LempelZivTable
{
/* maximum of nodes */
private int maxNodes;
/* identity of current node */
private int currentId;
/* list of pairs of table, the indices are the ids of nodes */
private ArrayList pairs;
/**
* Instantiates the <code>LempelZivTable</code> object.
* @param maxNodes the maximum of nodes. It must be
* lower than <code>Integer.MAX_VALUE</code>.
*/
public LempelZivTable (int maxNodes)
{
this.maxNodes = maxNodes;
this.currentId = 1;
this.pairs = new ArrayList(maxNodes + 1);
// to calculate the bit string of a node,
// the first two elements must be 'null'
this.pairs.add(0, null);
this.pairs.add(1, null);
} // end of constructor LempelZivTable
/**
* Adds to the given <code>restBits</code> the bit string of
* the given complete <code>pair</code>.
* @param pair complete pair.
* @param restBits the buffered bits which could not be saved,
* because to less bits.
* @return bit string.
*/
public LinkedList getBitsOfPair (short pair, LinkedList restBits)
{
LinkedList result = new LinkedList();
Pair currentPair = new Pair(pair);
if (this.maxNodes > this.currentId)
{
this.currentId++;
this.pairs.add(this.currentId, currentPair);
}
do
{
result.addFirst(new Boolean(currentPair.bit));
currentPair = (Pair)this.pairs.get(currentPair.predecessorId);
} while (currentPair != null);
// add the given restBits in front of the pair bit list
result.addAll(0, restBits);
return result;
} // end of getBitsOfPair
/**
* Adds to the given <code>restBits</code> the bit string of
* the given <code>nodeId</code> (last pair).
* @param nodeId positive node identity of uncomplete pair.
* @param restBits the buffered bits which could not be saved,
* because to less bits.
* @return bit string.
*/
public LinkedList getBitsOfNode (int nodeId, LinkedList restBits)
{
LinkedList result = new LinkedList();
Pair currentPair = (Pair)this.pairs.get(nodeId);
while (currentPair != null)
{
result.addFirst(new Boolean(currentPair.bit));
currentPair = (Pair)this.pairs.get(currentPair.predecessorId);
}
// add the given restBits in front of the pair bit list
result.addAll(0, restBits);
return result;
} // end of getBitsOfNode
/* Element for ArrayList pair */
private class Pair
{
/* the own bit */
private boolean bit;
/* the predecessor of this pair */
private int predecessorId;
/* instantiates a pair element */
private Pair (short pair)
{
this.bit = pair < 0;
if (this.bit)
{
this.predecessorId = (int)(-pair);
}
else
{
this.predecessorId = (int)pair;
}
} // end of constructor Pair
} // end of private class Pair
} // end of public class LempelZivTable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -