00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 namespace Antlr.Runtime.Collections
00036 {
00037 using System;
00038 using IDictionary = System.Collections.IDictionary;
00039 using IDictionaryEnumerator = System.Collections.IDictionaryEnumerator;
00040 using ICollection = System.Collections.ICollection;
00041 using IEnumerator = System.Collections.IEnumerator;
00042 using Hashtable = System.Collections.Hashtable;
00043 using ArrayList = System.Collections.ArrayList;
00044 using DictionaryEntry = System.Collections.DictionaryEntry;
00045 using StringBuilder = System.Text.StringBuilder;
00046
00051 public sealed class HashList : IDictionary
00052 {
00053 #region Helper classes
00054 private sealed class HashListEnumerator : IDictionaryEnumerator
00055 {
00056 internal enum EnumerationMode
00057 {
00058 Key,
00059 Value,
00060 Entry
00061 }
00062 private HashList _hashList;
00063 private ArrayList _orderList;
00064 private EnumerationMode _mode;
00065 private int _index;
00066 private int _version;
00067 private object _key;
00068 private object _value;
00069
00070 #region Constructors
00071
00072 internal HashListEnumerator()
00073 {
00074 _index = 0;
00075 _key = null;
00076 _value = null;
00077 }
00078
00079 internal HashListEnumerator(HashList hashList, EnumerationMode mode)
00080 {
00081 _hashList = hashList;
00082 _mode = mode;
00083 _version = hashList._version;
00084 _orderList = hashList._insertionOrderList;
00085 _index = 0;
00086 _key = null;
00087 _value = null;
00088 }
00089
00090 #endregion
00091
00092 #region IDictionaryEnumerator Members
00093
00094 public object Key
00095 {
00096 get
00097 {
00098 if (_key == null)
00099 {
00100 throw new InvalidOperationException("Enumeration has either not started or has already finished.");
00101 }
00102 return _key;
00103 }
00104 }
00105
00106 public object Value
00107 {
00108 get
00109 {
00110 if (_key == null)
00111 {
00112 throw new InvalidOperationException("Enumeration has either not started or has already finished.");
00113 }
00114 return _value;
00115 }
00116 }
00117
00118 public DictionaryEntry Entry
00119 {
00120 get
00121 {
00122 if (_key == null)
00123 {
00124 throw new InvalidOperationException("Enumeration has either not started or has already finished.");
00125 }
00126 return new DictionaryEntry(_key, _value);
00127 }
00128 }
00129
00130 #endregion
00131
00132 #region IEnumerator Members
00133
00134 public void Reset()
00135 {
00136 if (_version != _hashList._version)
00137 {
00138 throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
00139 }
00140 _index = 0;
00141 _key = null;
00142 _value = null;
00143 }
00144
00145 public object Current
00146 {
00147 get
00148 {
00149 if (_key == null)
00150 {
00151 throw new InvalidOperationException("Enumeration has either not started or has already finished.");
00152 }
00153
00154 if (_mode == EnumerationMode.Key)
00155 return _key;
00156 else if (_mode == EnumerationMode.Value)
00157 return _value;
00158
00159 return new DictionaryEntry(_key, _value);
00160 }
00161 }
00162
00163 public bool MoveNext()
00164 {
00165 if (_version != _hashList._version)
00166 {
00167 throw new InvalidOperationException("Collection was modified; enumeration operation may not execute.");
00168 }
00169
00170 if (_index < _orderList.Count)
00171 {
00172 _key = _orderList[_index];
00173 _value = _hashList[_key];
00174 _index++;
00175 return true;
00176 }
00177 _key = null;
00178 return false;
00179 }
00180
00181 #endregion
00182 }
00183
00184 private sealed class KeyCollection : ICollection
00185 {
00186 private HashList _hashList;
00187
00188 #region Constructors
00189
00190 internal KeyCollection()
00191 {
00192 }
00193
00194 internal KeyCollection(HashList hashList)
00195 {
00196 _hashList = hashList;
00197 }
00198
00199 #endregion
00200
00201 public override string ToString()
00202 {
00203 StringBuilder result = new StringBuilder();
00204
00205 result.Append("[");
00206 ArrayList keys = _hashList._insertionOrderList;
00207 for (int i = 0; i < keys.Count; i++)
00208 {
00209 if (i > 0)
00210 {
00211 result.Append(", ");
00212 }
00213 result.Append(keys[i]);
00214 }
00215 result.Append("]");
00216
00217 return result.ToString();
00218 }
00219
00220 public override bool Equals(object o)
00221 {
00222 if (o is KeyCollection)
00223 {
00224 KeyCollection other = (KeyCollection) o;
00225 if ((Count == 0) && (other.Count == 0))
00226 return true;
00227 else if (Count == other.Count)
00228 {
00229 for (int i = 0; i < Count; i++)
00230 {
00231 if ((_hashList._insertionOrderList[i] == other._hashList._insertionOrderList[i]) ||
00232 (_hashList._insertionOrderList[i].Equals(other._hashList._insertionOrderList[i])))
00233 return true;
00234 }
00235 }
00236 }
00237 return false;
00238 }
00239
00240 public override int GetHashCode()
00241 {
00242 return _hashList._insertionOrderList.GetHashCode();
00243 }
00244
00245 #region ICollection Members
00246
00247 public bool IsSynchronized
00248 {
00249 get { return _hashList.IsSynchronized; }
00250 }
00251
00252 public int Count
00253 {
00254 get { return _hashList.Count; }
00255 }
00256
00257 public void CopyTo(Array array, int index)
00258 {
00259 _hashList.CopyKeysTo(array, index);
00260 }
00261
00262 public object SyncRoot
00263 {
00264 get { return _hashList.SyncRoot; }
00265 }
00266
00267 #endregion
00268
00269 #region IEnumerable Members
00270
00271 public IEnumerator GetEnumerator()
00272 {
00273 return new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Key);
00274 }
00275
00276 #endregion
00277 }
00278
00279 private sealed class ValueCollection : ICollection
00280 {
00281 private HashList _hashList;
00282
00283 #region Constructors
00284
00285 internal ValueCollection()
00286 {
00287 }
00288
00289 internal ValueCollection(HashList hashList)
00290 {
00291 _hashList = hashList;
00292 }
00293
00294 #endregion
00295
00296 public override string ToString()
00297 {
00298 StringBuilder result = new StringBuilder();
00299
00300 result.Append("[");
00301 IEnumerator iter = new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Value);
00302 if (iter.MoveNext())
00303 {
00304 result.Append((iter.Current == null) ? "null" : iter.Current);
00305 while (iter.MoveNext())
00306 {
00307 result.Append(", ");
00308 result.Append((iter.Current == null) ? "null" : iter.Current);
00309 }
00310 }
00311 result.Append("]");
00312
00313 return result.ToString();
00314 }
00315
00316 #region ICollection Members
00317
00318 public bool IsSynchronized
00319 {
00320 get { return _hashList.IsSynchronized; }
00321 }
00322
00323 public int Count
00324 {
00325 get { return _hashList.Count; }
00326 }
00327
00328 public void CopyTo(Array array, int index)
00329 {
00330 _hashList.CopyValuesTo(array, index);
00331 }
00332
00333 public object SyncRoot
00334 {
00335 get { return _hashList.SyncRoot; }
00336 }
00337
00338 #endregion
00339
00340 #region IEnumerable Members
00341
00342 public IEnumerator GetEnumerator()
00343 {
00344 return new HashListEnumerator(_hashList, HashListEnumerator.EnumerationMode.Value);
00345 }
00346
00347 #endregion
00348 }
00349
00350 #endregion
00351
00352 private Hashtable _dictionary = new Hashtable();
00353 private ArrayList _insertionOrderList = new ArrayList();
00354 private int _version;
00355
00356 #region Constructors
00357
00358 public HashList() : this(-1)
00359 {
00360 }
00361
00362 public HashList(int capacity)
00363 {
00364 if (capacity < 0)
00365 {
00366 _dictionary = new Hashtable();
00367 _insertionOrderList = new ArrayList();
00368 }
00369 else
00370 {
00371 _dictionary = new Hashtable(capacity);
00372 _insertionOrderList = new ArrayList(capacity);
00373 }
00374 _version = 0;
00375 }
00376
00377 #endregion
00378
00379 #region IDictionary Members
00380
00381 public bool IsReadOnly { get { return _dictionary.IsReadOnly; } }
00382
00383 public IDictionaryEnumerator GetEnumerator()
00384 {
00385 return new HashListEnumerator(this, HashListEnumerator.EnumerationMode.Entry);
00386 }
00387
00388 public object this[object key]
00389 {
00390 get { return _dictionary[key]; }
00391 set
00392 {
00393 bool isNewEntry = !_dictionary.Contains(key);
00394 _dictionary[key] = value;
00395 if (isNewEntry)
00396 _insertionOrderList.Add(key);
00397 _version++;
00398 }
00399 }
00400
00401 public void Remove(object key)
00402 {
00403 _dictionary.Remove(key);
00404 _insertionOrderList.Remove(key);
00405 _version++;
00406 }
00407
00408 public bool Contains(object key)
00409 {
00410 return _dictionary.Contains(key);
00411 }
00412
00413 public void Clear()
00414 {
00415 _dictionary.Clear();
00416 _insertionOrderList.Clear();
00417 _version++;
00418 }
00419
00420 public ICollection Values
00421 {
00422 get { return new ValueCollection(this); }
00423 }
00424
00425 public void Add(object key, object value)
00426 {
00427 _dictionary.Add(key, value);
00428 _insertionOrderList.Add(key);
00429 _version++;
00430 }
00431
00432 public ICollection Keys
00433 {
00434 get { return new KeyCollection(this); }
00435 }
00436
00437 public bool IsFixedSize
00438 {
00439 get { return _dictionary.IsFixedSize; }
00440 }
00441
00442 #endregion
00443
00444 #region ICollection Members
00445
00446 public bool IsSynchronized
00447 {
00448 get { return _dictionary.IsSynchronized; }
00449 }
00450
00451 public int Count
00452 {
00453 get { return _dictionary.Count; }
00454 }
00455
00456 public void CopyTo(Array array, int index)
00457 {
00458 int len = _insertionOrderList.Count;
00459 for (int i = 0; i < len; i++)
00460 {
00461 DictionaryEntry e = new DictionaryEntry(_insertionOrderList[i], _dictionary[_insertionOrderList[i]]);
00462 array.SetValue(e, index++);
00463 }
00464 }
00465
00466 public object SyncRoot
00467 {
00468 get { return _dictionary.SyncRoot; }
00469 }
00470
00471 #endregion
00472
00473 #region IEnumerable Members
00474
00475 IEnumerator System.Collections.IEnumerable.GetEnumerator()
00476 {
00477 return new HashListEnumerator(this, HashListEnumerator.EnumerationMode.Entry);
00478 }
00479
00480 #endregion
00481
00482 private void CopyKeysTo(Array array, int index)
00483 {
00484 int len = _insertionOrderList.Count;
00485 for (int i = 0; i < len; i++)
00486 {
00487 array.SetValue(_insertionOrderList[i], index++);
00488 }
00489 }
00490
00491 private void CopyValuesTo(Array array, int index)
00492 {
00493 int len = _insertionOrderList.Count;
00494 for (int i = 0; i < len; i++)
00495 {
00496 array.SetValue(_dictionary[_insertionOrderList[i]], index++);
00497 }
00498 }
00499
00500 }
00501 }