.NET 实现Hashlist--可以排序的哈希表

来源:互联网 发布:java排序算法大到小 编辑:程序博客网 时间:2024/06/05 22:42
  1. /// <summary>
  2.     /// 
  3.     /// </summary>
  4.     public abstract class Hashlist : IDictionary, IEnumerable
  5.     {
  6.         //array list that contains all the keys 
  7.         //as they are inserted, the index is associated with
  8.         //a key so when pulling out values by index
  9.         //we can get the key for the index, pull from the 
  10.         //hashtable the proper value with the corresponding 
  11.         //key
  12.         //This is basically the same as a sorted list but
  13.         //does not sort the items, rather it leaves them in the
  14.         //order they were inserted - like a list
  15.         /// <summary>
  16.         /// 
  17.         /// </summary>
  18.         protected ArrayList m_oKeys = new ArrayList();
  19.         /// <summary>
  20.         /// 
  21.         /// </summary>
  22.         protected Hashtable m_oValues = new Hashtable();        
  23.         #region ICollection implementation
  24.         //ICollection implementation
  25.         /// <summary>
  26.         /// 
  27.         /// </summary>
  28.         public int Count
  29.         {
  30.             get{return m_oValues.Count;}
  31.         }
  32.         /// <summary>
  33.         /// 
  34.         /// </summary>
  35.         public bool IsSynchronized
  36.         {
  37.             get{return m_oValues.IsSynchronized;}
  38.         }
  39.         /// <summary>
  40.         /// 
  41.         /// </summary>
  42.         public object SyncRoot
  43.         {
  44.             get{return m_oValues.SyncRoot;}
  45.         }
  46.         /// <summary>
  47.         /// 
  48.         /// </summary>
  49.         /// <param name="oArray"></param>
  50.         /// <param name="iArrayIndex"></param>
  51.         public void CopyTo(System.Array oArray, int iArrayIndex)
  52.         {
  53.             m_oValues.CopyTo(oArray, iArrayIndex);
  54.         }
  55.         #endregion
  56.         #region IDictionary implementation
  57.         //IDictionary implementation
  58.         /// <summary>
  59.         /// 
  60.         /// </summary>
  61.         /// <param name="oKey"></param>
  62.         /// <param name="oValue"></param>
  63.         public void Add(object oKey, object oValue)
  64.         {
  65.             m_oKeys.Add(oKey);
  66.             m_oValues.Add(oKey, oValue);
  67.         }
  68.         /// <summary>
  69.         /// 
  70.         /// </summary>
  71.         public bool IsFixedSize
  72.         {
  73.             get{return m_oKeys.IsFixedSize;}
  74.         }
  75.         /// <summary>
  76.         /// 
  77.         /// </summary>
  78.         public bool IsReadOnly
  79.         {
  80.             get{return m_oKeys.IsReadOnly;}
  81.         }
  82.         /// <summary>
  83.         /// 
  84.         /// </summary>
  85.         public ICollection Keys
  86.         {
  87.             get{return m_oValues.Keys;}
  88.         }
  89.         /// <summary>
  90.         /// 
  91.         /// </summary>
  92.         public void Clear()
  93.         {
  94.             m_oValues.Clear();
  95.             m_oKeys.Clear();
  96.         }
  97.         /// <summary>
  98.         /// 
  99.         /// </summary>
  100.         /// <param name="oKey"></param>
  101.         /// <returns></returns>
  102.         public bool Contains(object oKey)
  103.         {
  104.             return m_oValues.Contains(oKey);
  105.         }
  106.         /// <summary>
  107.         /// 
  108.         /// </summary>
  109.         /// <param name="oKey"></param>
  110.         /// <returns></returns>
  111.         public bool ContainsKey(object oKey)
  112.         {
  113.             return m_oValues.ContainsKey(oKey);
  114.         }
  115.         /// <summary>
  116.         /// 
  117.         /// </summary>
  118.         /// <returns></returns>
  119.         public IDictionaryEnumerator GetEnumerator()
  120.         {
  121.             return m_oValues.GetEnumerator();
  122.         }   
  123.         /// <summary>
  124.         /// 
  125.         /// </summary>
  126.         /// <param name="oKey"></param>
  127.         public void Remove(object oKey)
  128.         {
  129.             m_oValues.Remove(oKey);
  130.             m_oKeys.Remove(oKey);
  131.         }
  132.         /// <summary>
  133.         /// 
  134.         /// </summary>
  135.         public object this[object oKey]
  136.         {
  137.             get{return m_oValues[oKey];}
  138.             set{m_oValues[oKey] = value;}
  139.         }
  140.         /// <summary>
  141.         /// 
  142.         /// </summary>
  143.         public ICollection Values
  144.         {
  145.             get{return m_oValues.Values;}
  146.         }
  147.         #endregion
  148.         #region IEnumerable implementation
  149.         IEnumerator IEnumerable.GetEnumerator()
  150.         {
  151.             return m_oValues.GetEnumerator();
  152.         }
  153.         #endregion
  154.         
  155.         #region Hashlist specialized implementation
  156.         //specialized indexer routines
  157.         /// <summary>
  158.         /// 
  159.         /// </summary>
  160.         public object this[string Key]
  161.         {
  162.             get{return m_oValues[Key];}
  163.         }
  164.         /// <summary>
  165.         /// 
  166.         /// </summary>
  167.         public object this[int Index]
  168.         {
  169.             get{return m_oValues[m_oKeys[Index]];}
  170.         }
  171.         #endregion
  172.     
  173.     }
原创粉丝点击