KeyedList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // System.Web.UI/KeyedList.cs
  3. //
  4. // Author: Todd Berman <[email protected]>
  5. //
  6. // (C) 2003 Todd Berman
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. #if NET_2_0
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. namespace System.Web.UI
  31. {
  32. public class KeyedList : IOrderedDictionary, IStateManager
  33. {
  34. private Hashtable objectTable = new Hashtable ();
  35. private ArrayList objectList = new ArrayList ();
  36. public void Add (object key, object value)
  37. {
  38. objectTable.Add (key, value);
  39. objectList.Add (new DictionaryEntry (key, value));
  40. }
  41. public void Clear ()
  42. {
  43. objectTable.Clear ();
  44. objectList.Clear ();
  45. }
  46. public bool Contains (object key)
  47. {
  48. return objectTable.Contains (key);
  49. }
  50. public void CopyTo (Array array, int idx)
  51. {
  52. objectTable.CopyTo (array, idx);
  53. }
  54. public void Insert (int idx, object key, object value)
  55. {
  56. if (idx > Count)
  57. throw new ArgumentOutOfRangeException ("index");
  58. objectTable.Add (key, value);
  59. objectList.Insert (idx, new DictionaryEntry (key, value));
  60. }
  61. public void Remove (object key)
  62. {
  63. objectTable.Remove (key);
  64. int index = IndexOf (key);
  65. if (index >= 0)
  66. objectList.RemoveAt (index);
  67. }
  68. public void RemoveAt (int idx)
  69. {
  70. if (idx >= Count)
  71. throw new ArgumentOutOfRangeException ("index");
  72. objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
  73. objectList.RemoveAt (idx);
  74. }
  75. IDictionaryEnumerator IDictionary.GetEnumerator ()
  76. {
  77. return new KeyedListEnumerator (objectList);
  78. }
  79. IEnumerator IEnumerable.GetEnumerator ()
  80. {
  81. return new KeyedListEnumerator (objectList);
  82. }
  83. void IStateManager.LoadViewState (object state)
  84. {
  85. if (state != null)
  86. {
  87. object[] states = (object[]) state;
  88. if (states[0] != null) {
  89. objectList = (ArrayList) states[0];
  90. for (int i = 0; i < objectList.Count; i++)
  91. {
  92. DictionaryEntry pair = (DictionaryEntry) objectList[i];
  93. objectTable.Add (pair.Key, pair.Value);
  94. }
  95. }
  96. }
  97. }
  98. object IStateManager.SaveViewState ()
  99. {
  100. object[] ret = new object[] { objectList };
  101. if (ret[0] == null)
  102. return null;
  103. return ret;
  104. }
  105. void IStateManager.TrackViewState ()
  106. {
  107. trackViewState = true;
  108. }
  109. public int Count {
  110. get { return objectList.Count; }
  111. }
  112. public bool IsFixedSize {
  113. get { return false; }
  114. }
  115. public bool IsReadOnly {
  116. get { return false; }
  117. }
  118. public bool IsSynchronized {
  119. get { return false; }
  120. }
  121. public object this[int idx] {
  122. get { return ((DictionaryEntry) objectList[idx]).Value; }
  123. set {
  124. if (idx < 0 || idx >= Count)
  125. throw new ArgumentOutOfRangeException ("index");
  126. object key = ((DictionaryEntry) objectList[idx]).Key;
  127. objectList[idx] = new DictionaryEntry (key, value);
  128. objectTable[key] = value;
  129. }
  130. }
  131. public object this[object key] {
  132. get { return objectTable[key]; }
  133. set {
  134. if (objectTable.Contains (key))
  135. {
  136. objectTable[key] = value;
  137. objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
  138. return;
  139. }
  140. Add (key, value);
  141. }
  142. }
  143. public ICollection Keys {
  144. get {
  145. ArrayList retList = new ArrayList ();
  146. for (int i = 0; i < objectList.Count; i++)
  147. {
  148. retList.Add ( ((DictionaryEntry)objectList[i]).Key );
  149. }
  150. return retList;
  151. }
  152. }
  153. public ICollection Values {
  154. get {
  155. ArrayList retList = new ArrayList ();
  156. for (int i = 0; i < objectList.Count; i++)
  157. {
  158. retList.Add ( ((DictionaryEntry)objectList[i]).Value );
  159. }
  160. return retList;
  161. }
  162. }
  163. public object SyncRoot {
  164. get { return this; }
  165. }
  166. private bool trackViewState;
  167. bool IStateManager.IsTrackingViewState {
  168. get { return trackViewState; }
  169. }
  170. private int IndexOf (object key)
  171. {
  172. for (int i = 0; i < objectList.Count; i++)
  173. {
  174. if (((DictionaryEntry) objectList[i]).Key.Equals (key))
  175. {
  176. return i;
  177. }
  178. }
  179. return -1;
  180. }
  181. }
  182. }
  183. #endif