📄 algorithms.cs
字号:
/// </summary>
[Serializable]
private class ReadOnlyCollection<T> : ICollection<T>
{
private ICollection<T> wrappedCollection; // The collection we are wrapping (never null).
/// <summary>
/// Create a ReadOnlyCollection wrapped around the given collection.
/// </summary>
/// <param name="wrappedCollection">Collection to wrap.</param>
public ReadOnlyCollection(ICollection<T> wrappedCollection)
{
this.wrappedCollection = wrappedCollection;
}
/// <summary>
/// Throws an NotSupportedException stating that this collection cannot be modified.
/// </summary>
private void MethodModifiesCollection()
{
throw new NotSupportedException(string.Format(Strings.CannotModifyCollection, "read-only collection"));
}
public IEnumerator<T> GetEnumerator()
{ return wrappedCollection.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator()
{ return ((IEnumerable)wrappedCollection).GetEnumerator(); }
public bool Contains(T item)
{ return wrappedCollection.Contains(item); }
public void CopyTo(T[] array, int arrayIndex)
{ wrappedCollection.CopyTo(array, arrayIndex); }
public int Count
{
get { return wrappedCollection.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public void Add(T item)
{ MethodModifiesCollection(); }
public void Clear()
{ MethodModifiesCollection(); }
public bool Remove(T item)
{ MethodModifiesCollection(); return false; }
}
/// <summary>
/// Returns a read-only view onto a collection. The returned ICollection<T> interface
/// only allows operations that do not change the collection: GetEnumerator, Contains, CopyTo,
/// Count. The ReadOnly property returns false, indicating that the collection is read-only. All other
/// methods on the interface throw a NotSupportedException.
/// </summary>
/// <remarks>The data in the underlying collection is not copied. If the underlying
/// collection is changed, then the read-only view also changes accordingly.</remarks>
/// <typeparam name="T">The type of items in the collection.</typeparam>
/// <param name="collection">The collection to wrap.</param>
/// <returns>A read-only view onto <paramref name="collection"/>. If <paramref name="collection"/> is null, then null is returned.</returns>
public static ICollection<T> ReadOnly<T>(ICollection<T> collection)
{
if (collection == null)
return null;
else
return new ReadOnlyCollection<T>(collection);
}
/// <summary>
/// The read-only IList<T> implementation that is used by the ReadOnly method.
/// Methods that modify the list throw a NotSupportedException, methods that don't
/// modify are fowarded through to the wrapped list.
/// </summary>
[Serializable]
private class ReadOnlyList<T> : IList<T>
{
private IList<T> wrappedList; // The list we are wrapping (never null).
/// <summary>
/// Create a ReadOnlyList wrapped around the given list.
/// </summary>
/// <param name="wrappedList">List to wrap.</param>
public ReadOnlyList(IList<T> wrappedList)
{
this.wrappedList = wrappedList;
}
/// <summary>
/// Throws an NotSupportedException stating that this collection cannot be modified.
/// </summary>
private void MethodModifiesCollection()
{
throw new NotSupportedException(string.Format(Strings.CannotModifyCollection, "read-only list"));
}
public IEnumerator<T> GetEnumerator()
{ return wrappedList.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator()
{ return ((IEnumerable)wrappedList).GetEnumerator(); }
public int IndexOf(T item)
{ return wrappedList.IndexOf(item); }
public bool Contains(T item)
{ return wrappedList.Contains(item); }
public void CopyTo(T[] array, int arrayIndex)
{ wrappedList.CopyTo(array, arrayIndex); }
public int Count
{
get { return wrappedList.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public T this[int index]
{
get { return wrappedList[index]; }
set { MethodModifiesCollection(); }
}
public void Add(T item)
{ MethodModifiesCollection(); }
public void Clear()
{ MethodModifiesCollection(); }
public void Insert(int index, T item)
{ MethodModifiesCollection(); }
public void RemoveAt(int index)
{ MethodModifiesCollection(); }
public bool Remove(T item)
{ MethodModifiesCollection(); return false; }
}
/// <summary>
/// Returns a read-only view onto a list. The returned IList<T> interface
/// only allows operations that do not change the list: GetEnumerator, Contains, CopyTo,
/// Count, IndexOf, and the get accessor of the indexer.
/// The IsReadOnly property returns true, indicating that the list is read-only. All other
/// methods on the interface throw a NotSupportedException.
/// </summary>
/// <remarks>The data in the underlying list is not copied. If the underlying
/// list is changed, then the read-only view also changes accordingly.</remarks>
/// <typeparam name="T">The type of items in the list.</typeparam>
/// <param name="list">The list to wrap.</param>
/// <returns>A read-only view onto <paramref name="list"/>. Returns null if <paramref name="list"/> is null.
/// If <paramref name="list"/> is already read-only, returns <paramref name="list"/>.</returns>
public static IList<T> ReadOnly<T>(IList<T> list)
{
if (list == null)
return null;
else if (list.IsReadOnly)
return list;
else
return new ReadOnlyList<T>(list);
}
/// <summary>
/// The private class that implements a read-only wrapped for
/// IDictionary <TKey,TValue>.
/// </summary>
[Serializable]
private class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
// The dictionary that is wrapped
private IDictionary<TKey, TValue> wrappedDictionary;
/// <summary>
/// Create a read-only dictionary wrapped around the given dictionary.
/// </summary>
/// <param name="wrappedDictionary">The IDictionary<TKey,TValue> to wrap.</param>
public ReadOnlyDictionary(IDictionary<TKey, TValue> wrappedDictionary)
{
this.wrappedDictionary = wrappedDictionary;
}
/// <summary>
/// Throws an NotSupportedException stating that this collection cannot be modified.
/// </summary>
private void MethodModifiesCollection()
{
throw new NotSupportedException(string.Format(Strings.CannotModifyCollection, "read-only dictionary"));
}
public void Add(TKey key, TValue value)
{ MethodModifiesCollection(); }
public bool ContainsKey(TKey key)
{ return wrappedDictionary.ContainsKey(key); }
public ICollection<TKey> Keys
{
get { return ReadOnly(wrappedDictionary.Keys); }
}
public ICollection<TValue> Values
{
get { return ReadOnly(wrappedDictionary.Values); }
}
public bool Remove(TKey key)
{
MethodModifiesCollection();
return false; // never reached
}
public bool TryGetValue(TKey key, out TValue value)
{ return wrappedDictionary.TryGetValue(key, out value); }
public TValue this[TKey key]
{
get { return wrappedDictionary[key];}
set { MethodModifiesCollection(); }
}
public void Add(KeyValuePair<TKey, TValue> item)
{ MethodModifiesCollection(); }
public void Clear()
{ MethodModifiesCollection(); }
public bool Contains(KeyValuePair<TKey, TValue> item)
{ return wrappedDictionary.Contains(item); }
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{ wrappedDictionary.CopyTo(array, arrayIndex); }
public int Count
{
get { return wrappedDictionary.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
MethodModifiesCollection();
return false; // never reached
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{ return wrappedDictionary.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator()
{ return ((IEnumerable)wrappedDictionary).GetEnumerator(); }
}
/// <summary>
/// Returns a read-only view onto a dictionary. The returned IDictionary<TKey,TValue> interface
/// only allows operations that do not change the dictionary.
/// The IsReadOnly property returns true, indicating that the dictionary is read-only. All other
/// methods on the interface throw a NotSupportedException.
/// </summary>
/// <remarks>The data in the underlying dictionary is not copied. If the underlying
/// dictionary is changed, then the read-only view also changes accordingly.</remarks>
/// <param name="dictionary">The dictionary to wrap.</param>
/// <returns>A read-only view onto <paramref name="dictionary"/>. Returns null if <paramref name="dictionary"/> is null.
/// If <paramref name="dictionary"/> is already read-only, returns <paramref name="dictionary"/>.</returns>
public static IDictionary<TKey,TValue> ReadOnly<TKey,TValue>(IDictionary<TKey,TValue> dictionary)
{
if (dictionary == null)
return null;
else if (dictionary.IsReadOnly)
return dictionary;
else
return new ReadOnlyDictionary<TKey,TValue>(dictionary);
}
/// <summary>
/// The class that provides a typed IEnumerator<T>
/// view onto an untyped IEnumerator interface.
/// </summary>
[Serializable]
private class TypedEnumerator<T> : IEnumerator<T>
{
private IEnumerator wrappedEnumerator;
/// <summary>
/// Create a typed IEnumerator<T>
/// view onto an untyped IEnumerator interface
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -