1 |
#Region "Microsoft.VisualBasic::4839b20ed2ec527efe1fcd22bfe599aa, Microsoft.VisualBasic.Core\Extensions\Extensions.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
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 |
|
100 |
|
101 |
|
102 |
|
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 |
|
114 |
|
115 |
|
116 |
|
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 |
|
127 |
|
128 |
|
129 |
|
130 |
|
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 |
|
151 |
|
152 |
|
153 |
|
154 |
|
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 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
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 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
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 |
|
212 |
|
213 |
|
214 |
|
215 |
|
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 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
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 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
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 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
272 |
<Extension> Public Function MD5(s$) As String
|
273 |
Return s.GetMd5Hash
|
274 |
End Function
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
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 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
<Extension> Public Function Second(Of T)(source As IEnumerable(Of T), Optional suppressError As Boolean = False, Optional [default] As T = Nothing) As 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 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
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 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
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 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
<Extension> Public Function [Get](Of T)(array As IEnumerable(Of T), index As Integer, Optional [default] As T = Nothing) As 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 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
<Extension> Public Function ElementAtOrDefault(Of T)(array As T(), index As Integer, Optional [default] As T = Nothing) As 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 String, Callback 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 String, Callback As Action(Of String))
|
439 |
Call host.GetIPEndPoint.SendMessage(request, Callback)
|
440 |
End Sub
|
441 |
|
442 |
#End Region
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
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 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
<Extension> Public Function ToBoolean(b As Long) As Boolean
|
473 |
If b = 0 Then
|
474 |
Return False
|
475 |
Else
|
476 |
Return True
|
477 |
End If
|
478 |
End Function
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
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$ = Nothing) As 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 String) As 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 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
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 = False) As 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 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
<Extension> Public Function NormalizeXMLString(str As String) As String
|
577 |
Dim sBuilder As StringBuilder = New StringBuilder(str)
|
578 |
|
579 |
Call sBuilder.Replace("&", "&")
|
580 |
Call sBuilder.Replace("""", """)
|
581 |
Call sBuilder.Replace("×", "×")
|
582 |
Call sBuilder.Replace("÷", "÷")
|
583 |
Call sBuilder.Replace("<", "<")
|
584 |
Call sBuilder.Replace(">", ">")
|
585 |
|
586 |
Return sBuilder.ToString
|
587 |
End Function
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
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 Date) As 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 Date) As 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 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
628 |
<Extension> Public Function Split(Of T)(source As IEnumerable(Of T), parTokens As Integer, Optional echo As Boolean = True) As T()()
|
629 |
Return source.SplitIterator(parTokens, echo).ToArray
|
630 |
End Function
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
<Extension>
|
641 |
Public Iterator Function SplitIterator(Of T)(source As IEnumerable(Of T), parTokens As Integer, Optional echo As Boolean = True) As 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 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
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 Nothing, New 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 |
|
691 |
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
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 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
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 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
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 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
<ExportAPI("File.Select",
|
748 |
Info:="Open the file open dialog to gets the file")>
|
749 |
Public Function SelectFile(Optional ext$ = "*.*", Optional title$ = Nothing) As 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 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
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 |
|
781 |
|
782 |
|
783 |
|
784 |
|
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 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
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 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
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 |
|
825 |
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
<ExportAPI("OffSet")>
|
831 |
<Extension> Public Function Offset(ByRef array As Integer(), intOffset As Integer) As 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 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
<ExportAPI("OffSet")>
|
845 |
<Extension> Public Function Offset(ByRef array As Long(), intOffset As Integer) As 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 |
|
853 |
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
<ExportAPI("Date.Parse")>
|
860 |
<Extension> Public Function ParseDateTime(s As String) As 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 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
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 |
|
898 |
|
899 |
|
900 |
|
901 |
|
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 |
|
909 |
|
910 |
|
911 |
|
912 |
|
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 |
|
920 |
|
921 |
|
922 |
|
923 |
|
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 |
|
932 |
|
933 |
|
934 |
|
935 |
|
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 |
|
945 |
|
946 |
|
947 |
|
948 |
|
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 |
|
959 |
|
960 |
|
961 |
|
962 |
|
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 |
|
974 |
|
975 |
|
976 |
|
977 |
|
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 |
|
990 |
|
991 |
|
992 |
|
993 |
|
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 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
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 |
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
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 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
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 |
|
1068 |
|
1069 |
|
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
<Extension> Public Function ModifyValue(Of T As Class)([property] As PropertyInfo, obj As T, valueModifier As Func(Of Object, Object)) 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 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
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 |
|
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
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 |
|
1135 |
|
1136 |
|
1137 |
|
1138 |
|
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 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
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 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
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 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 |
|
1181 |
|
1182 |
|
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 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
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 |
|
1211 |
|
1212 |
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
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 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
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 |
|
1242 |
|
1243 |
|
1244 |
|
1245 |
|
1246 |
|
1247 |
|
1248 |
#If FRAMEWORD_CORE Then
|
1249 |
<ExportAPI("Mv.Split")>
|
1250 |
Public Function SplitMV(DIR As String, <Parameter("DIR.MoveTo")> moveTo As String, Split As Integer) As Integer
|
1251 |
#Else
|
1252 |
Public Function SplitMV(dir As String, moveto As String, split As Integer) As 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 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
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 Double) As Boolean
|
1284 |
#Else
|
1285 |
<Extension> Public Function Is_NA_UHandle(n As Double) As 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 Single) As 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 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
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 = True, Optional 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 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
#If FRAMEWORD_CORE Then
|
1336 |
<ExportAPI("Get.Item")>
|
1337 |
<Extension> Public Function GetItem(Of T)(source As IEnumerable(Of T), index As Integer) As T
|
1338 |
#Else
|
1339 |
<Extension> Public Function GetItem(Of T)(source As IEnumerable(Of T), index As Integer) As 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 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
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 |
|
1364 |
|
1365 |
|
1366 |
Public Const Scan0 As Integer = 0
|
1367 |
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
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 |
|
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 |
|
1403 |
|
1404 |
|
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 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 |
|
1422 |
|
1423 |
|
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 |
|
1429 |
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
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 |
|
1445 |
|
1446 |
|
1447 |
|
1448 |
|
1449 |
|
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 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
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 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
<ExportAPI("Sequence.Random")>
|
1491 |
<Extension> Public Function SeqRandom(n As Integer) As Integer()
|
1492 |
Dim source As Integer() = n.Sequence.ToArray
|
1493 |
Dim Random As Integer() = source.Shuffles
|
1494 |
Return Random
|
1495 |
End Function
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
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 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
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 |
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
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 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
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 |
|
1589 |
|
1590 |
|
1591 |
|
1592 |
|
1593 |
|
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 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
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 |
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
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 |
|
1647 |
|
1648 |
|
1649 |
|
1650 |
|
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 Integer) As 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 |
|
1700 |
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
<Extension> Public Function ToDictionary(Of TKey, TValue)(
|
1709 |
source As IEnumerable(Of KeyValuePair(Of TKey, TValue)),
|
1710 |
Optional remoteDuplicates As Boolean = False) As 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 |
|
1733 |
'
|
1734 |
|
1735 |
|
1736 |
|
1737 |
|
1738 |
|
1739 |
'
|
1740 |
|
1741 |
'
|
1742 |
'
|
1743 |
'
|
1744 |
'
|
1745 |
'
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 |
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 |
|
1784 |
|
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 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
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 |
|
1823 |
|
1824 |
|
1825 |
|
1826 |
|
1827 |
|
1828 |
|
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 |
|
1837 |
|
1838 |
|
1839 |
|
1840 |
|
1841 |
|
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 |
|
1866 |
|
1867 |
|
1868 |
|
1869 |
|
1870 |
|
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 |
|
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 |
|
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 |
|
1921 |
|
1922 |
|
1923 |
|
1924 |
|
1925 |
|
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 |
|
1945 |
|
1946 |
|
1947 |
|
1948 |
|
1949 |
|
1950 |
|
1951 |
<ExportAPI("Shell")>
|
1952 |
<Extension> Public Function Shell(CLI As String) As IIORedirectAbstract
|
1953 |
Return CType(CLI, IORedirect)
|
1954 |
End Function
|
1955 |
#End If
|
1956 |
|
1957 |
|
1958 |
|
1959 |
|
1960 |
|
1961 |
|
1962 |
|
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 |
|
1988 |
|
1989 |
|
1990 |
|
1991 |
|
1992 |
|
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 |
|
2005 |
|
2006 |
|
2007 |
Friend Const null = Nothing
|
2008 |
Public Const void As Object = Nothing
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
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 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
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 |
|
2036 |
|
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 |
|
2048 |
|
2049 |
|
2050 |
|
2051 |
|
2052 |
|
2053 |
|
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
|