hashtable.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,511 行 · 第 1/3 页

CS
1,511
字号
				}			}#if !ECMA_COMPAT	// Get the hash code provider that is being used by this instance.	protected IHashCodeProvider hcp			{				get				{					return hcp__;				}				set				{					hcp__ = value;				}			}	// Get the comparer that is being used by this instance.	protected IComparer comparer			{				get				{					return comparer__;				}				set				{					comparer__ = value;					}			}#endif // !ECMA_COMPAT	// Synchronized hash table implementation.	//	// Note: We lock every operation on the underlying hash table,	// even if it is a read or enumerator operation.  This is because	// we cannot guarantee correct behaviour in symmetric multi-processing	// environments if we only lock write operations.	private sealed class SynchronizedHashtable : Hashtable, IEnumerable	{		// Internal state.		private new Hashtable table;		// Constructor.		public SynchronizedHashtable(Hashtable table)				{					this.table = table;				}#if CONFIG_SERIALIZATION		internal SynchronizedHashtable(SerializationInfo info,									   StreamingContext context)				: base(info, context)				{					table = (Hashtable)						(info.GetValue("ParentTable", typeof(Hashtable)));					if(table == null)					{						throw new SerializationException							(_("Serialize_StateMissing"));					}				}#endif		// Implement the ICloneable interface.		public override Object Clone()				{					lock(SyncRoot)					{						return new SynchronizedHashtable							((Hashtable)(table.Clone()));					}				}		// Implement the ICollection interface.		public override void CopyTo(Array array, int index)				{					lock(SyncRoot)					{						table.CopyTo(array, index);					}				}		public override int Count				{					get					{						lock(SyncRoot)						{							return table.Count;						}					}				}		public override bool IsSynchronized				{					get					{						return true;					}				}		public override Object SyncRoot				{					get					{						return table.SyncRoot;					}				}		// Implement the IDictionary interface.		public override void Add(Object key, Object value)				{					lock(SyncRoot)					{						table.Add(key, value);					}				}		public override void Clear()				{					lock(SyncRoot)					{						table.Clear();					}				}		public override bool Contains(Object key)				{					lock(SyncRoot)					{						return table.Contains(key);					}				}		public override IDictionaryEnumerator GetEnumerator()				{					lock(SyncRoot)					{						return new SynchronizedDictEnumerator							(SyncRoot, table.GetEnumerator());					}				}		public override void Remove(Object key)				{					lock(SyncRoot)					{						table.Remove(key);					}				}		public override bool IsFixedSize				{					get					{						lock(SyncRoot)						{							return table.IsFixedSize;						}					}				}		public override bool IsReadOnly				{					get					{						lock(SyncRoot)						{							return table.IsReadOnly;						}					}				}		public override Object this[Object key]				{					get					{						lock(SyncRoot)						{							return table[key];						}					}					set					{						lock(SyncRoot)						{							table[key] = value;						}					}				}		public override ICollection Keys				{					get					{						lock(SyncRoot)						{							return table.Keys;						}					}				}		public override ICollection Values				{					get					{						lock(SyncRoot)						{							return table.Values;						}					}				}			// Implement the IEnumerable interface.		IEnumerator IEnumerable.GetEnumerator()				{					lock(SyncRoot)					{						return new SynchronizedEnumerator							(SyncRoot, ((IEnumerable)table).GetEnumerator());					}				}		// Determine if this hash table contains a specific key.		public override bool ContainsKey(Object key)				{					lock(SyncRoot)					{						return table.ContainsKey(key);					}				}		// Determine if this hash table contains a specific value.		public override bool ContainsValue(Object value)				{					lock(SyncRoot)					{						return table.ContainsValue(value);					}				}		// Get the hash value for a key.		protected override int GetHash(Object key)				{					// We don't lock this because it does not modify					// the underlying hash table, or access fields					// that may be modified by other threads.					return table.GetHash(key);				}		// Determine if an item is equal to a key value.		protected override bool KeyEquals(Object item, Object key)				{					// We don't lock this because it does not modify					// the underlying hash table, or access fields					// that may be modified by other threads.					return table.KeyEquals(item, key);				}#if CONFIG_SERIALIZATION		// Get the serialization data for this object.		public override void GetObjectData(SerializationInfo info,										   StreamingContext context)				{					if(info == null)					{						throw new ArgumentNullException("info");					}					info.AddValue("ParentTable", table, typeof(Hashtable));				}		// Process a deserialization callback.		public override void OnDeserialization(Object sender)				{					// Nothing to do here for synchronized hash tables.				}#endif // CONFIG_SERIALIZATION	}; // SynchronizedHashtable	// Hashtable collection and dictionary enumerator.	private class HashtableEnum : IDictionaryEnumerator	{		// Internal state.		protected Hashtable table;		protected int       generation;		protected int		posn;		// Constructor.		public HashtableEnum(Hashtable table)				{					this.table = table;					generation = table.generation;					posn = -1;				}		// Implement the IEnumerator interface.		public bool MoveNext()				{					if(table.generation != generation)					{						throw new InvalidOperationException							(_("Invalid_CollectionModified"));					}					while(++posn < table.capacity)					{						if(table.table[posn].key != null && table.table[posn].key != removed)						{							return true;						}					}					posn = table.capacity;					return false;				}		public void Reset()				{					if(table.generation != generation)					{						throw new InvalidOperationException							(_("Invalid_CollectionModified"));					}					posn = -1;				}		public Object Current				{					get					{						return Entry;					}				}		// Implement the IDictionaryEnumerator interface.		public DictionaryEntry Entry				{					get					{						if(table.generation != generation)						{							throw new InvalidOperationException								(_("Invalid_CollectionModified"));						}						if(posn < 0 || posn >= table.capacity)						{							throw new InvalidOperationException								(_("Invalid_BadEnumeratorPosition"));						}						return new DictionaryEntry(table.table[posn].key,												   table.table[posn].value);					}				}		public Object Key				{					get					{						if(table.generation != generation)						{							throw new InvalidOperationException								(_("Invalid_CollectionModified"));						}						if(posn < 0 || posn >= table.capacity)						{							throw new InvalidOperationException								(_("Invalid_BadEnumeratorPosition"));						}						return table.table[posn].key;					}				}		public Object Value				{					get					{						if(table.generation != generation)						{							throw new InvalidOperationException								(_("Invalid_CollectionModified"));						}						if(posn < 0 || posn >= table.capacity)						{							throw new InvalidOperationException								(_("Invalid_BadEnumeratorPosition"));						}						return table.table[posn].value;					}				}	}; // class HashtableEnum	// Key/value enumerator class.	private sealed class HashtableKeyValueEnumerator : IEnumerator	{		// Internal state.		private IDictionaryEnumerator e;		private bool keys;		// Constructor.		public HashtableKeyValueEnumerator(IDictionaryEnumerator e, bool keys)				{					this.e = e;					this.keys = keys;				}		// Implement the IEnumerator interface.		public bool MoveNext()				{					return e.MoveNext();				}		public void Reset()				{					e.Reset();				}		public Object Current				{					get					{						if(keys)						{							return e.Key;						}						else						{							return e.Value;						}					}				}	}; // class HashtableKeyValueEnumerator	// Collection access to the keys or values in a hash table.	private sealed class HashtableKeyValueCollection : ICollection	{		// Internal state.		private Hashtable table;		private bool keys;		// Constructor.		public HashtableKeyValueCollection(Hashtable table, bool keys)				{					this.table = table;					this.keys = keys;				}		// Implement the ICollection interface.		public void CopyTo(Array array, int index)				{					if(array == null)					{						throw new ArgumentNullException("array");					}					else if(array.Rank != 1)					{						throw new ArgumentException(_("Arg_RankMustBe1"));					}					else if(index < array.GetLowerBound(0))					{						throw new ArgumentOutOfRangeException							("index", _("Arg_InvalidArrayIndex"));					}					else if(index > (array.GetLength(0) - table.Count))					{						throw new ArgumentException(_("Arg_InvalidArrayRange"));					}					else					{						IDictionaryEnumerator e = table.GetEnumerator();						while(e.MoveNext())						{							if(keys)							{								array.SetValue(e.Key, index++);							}							else							{								array.SetValue(e.Value, index++);							}						}					}				}		public int Count				{					get					{						return table.Count;					}				}		public bool IsSynchronized				{					get					{						return table.IsSynchronized;					}				}		public Object SyncRoot				{					get					{						return table.SyncRoot;					}				}		// Implement the IEnumerable interface.		public IEnumerator GetEnumerator()				{					return new HashtableKeyValueEnumerator						(table.GetEnumerator(), keys);				}	}; // class HashtableKeyCollection}; // class Hashtable}; // namespace System.Collections

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?