首页 > 开发 > 综合 > 正文

LZW算法的 C#实现

2024-07-21 02:17:30
字体:
来源:转载
供稿:网友

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表