1 #Region "Microsoft.VisualBasic::4839b20ed2ec527efe1fcd22bfe599aa, Microsoft.VisualBasic.Core\Extensions\Extensions.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     Module Extensions
35     
36     
37     Module Extensions
38     
39     '     Function: [Get], [Set], Add, (+3 Overloads) AddRange, AsRange
40     '               (+2 Overloads) Average, CheckDuplicated, Constrain, DataCounts, DateToString
41     '               DriverRun, ElementAtOrDefault, FirstNotEmpty, FormatTime, FuzzyMatching
42     '               GetHexInteger, (+2 OverloadsGetItem, (+2 OverloadsGetLength, IndexOf, InsertOrUpdate
43     '               Invoke, InvokeSet, Is_NA_UHandle, (+2 OverloadsIsNaNImaginary, IsNullorEmpty
44     '               (+14 OverloadsIsNullOrEmpty, (+4 Overloads) Join, (+2 Overloads) JoinBy, Keys, KeysJson
45     '               Log2, (+2 OverloadsLongSeq, MatrixToUltraLargeVector, MatrixTranspose, MatrixTransposeIgnoredDimensionAgreement
46     '               MD5, ModifyValue, NormalizeXMLString, NotNull, (+2 OverloadsOffset
47     '               ParseDateTime, Range, Remove, RemoveDuplicates, RemoveFirst
48     '               (+2 Overloads) RemoveLast, RunDriver, SaveAsTabularMapping, Second, SelectFile
49     '               SeqRandom, (+2 Overloads) Sequence, (+2 OverloadsSetValue, (+11 Overloads) ShadowCopy, Shell
50     '               Shuffles, Slice, Split, SplitIterator, (+2 Overloads) SplitMV
51     '               StdError, TakeRandomly, Takes, ToBoolean, ToDictionary
52     '               ToNormalizedPathString, ToStringArray, ToVector, (+3 Overloads) TrimNull, (+2 OverloadsTryGetValue
53     '               Unlist, WriteAddress
54     
55     '     Sub: Add, FillBlank, Removes, (+2 Overloads) SendMessage, Swap
56     '          SwapItem, SwapWith
57     
58     
59     
60     ' /********************************************************************************/
61
62 #End Region
63
64 Imports System.Collections.ObjectModel
65 Imports System.Drawing
66 Imports System.Globalization
67 Imports System.Reflection
68 Imports System.Runtime.CompilerServices
69 Imports System.Text
70 Imports Microsoft.VisualBasic.ApplicationServices
71 Imports Microsoft.VisualBasic.CommandLine
72 Imports Microsoft.VisualBasic.CommandLine.Reflection
73 Imports Microsoft.VisualBasic.ComponentModel
74 Imports Microsoft.VisualBasic.ComponentModel.Algorithm.base
75 Imports Microsoft.VisualBasic.ComponentModel.Collection
76 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
77 Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
78 Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
79 Imports Microsoft.VisualBasic.Language
80 Imports Microsoft.VisualBasic.Language.Vectorization
81 Imports Microsoft.VisualBasic.Linq
82 Imports Microsoft.VisualBasic.Linq.Extensions
83 Imports Microsoft.VisualBasic.Net.Tcp
84 Imports Microsoft.VisualBasic.Parallel
85 Imports Microsoft.VisualBasic.Scripting.MetaData
86 Imports Microsoft.VisualBasic.SecurityString
87 Imports Microsoft.VisualBasic.Serialization.JSON
88 Imports Microsoft.VisualBasic.Terminal
89 Imports Microsoft.VisualBasic.Text
90 Imports Microsoft.VisualBasic.Text.Levenshtein
91 Imports Microsoft.VisualBasic.Text.Similarity
92 Imports sys = System.Math
93
94 #Const FRAMEWORD_CORE = 1
95 #Const Yes = 1
96
97 #If FRAMEWORD_CORE = Yes Then
98
99 ''' <summary>
100 ''' Common extension methods library for convenient the programming job.
101 ''' </summary>
102 ''' <remarks></remarks>
103 <Package("Framework.Extensions",
104                     Description:="The common extension methods module in this Microsoft.VisualBasic program assembly." &
105                                  "Common extension methods library for convenient the programming job.",
106                     Publisher:="xie.guigang@gmail.com",
107                     Revision:=8655,
108                     Url:="http://github.com/xieguigang/sciBASIC#")>
109 <HideModuleName>
110 <Extension> Public Module Extensions
111 #Else
112
113 ''' <summary>
114 ''' Common extension methods library for convenient the programming job.
115 ''' </summary>
116 ''' <remarks></remarks>
117 Public Module Extensions
118 #End If
119
120     <MethodImpl(MethodImplOptions.AggressiveInlining)>
121     <Extension>
122     Public Function Average(range As DoubleRange) As Double
123         Return {range.Min, range.Max}.Average
124     End Function
125
126     ''' <summary>
127     ''' Create the numeric range from a numeric value collection
128     ''' </summary>
129     ''' <param name="data"></param>
130     ''' <returns></returns>
131     <Extension>
132     <MethodImpl(MethodImplOptions.AggressiveInlining)>
133     Public Function Range(data As IEnumerable(Of Double), Optional scale# = 1) As DoubleRange
134         Return New DoubleRange(data) * scale
135     End Function
136
137     <Extension>
138     Public Function Slice(range As DoubleRange, n%) As IEnumerable(Of DoubleRange)
139         Dim l = range.Length
140         Dim d = l / n
141         Dim parts = Math.seq(range.Min, range.Max, by:=d) _
142                         .SlideWindows(winSize:=2) _
143                         .Select(Function(w)
144                                     Return New DoubleRange(w)
145                                 End Function) _
146                         .ToArray
147         Return parts
148     End Function
149
150     ''' <summary>
151     ''' ``Math.Log(x, newBase:=2)``
152     ''' </summary>
153     ''' <param name="x#"></param>
154     ''' <returns></returns>
155     ''' 
156     <MethodImpl(MethodImplOptions.AggressiveInlining)>
157     <Extension> Public Function Log2(x#) As Double
158         Return sys.Log(x, newBase:=2)
159     End Function
160
161     ''' <summary>
162     ''' 将16进制的数字转换为10进制数
163     ''' </summary>
164     ''' <param name="hex$"></param>
165     ''' <returns></returns>
166     ''' <remarks>
167     ''' 因为直接使用vb的<see cref="Val"/>函数转换,在Linux上面可能会出错,所以需要在这里用.NET自己的方法来转换
168     ''' </remarks>
169     ''' 
170     <MethodImpl(MethodImplOptions.AggressiveInlining)>
171     Public Function GetHexInteger(hex$) As Integer
172         Dim num% = Integer.Parse(hex, NumberStyles.HexNumber)
173         Return num
174     End Function
175
176     ''' <summary>
177     ''' Save as a tsv file, with data format like: 
178     ''' 
179     ''' ```
180     ''' <see cref="NamedValue(Of String).Name"/>\t<see cref="NamedValue(Of String).Value"/>\t<see cref="NamedValue(Of String).Description"/>
181     ''' ```
182     ''' </summary>
183     ''' <param name="source"></param>
184     ''' <param name="path$"></param>
185     ''' <param name="encoding"></param>
186     ''' <returns></returns>
187     <Extension>
188     Public Function SaveAsTabularMapping(source As IEnumerable(Of NamedValue(Of String)),
189                                          path$,
190                                          Optional saveDescrib As Boolean = False,
191                                          Optional saveHeaders$() = Nothing,
192                                          Optional encoding As Encodings = Encodings.ASCII) As Boolean
193         Dim content = source _
194             .Select(Function(row)
195                         With row
196                             If saveDescrib Then
197                                 Return $"{ .Name}{ASCII.TAB}{ .Value}{ASCII.TAB}{ .Description}"
198                             Else
199                                 Return $"{ .Name}{ASCII.TAB}{ .Value}"
200                             End If
201                         End With
202                     End Function)
203
204         If saveHeaders.IsNullOrEmpty Then
205             Return content.SaveTo(path, encoding.CodePage)
206         Else
207             Return {saveHeaders.JoinBy(ASCII.TAB)}.JoinIterates(content).SaveTo(path, encoding.CodePage)
208         End If
209     End Function
210
211     ''' <summary>
212     ''' ``days, hh:mm:ss.ms``
213     ''' </summary>
214     ''' <param name="t"></param>
215     ''' <returns></returns>
216     ''' 
217     <MethodImpl(MethodImplOptions.AggressiveInlining)>
218     <Extension>
219     Public Function FormatTime(t As TimeSpan) As String
220         With t
221             Return $"{ZeroFill(.Days, 2)}, {ZeroFill(.Hours, 2)}:{ZeroFill(.Minutes, 2)}:{ZeroFill(.Seconds, 2)}.{ .Milliseconds}"
222         End With
223     End Function
224
225     <Extension>
226     Public Function Average(data As IEnumerable(Of TimeSpan)) As TimeSpan
227         Dim avg# = data.Select(Function(x) x.TotalMilliseconds).Average
228         Return TimeSpan.FromMilliseconds(avg)
229     End Function
230
231     ''' <summary>
232     ''' Returns all of the keys in a dictionary in json format
233     ''' </summary>
234     ''' <typeparam name="V"></typeparam>
235     ''' <param name="d"></param>
236     ''' <returns></returns>
237     ''' 
238     <MethodImpl(MethodImplOptions.AggressiveInlining)>
239     <Extension>
240     Public Function KeysJson(Of V)(d As Dictionary(Of String, V)) As String
241         Return d.Keys.ToArray.GetJson
242     End Function
243
244     ''' <summary>
245     ''' Returns the first not nothing object.
246     ''' </summary>
247     ''' <typeparam name="T">
248     ''' Due to the reason of value type is always not nothing, so that this generic type constrain as Class reference type.
249     ''' </typeparam>
250     ''' <param name="args"></param>
251     ''' <returns></returns>
252     Public Function NotNull(Of T As Class)(ParamArray args As T()) As T
253         If args.IsNullOrEmpty Then
254             Return Nothing
255         Else
256             For Each x In args
257                 If Not x Is Nothing Then
258                     Return x
259                 End If
260             Next
261         End If
262
263         Return Nothing
264     End Function
265
266     ''' <summary>
267     ''' Get target string's md5 hash code
268     ''' </summary>
269     ''' <param name="s$"></param>
270     ''' <returns></returns>
271     <MethodImpl(MethodImplOptions.AggressiveInlining)>
272     <Extension> Public Function MD5(s$) As String
273         Return s.GetMd5Hash
274     End Function
275
276     ''' <summary>
277     ''' Returns the first not null or empty string.
278     ''' </summary>
279     ''' <param name="args"></param>
280     ''' <returns></returns>
281     Public Function FirstNotEmpty(ParamArray args As String()) As String
282         If args.IsNullOrEmpty Then
283             Return ""
284         Else
285             For Each s As String In args
286                 If Not String.IsNullOrEmpty(s) Then
287                     Return s
288                 End If
289             Next
290         End If
291
292         Return ""
293     End Function
294
295     ''' <summary>
296     ''' Returns the second element in the source collection, if the collection 
297     ''' is nothing or elements count not enough, then will returns nothing if 
298     ''' the <paramref name="suppressError"/> option was opend, otherwise this 
299     ''' function will throw exception.
300     ''' </summary>
301     ''' <typeparam name="T"></typeparam>
302     ''' <param name="source"></param>
303     ''' <returns></returns>
304     <Extension> Public Function Second(Of T)(source As IEnumerable(Of T), Optional suppressError As Boolean = FalseOptional [default] As T = NothingAs T
305         For Each x As SeqValue(Of T) In source.SeqIterator
306             If x.i = 1 Then
307                 Return x.value
308             End If
309         Next
310
311         If Not suppressError Then
312             Throw New IndexOutOfRangeException
313         Else
314             Return [default]
315         End If
316     End Function
317
318     <Extension> Public Function Add(Of T As INamedValue)(ByRef table As Dictionary(Of String, T), obj As T) As Dictionary(Of String, T)
319         If table Is Nothing Then
320             table = New Dictionary(Of String, T)
321         End If
322         If table.ContainsKey(obj.Key) Then
323             Throw New Exception($"[{obj.Key}] was duplicated in the dictionary!")
324         Else
325             Call table.Add(obj.Key, obj)
326         End If
327
328         Return table
329     End Function
330
331     <Extension>
332     Public Function IndexOf(Of T)(source As Queue(Of T), x As T) As Integer
333         If source.IsNullOrEmpty Then
334             Return -1
335         Else
336             Return source.AsList.IndexOf(x)
337         End If
338     End Function
339
340     ''' <summary>
341     ''' Gets all keys value from the target <see cref="KeyValuePair"/> collection.
342     ''' </summary>
343     ''' <typeparam name="T1"></typeparam>
344     ''' <typeparam name="T2"></typeparam>
345     ''' <param name="source"></param>
346     ''' <returns></returns>
347     ''' 
348     <MethodImpl(MethodImplOptions.AggressiveInlining)>
349     <Extension> Public Function Keys(Of T1, T2)(source As IEnumerable(Of KeyValuePair(Of T1, T2))) As T1()
350         Return source.Select(Function(x) x.Key).ToArray
351     End Function
352
353     ''' <summary>
354     ''' Adds the elements of the specified collection to the end of the List`1.
355     ''' (会自动跳过空集合,这个方法是安全的)
356     ''' </summary>
357     ''' <typeparam name="T"></typeparam>
358     ''' <param name="list"></param>
359     ''' <param name="value">The collection whose elements should be added to the end of the List`1.</param>
360     <Extension> Public Sub Add(Of T)(ByRef list As List(Of T), ParamArray value As T())
361         If value.IsNullOrEmpty Then
362             Return
363         Else
364             Call list.AddRange(value)
365         End If
366     End Sub
367
368     ''' <summary>
369     ''' Safe get the specific index element from the target collection, is the index value invalid, then default value will be return.
370     ''' (假若下标越界的话会返回默认值)
371     ''' </summary>
372     ''' <typeparam name="T"></typeparam>
373     ''' <param name="array"></param>
374     ''' <param name="index"></param>
375     ''' <param name="[default]">Default value for invalid index is nothing.</param>
376     ''' <returns></returns>
377     <Extension> Public Function [Get](Of T)(array As IEnumerable(Of T), index As IntegerOptional [default] As T = NothingAs T
378         If array Is Nothing Then
379             Return [default]
380         End If
381
382         If index < 0 OrElse index >= array.Count Then
383             Return [default]
384         End If
385
386         Dim value As T = array(index)
387         Return value
388     End Function
389
390     ''' <summary>
391     ''' This is a safely method for gets the value in a array, if the index was outside of the boundary, then the default value will be return.
392     ''' (假若下标越界的话会返回默认值)
393     ''' </summary>
394     ''' <typeparam name="T"></typeparam>
395     ''' <param name="array"></param>
396     ''' <param name="index"></param>
397     ''' <param name="[default]">Default value for return when the array object is nothing or index outside of the boundary.</param>
398     ''' <returns></returns>
399     <Extension> Public Function ElementAtOrDefault(Of T)(array As T(), index As IntegerOptional [default] As T = NothingAs T
400         If array.IsNullOrEmpty Then
401             Return [default]
402         End If
403
404         If index < 0 OrElse index >= array.Length Then
405             Return [default]
406         End If
407
408         Dim value As T = array(index)
409         Return value
410     End Function
411
412     <Extension> Public Function [Set](Of T)(ByRef array As T(), index As Integer, value As T) As T()
413         If index < 0 Then
414             Return array
415         End If
416
417         If array.Length - 1 >= index Then
418             array(index) = value
419         Else
420             Dim copy As T() = New T(index) {}
421             Call System.Array.ConstrainedCopy(array, Scan0, copy, Scan0, array.Length)
422             copy(index) = value
423             array = copy
424         End If
425
426         Return array
427     End Function
428
429 #Region ""
430
431     <ExportAPI("SendMessage")>
432     <Extension> Public Sub SendMessage(host As System.Net.IPEndPoint, request As StringCallback As Action(Of String))
433         Dim client As New TcpRequest(host)
434         Call New Threading.Thread(Sub() Callback(client.SendMessage(request))).Start()
435     End Sub
436
437     <ExportAPI("SendMessage")>
438     <Extension> Public Sub SendMessage(host As Net.IPEndPoint, request As StringCallback As Action(Of String))
439         Call host.GetIPEndPoint.SendMessage(request, Callback)
440     End Sub
441
442 #End Region
443
444     ''' <summary>
445     ''' Constrain the inherits class type into the base type.
446     ''' (基类集合与继承类的集合约束)
447     ''' </summary>
448     ''' <typeparam name="T">继承类向基类进行约束</typeparam>
449     ''' <typeparam name="Tbase">基类</typeparam>
450     ''' <returns></returns>
451     Public Function Constrain(Of Tbase As Class, T As Tbase)(source As IEnumerable(Of T)) As Tbase()
452         If source Is Nothing Then
453             Return New Tbase() {}
454         End If
455
456         Dim array As T() = source.ToArray
457         Dim out As Tbase() = New Tbase(array.Length - 1) {}
458
459         For i As Integer = 0 To out.Length - 1
460             out(i) = source(i)
461         Next
462
463         Return out
464     End Function
465
466     ''' <summary>
467     ''' 0 -> False
468     ''' 1 -> True
469     ''' </summary>
470     ''' <param name="b"></param>
471     ''' <returns></returns>
472     <Extension> Public Function ToBoolean(b As LongAs Boolean
473         If b = 0 Then
474             Return False
475         Else
476             Return True
477         End If
478     End Function
479
480     ''' <summary>
481     ''' 假若不存在目标键名,则返回空值,默认值为空值
482     ''' </summary>
483     ''' <typeparam name="TKey"></typeparam>
484     ''' <typeparam name="TValue"></typeparam>
485     ''' <param name="table"></param>
486     ''' <param name="index"></param>
487     ''' <param name="[default]"></param>
488     ''' <returns></returns>
489     <Extension> Public Function TryGetValue(Of TKey, TValue)(table As Dictionary(Of TKey, TValue),
490                                                              index As TKey,
491                                                              Optional [default] As TValue = Nothing,
492                                                              Optional mute As Boolean = False,
493                                                              <CallerMemberName> Optional trace$ = NothingAs TValue
494         ' 表示空的,或者键名是空的,都意味着键名不存在与表之中
495         ' 直接返回默认值
496         If table Is Nothing Then
497 #If DEBUG Then
498             Call PrintException("Hash_table is nothing!")
499 #End If
500             Return [default]
501         ElseIf index Is Nothing Then
502 #If DEBUG Then
503             Call PrintException("Index key is nothing!")
504 #End If
505             Return [default]
506         ElseIf Not table.ContainsKey(index) Then
507 #If DEBUG Then
508             If Not mute Then
509                 Call PrintException($"missing_index:={Scripting.ToString(index)}!", trace)
510             End If
511 #End If
512             Return [default]
513         End If
514
515         Return table(index)
516     End Function
517
518     <Extension> Public Function TryGetValue(Of TKey, TValue, TProp)(hash As Dictionary(Of TKey, TValue), Index As TKey, prop As StringAs TProp
519         If hash Is Nothing Then
520             Return Nothing
521         End If
522
523         If Not hash.ContainsKey(Index) Then
524             Return Nothing
525         End If
526
527         Dim obj As TValue = hash(Index)
528         Dim propertyInfo As PropertyInfo = obj.GetType.GetProperty(prop)
529
530         If propertyInfo Is Nothing Then
531             Return Nothing
532         End If
533
534         Dim value As Object = propertyInfo.GetValue(obj, Nothing)
535         Return DirectCast(value, TProp)
536     End Function
537
538     ''' <summary>
539     ''' 
540     ''' </summary>
541     ''' <typeparam name="TKey"></typeparam>
542     ''' <typeparam name="TValue"></typeparam>
543     ''' <param name="table"></param>
544     ''' <param name="data"></param>
545     ''' <param name="replaceDuplicated">
546     ''' 这个函数参数决定了在遇到重复的键名称的时候的处理方法:
547     ''' 
548     ''' + 如果为真,则默认会用新的值来替换旧的值
549     ''' + 如果为False,则遇到重复的键名的时候会报错
550     ''' </param>
551     ''' <returns></returns>
552     <Extension> Public Function AddRange(Of TKey, TValue)(ByRef table As Dictionary(Of TKey, TValue),
553                                                           data As IEnumerable(Of KeyValuePair(Of TKey, TValue)),
554                                                           Optional replaceDuplicated As Boolean = FalseAs Dictionary(Of TKey, TValue)
555         If data Is Nothing Then
556             Return table
557         ElseIf replaceDuplicated Then
558             For Each obj In data
559                 table(obj.Key) = obj.Value
560             Next
561         Else
562             For Each obj In data
563                 table.Add(obj.Key, obj.Value)
564             Next
565         End If
566
567         Return table
568     End Function
569
570     ''' <summary>
571     ''' 对Xml文件之中的特殊字符进行转义处理
572     ''' </summary>
573     ''' <param name="str"></param>
574     ''' <returns></returns>
575     ''' <remarks></remarks>
576     <Extension> Public Function NormalizeXMLString(str As StringAs String
577         Dim sBuilder As StringBuilder = New StringBuilder(str)
578
579         Call sBuilder.Replace("&""&amp;")
580         Call sBuilder.Replace("""""&quot;")
581         Call sBuilder.Replace("×""&times;")
582         Call sBuilder.Replace("÷""&divide;")
583         Call sBuilder.Replace("<""&lt;")
584         Call sBuilder.Replace(">""&gt;")
585
586         Return sBuilder.ToString
587     End Function
588
589     ''' <summary>
590     ''' Format the datetime value in the format of yy/mm/dd hh:min
591     ''' </summary>
592     ''' <param name="dat"></param>
593     ''' <returns></returns>
594     <ExportAPI("Date.ToString"Info:="Format the datetime value in the format of yy/mm/dd hh:min")>
595     <Extension> Public Function DateToString(dat As DateAs String
596         Dim yy = dat.Year
597         Dim mm As String = dat.Month.FormatZero
598         Dim dd As String = dat.Day.FormatZero
599         Dim hh As String = dat.Hour.FormatZero
600         Dim mmin As String = dat.Minute.FormatZero
601
602         Return $"{yy}/{mm}/{dd} {hh}:{mmin}"
603     End Function
604
605     <ExportAPI("Date.ToNormalizedPathString")>
606     <Extension> Public Function ToNormalizedPathString(dat As DateAs String
607         Dim yy = dat.Year
608         Dim mm As String = dat.Month.FormatZero
609         Dim dd As String = dat.Day.FormatZero
610         Dim hh As String = dat.Hour.FormatZero
611         Dim mmin As String = dat.Minute.FormatZero
612
613         Return String.Format("{0}-{1}-{2} {3}.{4}", yy, mm, dd, hh, mmin)
614     End Function
615
616     ''' <summary>
617     ''' Data partitioning function.
618     ''' (将目标集合之中的数据按照<paramref name="parTokens"></paramref>参数分配到子集合之中,
619     ''' 这个函数之中不能够使用并行化Linq拓展,以保证元素之间的相互原有的顺序,
620     ''' 每一个子集和之中的元素数量为<paramref name="parTokens"/>)
621     ''' </summary>
622     ''' <typeparam name="T"></typeparam>
623     ''' <param name="source"></param>
624     ''' <param name="parTokens">每一个子集合之中的元素的数目</param>
625     ''' <returns></returns>
626     ''' <remarks></remarks>
627     <MethodImpl(MethodImplOptions.AggressiveInlining)>
628     <Extension> Public Function Split(Of T)(source As IEnumerable(Of T), parTokens As IntegerOptional echo As Boolean = TrueAs T()()
629         Return source.SplitIterator(parTokens, echo).ToArray
630     End Function
631
632     ''' <summary>
633     ''' Performance the partitioning operation on the input sequence.
634     ''' (请注意,这个函数只适用于数量较少的序列。对所输入的序列进行分区操作,<paramref name="parTokens"/>函数参数是每一个分区里面的元素的数量)
635     ''' </summary>
636     ''' <typeparam name="T"></typeparam>
637     ''' <param name="source"></param>
638     ''' <param name="parTokens"></param>
639     ''' <returns></returns>
640     <Extension>
641     Public Iterator Function SplitIterator(Of T)(source As IEnumerable(Of T), parTokens As IntegerOptional echo As Boolean = TrueAs IEnumerable(Of T())
642         Dim buf As T() = source.SafeQuery.ToArray
643         Dim n As Integer = buf.Length
644         Dim count As Integer
645
646         If echo AndAlso n >= 50000 Then
647             Call $"Start large data set(size:={n}) partitioning...".__DEBUG_ECHO
648         End If
649
650         For i As Integer = 0 To n - 1 Step parTokens
651             Dim buffer As T()
652
653             If n - i >= parTokens Then
654                 buffer = New T(parTokens - 1) {}
655             Else
656                 buffer = New T(n - i - 1) {}
657             End If
658
659             Call Array.ConstrainedCopy(buf, i, buffer, Scan0, buffer.Length)
660             Yield buffer
661
662             count += 1
663         Next
664
665         If echo AndAlso n >= 50000 Then
666             Call $"Large data set data partitioning(partitions:={count}) jobs done!".__DEBUG_ECHO
667         End If
668     End Function
669
670     ''' <summary>
671     ''' Merge two type specific collection.(函数会忽略掉空的集合,函数会构建一个新的集合,原有的集合不受影响)
672     ''' </summary>
673     ''' <typeparam name="T"></typeparam>
674     ''' <param name="source"></param>
675     ''' <param name="target"></param>
676     ''' <returns></returns>
677     <Extension> Public Function Join(Of T)(source As IEnumerable(Of T), target As IEnumerable(Of T)) As List(Of T)
678         Dim srcList As List(Of T) = If(source Is NothingNew List(Of T), source.AsList)
679         If Not target Is Nothing Then
680             Call srcList.AddRange(target)
681         End If
682         Return srcList
683     End Function
684
685     <MethodImpl(MethodImplOptions.AggressiveInlining)>
686     <Extension> Public Function Join(Of T)(source As IEnumerable(Of T), ParamArray data As T()) As List(Of T)
687         Return source.Join(target:=data)
688     End Function
689
690     ''' <summary>
691     ''' This is a safe function: if the source string collection is nothing, then whistle function will returns a empty string instead of throw exception. 
692     ''' (<see cref="String.Join"/>,这是一个安全的函数,当数组为空的时候回返回空字符串)
693     ''' </summary>
694     ''' <param name="tokens"></param>
695     ''' <param name="delimiter"></param>
696     ''' <returns></returns>
697     <Extension> Public Function JoinBy(tokens As IEnumerable(Of String), delimiter$) As String
698         If tokens Is Nothing Then
699             Return ""
700         End If
701         Return String.Join(delimiter, tokens.ToArray)
702     End Function
703
704     ''' <summary>
705     ''' <see cref="String.Join"/>,这是一个安全的函数,当数组为空的时候回返回空字符串
706     ''' </summary>
707     ''' <param name="values"></param>
708     ''' <param name="delimiter"></param>
709     ''' <returns></returns>
710     <Extension> Public Function JoinBy(values As IEnumerable(Of Integer), delimiter$) As String
711         If values Is Nothing Then
712             Return ""
713         End If
714         Return String.Join(delimiter, values.Select(Function(n) CStr(n)).ToArray)
715     End Function
716
717     <Extension> Public Function Join(Of T)(source As IEnumerable(Of T), data As T) As List(Of T)
718         Return source.Join({data})
719     End Function
720
721     ''' <summary>
722     ''' ``X, ....``
723     ''' 
724     ''' (这个函数是一个安全的函数,当<paramref name="collection"/>为空值的时候回忽略掉<paramref name="collection"/>,
725     ''' 只返回包含有一个<paramref name="obj"/>元素的列表)
726     ''' </summary>
727     ''' <typeparam name="T"></typeparam>
728     ''' <param name="obj"></param>
729     ''' <param name="collection"></param>
730     ''' <returns></returns>
731     <Extension> Public Function Join(Of T)(obj As T, collection As IEnumerable(Of T)) As List(Of T)
732         With New List(Of T) From {obj}
733             If Not collection Is Nothing Then
734                 Call .AddRange(collection)
735             End If
736
737             Return .ByRef
738         End With
739     End Function
740
741 #If FRAMEWORD_CORE Then
742     ''' <summary>
743     ''' Show open file dialog and return the selected file path.
744     ''' </summary>
745     ''' <param name="ext$"></param>
746     ''' <returns></returns>
747     <ExportAPI("File.Select",
748                Info:="Open the file open dialog to gets the file")>
749     Public Function SelectFile(Optional ext$ = "*.*"Optional title$ = NothingAs String
750         Dim mime$ = ext.GetMIMEDescrib.Details
751
752         Using Open As New OpenFileDialog With {
753             .Filter = $"{ext}|{ext}",
754             .Title = If(title.StringEmpty, $"Open {mime}", title)
755         }
756             If Open.ShowDialog = DialogResult.OK Then
757                 Return Open.FileName
758             Else
759                 Return Nothing
760             End If
761         End Using
762     End Function
763 #End If
764
765     ''' <summary>
766     ''' 本方法会执行外部命令并等待其执行完毕,函数返回状态值
767     ''' </summary>
768     ''' <param name="Process"></param>
769     ''' <returns></returns>
770     ''' <remarks></remarks>
771     '''
772     <ExportAPI("Invoke"Info:="Invoke a folked system process object to execute a parallel task.")>
773     <Extension> Public Function Invoke(Process As Process) As Integer
774         Call Process.Start()
775         Call Process.WaitForExit()
776         Return Process.ExitCode
777     End Function
778
779 #If FRAMEWORD_CORE Then
780     ''' <summary>
781     ''' 非线程的方式启动,当前线程会被阻塞在这里直到运行完毕
782     ''' </summary>
783     ''' <param name="driver"></param>
784     ''' <returns></returns>
785     <ExportAPI("Run"Info:="Running the object model driver, the target object should implement the driver interface.")>
786     Public Function RunDriver(driver As ITaskDriver) As Integer
787         Return driver.Run
788     End Function
789
790     ''' <summary>
791     ''' Run the driver in a new thread, NOTE: from this extension function calls, then run thread is already be started, 
792     ''' so that no needs of calling the method <see cref="Threading.Thread.Start()"/> again.
793     ''' (使用线程的方式启动,在函数调用之后,线程是已经启动了的,所以不需要再次调用<see cref="Threading.Thread.Start()"/>方法了)
794     ''' </summary>
795     ''' <param name="driver">The object which is implements the interface <see cref="ITaskDriver"/></param>
796     <ExportAPI("Run"Info:="Running the object model driver, the target object should implement the driver interface.")>
797     <Extension>
798     Public Function DriverRun(driver As ITaskDriver) As Threading.Thread
799         Return Parallel.RunTask(AddressOf driver.Run)
800     End Function
801 #End If
802
803     ''' <summary>
804     ''' Gets the element counts in the target data collection, if the collection object is nothing or empty
805     ''' then this function will returns ZERO, others returns Collection.Count.(返回一个数据集合之中的元素的数目,
806     ''' 假若这个集合是空值或者空的,则返回0,其他情况则返回Count拓展函数的结果)
807     ''' </summary>
808     ''' <typeparam name="T"></typeparam>
809     ''' <param name="collection"></param>
810     ''' <returns></returns>
811     ''' <remarks></remarks>
812     <Extension> Public Function DataCounts(Of T)(collection As IEnumerable(Of T)) As Integer
813         If collection Is Nothing Then
814             Return 0
815         ElseIf TypeOf collection Is T() Then
816             Return DirectCast(collection, T()).Length
817         ElseIf collection.GetType.IsInheritsFrom(GetType(System.Collections.Generic.List(Of T))) Then
818             Return DirectCast(collection, System.Collections.Generic.List(Of T)).Count
819         Else
820             Return Enumerable.Count(collection)
821         End If
822     End Function
823
824     ''' <summary>
825     ''' All of the number value in the target array offset a integer value.
826     ''' </summary>
827     ''' <param name="array"></param>
828     ''' <param name="intOffset"></param>
829     ''' <returns></returns>
830     <ExportAPI("OffSet")>
831     <Extension> Public Function Offset(ByRef array As Integer(), intOffset As IntegerAs Integer()
832         For i As Integer = 0 To array.Length - 1
833             array(i) = array(i) + intOffset
834         Next
835         Return array
836     End Function
837
838     ''' <summary>
839     ''' All of the number value in the target array offset a integer value.
840     ''' </summary>
841     ''' <param name="array"></param>
842     ''' <param name="intOffset"></param>
843     ''' <returns></returns>
844     <ExportAPI("OffSet")>
845     <Extension> Public Function Offset(ByRef array As Long(), intOffset As IntegerAs Long()
846         For i As Integer = 0 To array.Length - 1
847             array(i) = array(i) + intOffset
848         Next
849         Return array
850     End Function
851
852     ''' <summary>
853     ''' Parsing the dat value from the expression text, if any exception happend, a null date value will returned.
854     ''' (空字符串会返回空的日期)
855     ''' </summary>
856     ''' <param name="s"></param>
857     ''' <returns></returns>
858     '''
859     <ExportAPI("Date.Parse")>
860     <Extension> Public Function ParseDateTime(s As StringAs Date
861         If String.IsNullOrEmpty(s) Then
862             Return New Date
863         Else
864             Return DateTime.Parse(s)
865         End If
866     End Function
867
868 #Region ""
869
870     <Extension> Public Function InvokeSet(Of T As Class, Tvalue)(obj As T, [Property] As PropertyInfo, value As Tvalue) As T
871         Call [Property].SetValue(obj, value, Nothing)
872         Return obj
873     End Function
874
875     ''' <summary>
876     ''' Value assignment to the target variable.(将<paramref name="value"/>参数里面的值赋值给<paramref name="var"/>参数然后返回<paramref name="value"/>)
877     ''' </summary>
878     ''' <typeparam name="T"></typeparam>
879     ''' <param name="var"></param>
880     ''' <param name="value"></param>
881     ''' <returns></returns>
882     ''' <remarks></remarks>
883     ''' 
884     <MethodImpl(MethodImplOptions.AggressiveInlining)>
885     <Extension> Public Function SetValue(Of T)(ByRef var As T, value As T) As T
886         var = value
887         Return value
888     End Function
889
890     <MethodImpl(MethodImplOptions.AggressiveInlining)>
891     <Extension>
892     Public Function SetValue(Of T)(ByRef var As T, value As Func(Of T, T)) As T
893         var = value(arg:=var)
894         Return var
895     End Function
896
897     ''' <summary>
898     ''' Copy the source value directly to the target variable and then return the source value.
899     ''' </summary>
900     ''' <typeparam name="T"></typeparam>
901     ''' <param name="source"></param>
902     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T) As T
903         arg1 = source
904         arg2 = source
905         Return source
906     End Function
907
908     ''' <summary>
909     ''' Copy the source value directly to the target variable and then return the source value.
910     ''' </summary>
911     ''' <typeparam name="T"></typeparam>
912     ''' <param name="source"></param>
913     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T) As T
914         arg1 = source
915         arg2 = source
916         arg3 = source
917         Return source
918     End Function
919     ''' <summary>
920     ''' Copy the source value directly to the target variable and then return the source value.
921     ''' </summary>
922     ''' <typeparam name="T"></typeparam>
923     ''' <param name="source"></param>
924     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T) As T
925         arg1 = source
926         arg2 = source
927         arg3 = source
928         arg4 = source
929         Return source
930     End Function
931     ''' <summary>
932     ''' Copy the source value directly to the target variable and then return the source value.
933     ''' </summary>
934     ''' <typeparam name="T"></typeparam>
935     ''' <param name="source"></param>
936     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T) As T
937         arg1 = source
938         arg2 = source
939         arg3 = source
940         arg4 = source
941         arg5 = source
942         Return source
943     End Function
944     ''' <summary>
945     ''' Copy the source value directly to the target variable and then return the source value.
946     ''' </summary>
947     ''' <typeparam name="T"></typeparam>
948     ''' <param name="source"></param>
949     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T) As T
950         arg1 = source
951         arg2 = source
952         arg3 = source
953         arg4 = source
954         arg5 = source
955         arg6 = source
956         Return source
957     End Function
958     ''' <summary>
959     ''' Copy the source value directly to the target variable and then return the source value.
960     ''' </summary>
961     ''' <typeparam name="T"></typeparam>
962     ''' <param name="source"></param>
963     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T) As T
964         arg1 = source
965         arg2 = source
966         arg3 = source
967         arg4 = source
968         arg5 = source
969         arg6 = source
970         arg7 = source
971         Return source
972     End Function
973     ''' <summary>
974     ''' Copy the source value directly to the target variable and then return the source value.
975     ''' </summary>
976     ''' <typeparam name="T"></typeparam>
977     ''' <param name="source"></param>
978     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T, ByRef arg8 As T) As T
979         arg1 = source
980         arg2 = source
981         arg3 = source
982         arg4 = source
983         arg5 = source
984         arg6 = source
985         arg7 = source
986         arg8 = source
987         Return source
988     End Function
989     ''' <summary>
990     ''' Copy the source value directly to the target variable and then return the source value.
991     ''' </summary>
992     ''' <typeparam name="T"></typeparam>
993     ''' <param name="source"></param>
994     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T, ByRef arg8 As T, ByRef arg9 As T) As T
995         arg1 = source
996         arg2 = source
997         arg3 = source
998         arg4 = source
999         arg5 = source
1000         arg6 = source
1001         arg7 = source
1002         arg8 = source
1003         arg9 = source
1004         Return source
1005     End Function
1006     ''' <summary>
1007     ''' Copy the source value directly to the target variable and then return the source value.
1008     ''' </summary>
1009     ''' <typeparam name="T"></typeparam>
1010     ''' <param name="source"></param>
1011     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T, ByRef arg8 As T, ByRef arg9 As T, ByRef arg10 As T) As T
1012         arg1 = source
1013         arg2 = source
1014         arg3 = source
1015         arg4 = source
1016         arg5 = source
1017         arg6 = source
1018         arg7 = source
1019         arg8 = source
1020         arg9 = source
1021         arg10 = source
1022         Return source
1023     End Function
1024     ''' <summary>
1025     ''' Copy the source value directly to the target variable and then return the source value.
1026     ''' </summary>
1027     ''' <typeparam name="T"></typeparam>
1028     ''' <param name="source"></param>
1029     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T, ByRef arg8 As T, ByRef arg9 As T, ByRef arg10 As T, ByRef arg11 As T) As T
1030         arg1 = source
1031         arg2 = source
1032         arg3 = source
1033         arg4 = source
1034         arg5 = source
1035         arg6 = source
1036         arg7 = source
1037         arg8 = source
1038         arg9 = source
1039         arg10 = source
1040         arg11 = source
1041         Return source
1042     End Function
1043     ''' <summary>
1044     ''' Copy the source value directly to the target variable and then return the source value.
1045     ''' </summary>
1046     ''' <typeparam name="T"></typeparam>
1047     ''' <param name="source"></param>
1048     <Extension> Public Function ShadowCopy(Of T)(source As T, ByRef arg1 As T, ByRef arg2 As T, ByRef arg3 As T, ByRef arg4 As T, ByRef arg5 As T, ByRef arg6 As T, ByRef arg7 As T, ByRef arg8 As T, ByRef arg9 As T, ByRef arg10 As T, ByRef arg11 As T, ByRef arg12 As T) As T
1049         arg1 = source
1050         arg2 = source
1051         arg3 = source
1052         arg4 = source
1053         arg5 = source
1054         arg6 = source
1055         arg7 = source
1056         arg8 = source
1057         arg9 = source
1058         arg10 = source
1059         arg11 = source
1060         arg12 = source
1061         Return source
1062     End Function
1063 #End Region
1064
1065 #If NET_40 = 0 Then
1066
1067     ''' <summary>
1068     ''' Modify target object property value using a <paramref name="valueModifier">specific value provider</paramref> and then return original instance object.
1069     ''' (修改目标对象的属性之后返回目标对象)
1070     ''' </summary>
1071     ''' <typeparam name="T"></typeparam>
1072     ''' <param name="obj"></param>
1073     ''' <returns></returns>
1074     ''' <remarks></remarks>
1075     <Extension> Public Function ModifyValue(Of T As Class)([property] As PropertyInfo, obj As T, valueModifier As Func(Of ObjectObject)) As T
1076         Dim Value As Object = [property].GetValue(obj)
1077         Value = valueModifier(Value)
1078         Call [property].SetValue(obj, Value)
1079
1080         Return obj
1081     End Function
1082 #End If
1083
1084 #If FRAMEWORD_CORE Then
1085     ''' <summary>
1086     ''' Insert data or update the exists data in the dictionary, if the target object with <see cref="INamedValue.Key"/> 
1087     ''' is not exists in the dictionary, then will be insert, else the old value will be replaced with the parameter 
1088     ''' value <paramref name="item"/>.
1089     ''' (向字典对象之中更新或者插入新的数据,假若目标字典对象之中已经存在了一个数据的话,则会将原有的数据覆盖,并返回原来的数据)
1090     ''' </summary>
1091     ''' <typeparam name="T"></typeparam>
1092     ''' <param name="dict"></param>
1093     ''' <param name="item"></param>
1094     ''' <returns></returns>
1095     ''' <remarks></remarks>
1096     <Extension> Public Function InsertOrUpdate(Of T As INamedValue)(ByRef dict As Dictionary(Of String, T), item As T) As T
1097         Dim pre As T
1098
1099         If dict.ContainsKey(item.Key) Then
1100             pre = dict(item.Key)
1101
1102             Call dict.Remove(item.Key)
1103             Call $"data was updated: {Scripting.ToString(pre)} -> {item.Key}".__DEBUG_ECHO
1104         Else
1105             pre = item
1106         End If
1107
1108         Call dict.Add(item.Key, item)
1109
1110         Return pre
1111     End Function
1112
1113     ''' <summary>
1114     ''' Remove target object from dictionary.
1115     ''' </summary>
1116     ''' <typeparam name="T"></typeparam>
1117     ''' <param name="dict"></param>
1118     ''' <param name="item"></param>
1119     ''' <returns></returns>
1120     <Extension> Public Function Remove(Of T As INamedValue)(ByRef dict As Dictionary(Of String, T), item As T) As T
1121         Call dict.Remove(item.Key)
1122         Return item
1123     End Function
1124
1125     <Extension> Public Function AddRange(Of T As INamedValue)(ByRef dict As Dictionary(Of String, T), data As IEnumerable(Of T)) As Dictionary(Of String, T)
1126         For Each x As T In data
1127             Call InsertOrUpdate(dict, x)
1128         Next
1129
1130         Return dict
1131     End Function
1132 #End If
1133
1134     ''' <summary>
1135     ''' The <see cref="StringBuilder"/> object its content is nothing?
1136     ''' </summary>
1137     ''' <param name="sBuilder"></param>
1138     ''' <returns></returns>
1139     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1140     <Extension> Public Function IsNullOrEmpty(sBuilder As StringBuilder) As Boolean
1141         Return sBuilder Is Nothing OrElse sBuilder.Length = 0
1142     End Function
1143
1144     ''' <summary>
1145     ''' Merge the target array collection into one collection.(将目标数组的集合合并为一个数组)
1146     ''' </summary>
1147     ''' <typeparam name="T"></typeparam>
1148     ''' <param name="source"></param>
1149     ''' <returns></returns>
1150     ''' <remarks></remarks>
1151     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1152     <Extension> Public Function ToVector(Of T)(source As IEnumerable(Of IEnumerable(Of T))) As T()
1153         Return Unlist(source).ToArray
1154     End Function
1155
1156     ''' <summary>
1157     ''' Empty list will be skip and ignored.
1158     ''' (这是一个安全的方法,空集合会被自动跳过,并且这个函数总是返回一个集合不会返回空值)
1159     ''' </summary>
1160     ''' <typeparam name="T"></typeparam>
1161     ''' <param name="source"></param>
1162     ''' <returns></returns>
1163     <Extension> Public Function Unlist(Of T)(source As IEnumerable(Of IEnumerable(Of T))) As List(Of T)
1164         Dim list As New List(Of T)
1165
1166         For Each line As IEnumerable(Of T) In source
1167             If Not line Is Nothing Then
1168                 Call list.AddRange(collection:=line)
1169             End If
1170         Next
1171
1172         Return list
1173     End Function
1174
1175     ''' <summary>
1176     ''' Merge the target array collection into one collection.
1177     ''' (将目标数组的集合合并为一个数组,这个方法是提供给超大的集合的,即元素的数目非常的多的,即超过了<see cref="Integer"></see>的上限值)
1178     ''' </summary>
1179     ''' <typeparam name="T"></typeparam>
1180     ''' <param name="source"></param>
1181     ''' <returns></returns>
1182     ''' <remarks></remarks>
1183     <Extension> Public Function MatrixToUltraLargeVector(Of T)(source As IEnumerable(Of T())) As LinkedList(Of T)
1184         Dim lnkList As LinkedList(Of T) = New LinkedList(Of T)
1185
1186         For Each Line As T() In source
1187             For Each item As T In Line
1188                 Call lnkList.AddLast(item)
1189             Next
1190         Next
1191
1192         Return lnkList
1193     End Function
1194
1195     ''' <summary>
1196     ''' Add a linked list of a collection of specific type of data.
1197     ''' </summary>
1198     ''' <typeparam name="T"></typeparam>
1199     ''' <param name="list"></param>
1200     ''' <param name="data"></param>
1201     ''' <returns></returns>
1202     <Extension> Public Function AddRange(Of T)(list As LinkedList(Of T), data As IEnumerable(Of T)) As LinkedList(Of T)
1203         For Each item As T In data
1204             Call list.AddLast(item)
1205         Next
1206
1207         Return list
1208     End Function
1209
1210     ''' <summary>
1211     ''' 矩阵转置: 将矩阵之中的元素进行行列位置的互换
1212     ''' </summary>
1213     ''' <typeparam name="T">矩阵之中的元素类型</typeparam>
1214     ''' <param name="MAT">为了方便理解和使用,矩阵使用数组的数组来表示的</param>
1215     ''' <returns></returns>
1216     ''' <remarks></remarks>
1217     <Extension> Public Function MatrixTranspose(Of T)(MAT As IEnumerable(Of T())) As T()()
1218         Dim LQuery As T()() = (From i As Integer
1219                                In MAT.First.Sequence
1220                                Select (From Line As T() In MAT Select Line(i)).ToArray).ToArray
1221         Return LQuery
1222     End Function
1223
1224     ''' <summary>
1225     ''' 将矩阵之中的元素进行行列位置的互换,请注意,假若长度不一致的话,会按照最短的元素来转置,故而使用本函数可能会造成一些信息的丢失
1226     ''' </summary>
1227     ''' <typeparam name="T"></typeparam>
1228     ''' <param name="MAT"></param>
1229     ''' <returns></returns>
1230     ''' <remarks></remarks>
1231     <Extension> Public Function MatrixTransposeIgnoredDimensionAgreement(Of T)(MAT As IEnumerable(Of T())) As T()()
1232         Dim LQuery = (From i As Integer
1233                       In (From n As T()
1234                           In MAT
1235                           Select n.Length
1236                           Order By Length Ascending).First.Sequence
1237                       Select (From Line In MAT Select Line(i)).ToArray).ToArray
1238         Return LQuery
1239     End Function
1240
1241     ''' <summary>
1242     ''' 
1243     ''' </summary>
1244     ''' <param name="DIR">The source directory.</param>
1245     ''' <param name="moveTo"></param>
1246     ''' <param name="Split"></param>
1247     ''' <returns></returns>
1248 #If FRAMEWORD_CORE Then
1249     <ExportAPI("Mv.Split")>
1250     Public Function SplitMV(DIR As String, <Parameter("DIR.MoveTo")> moveTo As String, Split As IntegerAs Integer
1251 #Else
1252     Public Function SplitMV(dir As String, moveto As String, split As IntegerAs Integer
1253 #End If
1254         Dim Files As String() = FileIO.FileSystem.GetFiles(DIR, FileIO.SearchOption.SearchTopLevelOnly).ToArray
1255         Dim n As Integer
1256         Dim m As Integer = 1
1257
1258         For i As Integer = 0 To Files.Length - 1
1259             If n < Split Then
1260                 Call FileIO.FileSystem.MoveFile(Files(i), String.Format("{0}_{1}/{2}", moveTo, m, FileIO.FileSystem.GetFileInfo(Files(i)).Name))
1261                 n += 1
1262             Else
1263                 n = 0
1264                 m += 1
1265             End If
1266         Next
1267
1268         Return 0
1269     End Function
1270
1271 #If FRAMEWORD_CORE Then
1272     ''' <summary>
1273     ''' The target parameter <paramref name="n"/> value is NaN or not a real number or not?
1274     ''' (判断目标实数是否为一个无穷数或者非计算的数字,产生的原因主要来自于除0运算结果或者达到了
1275     ''' <see cref="Double"></see>的上限或者下限)
1276     ''' </summary>
1277     ''' <param name="n"></param>
1278     ''' <returns></returns>
1279     ''' <remarks></remarks>
1280     <ExportAPI("Double.Is.NA",
1281                Info:="Is this double type of the number is an NA type infinity number. this is major comes from the devided by ZERO.")>
1282     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1283     <Extension> Public Function IsNaNImaginary(n As DoubleAs Boolean
1284 #Else
1285     <Extension> Public Function Is_NA_UHandle(n As DoubleAs Boolean
1286 #End If
1287         Return Double.IsNaN(n) OrElse
1288             Double.IsInfinity(n) OrElse
1289             Double.IsNegativeInfinity(n) OrElse
1290             Double.IsPositiveInfinity(n)
1291     End Function
1292
1293     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1294     <Extension> Public Function IsNaNImaginary(n As SingleAs Boolean
1295         Return Single.IsNaN(n) OrElse
1296             Single.IsInfinity(n) OrElse
1297             Single.IsNegativeInfinity(n) OrElse
1298             Single.IsPositiveInfinity(n)
1299     End Function
1300 #If FRAMEWORD_CORE Then
1301
1302     ''' <summary>
1303     ''' Fuzzy match two string, this is useful for the text query or searching.
1304     ''' (请注意,这个函数是不会自动转换大小写的,如果是需要字符大小写不敏感,
1305     ''' 请先将query以及subject都转换为小写)
1306     ''' </summary>
1307     ''' <param name="query"></param>
1308     ''' <param name="Subject"></param>
1309     ''' <returns></returns>
1310     ''' <remarks></remarks>
1311     <ExportAPI("FuzzyMatch",
1312                Info:="Fuzzy match two string, this is useful for the text query or searching.")>
1313     <Extension> Public Function FuzzyMatching(query$, subject$, Optional tokenbased As Boolean = TrueOptional cutoff# = 0.8) As Boolean
1314         If tokenbased Then
1315             Dim similarity# = Evaluate(query, subject,,, )
1316             Return similarity >= cutoff
1317         Else
1318             Dim dist = LevenshteinDistance.ComputeDistance(query, subject)
1319             If dist Is Nothing Then
1320                 Return False
1321             Else
1322                 Return dist.MatchSimilarity >= cutoff
1323             End If
1324         End If
1325     End Function
1326 #End If
1327
1328     ''' <summary>
1329     ''' 这个是一个安全的方法,假若下标越界或者目标数据源为空的话,则会返回空值
1330     ''' </summary>
1331     ''' <typeparam name="T"></typeparam>
1332     ''' <param name="source"></param>
1333     ''' <param name="index"></param>
1334     ''' <returns></returns>
1335 #If FRAMEWORD_CORE Then
1336     <ExportAPI("Get.Item")>
1337     <Extension> Public Function GetItem(Of T)(source As IEnumerable(Of T), index As IntegerAs T
1338 #Else
1339     <Extension> Public Function GetItem(Of T)(source As IEnumerable(Of T), index As IntegerAs T
1340 #End If
1341         If source Is Nothing Then
1342             Return Nothing
1343         Else
1344             Return source.ElementAtOrDefault(index)
1345         End If
1346     End Function
1347
1348     ''' <summary>
1349     ''' 求取该数据集的标准差
1350     ''' </summary>
1351     ''' <param name="data"></param>
1352     ''' <returns></returns>
1353     ''' <remarks></remarks>
1354     '''
1355     <ExportAPI("StdError")>
1356     <Extension> Public Function StdError(data As IEnumerable(Of Double)) As Double
1357         Dim Average As Double = data.Average
1358         Dim Sum = (From n As Double In data Select (n - Average) ^ 2).Sum
1359         Sum /= data.Count
1360         Return Global.System.Math.Sqrt(Sum)
1361     End Function
1362
1363     ''' <summary>
1364     ''' The first element in a collection.
1365     ''' </summary>
1366     Public Const Scan0 As Integer = 0
1367
1368     ''' <summary>
1369     ''' 函数只返回有重复的数据
1370     ''' </summary>
1371     ''' <typeparam name="T"></typeparam>
1372     ''' <typeparam name="TTag"></typeparam>
1373     ''' <param name="source"></param>
1374     ''' <param name="getKey"></param>
1375     ''' <returns></returns>
1376     <Extension> Public Function CheckDuplicated(Of T, TTag)(source As IEnumerable(Of T),
1377                                                             getKey As Func(Of T, TTag)) _
1378                                                                    As GroupResult(Of T, TTag)()
1379         Dim Groups = From x As T
1380                      In source
1381                      Select x
1382                      Group x By tag = getKey(x) Into Group '
1383         Dim duplicates As GroupResult(Of T, TTag)() =
1384             LinqAPI.Exec(Of GroupResult(Of T, TTag)) <=
1385  _
1386                 From g
1387                 In Groups.AsParallel
1388                 Where g.Group.Count > 1
1389                 Select New GroupResult(Of T, TTag) With {
1390                     .Tag = g.tag,
1391                     .Group = g.Group.ToArray
1392                 }
1393
1394         Return duplicates
1395     End Function
1396
1397     ''' <summary>
1398     ''' 移除重复的对象,这个函数是根据对象所生成的标签来完成的
1399     ''' </summary>
1400     ''' <typeparam name="T"></typeparam>
1401     ''' <typeparam name="Tag"></typeparam>
1402     ''' <param name="source"></param>
1403     ''' <param name="getKey">得到对象的标签</param>
1404     ''' <returns></returns>
1405     <Extension> Public Function RemoveDuplicates(Of T, Tag)(source As IEnumerable(Of T), getKey As Func(Of T, Tag)) As T()
1406         Dim Groups = From obj As T
1407                      In source
1408                      Select obj
1409                      Group obj By objTag = getKey(obj) Into Group '
1410         Dim LQuery = (From obj In Groups Select obj.Group.First).ToArray
1411         Return LQuery
1412     End Function
1413
1414 #If FRAMEWORD_CORE Then
1415
1416     ''' <summary>
1417     ''' Remove all of the null object in the target object collection.
1418     ''' (这个是一个安全的方法,假若目标集合是空值,则函数会返回一个空的集合)
1419     ''' </summary>
1420     ''' <typeparam name="T"></typeparam>
1421     ''' <param name="source"></param>
1422     ''' <returns></returns>
1423     ''' <remarks></remarks>
1424     '''
1425     <ExportAPI("NullValue.Trim"Info:="Remove all of the null object in the target object collection")>
1426     <Extension> Public Function TrimNull(Of T As Class)(source As IEnumerable(Of T)) As T()
1427 #Else
1428     ''' <summary>
1429     ''' Remove all of the null object in the target object collection
1430     ''' </summary>
1431     ''' <typeparam name="T"></typeparam>
1432     ''' <param name="Collection"></param>
1433     ''' <returns></returns>
1434     ''' <remarks></remarks>
1435     <Extension> Public Function TrimNull(Of T As Class)(source As IEnumerable(Of T)) As T()
1436 #End If
1437         If source Is Nothing Then
1438             Return New T() {}
1439         Else
1440             Return (From x In source Where Not x Is Nothing Select x).ToArray
1441         End If
1442     End Function
1443
1444     ''' <summary>
1445     ''' Remove all of the null object in the target object collection
1446     ''' </summary>
1447     ''' <param name="source"></param>
1448     ''' <returns></returns>
1449     ''' <remarks></remarks>
1450     <Extension> Public Function TrimNull(source As IEnumerable(Of String)) As String()
1451         If source Is Nothing Then
1452             Return New String() {}
1453         Else
1454             Return (From x In source Where Not x.StringEmpty Select x).ToArray
1455         End If
1456     End Function
1457
1458     ''' <summary>
1459     ''' Return a collection with randomize element position in <paramref name="source">the original collection</paramref>.
1460     ''' (从原有序序列中获取一个随机元素的序列)
1461     ''' </summary>
1462     ''' <typeparam name="T"></typeparam>
1463     ''' <param name="source"></param>
1464     ''' <returns></returns>
1465     ''' <remarks></remarks>
1466     '''
1467     <ExportAPI("Shuffles")>
1468     <Extension> Public Function Shuffles(Of T)(source As IEnumerable(Of T)) As T()
1469         Dim tmp As New List(Of T)(source)
1470         Dim buf As T() = New T(tmp.Count - 1) {}
1471         Dim rand As New Random(Seed:=Math.Seed)
1472         Dim l As Integer = tmp.Count - 1
1473
1474         For i As Integer = 0 To buf.Length - 1
1475             Dim index As Integer = rand.Next(minValue:=0, maxValue:=l)
1476             buf(i) = tmp(index)
1477             Call tmp.RemoveAt(index)
1478             l -= 1
1479         Next
1480
1481         Return buf
1482     End Function
1483
1484     ''' <summary>
1485     ''' 返回n长度的序列数值,这些序列数值是打乱顺序的,但是升序排序之后会得到1:n的序列
1486     ''' 请注意,这个序列并不是随机数,而是将n长度的序列之中的元素打乱顺序的结果
1487     ''' </summary>
1488     ''' <param name="n"></param>
1489     ''' <returns></returns>
1490     <ExportAPI("Sequence.Random")>
1491     <Extension> Public Function SeqRandom(n As IntegerAs Integer()
1492         Dim source As Integer() = n.Sequence.ToArray
1493         Dim Random As Integer() = source.Shuffles
1494         Return Random
1495     End Function
1496
1497     ''' <summary>
1498     ''' 随机的在目标集合中选取指定数目的子集合
1499     ''' </summary>
1500     ''' <typeparam name="T"></typeparam>
1501     ''' <param name="source"></param>
1502     ''' <param name="counts">当目标数目大于或者等于目标集合的数目的时候,则返回目标集合</param>
1503     ''' <returns></returns>
1504     ''' <remarks></remarks>
1505     <Extension> Public Function TakeRandomly(Of T)(source As IEnumerable(Of T), counts%) As T()
1506         Dim array As T() = source.ToArray
1507
1508         If counts >= array.Length Then
1509             Return source
1510         Else
1511             Dim out As T() = New T(counts - 1) {}
1512             Dim input As New List(Of T)(array)
1513             Dim random As New Random
1514
1515             For i As Integer = 0 To counts - 1
1516                 Dim ind As Integer = random.Next(input.Count)
1517                 out(i) = input(ind)
1518                 Call input.RemoveAt(ind)
1519             Next
1520
1521             Return out
1522         End If
1523     End Function
1524
1525     ''' <summary>
1526     ''' Convert target object type collection into a string array using the Object.ToString() interface function.
1527     ''' </summary>
1528     ''' <typeparam name="T"></typeparam>
1529     ''' <param name="source"></param>
1530     ''' <returns></returns>
1531     ''' <remarks></remarks>
1532     <Extension> Public Function ToStringArray(Of T)(source As IEnumerable(Of T)) As String()
1533         If source Is Nothing Then
1534             Return {}
1535         End If
1536
1537         Dim LQuery$() = LinqAPI.Exec(Of String) _
1538  _
1539             () <= From item As T
1540                   In source
1541                   Let strItem As String = item?.ToString
1542                   Select strItem
1543
1544         Return LQuery
1545     End Function
1546
1547     <Extension> Public Sub Swap(Of T)(ByRef array As T(), a%, b%)
1548         Dim tmp As T = array(a)
1549         array(a) = array(b)
1550         array(b) = tmp
1551     End Sub
1552
1553     ''' <summary>
1554     ''' Swap the value in the two variables.
1555     ''' </summary>
1556     ''' <typeparam name="T"></typeparam>
1557     ''' <param name="obj1"></param>
1558     ''' <param name="obj2"></param>
1559     ''' <remarks></remarks>
1560     <Extension> Public Sub SwapWith(Of T)(ByRef obj1 As T, ByRef obj2 As T)
1561         Dim objTemp As T = obj1
1562         obj1 = obj2
1563         obj2 = objTemp
1564     End Sub
1565
1566     ''' <summary>
1567     ''' Swap the two item position in the target <paramref name="list">list</paramref>.
1568     ''' </summary>
1569     ''' <typeparam name="T"></typeparam>
1570     ''' <param name="list"></param>
1571     ''' <param name="obj_1"></param>
1572     ''' <param name="obj_2"></param>
1573     <Extension> Public Sub SwapItem(Of T)(ByRef list As List(Of T), obj_1 As T, obj_2 As T)
1574         Dim idx_1 As Integer = list.IndexOf(obj_1)
1575         Dim idx_2 As Integer = list.IndexOf(obj_2)
1576
1577         If idx_1 = -1 OrElse idx_2 = -1 Then
1578             Return
1579         End If
1580
1581         Call list.RemoveAt(idx_1)
1582         Call list.Insert(idx_1, obj_2)
1583         Call list.RemoveAt(idx_2)
1584         Call list.Insert(idx_2, obj_2)
1585     End Sub
1586
1587 #If FRAMEWORD_CORE Then
1588     ''' <summary>
1589     ''' Add array location index value for the <see cref="IAddressOf"/> elements in the sequence.
1590     ''' (为列表中的对象添加对象句柄值)
1591     ''' </summary>
1592     ''' <param name="source"></param>
1593     ''' <remarks></remarks>
1594     <Extension> Public Function WriteAddress(Of T As IAddressOf)(ByRef source As IEnumerable(Of T), Optional offset As Integer = 0) As T()
1595         Dim list As New List(Of T)
1596         Dim i As Integer = offset
1597
1598         For Each x As T In source
1599             Call x.Assign(address:=i)
1600
1601             i += 1
1602             list += x
1603         Next
1604
1605         Return list
1606     End Function
1607 #End If
1608
1609 #If FRAMEWORD_CORE Then
1610     ''' <summary>
1611     ''' Gets the subscript index of a generic collection.(获取某一个集合的下标的集合)
1612     ''' </summary>
1613     ''' <typeparam name="T">集合中的元素为任意类型的</typeparam>
1614     ''' <param name="source">目标集合对象</param>
1615     ''' <returns>A integer array of subscript index of the target generic collection.</returns>
1616     ''' <remarks></remarks>
1617     '''
1618     <ExportAPI("Sequence.Index"Info:="Gets the subscript index of a generic collection.")>
1619     <Extension> Public Iterator Function Sequence(Of T)(
1620                                         <Parameter("source""")> source As IEnumerable(Of T),
1621                                         <Parameter("index.OffSet""")> Optional offSet% = 0) _
1622                                      As <FunctionReturns("A integer array of subscript index of the target generic collection.")> IEnumerable(Of Integer)
1623 #Else
1624     ''' <summary>
1625     ''' 获取某一个集合的下标的集合
1626     ''' </summary>
1627     ''' <typeparam name="T">集合中的元素为任意类型的</typeparam>
1628     ''' <param name="Collection">目标集合对象</param>
1629     ''' <returns></returns>
1630     ''' <remarks></remarks>
1631     '''
1632     <Extension> Public Iterator Function Sequence(Of T)(source As IEnumerable(Of T), Optional offset As Integer = 0) As IEnumerable(Of Integer)
1633 #End If
1634         If source Is Nothing Then
1635             Return
1636         Else
1637             Dim i As Integer = offSet
1638
1639             For Each x As T In source
1640                 Yield i
1641                 i += 1
1642             Next
1643         End If
1644     End Function
1645
1646     ''' <summary>
1647     ''' Alias of the linq function <see cref="Enumerable.Range"/>
1648     ''' </summary>
1649     ''' <param name="range"></param>
1650     ''' <returns></returns>
1651     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1652     <Extension>
1653     Iterator Public Function Sequence(range As IntRange, Optional stepOffset% = 1) As IEnumerable(Of Integer)
1654         If stepOffset = 0 Then
1655             stepOffset = 1
1656 #If DEBUG Then
1657             Call $"step_offset is ZERO! This will caused a infinity loop, using default step `1`!".Warning
1658 #End If
1659         End If
1660
1661         For i As Integer = range.Min To range.Max Step stepOffset
1662             Yield i
1663         Next
1664     End Function
1665
1666     <MethodImpl(MethodImplOptions.AggressiveInlining)>
1667     <Extension>
1668     Public Function AsRange(ints As IEnumerable(Of Integer)) As IntRange
1669         Return New IntRange(ints)
1670     End Function
1671
1672     <Extension> Public Iterator Function LongSeq(Of T)(source As IEnumerable(Of T), Optional offset% = 0) As IEnumerable(Of Long)
1673         If source Is Nothing Then
1674             Return
1675         Else
1676             Dim i As Long = offset
1677
1678             For Each x As T In source
1679                 Yield i
1680                 i += 1
1681             Next
1682         End If
1683     End Function
1684
1685     <Extension> Public Function LongSeq(n&) As Long()
1686         Dim array&() = New Long(n - 1) {}
1687         For i As Long = 0 To array.Length - 1
1688             array(i) = i
1689         Next
1690         Return array
1691     End Function
1692
1693     <Extension> Public Function Takes(Of T)(source As T(), count As IntegerAs T()
1694         Dim bufs As T() = New T(count - 1) {}
1695         Call Array.ConstrainedCopy(source, Scan0, bufs, Scan0, count)
1696         Return bufs
1697     End Function
1698
1699     ''' <summary>
1700     ''' 将目标键值对对象的集合转换为一个字典对象
1701     ''' </summary>
1702     ''' <typeparam name="TKey"></typeparam>
1703     ''' <typeparam name="TValue"></typeparam>
1704     ''' <param name="source"></param>
1705     ''' <param name="remoteDuplicates">当这个参数为False的时候,出现重复的键名会抛出错误,当为True的时候,有重复的键名存在的话,可能会丢失一部分的数据</param>
1706     ''' <returns></returns>
1707     ''' <remarks></remarks>
1708     <Extension> Public Function ToDictionary(Of TKey, TValue)(
1709                                 source As IEnumerable(Of KeyValuePair(Of TKey, TValue)),
1710                        Optional remoteDuplicates As Boolean = FalseAs Dictionary(Of TKey, TValue)
1711
1712         If remoteDuplicates Then
1713             Dim table As New Dictionary(Of TKey, TValue)
1714
1715             For Each x In source
1716                 If table.ContainsKey(x.Key) Then
1717                     Call $"[Duplicated] {x.Key.ToString}".PrintException
1718                 Else
1719                     Call table.Add(x.Key, x.Value)
1720                 End If
1721             Next
1722
1723             Return table
1724         Else
1725             Dim dictionary As Dictionary(Of TKey, TValue) =
1726                 source.ToDictionary(Function(x) x.Key,
1727                                     Function(x) x.Value)
1728             Return dictionary
1729         End If
1730     End Function
1731
1732     ' 2018-6-11
1733     '
1734     ' 因为迭代器在访问linq序列的时候,对于非空序列,下面的IsNullOrEmpty函数总是会产生一次迭代
1735     ' 这个迭代可能会导致元素丢失的bug产生
1736     ' 所以在这里将这个linq函数注释掉
1737     ' 以后只需要判断迭代器是否是空值即可
1738
1739     '''' <summary>
1740     '''' This object collection is a null object or contains zero count items.
1741     '''' </summary>
1742     '''' <typeparam name="T"></typeparam>
1743     '''' <param name="source"></param>
1744     '''' <returns></returns>
1745     '''' <remarks></remarks>
1746     '<Extension> Public Function IsNullOrEmpty(Of T)(source As IEnumerable(Of T)) As Boolean
1747     '    If source Is Nothing Then
1748     '        Return True
1749     '    End If
1750
1751     '    Dim i% = -1
1752
1753     '    Using [try] = source.GetEnumerator
1754     '        Do While [try].MoveNext
1755
1756     '            ' debug view
1757     '            Dim null = [try].Current
1758
1759     '            ' 假若是存在元素的,则i的值会为零
1760     '            ' Some type of linq sequence not support this method.
1761     '            ' [try].Reset()
1762     '            i += 1
1763
1764     '            ' If is not empty, then this For loop will be used.
1765     '            Return False
1766     '        Loop
1767     '    End Using
1768
1769     '    ' 由于没有元素,所以For循环没有进行,i变量的值没有发生变化
1770     '    ' 使用count拓展进行判断或导致Linq被执行两次,现在使用FirstOrDefault来判断,
1771     '    ' 主需要查看第一个元素而不是便利整个Linq查询枚举, 从而提高了效率
1772     '    ' Due to the reason of source is empty, no elements, 
1773     '    ' so that i value Is Not changed as the For loop 
1774     '    ' didn 't used.
1775     '    Return i = -1
1776     'End Function
1777
1778     ''' <summary>
1779     ''' 字典之中是否是没有任何数据的?
1780     ''' </summary>
1781     ''' <typeparam name="TKey"></typeparam>
1782     ''' <typeparam name="TValue"></typeparam>
1783     ''' <param name="dict"></param>
1784     ''' <returns></returns>
1785     <Extension> Public Function IsNullOrEmpty(Of TKey, TValue)(dict As IDictionary(Of TKey, TValue)) As Boolean
1786         If dict Is Nothing Then
1787             Return True
1788         End If
1789         Return dict.Count = 0
1790     End Function
1791
1792     <Extension>
1793     Public Function IsNullOrEmpty(Of T As INamedValue)(table As Dictionary(Of T)) As Boolean
1794         If table Is Nothing Then
1795             Return True
1796         Else
1797             Return table.Count = 0
1798         End If
1799     End Function
1800
1801     ''' <summary>
1802     ''' 字典之中是否是没有任何数据的?
1803     ''' </summary>
1804     ''' <typeparam name="TKey"></typeparam>
1805     ''' <typeparam name="TValue"></typeparam>
1806     ''' <param name="dict"></param>
1807     ''' <returns></returns>
1808     <Extension> Public Function IsNullOrEmpty(Of TKey, TValue)(dict As IReadOnlyDictionary(Of TKey, TValue)) As Boolean
1809         If dict Is Nothing Then
1810             Return True
1811         End If
1812         Return dict.Count = 0
1813     End Function
1814
1815     <Extension> Public Function IsNullOrEmpty(Of TKey, TValue)(dict As ReadOnlyDictionary(Of TKey, TValue)) As Boolean
1816         If dict Is Nothing Then
1817             Return True
1818         End If
1819         Return dict.Count = 0
1820     End Function
1821
1822     ''' <summary>
1823     ''' 字典之中是否是没有任何数据的?
1824     ''' </summary>
1825     ''' <typeparam name="TKey"></typeparam>
1826     ''' <typeparam name="TValue"></typeparam>
1827     ''' <param name="dict"></param>
1828     ''' <returns></returns>
1829     <Extension> Public Function IsNullOrEmpty(Of TKey, TValue)(dict As Dictionary(Of TKey, TValue)) As Boolean
1830         If dict Is Nothing Then
1831             Return True
1832         End If
1833         Return dict.Count = 0
1834     End Function
1835
1836     ''' <summary>
1837     ''' 这个队列之中是否是没有任何数据的?
1838     ''' </summary>
1839     ''' <typeparam name="T"></typeparam>
1840     ''' <param name="queue"></param>
1841     ''' <returns></returns>
1842     <Extension> Public Function IsNullOrEmpty(Of T)(queue As Queue(Of T)) As Boolean
1843         If queue Is Nothing Then
1844             Return True
1845         End If
1846         Return queue.Count = 0
1847     End Function
1848
1849     <Extension>
1850     Public Function IsNullorEmpty(Of T)(vector As Vector(Of T)) As Boolean
1851         If vector Is Nothing Then
1852             Return True
1853         End If
1854         Return vector.Length = 0
1855     End Function
1856
1857     <Extension>
1858     Public Function IsNullOrEmpty(args As ArgumentCollection) As Boolean
1859         If args Is Nothing Then
1860             Return True
1861         End If
1862         Return args.Count = 0
1863     End Function
1864
1865     ''' <summary>
1866     ''' 这个动态列表之中是否是没有任何数据的?
1867     ''' </summary>
1868     ''' <typeparam name="T"></typeparam>
1869     ''' <param name="list"></param>
1870     ''' <returns></returns>
1871     <Extension> Public Function IsNullOrEmpty(Of T)(list As ICollection(Of T)) As Boolean
1872         If list Is Nothing Then
1873             Return True
1874         End If
1875         Return list.Count = 0
1876     End Function
1877
1878     <Extension> Public Function IsNullOrEmpty(Of T)(list As IList(Of T)) As Boolean
1879         If list Is Nothing Then
1880             Return True
1881         End If
1882         Return list.Count = 0
1883     End Function
1884
1885     <Extension> Public Function IsNullOrEmpty(Of T)(list As System.Collections.Generic.List(Of T)) As Boolean
1886         If list Is Nothing Then
1887             Return True
1888         End If
1889         Return list.Count = 0
1890     End Function
1891
1892     <Extension>
1893     Public Function IsNullOrEmpty(Of T)(collection As IReadOnlyCollection(Of T)) As Boolean
1894         If collection Is Nothing Then
1895             Return True
1896         Else
1897             Return collection.Count = 0
1898         End If
1899     End Function
1900
1901     <Extension>
1902     Public Function IsNullOrEmpty(Of T)(collection As ReadOnlyCollection(Of T)) As Boolean
1903         If collection Is Nothing Then
1904             Return True
1905         Else
1906             Return collection.Count = 0
1907         End If
1908     End Function
1909
1910     ''' <summary>
1911     ''' This object array is a null object or contains zero count items.(判断某一个对象数组是否为空)
1912     ''' </summary>
1913     ''' <typeparam name="T"></typeparam>
1914     ''' <returns></returns>
1915     ''' <remarks></remarks>
1916     <Extension> Public Function IsNullOrEmpty(Of T)(array As T()) As Boolean
1917         Return array Is Nothing OrElse array.Length = 0
1918     End Function
1919
1920     ''' <summary>
1921     ''' 0 for null object
1922     ''' </summary>
1923     ''' <typeparam name="T"></typeparam>
1924     ''' <param name="array"></param>
1925     ''' <returns></returns>
1926     <Extension> Public Function GetLength(Of T)(array As T()) As Integer
1927         If array Is Nothing Then
1928             Return 0
1929         Else
1930             Return array.Length
1931         End If
1932     End Function
1933
1934     <Extension> Public Function GetLength(Of T)(collect As IEnumerable(Of T)) As Integer
1935         If collect Is Nothing Then
1936             Return 0
1937         Else
1938             Return collect.Count
1939         End If
1940     End Function
1941
1942 #If FRAMEWORD_CORE Then
1943
1944     ''' <summary>
1945     ''' 执行一个命令行语句,并返回一个IO重定向对象,以获取被执行的目标命令的标准输出
1946     ''' </summary>
1947     ''' <param name="CLI"></param>
1948     ''' <returns></returns>
1949     ''' <remarks></remarks>
1950     '''
1951     <ExportAPI("Shell")>
1952     <Extension> Public Function Shell(CLI As StringAs IIORedirectAbstract
1953         Return CType(CLI, IORedirect)
1954     End Function
1955 #End If
1956
1957     ''' <summary>
1958     ''' 获取一个实数集合中所有元素的积
1959     ''' </summary>
1960     ''' <param name="source"></param>
1961     ''' <returns></returns>
1962     ''' <remarks></remarks>
1963     '''
1964     <ExportAPI("PI")>
1965     <Extension> Public Function π(source As IEnumerable(Of Double)) As Double
1966         If source Is Nothing Then
1967             Return 0
1968         End If
1969
1970         Dim result# = 1
1971         Dim stepInto As Boolean = False
1972
1973         For Each x As Double In source
1974             stepInto = True
1975             result *= x
1976         Next
1977
1978         If Not stepInto Then
1979             Return 0
1980         Else
1981             Return result
1982         End If
1983     End Function
1984
1985 #If FRAMEWORD_CORE Then
1986
1987     ''' <summary>
1988     ''' Fill the newly created image data with the specific color brush
1989     ''' </summary>
1990     ''' <param name="Image"></param>
1991     ''' <param name="FilledColor"></param>
1992     ''' <remarks></remarks>
1993     <Extension> Public Sub FillBlank(ByRef Image As Image, FilledColor As Brush)
1994         If Image Is Nothing Then
1995             Return
1996         End If
1997         Using gr As Graphics = Graphics.FromImage(Image)
1998             Dim R As New Rectangle(New Point, Image.Size)
1999             Call gr.FillRectangle(FilledColor, R)
2000         End Using
2001     End Sub
2002 #End If
2003
2004     ''' <summary>
2005     ''' Nothing
2006     ''' </summary>
2007     Friend Const null = Nothing
2008     Public Const void As Object = Nothing
2009
2010     ''' <summary>
2011     ''' Remove all of the element in the <paramref name="collection"></paramref> from target <paramref name="List">list</paramref>
2012     ''' </summary>
2013     ''' <typeparam name="T"></typeparam>
2014     ''' <param name="List"></param>
2015     ''' <param name="collection"></param>
2016     ''' <remarks></remarks>
2017     <Extension> Public Sub Removes(Of T)(ByRef List As List(Of T), collection As IEnumerable(Of T))
2018         For Each obj In collection
2019             Call List.Remove(obj)
2020         Next
2021     End Sub
2022
2023 #Region "Removes Last Element"
2024
2025     ''' <summary>
2026     ''' Removes the last element in the List object.(这个拓展函数同时兼容.NET框架的list类型以及sciBASIC之中的list类型)
2027     ''' </summary>
2028     ''' <typeparam name="T"></typeparam>
2029     ''' <param name="dotNETlist"></param>
2030     ''' <returns></returns>
2031     <Extension> Public Function RemoveLast(Of T)(ByRef dotNETlist As System.Collections.Generic.List(Of T)) As System.Collections.Generic.List(Of T)
2032         If dotNETlist.IsNullOrEmpty Then
2033             dotNETlist = New List(Of T)
2034
2035             ' 2018-1-25
2036             ' 需要将0和1分开来看,否则会造成最后一个元素永远都移除不了的bug
2037         ElseIf dotNETlist.Count = 1 Then
2038             dotNETlist.Clear()
2039         Else
2040             Dim i As Integer = dotNETlist.Count - 1
2041             Call dotNETlist.RemoveAt(i)
2042         End If
2043
2044         Return dotNETlist
2045     End Function
2046
2047     ''' <summary>
2048     ''' Removes the last element in the List object.
2049     ''' (这个拓展函数同时兼容.NET框架的list类型以及sciBASIC之中的<see cref="List(Of T)"/>类型)
2050     ''' </summary>
2051     ''' <typeparam name="T"></typeparam>
2052     ''' <param name="list"></param>
2053     ''' <returns></returns>
2054     <Extension> Public Function RemoveLast(Of T)(ByRef list As List(Of T)) As List(Of T)
2055         Return DirectCast(RemoveLast(dotNETlist:=list), List(Of T))
2056     End Function
2057
2058 #End Region
2059
2060     <Extension> Public Function RemoveFirst(Of T)(ByRef list As List(Of T)) As List(Of T)
2061         If list.IsNullOrEmpty OrElse list.Count = 1 Then
2062             list = New List(Of T)
2063         Else
2064             Call list.RemoveAt(Scan0)
2065         End If
2066
2067         Return list
2068     End Function
2069 End Module