⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symbolcollections.cs

📁 C#编写的c#编译器
💻 CS
📖 第 1 页 / 共 5 页
字号:
					return result;
				}
				
				set
				{
					rwLock.AcquireWriterLock(timeout);

					try
					{
						collection.Capacity = value;
					}
					finally
					{
						rwLock.ReleaseWriterLock();
					}
				}
			}

			public override int AddRange(ScopeCollection x)
			{
				int result = 0;
				rwLock.AcquireWriterLock(timeout);

				try
				{
					result = collection.AddRange(x);
				}
				finally
				{
					rwLock.ReleaseWriterLock();
				}

				return result;
			}

			public override int AddRange(Scope[] x)
			{
				int result = 0;
				rwLock.AcquireWriterLock(timeout);

				try
				{
					result = collection.AddRange(x);
				}
				finally
				{
					rwLock.ReleaseWriterLock();
				}

				return result;
			}
			#endregion
		}
		#endregion

		#region Nested Read Only Wrapper class
		private class ReadOnlyScopeCollection : ScopeCollection
		{
			#region Implementation (data)
			private ScopeCollection m_collection;
			#endregion

			#region Construction
			internal ReadOnlyScopeCollection(ScopeCollection list) : base(Tag.Default)
			{
				m_collection = list;
			}
			#endregion
			
			#region Type-safe ICollection
			public override void CopyTo(Scope[] array)
			{
				m_collection.CopyTo(array);
			}

			public override void CopyTo(Scope[] array, int start)
			{
				m_collection.CopyTo(array,start);
			}
			public override int Count
			{
				get { return m_collection.Count; }
			}

			public override bool IsSynchronized
			{
				get { return m_collection.IsSynchronized; }
			}

			public override object SyncRoot
			{
				get { return this.m_collection.SyncRoot; }
			}
			#endregion
			
			#region Type-safe IList
			public override Scope this[int i]
			{
				get { return m_collection[i]; }
				set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
			}

			public override int Add(Scope x)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}
			
			public override void Clear()
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}

			public override bool Contains(Scope x)
			{
				return m_collection.Contains(x);
			}

			public override int IndexOf(Scope x)
			{
				return m_collection.IndexOf(x);
			}

			public override void Insert(int pos, Scope x)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}

			public override void Remove(Scope x)
			{           
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}

			public override void RemoveAt(int pos)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}
			
			public override bool IsFixedSize
			{
				get {return true;}
			}

			public override bool IsReadOnly
			{
				get {return true;}
			}
			#endregion

			#region Type-safe IEnumerable
			public override IScopeCollectionEnumerator GetEnumerator()
			{
				return m_collection.GetEnumerator();
			}
			#endregion

			#region Public Helpers
			// (just to mimic some nice features of ArrayList)
			public override int Capacity
			{
				get { return m_collection.Capacity; }
				
				set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
			}

			public override int AddRange(ScopeCollection x)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}

			public override int AddRange(Scope[] x)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}
			#endregion
		}
		#endregion
	}
	#endregion
	#region TypeScopeCollection  
	/// <summary>
	///		A strongly-typed collection of <see cref="TypeScope"/> objects.
	/// </summary>
	[Serializable]
	public class TypeScopeCollection : ICollection, IList, IEnumerable, ICloneable
	{
		#region Interfaces
		/// <summary>
		///		Supports type-safe iteration over a <see cref="TypeScopeCollection"/>.
		/// </summary>
		public interface ITypeScopeCollectionEnumerator
		{
			/// <summary>
			///		Gets the current element in the collection.
			/// </summary>
			TypeScope Current {get;}

			/// <summary>
			///		Advances the enumerator to the next element in the collection.
			/// </summary>
			/// <exception cref="InvalidOperationException">
			///		The collection was modified after the enumerator was created.
			/// </exception>
			/// <returns>
			///		<c>true</c> if the enumerator was successfully advanced to the next element; 
			///		<c>false</c> if the enumerator has passed the end of the collection.
			/// </returns>
			bool MoveNext();

			/// <summary>
			///		Sets the enumerator to its initial position, before the first element in the collection.
			/// </summary>
			void Reset();
		}
		#endregion

		private const int DEFAULT_CAPACITY = 16;

		#region Implementation (data)
		private TypeScope[] m_array;
		private int m_count = 0;
		[NonSerialized]
		private int m_version = 0;
		#endregion
	
		#region Static Wrappers
		/// <summary>
		///		Creates a synchronized (thread-safe) wrapper for a 
		///     <c>TypeScopeCollection</c> instance.
		/// </summary>
		/// <returns>
		///     An <c>TypeScopeCollection</c> wrapper that is synchronized (thread-safe).
		/// </returns>
		public static TypeScopeCollection Synchronized(TypeScopeCollection list)
		{
			if(list==null)
				throw new ArgumentNullException("list");
			return new SyncTypeScopeCollection(list);
		}
		
		/// <summary>
		///		Creates a read-only wrapper for a 
		///     <c>TypeScopeCollection</c> instance.
		/// </summary>
		/// <returns>
		///     An <c>TypeScopeCollection</c> wrapper that is read-only.
		/// </returns>
		public static TypeScopeCollection ReadOnly(TypeScopeCollection list)
		{
			if(list==null)
				throw new ArgumentNullException("list");
			return new ReadOnlyTypeScopeCollection(list);
		}
		#endregion

		#region Construction
		/// <summary>
		///		Initializes a new instance of the <c>TypeScopeCollection</c> class
		///		that is empty and has the default initial capacity.
		/// </summary>
		public TypeScopeCollection()
		{
			m_array = new TypeScope[DEFAULT_CAPACITY];
		}
		
		/// <summary>
		///		Initializes a new instance of the <c>TypeScopeCollection</c> class
		///		that has the specified initial capacity.
		/// </summary>
		/// <param name="capacity">
		///		The number of elements that the new <c>TypeScopeCollection</c> is initially capable of storing.
		///	</param>
		public TypeScopeCollection(int capacity)
		{
			m_array = new TypeScope[capacity];
		}

		/// <summary>
		///		Initializes a new instance of the <c>TypeScopeCollection</c> class
		///		that contains elements copied from the specified <c>TypeScopeCollection</c>.
		/// </summary>
		/// <param name="c">The <c>TypeScopeCollection</c> whose elements are copied to the new collection.</param>
		public TypeScopeCollection(TypeScopeCollection c)
		{
			m_array = new TypeScope[c.Count];
			AddRange(c);
		}

		/// <summary>
		///		Initializes a new instance of the <c>TypeScopeCollection</c> class
		///		that contains elements copied from the specified <see cref="TypeScope"/> array.
		/// </summary>
		/// <param name="a">The <see cref="TypeScope"/> array whose elements are copied to the new list.</param>
		public TypeScopeCollection(TypeScope[] a)
		{
			m_array = new TypeScope[a.Length];
			AddRange(a);
		}
		
		protected enum Tag 
		{
			Default
		}

		protected TypeScopeCollection(Tag t)
		{
			m_array = null;
		}
		#endregion
		
		#region Operations (type-safe ICollection)
		/// <summary>
		///		Gets the number of elements actually contained in the <c>TypeScopeCollection</c>.
		/// </summary>
		public virtual int Count
		{
			get { return m_count; }
		}

		/// <summary>
		///		Copies the entire <c>TypeScopeCollection</c> to a one-dimensional
		///		<see cref="TypeScope"/> array.
		/// </summary>
		/// <param name="array">The one-dimensional <see cref="TypeScope"/> array to copy to.</param>
		public virtual void CopyTo(TypeScope[] array)
		{
			this.CopyTo(array, 0);
		}

		/// <summary>
		///		Copies the entire <c>TypeScopeCollection</c> to a one-dimensional
		///		<see cref="TypeScope"/> array, starting at the specified index of the target array.
		/// </summary>
		/// <param name="array">The one-dimensional <see cref="TypeScope"/> array to copy to.</param>
		/// <param name="start">The zero-based index in <paramref name="array"/> at which copying begins.</param>
		public virtual void CopyTo(TypeScope[] array, int start)
		{
			if (m_count > array.GetUpperBound(0) + 1 - start)
				throw new System.ArgumentException("Destination array was not long enough.");
			
			Array.Copy(m_array, 0, array, start, m_count); 
		}

		/// <summary>
		///		Gets a value indicating whether access to the collection is synchronized (thread-safe).
		/// </summary>
		/// <returns>true if access to the ICollection is synchronized (thread-safe); otherwise, false.</returns>
		public virtual bool IsSynchronized
		{
			get { return m_array.IsSynchronized; }
		}

		/// <summary>
		///		Gets an object that can be used to synchronize access to the collection.
		/// </summary>
		public virtual object SyncRoot
		{
			get { return m_array.SyncRoot; }
		}
		#endregion
		
		#region Operations (type-safe IList)
		/// <summary>
		///		Gets or sets the <see cref="TypeScope"/> at the specified index.
		/// </summary>
		/// <param name="index">The zero-based index of the element to get or set.</param>
		/// <exception cref="ArgumentOutOfRangeException">
		///		<para><paramref name="index"/> is less than zero</para>
		///		<para>-or-</para>
		///		<para><paramref name="index"/> is equal to or greater than <see cref="TypeScopeCollection.Count"/>.</para>
		/// </exception>
		public virtual TypeScope this[int index]
		{
			get
			{
				ValidateIndex(index); // throws
				return m_array[index]; 
			}
			set
			{
				ValidateIndex(index); // throws
				++m_version; 
				m_array[index] = value; 
			}
		}

		/// <summary>
		///		Adds a <see cref="TypeScope"/> to the end of the <c>TypeScopeCollection</c>.
		/// </summary>
		/// <param name="item">The <see cref="TypeScope"/> to be added to the end of the <c>TypeScopeCollection</c>.</param>
		/// <returns>The index at which the value has been added.</returns>
		public virtual int Add(TypeScope item)
		{
			if (m_count == m_array.Length)
				EnsureCapacity(m_count + 1);

			m_array[m_count] = item;
			m_version++;

			return m_count++;
		}
		
		/// <summary>
		///		Removes all elements from the <c>TypeScopeCollection</c>.
		/// </summary>
		public virtual void Clear()
		{
			++m_version;
			m_array = new TypeScope[DEFAULT_CAPACITY];
			m_count = 0;
		}
		
		/// <summary>
		///		Creates a shallow copy of the <see cref="TypeScopeCollection"/>.
		/// </summary>
		public virtual object Clone()
		{
			TypeScopeCollection newColl = new TypeScopeCollection(m_count);
			Array.Copy(m_array, 0, newColl.m_array, 0, m_count);
			newColl.m_count = m_count;
			newColl.m_version = m_version;

			return newColl;
		}

		/// <summary>
		///		Determines whether a given <see cref="TypeScope"/> is in the <c>TypeScopeCollection</c>.
		/// </summary>
		/// <param name="item">The <see cref="TypeScope"/> to check for.</param>
		/// <returns><c>true</c> if <paramref name="item"/> is found in the <c>TypeScopeCollection</c>; otherwise, <c>false</c>.</returns>
		public virtual bool Contains(TypeScope item)
		{
			for (int i=0; i != m_count; ++i)
				if (m_array[i].Equals(item))
					return true;
			return false;
		}

		/// <summary>
		///		Returns the zero-based index of the first occurrence of a <see cref="TypeScope"/>
		///		in the <c>TypeScopeCollection</c>.
		/// </summary>
		/// <param name="item">The <see cref="TypeScope"/> to locate in the <c>TypeScopeCollection</c>.</param>
		/// <returns>
		///		The zero-based index of the first occurrence of <paramref name="item"/> 
		///		in the entire <c>TypeScopeCollection</c>, if found; otherwise, -1.
		///	</returns>
		public virtual int IndexOf(TypeScope item)
		{
			for (int i=0; i != m_count; ++i)
				if (m_array[i].Equals(item))
					return i;
			return -1;
		}

		/// <summary>
		///		Inserts an element into the <c>TypeScopeCollection</c> at the specified index.
		/// </summary>
		/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
		/// <param name="item">The <see cref="TypeScope"/> to insert.</param>
		/// <exception cref="ArgumentOutOfRangeException">
		///		<para><paramref name="index"/> is less than zero</para>
		///		<para>-or-</para>
		///		<para><paramref name="index"/> is equal to or greater than <see cref="TypeScopeCollection.Count"/>.</para>
		/// </exception>
		public virtual void Insert(int index, TypeScope item)
		{
			ValidateIndex(index, true); // throws
			
			if (m_count == m_array.Length)
				EnsureCapacity(m_count + 1);

			if (index < m_count)
			{
				Array.Copy(m_array, index, m_array, index + 1, m_count - index);
			}

			m_array[index] = item;
			m_count++;
			m_version++;
		}

		/// <summary>
		///		Removes the first occurrence of a specific <see cref="TypeScope"/> from the <c>TypeScopeCollection</c>.
		/// </summary>
		/// <param name="item">The <see cref="TypeScope"/> to remove from the <c>TypeScopeCollection</c>.</param>
		/// <exception cref="ArgumentException">
		///		The specified <see cref="TypeScope"/> was not found in the <c>TypeScopeCollection</c>.
		/// </exception>
		public virtual void Remove(TypeScope item)
		{		   
			int i = IndexOf(item);
			if (i < 0)
				throw new System.ArgumentException("Cannot remove the specified item because it was

⌨️ 快捷键说明

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