1 #Region "Microsoft.VisualBasic::80998bcc073a79241c7a2426fac5e2e7, Microsoft.VisualBasic.Core\Extensions\Collection\Linq\VectorAssertExtensions.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 VectorAssertExtensions
35     
36     '         FunctionInsideAny, LengthEquals, TestPairData
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44 Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
45 Imports Microsoft.VisualBasic.Linq
46
47 Namespace Linq
48
49     Public Module VectorAssertExtensions
50
51         ''' <summary>
52         ''' Determine that is all of the collection <paramref name="array"/> have the same size? 
53         ''' </summary>
54         ''' <typeparam name="T"></typeparam>
55         ''' <param name="n%">collection size Length</param>
56         ''' <param name="any">Is required all of the sequence must be the length equals</param>
57         ''' <param name="array"></param>
58         ''' <returns></returns>
59         Public Function LengthEquals(Of T)(n%, any As BooleanParamArray array As IEnumerable(Of T)()) As Boolean
60             Dim c%() = array.Select(Function(s) s.Count).ToArray
61             Dim equals = c.Where(Function(x) x = n).ToArray
62
63             If any Then
64                 Return equals.Length > 0
65             Else
66                 Return equals.Length = array.Length
67             End If
68         End Function
69
70         ''' <summary>
71         ''' + False: 测试失败,不会满足<see cref="MappingData(Of T)(T(), T())"/>的条件
72         ''' + True: 可以使用<see cref="MappingData(Of T)(T(), T())"/>来生成Mapping匹配
73         ''' </summary>
74         ''' <typeparam name="T"></typeparam>
75         ''' <param name="a"></param>
76         ''' <param name="b"></param>
77         ''' <returns></returns>
78         Public Function TestPairData(Of T)(a As T(), b As T()) As Boolean
79             If a.Length <> b.Length AndAlso Not LengthEquals(1, True, a, b) Then
80                 Return False
81             Else
82                 Return True
83             End If
84         End Function
85
86         ''' <summary>
87         ''' Any of the element in source <paramref name="sites"/> is in a specific <paramref name="range"/>??
88         ''' </summary>
89         ''' <param name="range"></param>
90         ''' <param name="sites"></param>
91         ''' <returns></returns>
92         <Extension>
93         Public Function InsideAny(range As IntRange, sites As IEnumerable(Of Integer)) As Boolean
94             For Each x% In sites
95                 If range.IsInside(x) Then
96                     Return True
97                 End If
98             Next
99
100             Return False
101         End Function
102     End Module
103 End Namespace