PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System/System.Collections.Specialized/OrderedDictionary.cs

https://bitbucket.org/danipen/mono
C# | 393 lines | 306 code | 59 blank | 28 comment | 15 complexity | 1ac56b101ea8821ad5351c22cbcdcbc4 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Web.UI.WebControls.OrderedDictionary.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual (lluis@novell.com)
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Runtime.Serialization;
  29. namespace System.Collections.Specialized
  30. {
  31. [Serializable]
  32. public class OrderedDictionary : IOrderedDictionary, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
  33. {
  34. ArrayList list;
  35. Hashtable hash;
  36. bool readOnly;
  37. int initialCapacity;
  38. SerializationInfo serializationInfo;
  39. IEqualityComparer comparer;
  40. public OrderedDictionary ()
  41. {
  42. list = new ArrayList ();
  43. hash = new Hashtable ();
  44. }
  45. public OrderedDictionary (int capacity)
  46. {
  47. initialCapacity = (capacity < 0) ? 0 : capacity;
  48. list = new ArrayList (initialCapacity);
  49. hash = new Hashtable (initialCapacity);
  50. }
  51. public OrderedDictionary (IEqualityComparer comparer)
  52. {
  53. list = new ArrayList ();
  54. hash = new Hashtable (comparer);
  55. this.comparer = comparer;
  56. }
  57. public OrderedDictionary (int capacity, IEqualityComparer comparer)
  58. {
  59. initialCapacity = (capacity < 0) ? 0 : capacity;
  60. list = new ArrayList (initialCapacity);
  61. hash = new Hashtable (initialCapacity, comparer);
  62. this.comparer = comparer;
  63. }
  64. protected OrderedDictionary (SerializationInfo info, StreamingContext context)
  65. {
  66. serializationInfo = info;
  67. }
  68. protected virtual void OnDeserialization (object sender)
  69. {
  70. ((IDeserializationCallback) this).OnDeserialization (sender);
  71. }
  72. void IDeserializationCallback.OnDeserialization (object sender)
  73. {
  74. if (serializationInfo == null)
  75. return;
  76. comparer = (IEqualityComparer) serializationInfo.GetValue ("KeyComparer", typeof (IEqualityComparer));
  77. readOnly = serializationInfo.GetBoolean ("ReadOnly");
  78. initialCapacity = serializationInfo.GetInt32 ("InitialCapacity");
  79. if (list == null)
  80. list = new ArrayList ();
  81. else
  82. list.Clear ();
  83. hash = new Hashtable (comparer);
  84. object[] array = (object[]) serializationInfo.GetValue ("ArrayList", typeof(object[]));
  85. foreach (DictionaryEntry de in array) {
  86. hash.Add (de.Key, de.Value);
  87. list.Add (de);
  88. }
  89. }
  90. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  91. {
  92. if (info == null)
  93. throw new ArgumentNullException ("info");
  94. info.AddValue ("KeyComparer", comparer, typeof (IEqualityComparer));
  95. info.AddValue ("ReadOnly", readOnly);
  96. info.AddValue ("InitialCapacity", initialCapacity);
  97. object[] array = new object [hash.Count];
  98. hash.CopyTo (array, 0);
  99. info.AddValue ("ArrayList", array);
  100. }
  101. IEnumerator IEnumerable.GetEnumerator()
  102. {
  103. return list.GetEnumerator ();
  104. }
  105. public int Count {
  106. get {
  107. return list.Count;
  108. }
  109. }
  110. bool ICollection.IsSynchronized {
  111. get {
  112. return list.IsSynchronized;
  113. }
  114. }
  115. object ICollection.SyncRoot {
  116. get {
  117. return list.SyncRoot;
  118. }
  119. }
  120. public void CopyTo (Array array, int index)
  121. {
  122. list.CopyTo (array, index);
  123. }
  124. bool IDictionary.IsFixedSize {
  125. get {
  126. return false;
  127. }
  128. }
  129. public bool IsReadOnly
  130. {
  131. get {
  132. return readOnly;
  133. }
  134. }
  135. public object this [object key]
  136. {
  137. get { return hash [key]; }
  138. set {
  139. WriteCheck ();
  140. if (hash.Contains (key)) {
  141. int i = FindListEntry (key);
  142. list [i] = new DictionaryEntry (key, value);
  143. } else
  144. list.Add (new DictionaryEntry (key, value));
  145. hash [key] = value;
  146. }
  147. }
  148. public object this [int index]
  149. {
  150. get { return ((DictionaryEntry) list [index]).Value; }
  151. set {
  152. WriteCheck ();
  153. DictionaryEntry de = (DictionaryEntry) list [index];
  154. de.Value = value;
  155. // update (even on the list) isn't automatic
  156. list [index] = de;
  157. hash [de.Key] = value;
  158. }
  159. }
  160. public ICollection Keys
  161. {
  162. get {
  163. return new OrderedCollection (list, true);
  164. }
  165. }
  166. public ICollection Values
  167. {
  168. get {
  169. return new OrderedCollection (list, false);
  170. }
  171. }
  172. public void Add (object key, object value)
  173. {
  174. WriteCheck ();
  175. hash.Add (key, value);
  176. list.Add (new DictionaryEntry (key, value));
  177. }
  178. public void Clear()
  179. {
  180. WriteCheck ();
  181. hash.Clear ();
  182. list.Clear ();
  183. }
  184. public bool Contains (object key)
  185. {
  186. return hash.Contains (key);
  187. }
  188. public virtual IDictionaryEnumerator GetEnumerator()
  189. {
  190. return new OrderedEntryCollectionEnumerator (list.GetEnumerator ());
  191. }
  192. public void Remove (object key)
  193. {
  194. WriteCheck ();
  195. if (hash.Contains (key)) {
  196. hash.Remove (key);
  197. int i = FindListEntry (key);
  198. list.RemoveAt (i);
  199. }
  200. }
  201. int FindListEntry (object key)
  202. {
  203. for (int n=0; n<list.Count; n++) {
  204. DictionaryEntry de = (DictionaryEntry) list [n];
  205. if (comparer != null ? comparer.Equals(de.Key, key) : de.Key.Equals(key))
  206. return n;
  207. }
  208. return -1;
  209. }
  210. void WriteCheck ()
  211. {
  212. if (readOnly)
  213. throw new NotSupportedException ("Collection is read only");
  214. }
  215. public OrderedDictionary AsReadOnly ()
  216. {
  217. OrderedDictionary od = new OrderedDictionary ();
  218. od.list = list;
  219. od.hash = hash;
  220. od.comparer = comparer;
  221. od.readOnly = true;
  222. return od;
  223. }
  224. public void Insert (int index, object key, object value)
  225. {
  226. WriteCheck ();
  227. hash.Add (key, value);
  228. list.Insert (index, new DictionaryEntry (key, value));
  229. }
  230. public void RemoveAt (int index)
  231. {
  232. WriteCheck ();
  233. DictionaryEntry entry = (DictionaryEntry) list [index];
  234. list.RemoveAt (index);
  235. hash.Remove (entry.Key);
  236. }
  237. private class OrderedEntryCollectionEnumerator : IEnumerator, IDictionaryEnumerator
  238. {
  239. IEnumerator listEnumerator;
  240. public OrderedEntryCollectionEnumerator (IEnumerator listEnumerator)
  241. {
  242. this.listEnumerator = listEnumerator;
  243. }
  244. public bool MoveNext()
  245. {
  246. return listEnumerator.MoveNext ();
  247. }
  248. public void Reset()
  249. {
  250. listEnumerator.Reset ();
  251. }
  252. public object Current
  253. {
  254. get { return listEnumerator.Current; }
  255. }
  256. public DictionaryEntry Entry
  257. {
  258. get { return (DictionaryEntry) listEnumerator.Current; }
  259. }
  260. public object Key
  261. {
  262. get { return Entry.Key; }
  263. }
  264. public object Value
  265. {
  266. get { return Entry.Value; }
  267. }
  268. }
  269. private class OrderedCollection : ICollection
  270. {
  271. private ArrayList list;
  272. private bool isKeyList;
  273. public OrderedCollection (ArrayList list, bool isKeyList)
  274. {
  275. this.list = list;
  276. this.isKeyList = isKeyList;
  277. }
  278. public int Count {
  279. get {
  280. return list.Count;
  281. }
  282. }
  283. public bool IsSynchronized
  284. {
  285. get {
  286. return false;
  287. }
  288. }
  289. public object SyncRoot
  290. {
  291. get {
  292. return list.SyncRoot;
  293. }
  294. }
  295. public void CopyTo (Array array, int index)
  296. {
  297. for (int n=0; n<list.Count; n++) {
  298. DictionaryEntry de = (DictionaryEntry) list [n];
  299. if (isKeyList) array.SetValue (de.Key, index + n);
  300. else array.SetValue (de.Value, index + n);
  301. }
  302. }
  303. public IEnumerator GetEnumerator()
  304. {
  305. return new OrderedCollectionEnumerator (list.GetEnumerator (), isKeyList);
  306. }
  307. private class OrderedCollectionEnumerator : IEnumerator
  308. {
  309. private bool isKeyList;
  310. IEnumerator listEnumerator;
  311. public OrderedCollectionEnumerator (IEnumerator listEnumerator, bool isKeyList)
  312. {
  313. this.listEnumerator = listEnumerator;
  314. this.isKeyList = isKeyList;
  315. }
  316. public object Current
  317. {
  318. get {
  319. DictionaryEntry entry = (DictionaryEntry) listEnumerator.Current;
  320. return isKeyList ? entry.Key : entry.Value;
  321. }
  322. }
  323. public bool MoveNext()
  324. {
  325. return listEnumerator.MoveNext ();
  326. }
  327. public void Reset()
  328. {
  329. listEnumerator.Reset ();
  330. }
  331. }
  332. }
  333. }
  334. }