KeyedList.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. using System.Collections;
  28. using System.Collections.Specialized;
  29. namespace System.Web.UI
  30. {
  31. class KeyedList : IOrderedDictionary
  32. {
  33. Hashtable objectTable = new Hashtable ();
  34. ArrayList objectList = new ArrayList ();
  35. public void Add (object key, object value)
  36. {
  37. objectTable.Add (key, value);
  38. objectList.Add (new DictionaryEntry (key, value));
  39. }
  40. public void Clear ()
  41. {
  42. objectTable.Clear ();
  43. objectList.Clear ();
  44. }
  45. public bool Contains (object key)
  46. {
  47. return objectTable.Contains (key);
  48. }
  49. public void CopyTo (Array array, int idx)
  50. {
  51. objectTable.CopyTo (array, idx);
  52. }
  53. public void Insert (int idx, object key, object value)
  54. {
  55. if (idx > Count)
  56. throw new ArgumentOutOfRangeException ("index");
  57. objectTable.Add (key, value);
  58. objectList.Insert (idx, new DictionaryEntry (key, value));
  59. }
  60. public void Remove (object key)
  61. {
  62. objectTable.Remove (key);
  63. int index = IndexOf (key);
  64. if (index >= 0)
  65. objectList.RemoveAt (index);
  66. }
  67. public void RemoveAt (int idx)
  68. {
  69. if (idx >= Count)
  70. throw new ArgumentOutOfRangeException ("index");
  71. objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
  72. objectList.RemoveAt (idx);
  73. }
  74. IDictionaryEnumerator IDictionary.GetEnumerator ()
  75. {
  76. return new KeyedListEnumerator (objectList);
  77. }
  78. IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
  79. {
  80. return new KeyedListEnumerator (objectList);
  81. }
  82. IEnumerator IEnumerable.GetEnumerator ()
  83. {
  84. return new KeyedListEnumerator (objectList);
  85. }
  86. public int Count {
  87. get { return objectList.Count; }
  88. }
  89. public bool IsFixedSize {
  90. get { return false; }
  91. }
  92. public bool IsReadOnly {
  93. get { return false; }
  94. }
  95. public bool IsSynchronized {
  96. get { return false; }
  97. }
  98. public object this[int idx] {
  99. get { return ((DictionaryEntry) objectList[idx]).Value; }
  100. set {
  101. if (idx < 0 || idx >= Count)
  102. throw new ArgumentOutOfRangeException ("index");
  103. object key = ((DictionaryEntry) objectList[idx]).Key;
  104. objectList[idx] = new DictionaryEntry (key, value);
  105. objectTable[key] = value;
  106. }
  107. }
  108. public object this[object key] {
  109. get { return objectTable[key]; }
  110. set {
  111. if (objectTable.Contains (key))
  112. {
  113. objectTable[key] = value;
  114. objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
  115. return;
  116. }
  117. Add (key, value);
  118. }
  119. }
  120. public ICollection Keys {
  121. get {
  122. ArrayList retList = new ArrayList ();
  123. for (int i = 0; i < objectList.Count; i++)
  124. {
  125. retList.Add ( ((DictionaryEntry)objectList[i]).Key );
  126. }
  127. return retList;
  128. }
  129. }
  130. public ICollection Values {
  131. get {
  132. ArrayList retList = new ArrayList ();
  133. for (int i = 0; i < objectList.Count; i++)
  134. {
  135. retList.Add ( ((DictionaryEntry)objectList[i]).Value );
  136. }
  137. return retList;
  138. }
  139. }
  140. public object SyncRoot {
  141. get { return this; }
  142. }
  143. int IndexOf (object key)
  144. {
  145. for (int i = 0; i < objectList.Count; i++)
  146. {
  147. if (((DictionaryEntry) objectList[i]).Key.Equals (key))
  148. {
  149. return i;
  150. }
  151. }
  152. return -1;
  153. }
  154. }
  155. }