Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Paymon Khamooshi committed Dec 20, 2023
1 parent e99e785 commit eb522a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
47 changes: 29 additions & 18 deletions Olive/-Extensions/Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ public static string ToString<T>(this IEnumerable<T> @this, string separator, st
/// <param name="element">The item which is searched.</param>
public static int IndexOf<T>(this IEnumerable<T> @this, T element)
{
if (@this == null)
if (@this is null)
throw new NullReferenceException("No collection is given for the extension method IndexOf().");

if (@this.Contains(element) == false) return -1;
var result = 0;

foreach (var el in @this)
Expand Down Expand Up @@ -184,23 +183,32 @@ public static void RemoveWhere<T>(this IList<T> @this, Func<T, bool> selector)
/// <param name="items">The list of items which are not returned.</param>
public static IEnumerable<T> Except<T>(this IEnumerable<T> list, params T[] items)
{
if (items == null) return list;
if (items is null) return list;
return list.Except(new HashSet<T>(items));
}

/// <summary>
/// Gets all items of this list except those meeting a specified criteria.
/// </summary>
/// <param name="items">The list of items which are not returned.</param>
public static IEnumerable<T> Except<T>(this IEnumerable<T> list, HashSet<T> items)
{
if (items is null) return list;
return list.Where(x => !items.Contains(x));
}

/// <summary>
/// Gets all items of this list except those meeting a specified criteria.
/// </summary>
/// <param name="itemsToExclude">The list of items which are not returned.</param>
public static IEnumerable<T> Except<T>(this IEnumerable<T> @this, List<T> itemsToExclude) => @this.Except(itemsToExclude.ToArray());
public static IEnumerable<T> Except<T>(this IEnumerable<T> @this, List<T> itemsToExclude) => @this.Except(new HashSet<T>(itemsToExclude));

/// <summary>
/// Gets all items of this list except those meeting a specified criteria.
/// </summary>
/// <param name="itemsToExclude">The list of items which are not returned.</param>
public static IEnumerable<char> Except(this IEnumerable<char> @this, IEnumerable<char> itemsToExclude) =>
@this.Except(itemsToExclude.ToArray());
@this.Except(new HashSet<char>(itemsToExclude));

/// <summary>
/// Gets all items of this list except those meeting a specified criteria.
Expand All @@ -209,7 +217,7 @@ public static IEnumerable<char> Except(this IEnumerable<char> @this, IEnumerable
/// <param name="alsoDistinct">if {alsoDistinct} is true, it returns distinct elements from a sequence. The default value is false.</param>
public static IEnumerable<T> Except<T>(this IEnumerable<T> @this, IEnumerable<T> itemsToExclude, bool alsoDistinct = false)
{
var result = @this.Except(itemsToExclude.ToArray());
var result = @this.Except(new HashSet<T>(itemsToExclude));

if (alsoDistinct) result = result.Distinct();

Expand All @@ -221,7 +229,7 @@ public static IEnumerable<T> Except<T>(this IEnumerable<T> @this, IEnumerable<T>
/// </summary>
/// <param name="itemsToExclude">The list of items which are not returned.</param>
public static IEnumerable<string> Except(this IEnumerable<string> @this, IEnumerable<string> itemsToExclude) =>
@this.Except(itemsToExclude.ToArray());
@this.Except(new HashSet<string>(itemsToExclude));

/// <summary>
/// Gets all non-default items of this list.
Expand All @@ -231,7 +239,7 @@ public static IEnumerable<string> Except(this IEnumerable<string> @this, IEnumer
/// <summary>
/// Gets all Non-NULL items of this list.
/// </summary>
public static IEnumerable<T> ExceptNull<T>(this IEnumerable<T> @this) => @this.Where(i => i != null);
public static IEnumerable<T> ExceptNull<T>(this IEnumerable<T> @this) => @this.Except(i => i is null);

/// <summary>
/// Gets all Non-NULL items of this list.
Expand Down Expand Up @@ -485,8 +493,10 @@ public static bool IsEquivalentTo<T>(this IEnumerable<T> @this, IEnumerable<T> o

if (@this.Count() != other.Count()) return false;

var set = new HashSet<T>(other);

foreach (var item in @this)
if (!other.Contains(item)) return false;
if (!set.Contains(item)) return false;

return true;
}
Expand Down Expand Up @@ -595,8 +605,11 @@ public static IEnumerable<T> Distinct<T, TResult>(this IEnumerable<T> @this, Fun
/// Determines of this list contains all items of another given list.
/// </summary>
/// <param name="items">Determines the list of items which checked with the main list.</param>
public static bool ContainsAll<T>(this IEnumerable<T> @this, IEnumerable<T> items) =>
items.All(i => @this.Contains(i));
public static bool ContainsAll<T>(this IEnumerable<T> @this, IEnumerable<T> items)
{
var set = new HashSet<T>(@this);
return items.All(i => set.Contains(i));
}

/// <summary>
/// Determines if this list contains any of the specified items.
Expand Down Expand Up @@ -631,18 +644,16 @@ public static bool Intersects<T>(this IEnumerable<T> @this, IEnumerable<T> other
var countList = (@this as ICollection)?.Count;
var countOther = (otherList as ICollection)?.Count;

if (countList == null || countOther == null || countOther < countList)
if (countList is null || countOther is null || countOther < countList)
{
foreach (var item in otherList)
if (@this.Contains(item)) return true;
var set = new HashSet<T>(@this);
return otherList.Any(set.Contains);
}
else
{
foreach (var item in @this)
if (otherList.Contains(item)) return true;
var set = new HashSet<T>(otherList);
return @this.Any(set.Contains);
}

return false;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Olive/Olive.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.357.0</Version>
<Version>2.1.358.0</Version>
<OutputType>Library</OutputType>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down

0 comments on commit eb522a3

Please sign in to comment.