📄 collections.cs
字号:
using System;
using System.Collections;
namespace DDW.CSharp.Dom
{
#region CompileUnitCollection
/// <summary>
/// A strongly-typed collection of <see cref="CompileUnit"/> objects.
/// </summary>
[Serializable]
public class CompileUnitCollection : CSharpGraph, ICollection, IList, IEnumerable, ICloneable
{
public override GraphTypes GraphType{get{return GraphTypes.CompileUnitCollection;}}
#region Interfaces
/// <summary>
/// Supports type-safe iteration over a <see cref="CompileUnitCollection"/>.
/// </summary>
public interface ICompileUnitCollectionEnumerator
{
/// <summary>
/// Gets the current element in the collection.
/// </summary>
CompileUnit 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 CompileUnit[] 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>CompileUnitCollection</c> instance.
/// </summary>
/// <returns>
/// An <c>CompileUnitCollection</c> wrapper that is synchronized (thread-safe).
/// </returns>
public static CompileUnitCollection Synchronized(CompileUnitCollection list)
{
if(list==null)
throw new ArgumentNullException("list");
return new SyncCompileUnitCollection(list);
}
/// <summary>
/// Creates a read-only wrapper for a
/// <c>CompileUnitCollection</c> instance.
/// </summary>
/// <returns>
/// An <c>CompileUnitCollection</c> wrapper that is read-only.
/// </returns>
public static CompileUnitCollection ReadOnly(CompileUnitCollection list)
{
if(list==null)
throw new ArgumentNullException("list");
return new ReadOnlyCompileUnitCollection(list);
}
#endregion
#region Construction
/// <summary>
/// Initializes a new instance of the <c>CompileUnitCollection</c> class
/// that is empty and has the default initial capacity.
/// </summary>
public CompileUnitCollection()
{
m_array = new CompileUnit[DEFAULT_CAPACITY];
}
/// <summary>
/// Initializes a new instance of the <c>CompileUnitCollection</c> class
/// that has the specified initial capacity.
/// </summary>
/// <param name="capacity">
/// The number of elements that the new <c>CompileUnitCollection</c> is initially capable of storing.
/// </param>
public CompileUnitCollection(int capacity)
{
m_array = new CompileUnit[capacity];
}
/// <summary>
/// Initializes a new instance of the <c>CompileUnitCollection</c> class
/// that contains elements copied from the specified <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="c">The <c>CompileUnitCollection</c> whose elements are copied to the new collection.</param>
public CompileUnitCollection(CompileUnitCollection c)
{
m_array = new CompileUnit[c.Count];
AddRange(c);
}
/// <summary>
/// Initializes a new instance of the <c>CompileUnitCollection</c> class
/// that contains elements copied from the specified <see cref="CompileUnit"/> array.
/// </summary>
/// <param name="a">The <see cref="CompileUnit"/> array whose elements are copied to the new list.</param>
public CompileUnitCollection(CompileUnit[] a)
{
m_array = new CompileUnit[a.Length];
AddRange(a);
}
protected enum Tag
{
Default
}
protected CompileUnitCollection(Tag t)
{
m_array = null;
}
#endregion
#region Operations (type-safe ICollection)
/// <summary>
/// Gets the number of elements actually contained in the <c>CompileUnitCollection</c>.
/// </summary>
public virtual int Count
{
get { return m_count; }
}
/// <summary>
/// Copies the entire <c>CompileUnitCollection</c> to a one-dimensional
/// <see cref="CompileUnit"/> array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="CompileUnit"/> array to copy to.</param>
public virtual void CopyTo(CompileUnit[] array)
{
this.CopyTo(array, 0);
}
/// <summary>
/// Copies the entire <c>CompileUnitCollection</c> to a one-dimensional
/// <see cref="CompileUnit"/> array, starting at the specified index of the target array.
/// </summary>
/// <param name="array">The one-dimensional <see cref="CompileUnit"/> 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(CompileUnit[] 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="CompileUnit"/> 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="CompileUnitCollection.Count"/>.</para>
/// </exception>
public virtual CompileUnit 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="CompileUnit"/> to the end of the <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="item">The <see cref="CompileUnit"/> to be added to the end of the <c>CompileUnitCollection</c>.</param>
/// <returns>The index at which the value has been added.</returns>
public virtual int Add(CompileUnit 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>CompileUnitCollection</c>.
/// </summary>
public virtual void Clear()
{
++m_version;
m_array = new CompileUnit[DEFAULT_CAPACITY];
m_count = 0;
}
/// <summary>
/// Creates a shallow copy of the <see cref="CompileUnitCollection"/>.
/// </summary>
public virtual object Clone()
{
CompileUnitCollection newColl = new CompileUnitCollection(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="CompileUnit"/> is in the <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="item">The <see cref="CompileUnit"/> to check for.</param>
/// <returns><c>true</c> if <paramref name="item"/> is found in the <c>CompileUnitCollection</c>; otherwise, <c>false</c>.</returns>
public virtual bool Contains(CompileUnit 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="CompileUnit"/>
/// in the <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="item">The <see cref="CompileUnit"/> to locate in the <c>CompileUnitCollection</c>.</param>
/// <returns>
/// The zero-based index of the first occurrence of <paramref name="item"/>
/// in the entire <c>CompileUnitCollection</c>, if found; otherwise, -1.
/// </returns>
public virtual int IndexOf(CompileUnit 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>CompileUnitCollection</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="CompileUnit"/> 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="CompileUnitCollection.Count"/>.</para>
/// </exception>
public virtual void Insert(int index, CompileUnit 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="CompileUnit"/> from the <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="item">The <see cref="CompileUnit"/> to remove from the <c>CompileUnitCollection</c>.</param>
/// <exception cref="ArgumentException">
/// The specified <see cref="CompileUnit"/> was not found in the <c>CompileUnitCollection</c>.
/// </exception>
public virtual void Remove(CompileUnit item)
{
int i = IndexOf(item);
if (i < 0)
throw new System.ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
++m_version;
RemoveAt(i);
}
/// <summary>
/// Removes the element at the specified index of the <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="index">The zero-based index of the element to remove.</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="CompileUnitCollection.Count"/>.</para>
/// </exception>
public virtual void RemoveAt(int index)
{
ValidateIndex(index); // throws
m_count--;
if (index < m_count)
{
Array.Copy(m_array, index + 1, m_array, index, m_count - index);
}
// We can't set the deleted entry equal to null, because it might be a value type.
// Instead, we'll create an empty single-element array of the right type and copy it
// over the entry we want to erase.
CompileUnit[] temp = new CompileUnit[1];
Array.Copy(temp, 0, m_array, m_count, 1);
m_version++;
}
/// <summary>
/// Gets a value indicating whether the collection has a fixed size.
/// </summary>
/// <value>true if the collection has a fixed size; otherwise, false. The default is false</value>
public virtual bool IsFixedSize
{
get { return false; }
}
/// <summary>
/// gets a value indicating whether the <B>IList</B> is read-only.
/// </summary>
/// <value>true if the collection is read-only; otherwise, false. The default is false</value>
public virtual bool IsReadOnly
{
get { return false; }
}
#endregion
#region Operations (type-safe IEnumerable)
/// <summary>
/// Returns an enumerator that can iterate through the <c>CompileUnitCollection</c>.
/// </summary>
/// <returns>An <see cref="Enumerator"/> for the entire <c>CompileUnitCollection</c>.</returns>
public virtual ICompileUnitCollectionEnumerator GetEnumerator()
{
return new Enumerator(this);
}
#endregion
#region Public helpers (just to mimic some nice features of ArrayList)
/// <summary>
/// Gets or sets the number of elements the <c>CompileUnitCollection</c> can contain.
/// </summary>
public virtual int Capacity
{
get { return m_array.Length; }
set
{
if (value < m_count)
value = m_count;
if (value != m_array.Length)
{
if (value > 0)
{
CompileUnit[] temp = new CompileUnit[value];
Array.Copy(m_array, temp, m_count);
m_array = temp;
}
else
{
m_array = new CompileUnit[DEFAULT_CAPACITY];
}
}
}
}
/// <summary>
/// Adds the elements of another <c>CompileUnitCollection</c> to the current <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="x">The <c>CompileUnitCollection</c> whose elements should be added to the end of the current <c>CompileUnitCollection</c>.</param>
/// <returns>The new <see cref="CompileUnitCollection.Count"/> of the <c>CompileUnitCollection</c>.</returns>
public virtual int AddRange(CompileUnitCollection x)
{
if (m_count + x.Count >= m_array.Length)
EnsureCapacity(m_count + x.Count);
Array.Copy(x.m_array, 0, m_array, m_count, x.Count);
m_count += x.Count;
m_version++;
return m_count;
}
/// <summary>
/// Adds the elements of a <see cref="CompileUnit"/> array to the current <c>CompileUnitCollection</c>.
/// </summary>
/// <param name="x">The <see cref="CompileUnit"/> array whose elements should be added to the end of the <c>CompileUnitCollection</c>.</param>
/// <returns>The new <see cref="CompileUnitCollection.Count"/> of the <c>CompileUnitCollection</c>.</returns>
public virtual int AddRange(CompileUnit[] x)
{
if (m_count + x.Length >= m_array.Length)
EnsureCapacity(m_count + x.Length);
Array.Copy(x, 0, m_array, m_count, x.Length);
m_count += x.Length;
m_version++;
return m_count;
}
/// <summary>
/// Sets the capacity to the actual number of elements.
/// </summary>
public virtual void TrimToSize()
{
this.Capacity = m_count;
}
#endregion
#region Implementation (helpers)
/// <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="CompileUnitCollection.Count"/>.</para>
/// </exception>
private void ValidateIndex(int i)
{
ValidateIndex(i, false);
}
/// <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="CompileUnitCollection.Count"/>.</para>
/// </exception>
private void ValidateIndex(int i, bool allowEqualEnd)
{
int max = (allowEqualEnd)?(m_count):(m_count-1);
if (i < 0 || i > max)
throw new System.ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection.", (object)i, "Specified argument was out of the range of valid values.");
}
private void EnsureCapacity(int min)
{
int newCapacity = ((m_array.Length == 0) ? DEFAULT_CAPACITY : m_array.Length * 2);
if (newCapacity < min)
newCapacity = min;
this.Capacity = newCapacity;
}
#endregion
#region Implementation (ICollection)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -