2 // System.Web.UI/KeyedList.cs
4 // Author: Todd Berman <tberman@gentoo.org>
6 // (C) 2003 Todd Berman
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Specialized;
32 namespace System.Web.UI
35 internal class KeyedList : IOrderedDictionary, IStateManager
38 Hashtable objectTable = new Hashtable ();
39 ArrayList objectList = new ArrayList ();
41 public void Add (object key, object value)
43 objectTable.Add (key, value);
44 objectList.Add (new DictionaryEntry (key, value));
53 public bool Contains (object key)
55 return objectTable.Contains (key);
58 public void CopyTo (Array array, int idx)
60 objectTable.CopyTo (array, idx);
63 public void Insert (int idx, object key, object value)
66 throw new ArgumentOutOfRangeException ("index");
68 objectTable.Add (key, value);
69 objectList.Insert (idx, new DictionaryEntry (key, value));
72 public void Remove (object key)
74 objectTable.Remove (key);
75 int index = IndexOf (key);
77 objectList.RemoveAt (index);
80 public void RemoveAt (int idx)
83 throw new ArgumentOutOfRangeException ("index");
85 objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
86 objectList.RemoveAt (idx);
89 IDictionaryEnumerator IDictionary.GetEnumerator ()
91 return new KeyedListEnumerator (objectList);
94 IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
96 return new KeyedListEnumerator (objectList);
99 IEnumerator IEnumerable.GetEnumerator ()
101 return new KeyedListEnumerator (objectList);
104 void IStateManager.LoadViewState (object state)
108 object[] states = (object[]) state;
109 if (states[0] != null) {
110 objectList = (ArrayList) states[0];
111 for (int i = 0; i < objectList.Count; i++)
113 DictionaryEntry pair = (DictionaryEntry) objectList[i];
114 objectTable.Add (pair.Key, pair.Value);
120 object IStateManager.SaveViewState ()
122 object[] ret = new object[] { objectList };
129 void IStateManager.TrackViewState ()
131 trackViewState = true;
135 get { return objectList.Count; }
138 public bool IsFixedSize {
139 get { return false; }
142 public bool IsReadOnly {
143 get { return false; }
146 public bool IsSynchronized {
147 get { return false; }
150 public object this[int idx] {
151 get { return ((DictionaryEntry) objectList[idx]).Value; }
153 if (idx < 0 || idx >= Count)
154 throw new ArgumentOutOfRangeException ("index");
156 object key = ((DictionaryEntry) objectList[idx]).Key;
157 objectList[idx] = new DictionaryEntry (key, value);
158 objectTable[key] = value;
162 public object this[object key] {
163 get { return objectTable[key]; }
165 if (objectTable.Contains (key))
167 objectTable[key] = value;
168 objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
175 public ICollection Keys {
177 ArrayList retList = new ArrayList ();
178 for (int i = 0; i < objectList.Count; i++)
180 retList.Add ( ((DictionaryEntry)objectList[i]).Key );
186 public ICollection Values {
188 ArrayList retList = new ArrayList ();
189 for (int i = 0; i < objectList.Count; i++)
191 retList.Add ( ((DictionaryEntry)objectList[i]).Value );
197 public object SyncRoot {
203 bool IStateManager.IsTrackingViewState {
204 get { return trackViewState; }
207 int IndexOf (object key)
209 for (int i = 0; i < objectList.Count; i++)
211 if (((DictionaryEntry) objectList[i]).Key.Equals (key))