2 // System.Web.UI/KeyedList.cs
4 // Author: Todd Berman <tberman@gentoo.org>
6 // (C) 2003 Todd Berman
10 using System.Collections;
11 using System.Collections.Specialized;
13 namespace System.Web.UI
16 public class KeyedList : IOrderedDictionary, IStateManager
19 private Hashtable objectTable = new Hashtable ();
20 private ArrayList objectList = new ArrayList ();
22 public void Add (object key, object value)
24 objectTable.Add (key, value);
25 objectList.Add (new DictionaryEntry (key, value));
34 public bool Contains (object key)
36 return objectTable.Contains (key);
39 public void CopyTo (Array array, int idx)
41 objectTable.CopyTo (array, idx);
44 public void Insert (int idx, object key, object value)
47 throw new ArgumentOutOfRangeException ("index");
49 objectTable.Add (key, value);
50 objectList.Insert (idx, new DictionaryEntry (key, value));
53 public void Remove (object key)
55 objectTable.Remove (key);
56 objectList.RemoveAt (IndexOf (key));
59 public void RemoveAt (int idx)
62 throw new ArgumentOutOfRangeException ("index");
64 objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
65 objectList.RemoveAt (idx);
68 private IDictionaryEnumerator IDictionary.GetEnumerator ()
70 return new KeyedListEnumerator (objectList);
73 private IEnumerator IEnumerable.GetEnumerator ()
75 return new KeyedListEnumerator (objectList);
78 private void IStateManager.LoadViewState (object state)
82 object[] states = (object[]) state;
83 if (states[0] != null) {
84 objectList = (ArrayList) states[0];
85 for (int i = 0; i < objectList.Count; i++)
87 DictionaryEntry pair = (DictionaryEntry) objectList[i];
88 objectTable.Add (pair.Key, pair.Value);
94 private object IStateManager.SaveViewState ()
96 object[] ret = new object[] { objectList };
103 private void IStateManager.TrackViewState ()
105 trackViewState = true;
109 get { return objectList.Count; }
112 public bool IsFixedSize {
113 get { return false; }
116 public bool IsReadOnly {
117 get { return false; }
120 public bool IsSynchronized {
121 get { return false; }
124 public object this[int idx] {
125 get { return ((DictionaryEntry) objectList[idx]).Value; }
127 if (idx < 0 || idx >= Count)
128 throw new ArgumentOutOfRangeException ("index");
130 object key = ((DictionaryEntry) objectList[idx]).Key;
131 objectList[idx] = new DictionaryEntry (key, value);
132 objectTable[key] = value;
136 public object this[object key] {
137 get { return objectTable[key]; }
139 if (objectTable.Contains (key))
141 objectTable[key] = value;
142 objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
149 public ICollection Keys {
151 ArrayList retList = new ArrayList ();
152 for (int i = 0; i < objectList.Count; i++)
154 retList.Add ( ((DictionaryEntry)objectList[i]).Key );
160 public ICollection Values {
162 ArrayList retList = new ArrayList ();
163 for (int i = 0; i < objectList.Count; i++)
165 retList.Add ( ((DictionaryEntry)objectList[i]).Value );
171 public object SyncRoot {
175 private bool trackViewState;
176 private bool IStateManager.IsTrackingViewState {
177 get { return trackViewState; }
180 private int IndexOf (object key)
182 for (int i = 0; i < objectList.Count; i++)
184 if (((DictionaryEntry) objectList[i]).Key.Equals (key))