KeyedList.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. objectList.RemoveAt (IndexOf (key));
  65. }
  66. public void RemoveAt (int idx)
  67. {
  68. if (idx >= Count)
  69. throw new ArgumentOutOfRangeException ("index");
  70. objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
  71. objectList.RemoveAt (idx);
  72. }
  73. IDictionaryEnumerator IDictionary.GetEnumerator ()
  74. {
  75. return new KeyedListEnumerator (objectList);
  76. }
  77. IEnumerator IEnumerable.GetEnumerator ()
  78. {
  79. return new KeyedListEnumerator (objectList);
  80. }
  81. void IStateManager.LoadViewState (object state)
  82. {
  83. if (state != null)
  84. {
  85. object[] states = (object[]) state;
  86. if (states[0] != null) {
  87. objectList = (ArrayList) states[0];
  88. for (int i = 0; i < objectList.Count; i++)
  89. {
  90. DictionaryEntry pair = (DictionaryEntry) objectList[i];
  91. objectTable.Add (pair.Key, pair.Value);
  92. }
  93. }
  94. }
  95. }
  96. object IStateManager.SaveViewState ()
  97. {
  98. object[] ret = new object[] { objectList };
  99. if (ret[0] == null)
  100. return null;
  101. return ret;
  102. }
  103. void IStateManager.TrackViewState ()
  104. {
  105. trackViewState = true;
  106. }
  107. public int Count {
  108. get { return objectList.Count; }
  109. }
  110. public bool IsFixedSize {
  111. get { return false; }
  112. }
  113. public bool IsReadOnly {
  114. get { return false; }
  115. }
  116. public bool IsSynchronized {
  117. get { return false; }
  118. }
  119. public object this[int idx] {
  120. get { return ((DictionaryEntry) objectList[idx]).Value; }
  121. set {
  122. if (idx < 0 || idx >= Count)
  123. throw new ArgumentOutOfRangeException ("index");
  124. object key = ((DictionaryEntry) objectList[idx]).Key;
  125. objectList[idx] = new DictionaryEntry (key, value);
  126. objectTable[key] = value;
  127. }
  128. }
  129. public object this[object key] {
  130. get { return objectTable[key]; }
  131. set {
  132. if (objectTable.Contains (key))
  133. {
  134. objectTable[key] = value;
  135. objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
  136. return;
  137. }
  138. Add (key, value);
  139. }
  140. }
  141. public ICollection Keys {
  142. get {
  143. ArrayList retList = new ArrayList ();
  144. for (int i = 0; i < objectList.Count; i++)
  145. {
  146. retList.Add ( ((DictionaryEntry)objectList[i]).Key );
  147. }
  148. return retList;
  149. }
  150. }
  151. public ICollection Values {
  152. get {
  153. ArrayList retList = new ArrayList ();
  154. for (int i = 0; i < objectList.Count; i++)
  155. {
  156. retList.Add ( ((DictionaryEntry)objectList[i]).Value );
  157. }
  158. return retList;
  159. }
  160. }
  161. public object SyncRoot {
  162. get { return this; }
  163. }
  164. private bool trackViewState;
  165. bool IStateManager.IsTrackingViewState {
  166. get { return trackViewState; }
  167. }
  168. private int IndexOf (object key)
  169. {
  170. for (int i = 0; i < objectList.Count; i++)
  171. {
  172. if (((DictionaryEntry) objectList[i]).Key.Equals (key))
  173. {
  174. return i;
  175. }
  176. }
  177. return -1;
  178. }
  179. }
  180. }
  181. #endif