频道栏目
首页 > 程序开发 > Web开发 > ASP.Net > 正文
LZW算法的 C#实现
2006-07-14 07:04:14           
收藏   我要投稿

#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.Cle

点击复制链接 与好友分享!回本站首页
相关TAG标签 算法
上一篇:在VB.Net中创建使用控件数组
下一篇:在.NET上如何根据字符串动态创建控件
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站