update
This commit is contained in:
@@ -60,6 +60,7 @@ namespace BITKit
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IEnumerable<T[]> Combinations<T>(IEnumerable<T> source)
|
||||
{
|
||||
if (null == source) throw new ArgumentNullException(nameof(source));
|
||||
@@ -74,13 +75,14 @@ namespace BITKit
|
||||
switch (self)
|
||||
{
|
||||
case List<T> list:
|
||||
list.Insert(index,item);
|
||||
list.Insert(index, item);
|
||||
break;
|
||||
default:
|
||||
var _list = self.ToList();
|
||||
_list.Insert(index,item);
|
||||
_list.Insert(index, item);
|
||||
return _list;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -91,8 +93,10 @@ namespace BITKit
|
||||
{
|
||||
value = self.ElementAt(0);
|
||||
}
|
||||
|
||||
return value is not null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 组合集合中的所有元素
|
||||
/// </summary>
|
||||
@@ -113,6 +117,7 @@ namespace BITKit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取集合中所有的组合,每个组合中的元素个数为输入集合的元素个数,每个元素只出现一次
|
||||
/// </summary>
|
||||
@@ -132,5 +137,34 @@ namespace BITKit
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Linq Distinct扩展方法,可以传入自定义比较器
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Distinct<T>(
|
||||
this IEnumerable<T> source, Func<T, T, bool> comparer)
|
||||
where T : class
|
||||
=> source.Distinct(new DynamicEqualityComparer<T>(comparer));
|
||||
/// <summary>
|
||||
/// 动态自定义比较器
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly Func<T, T, bool> _func;
|
||||
|
||||
public DynamicEqualityComparer(Func<T, T, bool> func)
|
||||
{
|
||||
_func = func;
|
||||
}
|
||||
|
||||
public bool Equals(T x, T y) => _func(x, y);
|
||||
|
||||
public int GetHashCode(T obj) => 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user