2 // System.Web.UI.WebControls.OrderedDictionary.cs
\r
5 // Lluis Sanchez Gual (lluis@novell.com)
\r
7 // (C) 2005 Novell, Inc (http://www.novell.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
34 using System.Runtime.Serialization;
\r
36 namespace System.Collections.Specialized
\r
39 public class OrderedDictionary : IOrderedDictionary, IDictionary, ICollection, IEnumerable, ISerializable
\r
44 int initialCapacity;
\r
46 public OrderedDictionary ()
\r
48 list = new ArrayList ();
\r
49 hash = new Hashtable ();
\r
52 public OrderedDictionary (int capacity)
\r
54 initialCapacity = capacity;
\r
55 list = new ArrayList (capacity);
\r
56 hash = new Hashtable (capacity);
\r
59 public OrderedDictionary (SerializationInfo info, StreamingContext context)
\r
61 readOnly = info.GetBoolean ("ReadOnly");
\r
62 initialCapacity = info.GetInt32 ("InitialCapacity");
\r
63 hash = (Hashtable) info.GetValue ("HashTable", typeof(Hashtable));
\r
64 list = (ArrayList) info.GetValue ("ArrayList", typeof(ArrayList));
\r
67 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
\r
69 info.AddValue ("ReadOnly", readOnly);
\r
70 info.AddValue ("InitialCapacity", initialCapacity);
\r
71 info.AddValue ("HashTable", hash);
\r
72 info.AddValue ("ArrayList", list);
\r
75 IEnumerator IEnumerable.GetEnumerator()
\r
77 return list.GetEnumerator ();
\r
86 public bool IsSynchronized {
\r
88 return list.IsSynchronized;
\r
92 public object SyncRoot {
\r
94 return list.SyncRoot;
\r
98 public void CopyTo (Array array, int index)
\r
100 list.CopyTo (array, index);
\r
103 public bool IsFixedSize
\r
110 public virtual bool IsReadOnly
\r
117 public virtual object this [object key]
\r
119 get { return hash [key]; }
\r
122 if (hash.Contains (key)) {
\r
123 int i = FindListEntry (key);
\r
124 list [i] = new DictionaryEntry (key, value);
\r
126 list.Add (new DictionaryEntry (key, value));
\r
128 hash [key] = value;
\r
132 public virtual object this [int index]
\r
134 get { return ((DictionaryEntry) list [index]).Value; }
\r
137 DictionaryEntry de = (DictionaryEntry) list [index];
\r
142 public virtual ICollection Keys
\r
145 return new OrderedCollection (list, true);
\r
149 public virtual ICollection Values
\r
152 return new OrderedCollection (list, false);
\r
156 public void Add (object key, object value)
\r
159 hash.Add (key, value);
\r
160 list.Add (new DictionaryEntry (key, value));
\r
163 public void Clear()
\r
170 public bool Contains (object key)
\r
172 return hash.Contains (key);
\r
175 public virtual IDictionaryEnumerator GetEnumerator()
\r
177 return new OrderedEntryCollectionEnumerator (list.GetEnumerator ());
\r
180 public void Remove (object key)
\r
184 if (hash.Contains (key)) {
\r
186 int i = FindListEntry (key);
\r
191 int FindListEntry (object key)
\r
193 for (int n=0; n<list.Count; n++) {
\r
194 DictionaryEntry de = (DictionaryEntry) list [n];
\r
195 if (de.Key.Equals (key))
\r
204 throw new InvalidOperationException ("Collection is read only");
\r
207 public OrderedDictionary AsReadOnly ()
\r
209 OrderedDictionary od = new OrderedDictionary ();
\r
212 od.readOnly = true;
\r
216 public void Insert (int index, object key, object value)
\r
219 hash.Add (key, value);
\r
220 list.Insert (index, new DictionaryEntry (key, value));
\r
223 public void RemoveAt (int index)
\r
226 DictionaryEntry entry = (DictionaryEntry) list [index];
\r
227 list.RemoveAt (index);
\r
228 hash.Remove (entry.Key);
\r
231 private class OrderedEntryCollectionEnumerator : IEnumerator, IDictionaryEnumerator
\r
233 IEnumerator listEnumerator;
\r
235 public OrderedEntryCollectionEnumerator (IEnumerator listEnumerator)
\r
237 this.listEnumerator = listEnumerator;
\r
240 public bool MoveNext()
\r
242 return listEnumerator.MoveNext ();
\r
245 public void Reset()
\r
247 listEnumerator.Reset ();
\r
250 public object Current
\r
252 get { return listEnumerator.Current; }
\r
255 public DictionaryEntry Entry
\r
257 get { return (DictionaryEntry) listEnumerator.Current; }
\r
262 get { return Entry.Key; }
\r
265 public object Value
\r
267 get { return Entry.Value; }
\r
271 private class OrderedCollection : ICollection
\r
273 private ArrayList list;
\r
274 private bool isKeyList;
\r
276 public OrderedCollection (ArrayList list, bool isKeyList)
\r
279 this.isKeyList = isKeyList;
\r
288 public bool IsSynchronized
\r
295 public object SyncRoot
\r
298 return list.SyncRoot;
\r
302 public void CopyTo (Array array, int index)
\r
304 for (int n=0; n<list.Count; n++) {
\r
305 DictionaryEntry de = (DictionaryEntry) list [n];
\r
306 if (isKeyList) array.SetValue (de.Key, index + n);
\r
307 else array.SetValue (de.Value, index + n);
\r
311 public IEnumerator GetEnumerator()
\r
313 return new OrderedCollectionEnumerator (list.GetEnumerator (), isKeyList);
\r
316 private class OrderedCollectionEnumerator : IEnumerator
\r
318 private bool isKeyList;
\r
319 IEnumerator listEnumerator;
\r
321 public OrderedCollectionEnumerator (IEnumerator listEnumerator, bool isKeyList)
\r
323 this.listEnumerator = listEnumerator;
\r
324 this.isKeyList = isKeyList;
\r
327 public object Current
\r
330 DictionaryEntry entry = (DictionaryEntry) listEnumerator.Current;
\r
331 return isKeyList ? entry.Key : entry.Value;
\r
335 public bool MoveNext()
\r
337 return listEnumerator.MoveNext ();
\r
340 public void Reset()
\r
342 listEnumerator.Reset ();
\r