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.
31 using System.Collections;
32 using System.Collections.Specialized;
34 namespace System.Web.UI
37 internal class KeyedList : IOrderedDictionary, IStateManager
40 private Hashtable objectTable = new Hashtable ();
41 private ArrayList objectList = new ArrayList ();
43 public void Add (object key, object value)
45 objectTable.Add (key, value);
46 objectList.Add (new DictionaryEntry (key, value));
55 public bool Contains (object key)
57 return objectTable.Contains (key);
60 public void CopyTo (Array array, int idx)
62 objectTable.CopyTo (array, idx);
65 public void Insert (int idx, object key, object value)
68 throw new ArgumentOutOfRangeException ("index");
70 objectTable.Add (key, value);
71 objectList.Insert (idx, new DictionaryEntry (key, value));
74 public void Remove (object key)
76 objectTable.Remove (key);
77 int index = IndexOf (key);
79 objectList.RemoveAt (index);
82 public void RemoveAt (int idx)
85 throw new ArgumentOutOfRangeException ("index");
87 objectTable.Remove ( ((DictionaryEntry)objectList[idx]).Key );
88 objectList.RemoveAt (idx);
91 IDictionaryEnumerator IDictionary.GetEnumerator ()
93 return new KeyedListEnumerator (objectList);
96 IDictionaryEnumerator IOrderedDictionary.GetEnumerator ()
98 return new KeyedListEnumerator (objectList);
101 IEnumerator IEnumerable.GetEnumerator ()
103 return new KeyedListEnumerator (objectList);
106 void IStateManager.LoadViewState (object state)
110 object[] states = (object[]) state;
111 if (states[0] != null) {
112 objectList = (ArrayList) states[0];
113 for (int i = 0; i < objectList.Count; i++)
115 DictionaryEntry pair = (DictionaryEntry) objectList[i];
116 objectTable.Add (pair.Key, pair.Value);
122 object IStateManager.SaveViewState ()
124 object[] ret = new object[] { objectList };
131 void IStateManager.TrackViewState ()
133 trackViewState = true;
137 get { return objectList.Count; }
140 public bool IsFixedSize {
141 get { return false; }
144 public bool IsReadOnly {
145 get { return false; }
148 public bool IsSynchronized {
149 get { return false; }
152 public object this[int idx] {
153 get { return ((DictionaryEntry) objectList[idx]).Value; }
155 if (idx < 0 || idx >= Count)
156 throw new ArgumentOutOfRangeException ("index");
158 object key = ((DictionaryEntry) objectList[idx]).Key;
159 objectList[idx] = new DictionaryEntry (key, value);
160 objectTable[key] = value;
164 public object this[object key] {
165 get { return objectTable[key]; }
167 if (objectTable.Contains (key))
169 objectTable[key] = value;
170 objectTable[IndexOf (key)] = new DictionaryEntry (key, value);
177 public ICollection Keys {
179 ArrayList retList = new ArrayList ();
180 for (int i = 0; i < objectList.Count; i++)
182 retList.Add ( ((DictionaryEntry)objectList[i]).Key );
188 public ICollection Values {
190 ArrayList retList = new ArrayList ();
191 for (int i = 0; i < objectList.Count; i++)
193 retList.Add ( ((DictionaryEntry)objectList[i]).Value );
199 public object SyncRoot {
203 private bool trackViewState;
205 bool IStateManager.IsTrackingViewState {
206 get { return trackViewState; }
209 private int IndexOf (object key)
211 for (int i = 0; i < objectList.Count; i++)
213 if (((DictionaryEntry) objectList[i]).Key.Equals (key))