C # Implementation of LZW algorithm

Source: Internet
Author: User
Tags bool readline tostring
Algorithm

#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 ());
}

}
private void Displaydecodecodestream ()
{
System.Console.WriteLine ("*********_decodecodestream************");
int count = _decodecodestream.length;
for (int i = 0; i < count; i++)
{
System.Console.WriteLine ("{0}", _decodecodestream[i]);
}
}
private void Addoutcharstream (Object str)
{
_outcharstream.add (str);
}
private void Adddecodedictionary (Object str)
{
_decodedictionary.add (str);
}
private bool Isindecodedictionary (int cw)
{
BOOL result = FALSE;
int count = _decodedictionary.count;
if (CW <= count-1)
{
result = true;
}
return result;
}
public void Decode ()
{
Initdecodedictionary ();
Initoutcharstream ();
int cw = 0;
int pw = 0;
String Prefix = "";
String C= "";
CW = _decodecodestream[0]-1;
This. Addoutcharstream (THIS._DECODEDICTIONARY[CW]);
PW = CW;
int count = _decodecodestream.length;
if (count = 0) return;
for (int i = 1; i < count; i++)
{
CW = _decodecodestream[i]-1;
if (Isindecodedictionary (CW))
{
This. Addoutcharstream (THIS._DECODEDICTIONARY[CW]);
Prefix = THIS._DECODEDICTIONARY[PW]. ToString ();
c = (THIS._DECODEDICTIONARY[CW]. ToString ()) [0]. ToString ();
This. Adddecodedictionary (Prefix + c);
}
Else
{
Prefix = THIS._DECODEDICTIONARY[PW]. ToString ();
c = prefix[0]. ToString ();
This. Addoutcharstream (Prefix + c);
This. Adddecodedictionary (Prefix + c);
}
PW = CW;
}
#if Debugdisplay
Displayoutcharstream ();
Displaydecodecodestream ();
#if debugdictionary
Displaydecodedictionary ();
#endif
#endif
}
#endregion
}
}

#undef Debug
Using System;

Namespace LZW
{
Class Class1
{
[STAThread]
static void Main (string[] args)
{
CLZW LZW = new CLZW ();
#if Debug
Lzw. Incharstream = "ABBABABACCBBAAA";
#else
System.Console.WriteLine ("Enter the Tests Chararray [a-za-z0-9]:");
Lzw. Incharstream = System.Console.ReadLine ();
#endif
System.Console.WriteLine ("The coding ...");
Lzw. Coding ();
System.Console.WriteLine ("The DeCode ...");
Lzw. Setdecodescodetream (LZW. Codingcodestream);
Lzw. Decode ();
System.Console.ReadLine ();
}
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.