#undef debug
#define debugdisplay
#undef debugdictionary
using system;
using system.collections;
namespace lzw
{
public class clzw
{
#region constrcut
public clzw()
{
}
#endregion
#region coding
public string incharstream
{
set { _incharstream = value; }
get {return _incharstream; }
}
public arraylist codingcodestream
{
get {return _codingcodestream;}
}
public arraylist codingdictionary
{
get {return _codingdictionary;}
}
private void initcodingdictionary()
{
_codingdictionary.clear();
#if debug
_codingdictionary.add("a");
_codingdictionary.add("b");
_codingdictionary.add("c");
#else
for(int i = 0; i < 256; i++)
{
_codingdictionary.add((char)i);
}
#endif
}
private void addcodingdictionary(object str)
{
_codingdictionary.add(str);
}
private void addcodingcodestream(object str)
{
_codingcodestream.add(str);
}
private bool isincodingdictionary(string prefix)
{
bool result = false;
int count = _codingdictionary.count;
for(int i = 0; i < count; i++)
{
string temp = _codingdictionary[i].tostring();
if (temp.indexof(prefix) >= 0)
{
result = true;
break;
}
}
return result;
}
private string getindexcodingdictionary(string prefix)
{
string result ="0";
int count = _codingdictionary.count;
for(int i = 0; i < count; i++)
{
string temp = _codingdictionary[i].tostring();
if (temp.indexof(prefix) >= 0)
{
result = convert.tostring(i + 1);
break;
}
}
return result;
}
private void displaycodingcodestream()
{
system.console.writeline("*********_codingcodestream************");
for(int i = 0; i < _codingcodestream.count; i++)
{
system.console.writeline(_codingcodestream[i].tostring());
}
}
private void displaycodingdictionary()
{
system.console.writeline("*********_codingdictionary************");
for(int i = 0; i < _codingdictionary.count; i++)
{
system.console.writeline(_codingdictionary[i].tostring());
}
}
private void displayincharstream()
{
system.console.writeline("*********_incharstream************");
system.console.writeline(_incharstream);
}
private void initcodingcodestream()
{
_codingcodestream.clear();
}
private arraylist _codingdictionary = new arraylist();
private string _incharstream = "";
private arraylist _codingcodestream = new arraylist();
public void coding()
{
string prefix ="" ;
string c ="";
string prefixindex= "0";
int count = _incharstream.length;
if (count == 0) return ;
initcodingdictionary();
initcodingcodestream();
prefix = _incharstream[0].tostring();
for(int i = 1; i < count; i++)
{
c = _incharstream[i].tostring();
if (isincodingdictionary( prefix + c))
{
prefix += c;
}
else
{
prefixindex = getindexcodingdictionary(prefix);
addcodingcodestream(prefixindex);
addcodingdictionary( prefix + c);
prefix = c;
}
}
prefixindex = getindexcodingdictionary(prefix);
addcodingcodestream(prefixindex);
#if debugdisplay
displayincharstream();
displaycodingcodestream();
#if debugdictionary
displaycodingdictionary();
#endif
#endif
}
#endregion
#region decode
private arraylist _decodedictionary = new arraylist();
private arraylist _outcharstream = new arraylist();
private int[] _decodecodestream ;
public void setdecodescodetream(int[] obj)
{
int count = obj.length;
_decodecodestream = new int[count];
for(int i =0; i < count ; i++)
{
_decodecodestream[i] = obj[i];
}
}
public void setdecodescodetream(arraylist obj)
{
int count = obj.count;
_decodecodestream = new int[count];
for(int i =0; i < count ; i++)
{
_decodecodestream[i] = system.convert.toint32(obj[i]);
}
}
public int[] getdecodecodestream()
{
return _decodecodestream;
}
public string outcharstream
{
get
{
string result = "";
for(int i = 0,count = _outcharstream.count; i < count; i++)
{
result += _outcharstream[i].tostring();
}
return result;
}
}
public arraylist decodedictionary
{
get
{
return _decodedictionary;
}
}
private void initdecodedictionary()
{
_decodedictionary.clear();
#if debug
_decodedictionary.add("a");
_decodedictionary.add("b");
_decodedictionary.add("c");
#else
for(int i = 0; i < 256; i++)
{
_decodedictionary.add((char)i);
}
#endif
}
private void initoutcharstream()
{
_outcharstream.clear();
}
private void displayoutcharstream()
{
system.console.writeline("*********_outcharstream************");
string temp = "";
for(int i = 0; i < _outcharstream.count; i++)
{
temp = temp + (_outcharstream[i].tostring());
}
system.console.writeline(temp);
}
private void displaydecodedictionary()
{
system.console.writeline("*********_decodedictionary************");
for(int i = 0; i < _decodedictionary.count; i++)
{
system.console.writeline(_decodedictionary[i].tostring());
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器