/// <summary> /// Provides an implementation of <see cref="ISet{T}.IsSubsetOf"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <param name="comparer">The comparer.</param> /// <returns>The result of <see cref="ISet{T}.IsSubsetOf"/>.</returns> /// <remarks> /// <para> /// From <see href="https://msdn.microsoft.com/en-us/library/dd412074(v=vs.110).aspx"/>; /// Determines whether a set is a subset of a specified collection. /// </para><para> /// If other contains the same elements as the current set, the current set is still considered a /// subset of other. /// </para><para> /// This method always returns false if the current set has elements that are not in other. /// </para> /// </remarks> public static bool IsSubsetOf <T>(ISet <T> source, IEnumerable <T> other, IEqualityComparer <T> comparer) { Contracts.Requires.That(source != null); Contracts.Requires.That(comparer != null); ISetContracts.IsSubsetOf(other); return(IsSubsetOf(source, other as HashSet <T> ?? new HashSet <T>(other, comparer))); }
/// <inheritdoc /> public void UnionWith(IEnumerable <IDisposable> other) { ISetContracts.UnionWith(this, other); if (this.IsDisposed) { return; } this.disposables.UnionWith(other); }
/// <inheritdoc /> public void SymmetricExceptWith(IEnumerable <IDisposable> other) { ISetContracts.SymmetricExceptWith(this, other); if (this.IsDisposed) { return; } this.disposables.SymmetricExceptWith(other); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.IsSubsetOf"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <returns>The result of <see cref="ISet{T}.IsSubsetOf"/>.</returns> /// <remarks> /// <para> /// From <see href="https://msdn.microsoft.com/en-us/library/dd412074(v=vs.110).aspx"/>; /// Determines whether a set is a subset of a specified collection. /// </para><para> /// If other contains the same elements as the current set, the current set is still considered a /// subset of other. /// </para><para> /// This method always returns false if the current set has elements that are not in other. /// </para> /// </remarks> public static bool IsSubsetOf <T>(ISet <T> source, HashSet <T> other) { Contracts.Requires.That(source != null); ISetContracts.IsSubsetOf(other); if (source.Count > other.Count) { return(false); } return(source.All(other.Contains)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.IsProperSupersetOf"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <returns>The result of <see cref="ISet{T}.IsProperSupersetOf"/>.</returns> /// <remarks> /// <para> /// From <see href="https://msdn.microsoft.com/en-us/library/dd411711(v=vs.110).aspx"/>; /// Determines whether the current set is a proper (strict) superset of a specified collection. /// </para><para> /// If the current set is a proper superset of other, the current set must have at least one element /// that other does not have. /// </para><para> /// An empty set is a proper superset of any other collection.Therefore, this method returns true if /// the collection represented by the other parameter is empty, unless the current set is also empty. /// </para><para> /// This method always returns false if the number of elements in the current set is less than or equal /// to the number of elements in other. /// </para> /// </remarks> public static bool IsProperSupersetOf <T>(ISet <T> source, HashSet <T> other) { Contracts.Requires.That(source != null); ISetContracts.IsProperSupersetOf(other); if (source.Count <= other.Count) { return(false); } return(other.IsProperSubsetOf(source)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.SetEquals"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <returns>The result of <see cref="ISet{T}.SetEquals"/>.</returns> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd412096(v=vs.110).aspx"/>; /// Determines whether the current set and the specified collection contain the same elements. /// </remarks> public static bool SetEquals <T>(ISet <T> source, HashSet <T> other) { Contracts.Requires.That(source != null); ISetContracts.SetEquals(other); if (source.Count != other.Count) { return(false); } return(other.SetEquals(source)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.Overlaps"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <returns>The result of <see cref="ISet{T}.Overlaps"/>.</returns> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd412095(v=vs.110).aspx"/>; /// Determines whether the current set overlaps with the specified collection. /// </remarks> public static bool Overlaps <T>(ISet <T> source, IEnumerable <T> other) { Contracts.Requires.That(source != null); ISetContracts.Overlaps(other); if (source.Count == 0 || other.IsEmpty()) { return(false); } return(other.Any(source.Contains)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.IntersectWith" />. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}" />.</param> /// <param name="other">The other enumerable.</param> /// <param name="comparer">The comparer.</param> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd394889(v=vs.110).aspx" />; /// Modifies the current set so that it contains only elements that are also in a specified collection. /// </remarks> public static void IntersectWith <T>(ISet <T> source, IEnumerable <T> other, IEqualityComparer <T> comparer) { Contracts.Requires.That(source != null); Contracts.Requires.That(comparer != null); ISetContracts.IntersectWith(source, other); if (source.IsReadOnly) { return; } IntersectWith(source, other as HashSet <T> ?? new HashSet <T>(other, comparer)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.IsSupersetOf"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <returns>The result of <see cref="ISet{T}.IsSupersetOf"/>.</returns> /// <remarks> /// <para> /// From <see href="https://msdn.microsoft.com/en-us/library/dd382354(v=vs.110).aspx"/>; /// Determines whether the current set is a superset of a specified collection. /// </para><para> /// If other contains the same elements as the current set, the current set is still considered /// a superset of other. /// </para><para> /// This method always returns false if the current set has fewer elements than other. /// </para> /// </remarks> public static bool IsSupersetOf <T>(ISet <T> source, IEnumerable <T> other) { Contracts.Requires.That(source != null); ISetContracts.IsSupersetOf(other); ICollection <T> otherCollection = other as ICollection <T>; if (otherCollection != null && source.Count < otherCollection.Count) { return(false); } return(other.All(source.Contains)); }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.ExceptWith"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd382043(v=vs.110).aspx"/>; /// Removes all elements in the specified collection from the current set. /// </remarks> public static void ExceptWith <T>(ISet <T> source, IEnumerable <T> other) { Contracts.Requires.That(source != null); ISetContracts.ExceptWith(source, other); if (source.IsReadOnly) { return; } foreach (T value in other) { source.Remove(value); } }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.SymmetricExceptWith"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd411718(v=vs.110).aspx"/>; /// 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. /// </remarks> public static void SymmetricExceptWith <T>(ISet <T> source, IEnumerable <T> other) { Contracts.Requires.That(source != null); ISetContracts.SymmetricExceptWith(source, other); if (source.IsReadOnly) { return; } foreach (T value in other) { // if source contains the value, remove it, otherwise add it if (!source.Remove(value)) { source.Add(value); } } }
/// <summary> /// Provides an implementation of <see cref="ISet{T}.IntersectWith"/>. /// </summary> /// <typeparam name="T">The type of values in the collections.</typeparam> /// <param name="source">The source collection that is implementing <see cref="ISet{T}"/>.</param> /// <param name="other">The other enumerable.</param> /// <remarks> /// From <see href="https://msdn.microsoft.com/en-us/library/dd394889(v=vs.110).aspx"/>; /// Modifies the current set so that it contains only elements that are also in a specified collection. /// </remarks> public static void IntersectWith <T>(ISet <T> source, HashSet <T> other) { Contracts.Requires.That(source != null); ISetContracts.IntersectWith(source, other); if (source.IsReadOnly) { return; } List <T> valuesToRemove = new List <T>(); foreach (T value in source) { if (!other.Contains(value)) { valuesToRemove.Add(value); } } source.RemoveMany(valuesToRemove); }
/// <inheritdoc /> public bool SetEquals(IEnumerable <IDisposable> other) { ISetContracts.SetEquals(other); return(this.disposables.SetEquals(other)); }
/// <inheritdoc /> public bool Overlaps(IEnumerable <IDisposable> other) { ISetContracts.Overlaps(other); return(this.disposables.Overlaps(other)); }
/// <inheritdoc /> public bool IsSupersetOf(IEnumerable <IDisposable> other) { ISetContracts.IsSupersetOf(other); return(this.disposables.IsSupersetOf(other)); }