KeyedList.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // System.Web.UI/KeyedList.cs
  3. //
  4. // Author: Todd Berman <[email protected]>
  5. //
  6. // (C) 2003 Todd Berman
  7. #if NET_1_2
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. namespace System.Web.UI
  11. {
  12. public class KeyedList : IOrderedDictionary, IStateManager
  13. {
  14. private Hashtable objectTable = new Hashtable ();
  15. private ArrayList objectList = new ArrayList ();
  16. public void Add (object key, object value)
  17. {
  18. objectTable.Add (key, value);
  19. objectList.Add (new DictionaryEntry (key, value));
  20. }
  21. public void Clear ()
  22. {
  23. objectTable.Clear ();
  24. objectList.Clear ();
  25. }
  26. public bool Contains (object key)
  27. {
  28. return objectTable.Contains (key);
  29. }
  30. public void CopyTo (Array array, int idx)
  31. {
  32. objectTable.CopyTo (array, idx);
  33. }
  34. public void Insert (int idx, object key, object value)
  35. {
  36. if (idx > Count)
  37. throw new ArgumentOutOfRangeException ("index");
  38. objectTable.Add (key, value);
  39. objectList.Insert (idx, new DictionaryEntry (key, value));
  40. }
  41. public void Remove (object key)
  42. {
  43. objectTable.Remove (key);
  44. objectList.RemoveAt (IndexOf (key));
  45. }
  46. public void RemoveAt (int idx)
  47. {
  48. if (idx >= Count)
  49. throw new ArgumentOutOfRangeException ("index");
  50. objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
  51. objectList.RemoveAt (idx);
  52. }
  53. private IDictionaryEnumerator IDictionary.GetEnumerator ()
  54. {
  55. return new KeyedListEnumerator (objectList);
  56. }
  57. private IEnumerator IEnumerable.GetEnumerator ()
  58. {
  59. return new KeyedListEnumerator (objectList);
  60. }
  61. private void IStateManager.LoadViewState (object state)
  62. {
  63. if (state != null)
  64. {
  65. object[] states = (object[]) state;
  66. if (states[0] != null) {
  67. objectList = (ArrayList) states[0];
  68. for (int i = 0; i < objectList.Count; i++)
  69. {
  70. DictionaryEntry pair = (DictionaryEntry) objectList[i];
  71. objectTable.Add (pair.Key, pair.Value);
  72. }
  73. }
  74. }
  75. }
  76. private object IStateManager.SaveViewState ()
  77. {
  78. object[] ret = new object[] { objectList };
  79. if (ret[0] == null)
  80. return null;
  81. return ret;
  82. }
  83. private void IStateManager.TrackViewState ()
  84. {
  85. trackViewState = true;
  86. }
  87. public int Count {
  88. get { return objectList.Count; }
  89. }
  90. public bool IsFixedSize {
  91. get { return false; }
  92. }
  93. public bool IsReadOnly {
  94. get { return false; }
  95. }
  96. public bool IsSynchronized {
  97. get { return false; }
  98. }
  99. public object this[int idx] {
  100. get { return ((DictionaryEntry) objectList[idx]).Value; }
  101. set {
  102. if (idx < 0 || idx >= Count)
  103. throw new ArgumentOutOfRangeException ("index");
  104. object key = ((DictionaryEntry) objectList[idx]).Key;
  105. objectList[idx] = new DictionaryEntry (key, value);
  106. objectTable[key] = value;
  107. }
  108. }
  109. public object this[object key] {
  110. get { return objectTable[key]; }
  111. set {
  112. if (objectTable.Contains (key))
  113. {
  114. objectTable[key] = value;
  115. objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
  116. return;
  117. }
  118. Add (key, value);
  119. }
  120. }
  121. public ICollection Keys {
  122. get {
  123. ArrayList retList = new ArrayList ();
  124. for (int i = 0; i < objectList.Count; i++)
  125. {
  126. retList.Add ( ((DictionaryEntry)objectList[i]).Key );
  127. }
  128. return retList;
  129. }
  130. }
  131. public ICollection Values {
  132. get {
  133. ArrayList retList = new ArrayList ();
  134. for (int i = 0; i < objectList.Count; i++)
  135. {
  136. retList.Add ( ((DictionaryEntry)objectList[i]).Value );
  137. }
  138. return retList;
  139. }
  140. }
  141. public object SyncRoot {
  142. get { return this; }
  143. }
  144. private bool trackViewState;
  145. private bool IStateManager.IsTrackingViewState {
  146. get { return trackViewState; }
  147. }
  148. private int IndexOf (object key)
  149. {
  150. for (int i = 0; i < objectList.Count; i++)
  151. {
  152. if (((DictionaryEntry) objectList[i]).Key.Equals (key))
  153. {
  154. return i;
  155. }
  156. }
  157. return -1;
  158. }
  159. }
  160. }
  161. #endif