Removed all backwards compatibility code and instead referenced Theraot.Core.

pull/30/head
David Hall 2018-12-11 11:34:00 -07:00
parent 5282b5225d
commit 99364ad83d
8 changed files with 4 additions and 1292 deletions

View File

@ -1,11 +0,0 @@
#if (NET20)
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Attribute allowing extenders to be used with .NET Framework 2.0.
/// </summary>
public sealed class ExtensionAttribute : Attribute
{
}
}
#endif

View File

@ -1,46 +0,0 @@
#if (NET20)
namespace System
{
/// <summary>Encapsulates a method that returns a value of the type specified by the TResult parameter.</summary>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<out TResult>();
/// <summary>Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.</summary>
/// <typeparam name="T">The type of the parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg">The parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<in T, out TResult>(T arg);
/// <summary>Encapsulates a method that has two parameters and returns a value of the type specified by the TResult parameter.</summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
/// <summary>Encapsulates a method that has two parameters and returns a value of the type specified by the TResult parameter.</summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T3">The type of the third parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="TResult">The type of the return value of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
/// <param name="arg3">The third parameter of the method that this delegate encapsulates.</param>
/// <returns>The return value of the method that this delegate encapsulates.</returns>
public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3);
/// <summary>Encapsulates a method that has no parameters and does not return a value.</summary>
public delegate void Action();
/// <summary>Encapsulates a method that has two parameters and does not return a value.</summary>
/// <typeparam name="T1">The type of the first parameter of the method that this delegate encapsulates.</typeparam>
/// <typeparam name="T2">The type of the second parameter of the method that this delegate encapsulates.</typeparam>
/// <param name="arg1">The first parameter of the method that this delegate encapsulates.</param>
/// <param name="arg2">The second parameter of the method that this delegate encapsulates.</param>
public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
}
#endif

View File

@ -1,522 +0,0 @@
#if NET20
using System.Linq;
using System.Runtime.Serialization;
namespace System.Collections.Generic
{
/// <summary>Provides the base interface for the abstraction of sets.</summary>
public interface ISet<T> : ICollection<T>
{
/// <summary>Adds an element to the current set and returns a value to indicate if the element was successfully added.</summary>
/// <param name="item">The element to add to the set.</param>
/// <returns><c>true</c> if the element is added to the set; <c>false</c> if the element is already in the set.</returns>
new bool Add(T item);
/// <summary>Removes all elements in the specified collection from the current set.</summary>
/// <param name="other">The collection of items to remove from the set.</param>
void ExceptWith(IEnumerable<T> other);
/// <summary>Modifies the current set so that it contains only elements that are also in a specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
void IntersectWith(IEnumerable<T> other);
/// <summary>Determines whether the current set is a proper (strict) subset of a specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set is a proper subset of <paramref name="other"/>; otherwise, <c>false</c>.</returns>
bool IsProperSubsetOf(IEnumerable<T> other);
/// <summary>Determines whether the current set is a proper (strict) superset of a specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set is a proper superset of <paramref name="other"/>; otherwise, <c>false</c>.</returns>
bool IsProperSupersetOf(IEnumerable<T> other);
/// <summary>Determines whether a set is a subset of a specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set is a subset of <paramref name="other"/>; otherwise, <c>false</c>.</returns>
bool IsSubsetOf(IEnumerable<T> other);
/// <summary>Determines whether the current set is a superset of a specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set is a superset of <paramref name="other"/>; otherwise, <c>false</c>.</returns>
bool IsSupersetOf(IEnumerable<T> other);
/// <summary>Determines whether the current set overlaps with the specified collection.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set and <paramref name="other"/> share at least one common element; otherwise, <c>false</c>.</returns>
bool Overlaps(IEnumerable<T> other);
/// <summary>Determines whether the current set and the specified collection contain the same elements.</summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns><c>true</c> if the current set is equal to <paramref name="other"/>; otherwise, <c>false</c>.</returns>
bool SetEquals(IEnumerable<T> other);
/// <summary>
/// Modifies the current set so that it contains only elements that are present either in the current set or in the specified
/// collection, but not both.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
void SymmetricExceptWith(IEnumerable<T> other);
/// <summary>
/// Modifies the current set so that it contains all elements that are present in the current set, in the specified collection, or in both.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
void UnionWith(IEnumerable<T> other);
}
/// <summary>
/// Represents a set of values. This is less efficient than the native implementation in .NET versions after 2.0, but functionally equivalent.
/// </summary>
/// <typeparam name="T">The type of elements in the hash set.</typeparam>
public class HashSet<T> : ISet<T>, IReadOnlyCollection<T>, ISerializable, IDeserializationCallback
{
private readonly Dictionary<T, object> dict;
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that is empty and uses the default equality comparer for the set type.
/// </summary>
public HashSet() : this((IEqualityComparer<T>)null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that uses the default equality comparer for the set type,
/// contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied..
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new set.</param>
public HashSet(IEnumerable<T> collection) : this(collection, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that is empty and uses the specified equality comparer for the
/// set type..
/// </summary>
/// <param name="comparer">
/// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing values in the set, or <c>null</c> to use the default
/// <see cref="EqualityComparer{T}"/> implementation for the set type.
/// </param>
public HashSet(IEqualityComparer<T> comparer)
{
dict = new Dictionary<T, object>(comparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that uses the specified equality comparer for the set type,
/// contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied..
/// </summary>
/// <param name="collection">The collection whose elements are copied to the new set.</param>
/// <param name="comparer">
/// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing values in the set, or <c>null</c> to use the default
/// <see cref="EqualityComparer{T}"/> implementation for the set type.
/// </param>
public HashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
{
dict = new Dictionary<T, object>(comparer);
foreach (var elem in collection)
{
Add(elem);
}
}
/* ***** Unnecessary since implemented in .NET 4.7.2
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that is empty, but has reserved space for capacity items and
/// uses the default equality comparer for the set type..
/// </summary>
/// <param name="capacity">The initial size of the <see cref="HashSet{T}"/></param>
public HashSet(int capacity) : this(capacity, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet{T}"/> class that uses the specified equality comparer for the set type, and
/// has sufficient capacity to accommodate capacity elements..
/// </summary>
/// <param name="capacity">The initial size of the <see cref="HashSet{T}"/></param>
/// <param name="comparer">
/// The <see cref="IEqualityComparer{T}"/> implementation to use when comparing values in the set, or <c>null</c> to use the default
/// <see cref="EqualityComparer{T}"/> implementation for the set type.
/// </param>
public HashSet(int capacity, IEqualityComparer<T> comparer)
{
dict = new Dictionary<T, object>(capacity, comparer);
}
*/
/// <summary>Gets the <see cref="IEqualityComparer{T}"/> object that is used to determine equality for the values in the set.</summary>
public IEqualityComparer<T> Comparer => dict.Comparer;
/// <inheritdoc cref="ICollection{T}.Count"/>
public int Count => dict.Count;
/// <inheritdoc/>
bool ICollection<T>.IsReadOnly { get; } = false;
/// <inheritdoc/>
public bool Add(T item)
{
if (null == item)
{
throw new ArgumentNullException(nameof(item));
}
if (Contains(item))
{
return false;
}
dict[item] = null;
return true;
}
/// <inheritdoc/>
public void Clear()
{
dict.Clear();
}
/// <inheritdoc/>
public bool Contains(T item)
{
return item != null && dict.ContainsKey(item);
}
/// <inheritdoc/>
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0 || arrayIndex >= array.Length || arrayIndex >= Count)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
dict.Keys.CopyTo(array, arrayIndex);
}
/// <inheritdoc/>
public void ExceptWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0)
{
return;
}
if (other == this)
{
Clear();
return;
}
foreach (T elem in other)
{
dict.Remove(elem);
}
}
/// <inheritdoc/>
public IEnumerator<T> GetEnumerator()
{
return dict.Keys.GetEnumerator();
}
/// <inheritdoc/>
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
dict.GetObjectData(info, context);
}
/// <inheritdoc/>
public void IntersectWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (other is ICollection<T> c && c.Count == 0)
{
Clear();
return;
}
var l = this.ToList();
foreach (var elem in l)
{
// ReSharper disable once PossibleMultipleEnumeration
if (!other.Contains(elem))
{
Remove(elem);
}
}
}
/// <inheritdoc/>
public bool IsProperSubsetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0 && other is ICollection<T> c)
{
return c.Count > 0;
}
CheckUniqueAndUnfoundElements(other, out int unique, out int unfound, false);
return unique == Count && unfound > 0;
}
/// <inheritdoc/>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0)
{
return false;
}
if (other is ICollection<T> c && c.Count == 0)
{
return true;
}
CheckUniqueAndUnfoundElements(other, out int unique, out int unfound, true);
return unique < Count && unfound == 0;
}
/// <inheritdoc/>
public bool IsSubsetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0)
{
return true;
}
CheckUniqueAndUnfoundElements(other, out int unique, out int unfound, false);
return unique == Count && unfound >= 0;
}
/// <inheritdoc/>
public bool IsSupersetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (other is ICollection<T> c && c.Count == 0)
{
return true;
}
foreach (T elem in other)
{
if (!Contains(elem))
{
return false;
}
}
return true;
}
/// <inheritdoc/>
public virtual void OnDeserialization(object sender)
{
dict.OnDeserialization(sender);
}
/// <inheritdoc/>
public bool Overlaps(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0)
{
return false;
}
foreach (T elem in other)
{
if (Contains(elem))
{
return true;
}
}
return false;
}
/// <inheritdoc/>
public bool Remove(T item)
{
return item != null && dict.Remove(item);
}
/// <inheritdoc/>
public bool SetEquals(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (other is ICollection<T> c && c.Count != Count)
{
return false;
}
CheckUniqueAndUnfoundElements(other, out int unique, out int unfound, true);
return unique == Count && unfound == 0;
}
/// <inheritdoc/>
public void SymmetricExceptWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (Count == 0)
{
UnionWith(other);
return;
}
if (other == this)
{
Clear();
return;
}
HashSet<T> oh = new HashSet<T>(other, Comparer);
List<T> dup = this.ToList();
ExceptWith(oh);
oh.ExceptWith(dup);
UnionWith(oh);
}
/// <summary>Searches the set for a given value and returns the equal value it finds, if any.</summary>
/// <param name="equalValue">The value to search for.</param>
/// <param name="actualValue">
/// The value from the set that the search found, or the default value of T when the search yielded no match.
/// </param>
/// <returns>A value indicating whether the search was successful.</returns>
/// <remarks>
/// This can be useful when you want to reuse a previously stored reference instead of a newly constructed one (so that more sharing
/// of references can occur) or to look up a value that has more complete data than the value you currently have, although their
/// comparer functions indicate they are equal.
/// </remarks>
public bool TryGetValue(T equalValue, out T actualValue)
{
foreach (T k in dict.Keys)
{
if (!Comparer.Equals(k, equalValue))
{
continue;
}
actualValue = k;
return true;
}
actualValue = default;
return false;
}
/// <inheritdoc/>
public void UnionWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
if (this == other)
{
return;
}
foreach (T elem in other)
{
Add(elem);
}
}
/// <inheritdoc/>
void ICollection<T>.Add(T item)
{
Add(item);
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private void CheckUniqueAndUnfoundElements(IEnumerable<T> other, out int unique, out int unfound, bool returnIfUnfound)
{
if (Count == 0)
{
unique = 0;
unfound = other.Any() ? 1 : 0;
return;
}
unfound = 0;
unique = 0;
List<T> l = this.ToList();
BitArray bits = new BitArray(l.Count);
foreach (T o in other)
{
int index = l.IndexOf(o);
if (index >= 0)
{
if (bits[index])
{
continue;
}
// item hasn't been seen yet
bits[index] = true;
unique++;
}
else
{
unfound++;
if (returnIfUnfound)
{
break;
}
}
}
}
}
}
#endif

View File

@ -1,474 +0,0 @@
#if (NET20)
using System.Collections;
using System.Collections.Generic;
namespace System.Linq
{
/// <summary>Provides a set of static (Shared in Visual Basic) methods for querying objects that implement <see cref="IEnumerable{T}"/>.</summary>
public static class Enumerable
{
/// <summary>Determines whether all elements of a sequence satisfy a condition.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> whose elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns><c>true</c> if all elements in the source sequence pass the test in the specified predicate; otherwise, <c>false</c>.</returns>
public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (TSource element in source)
if (!predicate(element)) return false;
return true;
}
/// <summary>Determines whether any element of a sequence satisfies a condition.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> whose elements to apply the predicate to.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns><c>true</c> if any elements in the source sequence pass the test in the specified predicate; otherwise, <c>false</c>.</returns>
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate = null)
{
if (source == null) throw new ArgumentNullException(nameof(source));
foreach (TSource element in source)
if (predicate == null || predicate(element)) return true;
return false;
}
/// <summary>Casts the elements of an <see cref="IEnumerable"/> to the specified type.</summary>
/// <typeparam name="TResult">The type to cast the elements of source to.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> that contains the elements to be cast to type <typeparamref name="TResult"/>.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains each element of the source sequence cast to the specified type.</returns>
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
{
foreach (var i in source)
yield return (TResult)i;
}
/// <summary>Determines whether a sequence contains a specified element by using the default equality comparer.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">A sequence in which to locate a value.</param>
/// <param name="value">The value to locate in the sequence.</param>
/// <returns><c>true</c> if the source sequence contains an element that has the specified value; otherwise, <c>false</c>.</returns>
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value)
{
foreach (var i in source)
if (i.Equals(value)) return true;
return false;
}
/// <summary>Returns the number of elements in a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">A sequence that contains elements to be counted.</param>
/// <returns>The number of elements in the input sequence.</returns>
public static int Count<TSource>(this IEnumerable<TSource> source)
{
switch (source)
{
case null:
throw new ArgumentNullException(nameof(source));
case ICollection<TSource> c:
return c.Count;
case ICollection ngc:
return ngc.Count;
default:
var i = 0;
foreach (var e in source) i++;
return i;
}
}
/// <summary>Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The sequence to return the specified value for if it is empty.</param>
/// <param name="defaultValue">The value to return if the sequence is empty. This value defaults to <c>default(TSource)</c>.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains <paramref name="defaultValue"/> if source is empty; otherwise, source.</returns>
public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source, TSource defaultValue = default)
{
if (source == null) throw new ArgumentNullException(nameof(source));
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
do
{
yield return e.Current;
} while (e.MoveNext());
}
else
{
yield return defaultValue;
}
}
}
/// <summary>Returns distinct elements from a sequence by using the default equality comparer to compare values.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains distinct elements from the source sequence.</returns>
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
{
var set = new Hashtable();
foreach (var element in source)
if (!set.ContainsKey(element))
{
set.Add(element, null);
yield return element;
}
}
/// <summary>Returns the first element of a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> to return the first element of.</param>
/// <returns>The first element in the specified sequence.</returns>
public static TSource First<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source is IList<TSource> list)
{
if (list.Count > 0) return list[0];
}
else
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext()) return e.Current;
}
}
throw new InvalidOperationException(@"No elements");
}
/// <summary>Returns the first element of a sequence that satisfies a specified condition.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> to return the first element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>The first element in the sequence that passes the test in the specified predicate function.</returns>
public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (var element in source)
if (predicate(element)) return element;
throw new InvalidOperationException(@"No match");
}
/// <summary>Returns the first element of a sequence, or a default value if the sequence contains no elements.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> to return the first element of.</param>
/// <returns><c>default( <typeparamref name="TSource"/>)</c> if <paramref name="source"/> is empty; otherwise, the first element in <paramref name="source"/>.</returns>
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source is IList<TSource> list)
{
if (list.Count > 0) return list[0];
}
else
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext()) return e.Current;
}
}
return default;
}
/// <summary>Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to return an element from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns><c>default(<typeparamref name="TSource"/>)</c> if <paramref name="source"/> is empty or if no element passes the test specified by <paramref name="predicate"/>; otherwise, the first element in <paramref name="source"/> that passes the test specified by <paramref name="predicate"/>.</returns>
public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (var element in source)
if (predicate(element)) return element;
return default;
}
/// <summary>Returns the minimum value in a generic sequence.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence of values to determine the minimum value of.</param>
/// <returns>The minimum value in the sequence.</returns>
public static TSource Min<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var comparer = Comparer<TSource>.Default;
var value = default(TSource);
if (value == null)
{
foreach (var x in source)
{
if (x != null && (value == null || comparer.Compare(x, value) < 0))
value = x;
}
return value;
}
var hasValue = false;
foreach (var x in source)
{
if (hasValue)
{
if (comparer.Compare(x, value) < 0)
value = x;
}
else
{
value = x;
hasValue = true;
}
}
if (hasValue) return value;
throw new InvalidOperationException("No elements");
}
/// <summary>Returns the maximum value in a generic sequence.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence of values to determine the maximum value of.</param>
/// <returns>The maximum value in the sequence.</returns>
public static TSource Max<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var comparer = Comparer<TSource>.Default;
var value = default(TSource);
if (value == null) {
foreach (var x in source) {
if (x != null && (value == null || comparer.Compare(x, value) > 0))
value = x;
}
return value;
}
var hasValue = false;
foreach (var x in source) {
if (hasValue) {
if (comparer.Compare(x, value) > 0)
value = x;
}
else {
value = x;
hasValue = true;
}
}
if (hasValue) return value;
throw new InvalidOperationException("No elements");
}
/// <summary>Filters the elements of an <see cref="IEnumerable"/> based on a specified type.</summary>
/// <typeparam name="TResult">The type to filter the elements of the sequence on.</typeparam>
/// <param name="source">The <see cref="IEnumerable"/> whose elements to filter.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains elements from the input sequence of type <typeparamref name="TResult"/>.</returns>
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
foreach (object obj in source)
{
if (obj is TResult) yield return (TResult)obj;
}
}
/// <summary>Sorts the elements of a sequence in ascending order according to a key.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
/// <param name="source">A sequence of values to order.</param>
/// <param name="keySelector">A function to extract a key from an element.</param>
/// <returns>An <see cref="IEnumerable{T}"/> whose elements are sorted according to a key.</returns>
public static IEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var d = new SortedDictionary<TKey, TSource>();
foreach (var item in source)
d.Add(keySelector(item), item);
return d.Values;
}
/// <summary>Sorts the elements of a sequence in descending order.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
/// <param name="source">A sequence of values to order.</param>
/// <param name="keySelector">A function to extract a key from an element.</param>
/// <returns>An <see cref="IEnumerable{T}"/> whose elements are sorted in descending order according to a key.</returns>
public static IEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var d = new SortedDictionary<TKey, TSource>();
foreach (var item in source)
d.Add(keySelector(item), item);
return d.Values.Reverse();
}
/// <summary>Inverts the order of the elements in a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence of values to reverse.</param>
/// <returns>A sequence whose elements correspond to those of the input sequence in reverse order.</returns>
public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
IList<TSource> items = source as IList<TSource> ?? source.ToList();
for (int i = items.Count - 1; i >= 0; i--) yield return items[i];
}
/// <summary>Projects each element of a sequence into a new form.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TResult">The type of the value returned by <paramref name="selector"/>.</typeparam>
/// <param name="source">A sequence of values to invoke a transform function on.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An <see cref="IEnumerable{T}"/> whose elements are the result of invoking the transform function on each element of <paramref name="source"/>.</returns>
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
foreach (var i in source)
yield return selector(i);
}
/// <summary>Projects each element of a sequence to an <see cref="IEnumerable{T}"/> and flattens the resulting sequences into one sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TResult">The type of the elements of the sequence returned by <paramref name="selector"/>.</typeparam>
/// <param name="source">A sequence of values to project.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>An <see cref="IEnumerable{T}"/> whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.</returns>
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));
foreach (TSource element in source)
{
foreach (TResult subElement in selector(element))
{
yield return subElement;
}
}
}
/// <summary>Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to return a single element from.</param>
/// <param name="predicate">A function to test an element for a condition.</param>
/// <returns>The single element of the input sequence that satisfies a condition.</returns>
public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
var result = default(TSource);
long count = 0;
foreach (var element in source)
{
if (!predicate(element)) continue;
result = element;
checked { count++; }
}
if (count == 0) throw new InvalidOperationException(@"No matches");
if (count != 1) throw new InvalidOperationException(@"More than one match.");
return result;
}
/// <summary>Computes the sum of a sequence of nullable <see cref="Int32"/> values.</summary>
/// <param name="source">A sequence of nullable <see cref="Int32"/> values to calculate the sum of.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static int Sum(this IEnumerable<int> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
int sum = 0;
checked
{
foreach (int v in source) sum += v;
}
return sum;
}
/// <summary>
/// Computes the sum of the sequence of nullable <see cref="Int32"/> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">A sequence of values that are used to calculate a sum.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>The sum of the projected values.</returns>
public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector) => Sum(Select(source, selector));
/// <summary>Returns a specified number of contiguous elements from the start of a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence to return elements from.</param>
/// <param name="count">The number of elements to return.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains the specified number of elements from the start of the input sequence.</returns>
public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (count > 0)
{
foreach (TSource element in source)
{
yield return element;
if (--count == 0) break;
}
}
}
/// <summary>Returns elements from a sequence as long as a specified condition is true.</summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence to return elements from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains the elements from the input sequence that occur before the element at which the test no longer passes.</returns>
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (TSource element in source)
{
if (!predicate(element)) break;
yield return element;
}
}
/// <summary>Creates an array from a <see cref="IEnumerable"/>.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to create an array from.</param>
/// <returns>An array that contains the elements from the input sequence.</returns>
public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source) => ToList(source).ToArray();
/// <summary>
/// Creates a <see cref="Dictionary{TKey,TValue}"/> from an <see cref="IEnumerable{T}"/> according to a specified key selector function, a comparer, and
/// an element selector function.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
/// <typeparam name="TElement">The type of the value returned by <paramref name="elementSelector"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to create a <see cref="Dictionary{TKey,TValue}"/> from.</param>
/// <param name="keySelector">A function to extract a key from each element.</param>
/// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
/// <param name="comparer">An <see cref="IEqualityComparer{T}"/> to compare keys.</param>
/// <returns>A <see cref="Dictionary{TKey,TValue}"/> that contains values of type TElement selected from the input sequence.</returns>
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
if (elementSelector == null) throw new ArgumentNullException(nameof(elementSelector));
var d = new Dictionary<TKey, TElement>(comparer);
foreach (var element in source) d.Add(keySelector(element), elementSelector(element));
return d;
}
/// <summary>Creates a <see cref="List{T}"/> from an <see cref="IEnumerable{T}"/>.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to create a <see cref="List{T}"/> from.</param>
/// <returns>A <see cref="List{T}"/> that contains elements from the input sequence.</returns>
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
var l = new List<TSource>();
foreach (var i in source)
l.Add(i);
return l;
}
/// <summary>Filters a sequence of values based on a predicate.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}"/> to filter.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>An <see cref="IEnumerable{T}"/> that contains elements from the input sequence that satisfy the condition.</returns>
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (var i in source)
if (predicate(i)) yield return i;
}
}
}
#endif

View File

@ -1,63 +0,0 @@
#if NET20 || NET35 || NET40
namespace System.Collections.Generic
{
/// <summary>Represents a strongly-typed, read-only collection of elements.</summary>
/// <typeparam name="T">The type of the elements.</typeparam>
/// <seealso cref="System.Collections.Generic.IEnumerable{T}"/>
public interface IReadOnlyCollection<T> : IEnumerable<T>
{
/// <summary>Gets the number of elements in the collection.</summary>
/// <value>The number of elements in the collection.</value>
int Count { get; }
}
/// <summary>Represents a generic read-only collection of key/value pairs.</summary>
/// <typeparam name="TKey">The type of keys in the read-only dictionary.</typeparam>
/// <typeparam name="TValue">The type of values in the read-only dictionary.</typeparam>
public interface IReadOnlyDictionary<TKey, TValue> : IReadOnlyCollection<KeyValuePair<TKey, TValue>>
{
/// <summary>Determines whether the read-only dictionary contains an element that has the specified key.</summary>
/// <param name="key">The key to locate.</param>
/// <returns>
/// <see langword="true"/> if the read-only dictionary contains an element that has the specified key; otherwise, <see langword="false"/>.
/// </returns>
bool ContainsKey(TKey key);
/// <summary>Gets the value that is associated with the specified key.</summary>
/// <param name="key">The key to locate.</param>
/// <param name="value">
/// When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the
/// type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.
/// </param>
/// <returns>
/// <see langword="true"/> if the object that implements the <see cref="IReadOnlyDictionary{TKey, TValue}"/> interface contains an
/// element that has the specified key; otherwise, <see langword="false"/>.
/// </returns>
bool TryGetValue(TKey key, out TValue value);
/// <summary>Gets the element that has the specified key in the read-only dictionary.</summary>
/// <value>The key to locate.</value>
/// <returns>The element that has the specified key in the read-only dictionary.</returns>
TValue this[TKey key] { get; }
/// <summary>Gets an enumerable collection that contains the keys in the read-only dictionary.</summary>
/// <value>An enumerable collection that contains the keys in the read-only dictionary.</value>
IEnumerable<TKey> Keys { get; }
/// <summary>Gets an enumerable collection that contains the values in the read-only dictionary.</summary>
/// <value>An enumerable collection that contains the values in the read-only dictionary.</value>
IEnumerable<TValue> Values { get; }
}
/// <summary>Represents a read-only collection of elements that can be accessed by index.</summary>
/// <typeparam name="T">The type of elements in the read-only list.</typeparam>
/// <seealso cref="System.Collections.Generic.IReadOnlyCollection{T}"/>
public interface IReadOnlyList<T> : IReadOnlyCollection<T>
{
/// <summary>Gets the element at the specified index in the read-only list.</summary>
/// <value>The element at the specified index in the read-only list.</value>
/// <param name="index">The zero-based index of the element to get.</param>
T this[int index] { get; }
}
}
#endif

View File

@ -1,142 +0,0 @@
#if (NET20)
namespace System.Collections.Specialized
{
/// <summary>
/// Stub
/// </summary>
public interface INotifyCollectionChanged
{
/// <summary>
/// Stub
/// </summary>
event NotifyCollectionChangedEventHandler CollectionChanged;
}
/// <summary>
/// Stub
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
public delegate void NotifyCollectionChangedEventHandler(object sender, NotifyCollectionChangedEventArgs e);
/// <summary>
/// Stub
/// </summary>
public class NotifyCollectionChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="NotifyCollectionChangedEventArgs"/> class.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="newItems">The new items.</param>
/// <param name="oldItems">The old items.</param>
/// <param name="newIndex">The new index.</param>
/// <param name="oldIndex">The old index.</param>
internal NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, IList oldItems, int newIndex, int oldIndex)
{
Action = action;
NewItems = newItems;
NewStartingIndex = newIndex;
OldItems = oldItems;
OldStartingIndex = oldIndex;
}
/// <summary>
/// Initializes a new instance of the <see cref="NotifyCollectionChangedEventArgs"/> class.
/// </summary>
/// <param name="action">The action.</param>
public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action) :
this(action, null, null, -1, -1) { }
/// <summary>
/// Initializes a new instance of the <see cref="NotifyCollectionChangedEventArgs"/> class.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="item">The item.</param>
/// <param name="idx">The index.</param>
public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object item, int idx = -1) :
this(action, new object[] { item }, null, idx, -1) { }
/// <summary>
/// Initializes a new instance of the <see cref="NotifyCollectionChangedEventArgs"/> class.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="item">The item.</param>
/// <param name="item2">The item2.</param>
/// <param name="idx">The index.</param>
public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object item, object item2, int idx) :
this(action, new object[] { item }, new object[] { item2 }, idx, -1) { }
/// <summary>
/// Initializes a new instance of the <see cref="NotifyCollectionChangedEventArgs"/> class.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="newItems">The new items.</param>
public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems) :
this(action, newItems, null, -1, -1) { }
/// <summary>
/// Gets the action.
/// </summary>
/// <value>
/// The action.
/// </value>
public NotifyCollectionChangedAction Action { get; }
/// <summary>
/// Gets the new items.
/// </summary>
/// <value>
/// The new items.
/// </value>
public IList NewItems { get; }
/// <summary>
/// Gets the new index of the starting.
/// </summary>
/// <value>
/// The new index of the starting.
/// </value>
public int NewStartingIndex { get; }
/// <summary>
/// Gets the old items.
/// </summary>
/// <value>
/// The old items.
/// </value>
public IList OldItems { get; }
/// <summary>
/// Gets the old index of the starting.
/// </summary>
/// <value>
/// The old index of the starting.
/// </value>
public int OldStartingIndex { get; }
}
/// <summary>
/// Stub
/// </summary>
public enum NotifyCollectionChangedAction
{
/// <summary>
/// The add
/// </summary>
Add = 0,
/// <summary>
/// The move
/// </summary>
Move = 3,
/// <summary>
/// The remove
/// </summary>
Remove = 1,
/// <summary>
/// The replace
/// </summary>
Replace = 2,
/// <summary>
/// The reset
/// </summary>
Reset = 4
}
}
#endif

View File

@ -1,33 +0,0 @@
#if (NET20 || NET35)
namespace System
{
/// <summary>Represents a 2-tuple, or pair.</summary>
/// <typeparam name="T1">The type of the tuple's first component.</typeparam>
/// <typeparam name="T2">The type of the tuple's second component.</typeparam>
[Serializable]
public class Tuple<T1, T2>
{
private readonly T1 m_Item1;
private readonly T2 m_Item2;
/// <summary>Initializes a new instance of the <see cref="Tuple{T1, T2}"/> class.</summary>
/// <param name="item1">The value of the tuple's first component.</param>
/// <param name="item2">The value of the tuple's second component.</param>
public Tuple(T1 item1, T2 item2) { m_Item1 = item1; m_Item2 = item2; }
/// <summary>Gets the value of the current <see cref="Tuple{T1, T2}"/> object's first component.</summary>
/// <value>The value of the current <see cref="Tuple{T1, T2}"/> object's first component.</value>
public T1 Item1 => m_Item1;
/// <summary>Gets the value of the current <see cref="Tuple{T1, T2}"/> object's second component.</summary>
/// <value>The value of the current <see cref="Tuple{T1, T2}"/> object's second component.</value>
public T2 Item2 => m_Item2;
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString() => $"({m_Item1}, {m_Item2})";
}
}
#endif

View File

@ -9,7 +9,7 @@
* Memory stream based on marshaled memory</Description>
<Copyright>Copyright © 2017-2018</Copyright>
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
<VersionPrefix>2.0.1</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<TargetFrameworks>net20;net35;net40;net45;netstandard20</TargetFrameworks>
<AssemblyName>Vanara.Core</AssemblyName>
<PackageId>$(AssemblyName)</PackageId>
@ -73,4 +73,7 @@ CorrepsondingAction, StringListPackMethod
<None Include="Collections\Tree.cs" />
<None Include="Collections\VaList.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Theraot.Core" Version="2.1.0" Condition=" '$(TargetFramework)' != 'netstandard20' " />
</ItemGroup>
</Project>