1 | #Region "Microsoft.VisualBasic::ba2a54e6da558b22d1f9a4bbc54ac7a8, Microsoft.VisualBasic.Core\Extensions\Collection\Linq\VectorAssertor.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 | ' Structure VectorAssertor |
35 | ' |
36 | ' Function: ToString |
37 | ' Operators: <>, =, (+2 Overloads) Like |
38 | ' |
39 | ' |
40 | ' /********************************************************************************/ |
41 | |
42 | #End Region |
43 | |
44 | Namespace Linq |
45 | |
46 | Public Structure VectorAssertor(Of T) |
47 | |
48 | Dim ALL As T |
49 | Dim equal As Func(Of T, T, Boolean) |
50 | Dim likes# |
51 | |
52 | Public Overrides Function ToString() As String |
53 | Return ALL.ToString |
54 | End Function |
55 | |
56 | Public Shared Operator Like(list As IEnumerable(Of T), assert As VectorAssertor(Of T)) As Boolean |
57 | Dim array As T() = list.ToArray |
58 | Dim n# |
59 | |
60 | With assert |
61 | If .equal Is Nothing Then |
62 | n = array.Where(Function(x) x.Equals(.ALL)).Count |
63 | Else |
64 | n = array.Where(Function(x) .equal(x, .ALL)).Count |
65 | End If |
66 | |
67 | If .likes = 0R Then |
68 | Return (n / array.Length) >= 0.65 |
69 | Else |
70 | Return (n / array.Length) >= .likes |
71 | End If |
72 | End With |
73 | End Operator |
74 | |
75 | ''' <summary> |
76 | ''' ALL elements in target <paramref name="list"/> equals to <paramref name="assert"/> value |
77 | ''' </summary> |
78 | ''' <param name="list"></param> |
79 | ''' <param name="assert"></param> |
80 | ''' <returns></returns> |
81 | Public Shared Operator =(list As IEnumerable(Of T), assert As VectorAssertor(Of T)) As Boolean |
82 | With assert |
83 | If .equal Is Nothing Then |
84 | Return list.All(Function(x) x.Equals(.ALL)) |
85 | Else |
86 | Return list.All(Function(x) .equal(x, .ALL)) |
87 | End If |
88 | End With |
89 | End Operator |
90 | |
91 | Public Shared Operator <>(list As IEnumerable(Of T), assert As VectorAssertor(Of T)) As Boolean |
92 | Return Not list = assert |
93 | End Operator |
94 | End Structure |
95 | End Namespace |