|
import java.util.Hashtable;
|
|
public class LZW
|
{
|
|
int count;
|
|
LZW()
|
{
|
inputDict = new Hashtable();
|
outputDict = new Hashtable();
|
|
w = new StringBuffer("");
|
v = new StringBuffer("");
|
|
count = 0;
|
}
|
|
/**/
|
public int Encode(char k) // throws DoNotSend
|
{
|
int temp;
|
String s = "" + w + k;
|
if (inputDict.containsKey(s))
|
{
|
w = w.append(k);
|
// throw new DoNotSend();
|
return -1;
|
/*
|
Integer grrr = (Integer) inputDict.put(s,s);
|
inputDict.put(grrr,s);
|
return grrr.intValue();
|
*/
|
} else
|
{
|
Integer val = new Integer(count);
|
inputDict.put(s, val);
|
outputDict.put(val, s);
|
w = new StringBuffer("" + k);
|
return count++;
|
/*
|
Integer K = (Integer)inputDict.get("" + w);
|
temp = K.intValue();
|
return temp;
|
*/
|
}
|
}
|
|
public String Decode(int k)
|
{
|
Integer K = new Integer(k);
|
return (String)outputDict.get(K);
|
|
/*
|
String entry;
|
|
if (v.length() == 0)
|
{
|
v = new StringBuffer((String)outputDict.get(new Integer(k)));
|
return (v.toString());
|
} else
|
{
|
entry = new String((String)outputDict.get(K));
|
|
if (entry.length() == 0)
|
{ // remove this conditional to cause LZW bug
|
entry = new String(v.toString() + v.charAt(0));
|
}
|
|
outputDict.put(K, "" + v + entry.charAt(0));
|
|
v = new StringBuffer(entry);
|
return entry;
|
}
|
*/
|
}
|
|
Hashtable inputDict;
|
Hashtable outputDict;
|
StringBuffer w;
|
StringBuffer v;
|
}
|
|
class DoNotSend extends Exception
|
{
|
}
|