@ -4,91 +4,166 @@ using System.Collections.Generic;
using System.Collections.Specialized ;
using System.Text ;
namespace Apewer.Internals
namespace Apewer
{
internal class CollectionHelper
/// <summary>集合的实用工具。</summary>
public static class CollectionUtility
{
#region 排序。
#region 判断
public static List < T > Sort < T > ( List < T > list , Func < T , T , int > comparison )
/// <summary>判断集合为空。</summary>
public static bool IsEmpty < T > ( IEnumerable < T > objects )
{
if ( list = = null ) return null ;
if ( comparison = = null ) return list ;
list . Sort ( new Comparison < T > ( comparison ) ) ;
return list ;
if ( objects = = null ) return true ;
if ( objects is T [ ] ) return ( ( T [ ] ) objects ) . LongLength < 1L ;
if ( objects is ICollection < T > ) return ( ( ICollection < T > ) objects ) . Count < 1 ;
foreach ( var item in objects ) return false ;
return true ;
}
public static List < T > Ascend < T > ( List < T > list ) where T : IComparable < T >
/// <summary>判断集合存在元素。</summary>
public static bool NotEmpty < T > ( IEnumerable < T > objects )
{
if ( list = = null ) return null ;
list . Sort ( ( a , b ) = > a . CompareTo ( b ) ) ;
return list ;
if ( objects = = null ) return false ;
if ( objects is T [ ] ) return ( ( T [ ] ) objects ) . LongLength > 0L ;
if ( objects is ICollection < T > ) return ( ( ICollection < T > ) objects ) . Count > 0 ;
foreach ( var item in objects ) return true ;
return false ;
}
public static List < T > Descend < T > ( List < T > list ) where T : IComparable < T >
/// <summary>获取集合中元素的数量。</summary>
public static int Count < T > ( IEnumerable < T > objects )
{
if ( list = = null ) return null ;
list . Sort ( ( a , b ) = > - a . CompareTo ( b ) ) ;
return list ;
if ( objects = = null ) return 0 ;
var array = objects as T [ ] ;
if ( array ! = null ) return array . Length ;
var collection = objects as ICollection < T > ;
if ( collection ! = null ) return collection . Count ;
var count = 0 ;
foreach ( var cell in objects ) count + + ;
return count ;
}
public static Dictionary < TKey , TValue > SortKey < TKey , TValue > ( Dictionary < TKey , TValue > dict , Func < TKey , TKey , int > comparison )
/// <summary>检查集合是否包含 item。</summary>
public static bool Contains < T > ( IEnumerable < T > objects , T cell )
{
if ( dict = = null ) return null ;
if ( comparison = = null ) return null ;
var list = new List < KeyValuePair < TKey , TValue > > ( dict ) ;
list . Sort ( new Comparison < KeyValuePair < TKey , TValue > > ( ( a , b ) = > comparison ( a . Key , b . Key ) ) ) ;
dict . Clear ( ) ;
foreach ( var item in list ) dict . Add ( item . Key , item . Value ) ;
return dict ;
}
if ( objects = = null ) return false ;
public static Dictionary < TKey , TValue > SortValue < TKey , TValue > ( Dictionary < TKey , TValue > dict , Func < TValue , TValue , int > comparison )
// objects 实现了含有 Contains 方法的接口。
if ( objects is ICollection < T > ) return ( ( ICollection < T > ) objects ) . Contains ( cell ) ;
// cell 无效。
if ( cell = = null )
{
if ( dict = = null ) return null ;
if ( comparison = = null ) return dict ;
var list = new List < KeyValuePair < TKey , TValue > > ( dict ) ;
list . Sort ( new Comparison < KeyValuePair < TKey , TValue > > ( ( a , b ) = > comparison ( a . Value , b . Value ) ) ) ;
dict . Clear ( ) ;
foreach ( var item in list ) dict . Add ( item . Key , item . Value ) ;
return dict ;
foreach ( var i in objects )
{
if ( i = = null ) return true ;
}
return false ;
}
#endregion
// cell 有效,进行默认比较。
var comparer = EqualityComparer < T > . Default ;
foreach ( var i in objects )
{
if ( comparer . Equals ( i , cell ) ) return true ;
}
return false ;
}
public static T First < T > ( IEnumerable < T > collection , T failed = default ( T ) )
/// <summary>获取 item 在集合中的偏移位置,不存在时返回 -1。</summary>
public static int IndexOf < T > ( IEnumerable < T > objects , T item )
{
if ( collection = = null ) return failed ;
if ( objects = = null ) return - 1 ;
if ( objects is IList list ) return list . IndexOf ( item ) ;
var array = collection as T [ ] ;
if ( array ! = null ) return array . Length > 0 ? array [ 0 ] : failed ;
if ( item = = null )
{
if ( objects is T [ ] array )
{
var length = array . Length ;
for ( var i = 0 ; i < length ; i + + )
{
if ( array [ i ] = = null ) return i ;
}
return - 1 ;
}
var list = collection as IList < T > ;
if ( list ! = null ) return list . Count > 0 ? list [ 0 ] : failed ;
var index = 0 ;
foreach ( var obj in objects )
{
if ( obj = = null ) return index ;
index + + ;
}
return - 1 ;
}
else
{
var comparer = EqualityComparer < T > . Default ;
foreach ( var item in collection ) return item ;
return failed ;
if ( objects is T [ ] array )
{
var length = array . Length ;
for ( var i = 0 ; i < length ; i + + )
{
if ( comparer . Equals ( item , array [ i ] ) ) return i ;
}
return - 1 ;
}
public static T Last < T > ( IEnumerable < T > collection , T failed = default ( T ) )
var index = 0 ;
foreach ( var obj in objects )
{
if ( collection = = null ) return failed ;
if ( comparer . Equals ( item , obj ) ) return index ;
index + + ;
}
return - 1 ;
}
}
var array = collection as T [ ] ;
if ( array ! = null ) return array . Length > 0 ? array [ array . Length - 1 ] : failed ;
#endregion
var list = collection as IList < T > ;
if ( list ! = null ) return list . Count > 0 ? list [ list . Count - 1 ] : failed ;
#region 类型转换
var value = failed ;
foreach ( var item in collection ) value = item ;
return value ;
/// <summary>转换模型类型。</summary>
public static TDst [ ] As < TSrc , TDst > ( this TSrc [ ] array ) where TDst : class
{
if ( array = = null ) return null ;
var count = array . Length ;
var output = new TDst [ count ] ;
for ( var i = 0 ; i < count ; i + + )
{
var item = array [ i ] ;
if ( item = = null ) continue ;
output [ i ] = item as TDst ;
}
return output ;
}
// 安全转换为 List<T> 对象。可指定排除 NULL 值元素。
public static List < T > ToList < T > ( IEnumerable < T > objects , bool excludeNull = false )
/// <summary>转换模型类型。</summary>
public static TDst [ ] As < TSrc , TDst > ( this TSrc [ ] array , Func < TSrc , TDst > convert )
{
if ( convert = = null ) throw new ArgumentNullException ( nameof ( convert ) ) ;
if ( array = = null ) return null ;
var count = array . Length ;
var output = new TDst [ count ] ;
for ( var i = 0 ; i < count ; i + + )
{
var item = array [ i ] ;
if ( item = = null ) continue ;
output [ i ] = convert ( item ) ;
}
return output ;
}
/// <summary>安全转换为 List<<typeparamref name="T"/>> 对象。可指定排除 NULL 值元素。</summary>
public static List < T > List < T > ( IEnumerable < T > objects , bool excludeNull = false )
{
if ( objects = = null ) return new List < T > ( ) ;
@ -122,8 +197,8 @@ namespace Apewer.Internals
return list ;
}
// 安全转换为 T[] 对象。可指定排除 NULL 值元素。
public static T [ ] To Array< T > ( IEnumerable < T > objects , bool excludeNull = false )
/// <summary> 安全转换为 <<typeparamref name=" T"/>> [] 对象。可指定排除 NULL 值元素。</summary>
public static T [ ] Array < T > ( IEnumerable < T > objects , bool excludeNull = false )
{
if ( objects = = null ) return new T [ 0 ] ;
if ( objects is T [ ] ) return ( T [ ] ) objects ;
@ -148,7 +223,7 @@ namespace Apewer.Internals
{
capacity + = group ;
var temp = new T [ capacity ] ;
Array . Copy ( array , 0 , temp , 0 , added ) ;
System . Array . Copy ( array , 0 , temp , 0 , added ) ;
array = temp ;
}
array [ added ] = item ;
@ -156,115 +231,84 @@ namespace Apewer.Internals
}
if ( added < 1 | | added = = capacity ) return array ;
var collapsed = new T [ added ] ;
Array . Copy ( array , 0 , collapsed , 0 , added ) ;
System . Array . Copy ( array , 0 , collapsed , 0 , added ) ;
return collapsed ;
}
}
public static bool IsEmpty < T > ( IEnumerable < T > objects )
{
if ( objects = = null ) return true ;
if ( objects is T [ ] ) return ( ( T [ ] ) objects ) . LongLength = = 0 ;
if ( objects is ICollection < T > ) return ( ( ICollection < T > ) objects ) . Count = = 0 ;
foreach ( var i in objects ) return false ;
return true ;
}
public static bool NotEmpty < T > ( IEnumerable < T > objects ) = > ! IsEmpty < T > ( objects ) ;
public static int IndexOf < T > ( IEnumerable < T > objects , T item )
{
if ( objects = = null ) return - 1 ;
if ( objects is IList list ) return list . IndexOf ( item ) ;
/// <summary>生成 StringPairs 对象实例为副本。</summary>
public static StringPairs StringPairs ( NameValueCollection @this ) = > Apewer . StringPairs . From ( @this ) ;
if ( item = = null )
/// <summary>转换集合为数组。</summary>
/// <param name="collection"></param>
/// <returns></returns>
public static Dictionary < string , string [ ] > Dictionary ( NameValueCollection collection )
{
if ( objects is T [ ] array )
{
var length = array . Length ;
for ( var i = 0 ; i < length ; i + + )
if ( collection = = null ) return null ;
var count = collection . Count ;
var dict = new Dictionary < string , string [ ] > ( ) ;
for ( var i = 0 ; i < count ; i + + )
{
if ( array [ i ] = = null ) return i ;
var key = collection . GetKey ( i ) ;
var values = collection . GetValues ( i ) ;
dict . Add ( key , values ) ;
}
return - 1 ;
return dict ;
}
var index = 0 ;
foreach ( var obj in objects )
{
if ( obj = = null ) return index ;
index + + ;
}
return - 1 ;
}
else
/// <summary>转换集合为字典。</summary>
/// <typeparam name="TKey">字典 Key 的类型。</typeparam>
/// <typeparam name="TValue">字典 Value 的类型。</typeparam>
/// <param name="items">要转换的集合。</param>
/// <param name="key">根据元素获取 Key 的函数。</param>
/// <exception cref="ArgumentNullException"></exception>
public static Dictionary < TKey , TValue > Dictionary < TKey , TValue > ( IEnumerable < TValue > items , Func < TValue , TKey > key )
{
var comparer = EqualityComparer < T > . Default ;
if ( items = = null ) throw new ArgumentNullException ( nameof ( items ) ) ;
if ( key = = null ) throw new ArgumentNullException ( nameof ( key ) ) ;
if ( objects is T [ ] array )
{
var length = array . Length ;
for ( var i = 0 ; i < length ; i + + )
var dict = new Dictionary < TKey , TValue > ( ) ;
foreach ( var i in items )
{
if ( comparer . Equals ( item , array [ i ] ) ) return i ;
}
return - 1 ;
}
if ( i = = null ) continue ;
var index = 0 ;
foreach ( var obj in objects )
{
if ( comparer . Equals ( item , obj ) ) return index ;
index + + ;
}
return - 1 ;
var k = key ( i ) ;
if ( k . IsNull ( ) ) continue ;
if ( dict . ContainsKey ( k ) ) continue ;
dict . Add ( k , i ) ;
}
return dict ;
}
// 判断集合包含特定值。
public static bool Contains < T > ( IEnumerable < T > objects , T cell )
{
if ( objects = = null ) return false ;
#endregion
// objects 实现了含有 Contains 方法的接口。
if ( objects is ICollection < T > ) return ( ( ICollection < T > ) objects ) . Contains ( cell ) ;
#region 修改集合
// cell 无效。
if ( cell = = null )
{
foreach ( var i in objects )
/// <summary>添加多个元素。</summary>
public static void Add < T > ( List < T > list , params T [ ] items )
{
if ( i = = null ) return true ;
}
return false ;
if ( list ! = null & & items ! = null ) list . AddRange ( items ) ;
}
// cell 有效,进行默认比较。
var comparer = EqualityComparer < T > . Default ;
foreach ( var i in objects )
/// <summary>添加多个元素。</summary>
public static IList < T > Add < T > ( IList < T > list , IEnumerable < T > items )
{
if ( comparer . Equals ( i , cell ) ) return true ;
if ( list ! = null & & items ! = null )
{
foreach ( var item in items ) list . Add ( item ) ;
}
return false ;
return list ;
}
// 获取集合中元素的数量。
public static int Count < T > ( IEnumerable < T > objects )
/// <summary>添加元素。</summary>
public static bool Add < TKey , TValue > ( IList < KeyValuePair < TKey , TValue > > list , TKey key , TValue value )
{
if ( objects = = null ) return 0 ;
var array = objects as T [ ] ;
if ( array ! = null ) return array . Length ;
var collection = objects as ICollection < T > ;
if ( collection ! = null ) return collection . Count ;
var count = 0 ;
foreach ( var cell in objects ) count + + ;
return count ;
if ( list = = null ) return false ;
list . Add ( new KeyValuePair < TKey , TValue > ( key , value ) ) ;
return true ;
}
// 对元素去重,且去除 NULL 值。
/// <summary>对元素去重,且去除 NULL 值。</summary>
public static T [ ] Distinct < T > ( IEnumerable < T > items )
{
if ( items = = null ) throw new ArgumentNullException ( nameof ( items ) ) ;
@ -295,13 +339,20 @@ namespace Apewer.Internals
if ( added < count )
{
var temp = new T [ added ] ;
Array . Copy ( array , 0 , temp , 0 , added ) ;
System . Array . Copy ( array , 0 , temp , 0 , added ) ;
array = temp ;
}
return array ;
}
// 获取可枚举集合的部分元素。
/// <summary>获取可枚举集合的部分元素。</summary>
/// <typeparam name="T">集合元素的类型。</typeparam>
/// <param name="objects">原集合。</param>
/// <param name="skip">在集合前段要跳过的元素数量。</param>
/// <param name="count">要获取的元素数量,指定为负数时不限元素数量。</param>
/// <param name="stuffer">填充器,获取范围超出原集合的部分,使用此方法填充元素;此函数默认返回 <typeparamref name="T"/> 的默认值。</param>
/// <returns>数量符合 count 的数组。</returns>
public static T [ ] Slice < T > ( IEnumerable < T > objects , int skip = 0 , int count = - 1 , Func < T > stuffer = null )
{
if ( count = = 0 ) return new T [ 0 ] ;
@ -318,6 +369,7 @@ namespace Apewer.Internals
if ( count > 0 & & added = = count ) return ab . Export ( ) ;
}
}
if ( objects ! = null )
{
var offset = 0 ;
@ -334,45 +386,161 @@ namespace Apewer.Internals
if ( count > 0 & & added = = count ) return ab . Export ( ) ;
}
}
while ( added < count )
{
ab . Add ( stuffer = = null ? default : stuffer ( ) ) ;
added + + ;
}
return ab . Export ( ) ;
}
public static IList < T > Add < T > ( IList < T > list , IEnumerable < T > items )
#endregion
#region 排序
/// <summary>对列表中的元素排序。</summary>
public static List < T > Sort < T > ( List < T > list , Func < T , T , int > comparison )
{
if ( list ! = null & & items ! = null )
if ( list = = null ) return null ;
if ( comparison = = null ) return list ;
list . Sort ( new Comparison < T > ( comparison ) ) ;
return list ;
}
/// <summary>对数组排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static T [ ] Sort < T > ( T [ ] array , Func < T , T , int > comparison )
{
foreach ( var item in items ) list . Add ( item ) ;
if ( array = = null ) return array ;
if ( comparison = = null ) return array ;
System . Array . Sort ( array , new Comparison < T > ( comparison ) ) ;
return array ;
}
/// <summary>获取集合中的第一个元素。可指定失败时的默认返回值。</summary>
public static T First < T > ( IEnumerable < T > collection , T failed = default ( T ) )
{
if ( collection = = null ) return failed ;
var array = collection as T [ ] ;
if ( array ! = null ) return array . Length > 0 ? array [ 0 ] : failed ;
var list = collection as IList < T > ;
if ( list ! = null ) return list . Count > 0 ? list [ 0 ] : failed ;
foreach ( var item in collection ) return item ;
return failed ;
}
/// <summary>获取集合中的最后一个元素。可指定失败时的默认返回值。</summary>
public static T Last < T > ( IEnumerable < T > collection , T failed = default ( T ) )
{
if ( collection = = null ) return failed ;
var array = collection as T [ ] ;
if ( array ! = null ) return array . Length > 0 ? array [ array . Length - 1 ] : failed ;
var list = collection as IList < T > ;
if ( list ! = null ) return list . Count > 0 ? list [ list . Count - 1 ] : failed ;
var value = failed ;
foreach ( var item in collection ) value = item ;
return value ;
}
/// <summary>对数组升序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static T [ ] Ascend < T > ( T [ ] array ) where T : IComparable < T >
{
if ( array = = null ) return null ;
array . Sort ( ( a , b ) = > a . CompareTo ( b ) ) ;
return array ;
}
/// <summary>对数组升序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static List < T > Ascend < T > ( List < T > list ) where T : IComparable < T >
{
if ( list = = null ) return null ;
list . Sort ( ( a , b ) = > a . CompareTo ( b ) ) ;
return list ;
}
public static bool Add < TKey , TValue > ( IList < KeyValuePair < TKey , TValue > > list , TKey key , TValue value )
/// <summary>对数组升序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static void Ascend < T , TProp > ( T [ ] array , Func < T , TProp > func ) where TProp : IComparable < TProp >
{
if ( list = = null ) return false ;
list . Add ( new KeyValuePair < TKey , TValue > ( key , value ) ) ;
return true ;
if ( array ! = null & & func ! = null ) Sort ( array , ( a , b ) = > func ( a ) . CompareTo ( func ( b ) ) ) ;
}
internal static Dictionary < string , string [ ] > Dict ( NameValueCollection collection )
/// <summary>对数组降序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static T [ ] Descend < T > ( T [ ] array ) where T : IComparable < T >
{
if ( collection = = null ) return null ;
var count = collection . Count ;
var dict = new Dictionary < string , string [ ] > ( ) ;
for ( var i = 0 ; i < count ; i + + )
if ( array = = null ) return array ;
Sort ( array , ( a , b ) = > 0 - a . CompareTo ( b ) ) ;
return array ;
}
/// <summary>对数组降序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static List < T > Descend < T > ( List < T > list ) where T : IComparable < T >
{
var key = collection . GetKey ( i ) ;
var values = collection . GetValues ( i ) ;
dict . Add ( key , values ) ;
if ( list = = null ) return null ;
list . Sort ( ( a , b ) = > - a . CompareTo ( b ) ) ;
return list ;
}
/// <summary>对数组降序排序。</summary>
/// <exception cref="NullReferenceException"></exception>
public static void Descend < T , TProp > ( T [ ] array , Func < T , TProp > func ) where TProp : IComparable < TProp >
{
if ( array ! = null & & func ! = null ) Sort ( array , ( a , b ) = > 0 - func ( a ) . CompareTo ( func ( b ) ) ) ;
}
/// <summary>对字典中的键排序。</summary>
public static Dictionary < TKey , TValue > SortKey < TKey , TValue > ( Dictionary < TKey , TValue > dict , Func < TKey , TKey , int > comparison )
{
if ( dict = = null ) return null ;
if ( comparison = = null ) return null ;
var list = new List < KeyValuePair < TKey , TValue > > ( dict ) ;
list . Sort ( new Comparison < KeyValuePair < TKey , TValue > > ( ( a , b ) = > comparison ( a . Key , b . Key ) ) ) ;
dict . Clear ( ) ;
foreach ( var item in list ) dict . Add ( item . Key , item . Value ) ;
return dict ;
}
/// <summary>对字典中的键排序。</summary>
public static Dictionary < TKey , TValue > SortKey < TKey , TValue > ( Dictionary < TKey , TValue > @this ) where TKey : IComparable < TKey >
{
return SortKey ( @this , ( a , b ) = > a . CompareTo ( b ) ) ;
}
/// <summary>对字典中的值排序。</summary>
public static Dictionary < TKey , TValue > SortValue < TKey , TValue > ( Dictionary < TKey , TValue > dict , Func < TValue , TValue , int > comparison )
{
if ( dict = = null ) return null ;
if ( comparison = = null ) return dict ;
var list = new List < KeyValuePair < TKey , TValue > > ( dict ) ;
list . Sort ( new Comparison < KeyValuePair < TKey , TValue > > ( ( a , b ) = > comparison ( a . Value , b . Value ) ) ) ;
dict . Clear ( ) ;
foreach ( var item in list ) dict . Add ( item . Key , item . Value ) ;
return dict ;
}
public static object [ ] ParseParams ( object cells )
/// <summary>对字典中的值排序。</summary>
public static Dictionary < TKey , TValue > SortValue < TKey , TValue > ( Dictionary < TKey , TValue > @this ) where TValue : IComparable < TValue >
{
return SortValue ( @this , ( a , b ) = > a . CompareTo ( b ) ) ;
}
#endregion
#region params
internal static object [ ] ParseParams ( object cells )
{
var parsed = new ArrayBuilder < object > ( ) ;
ParseParams ( cells , parsed , 1 ) ;
@ -407,6 +575,8 @@ namespace Apewer.Internals
parsed . Add ( cells . ToString ( ) ) ;
}
#endregion
}
}