You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.2 KiB
127 lines
3.2 KiB
5 years ago
|
// Needed for NET40
|
||
|
#if !NET_4_6
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using LinqInternal.Collections.ThreadSafe;
|
||
|
using LinqInternal.Core;
|
||
|
|
||
|
namespace LinqInternal.Collections.Specialized
|
||
|
{
|
||
|
[System.Diagnostics.DebuggerNonUserCode]
|
||
|
internal partial class DelegatedCollection<T> : ICollection<T>
|
||
|
{
|
||
|
private readonly IReadOnlyCollection<T> _readOnly;
|
||
|
private readonly Func<ICollection<T>> _wrapped;
|
||
|
|
||
|
public DelegatedCollection(Func<ICollection<T>> wrapped)
|
||
|
{
|
||
|
if (wrapped == null)
|
||
|
{
|
||
|
throw new ArgumentNullException("wrapped");
|
||
|
}
|
||
|
_wrapped = wrapped;
|
||
|
_readOnly = new ExtendedReadOnlyCollection<T>(this);
|
||
|
}
|
||
|
|
||
|
public IReadOnlyCollection<T> AsReadOnly
|
||
|
{
|
||
|
get { return _readOnly; }
|
||
|
}
|
||
|
|
||
|
public int Count
|
||
|
{
|
||
|
get { return Instance.Count; }
|
||
|
}
|
||
|
|
||
|
public bool IsReadOnly
|
||
|
{
|
||
|
get { return Instance.IsReadOnly; }
|
||
|
}
|
||
|
|
||
|
private ICollection<T> Instance
|
||
|
{
|
||
|
get { return _wrapped.Invoke() ?? ArrayReservoir<T>.EmptyArray; }
|
||
|
}
|
||
|
|
||
|
public void Add(T item)
|
||
|
{
|
||
|
Instance.Add(item);
|
||
|
}
|
||
|
|
||
|
public void Clear()
|
||
|
{
|
||
|
Instance.Clear();
|
||
|
}
|
||
|
|
||
|
public bool Contains(T item)
|
||
|
{
|
||
|
return Instance.Contains(item);
|
||
|
}
|
||
|
|
||
|
public bool Contains(T item, IEqualityComparer<T> comparer)
|
||
|
{
|
||
|
return System.Linq.Enumerable.Contains(Instance, item, comparer);
|
||
|
}
|
||
|
|
||
|
public void CopyTo(T[] array, int arrayIndex)
|
||
|
{
|
||
|
Instance.CopyTo(array, arrayIndex);
|
||
|
}
|
||
|
|
||
|
public void CopyTo(T[] array)
|
||
|
{
|
||
|
Instance.CopyTo(array, 0);
|
||
|
}
|
||
|
|
||
|
public void CopyTo(T[] array, int arrayIndex, int countLimit)
|
||
|
{
|
||
|
Extensions.CanCopyTo(array, arrayIndex, countLimit);
|
||
|
Instance.CopyTo(array, arrayIndex, countLimit);
|
||
|
}
|
||
|
|
||
|
public IEnumerator<T> GetEnumerator()
|
||
|
{
|
||
|
var collection = Instance;
|
||
|
foreach (var item in collection)
|
||
|
{
|
||
|
if (!ReferenceEquals(collection, Instance))
|
||
|
{
|
||
|
throw new InvalidOperationException();
|
||
|
}
|
||
|
yield return item;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public bool Remove(T item)
|
||
|
{
|
||
|
return Instance.Remove(item);
|
||
|
}
|
||
|
|
||
|
public bool Remove(T item, IEqualityComparer<T> comparer)
|
||
|
{
|
||
|
if (comparer == null)
|
||
|
{
|
||
|
comparer = EqualityComparer<T>.Default;
|
||
|
}
|
||
|
foreach (var foundItem in Instance.RemoveWhereEnumerable(input => comparer.Equals(input, item)))
|
||
|
{
|
||
|
GC.KeepAlive(foundItem);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
||
|
{
|
||
|
return GetEnumerator();
|
||
|
}
|
||
|
|
||
|
public T[] ToArray()
|
||
|
{
|
||
|
var array = new T[Instance.Count];
|
||
|
Instance.CopyTo(array, 0);
|
||
|
return array;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#endif
|