1 #Region "Microsoft.VisualBasic::0df6611a225f55684a906c686989ea18, Microsoft.VisualBasic.Core\Language\Linq\Vectorization\BooleanVector.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     '     Class BooleanVector
35     
36     '         Properties: [False], [True], IsLogical
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: Sum, ToString
40     '         Operators: (+2 OverloadsIsFalse, (+2 OverloadsIsTrue, (+2 OverloadsNot, (+4 OverloadsOr
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 Imports System.Runtime.CompilerServices
48 Imports Microsoft.VisualBasic.Linq
49
50 Namespace Language.Vectorization
51
52     ''' <summary>
53     ''' <see cref="System.Boolean"/> Array
54     ''' </summary>
55     Public Class BooleanVector : Inherits Vector(Of Boolean)
56
57         ''' <summary>
58         ''' Only one boolean value ``True`` in the array list
59         ''' </summary>
60         ''' <returns></returns>
61         Public Shared ReadOnly Property [True] As BooleanVector
62             <MethodImpl(MethodImplOptions.AggressiveInlining)>
63             Get
64                 Return New BooleanVector({True})
65             End Get
66         End Property
67
68         ''' <summary>
69         ''' Only one boolean value ``False`` in the array list
70         ''' </summary>
71         ''' <returns></returns>
72         Public Shared ReadOnly Property [False] As BooleanVector
73             <MethodImpl(MethodImplOptions.AggressiveInlining)>
74             Get
75                 Return New BooleanVector({False})
76             End Get
77         End Property
78
79         Public ReadOnly Property IsLogical As Boolean
80             <MethodImpl(MethodImplOptions.AggressiveInlining)>
81             Get
82                 Return buffer.Length = 1
83             End Get
84         End Property
85
86         <MethodImpl(MethodImplOptions.AggressiveInlining)>
87         Sub New(b As IEnumerable(Of Boolean))
88             MyBase.New(b)
89         End Sub
90
91         Public Overrides Function ToString() As String
92             Dim countTrue% = Linq.Which.IsTrue(buffer).Count
93             Dim countFalse% = Linq.Which.IsTrue(Not Me).Count
94
95             Return $"ALL({Length}) = {countTrue} true + {countFalse} false"
96         End Function
97
98         <MethodImpl(MethodImplOptions.AggressiveInlining)>
99         Public Shared Function Sum(b As BooleanVector) As Integer
100             Return b.Select(Function(x) If(x, 1, 0)).Sum
101         End Function
102
103         ''' <summary>
104         ''' And
105         ''' </summary>
106         ''' <param name="x"></param>
107         ''' <param name="y"></param>
108         ''' <returns></returns>
109         ''' 
110         <MethodImpl(MethodImplOptions.AggressiveInlining)>
111         Public Overloads Shared Operator &(x As Boolean, y As BooleanVector) As BooleanVector
112             Return New BooleanVector(From b As Boolean In y Select b AndAlso x)
113         End Operator
114
115         ''' <summary>
116         ''' AndAlso Y
117         ''' </summary>
118         ''' <param name="x"></param>
119         ''' <param name="y"></param>
120         ''' <returns></returns>
121         ''' 
122         <MethodImpl(MethodImplOptions.AggressiveInlining)>
123         Public Overloads Shared Operator &(x As BooleanVector, y As BooleanVector) As BooleanVector
124             Return New BooleanVector(From i As SeqValue(Of BooleanIn x.SeqIterator Select i.value AndAlso y(i))
125         End Operator
126
127         ''' <summary>
128         ''' 将逻辑向量之中的每一个逻辑值都进行翻转
129         ''' </summary>
130         ''' <param name="x"></param>
131         ''' <returns></returns>
132         ''' 
133         <MethodImpl(MethodImplOptions.AggressiveInlining)>
134         Public Shared Operator Not(x As BooleanVector) As BooleanVector
135             Return New BooleanVector((From b As Boolean In x Select Not b).ToArray)
136         End Operator
137
138         ''' <summary>
139         ''' x(0)
140         ''' </summary>
141         ''' <param name="x"></param>
142         ''' <returns></returns>
143         ''' 
144         <MethodImpl(MethodImplOptions.AggressiveInlining)>
145         Public Overloads Shared Narrowing Operator CType(x As BooleanVector) As Boolean
146             Return x(0)
147         End Operator
148
149         <MethodImpl(MethodImplOptions.AggressiveInlining)>
150         Public Shared Widening Operator CType(b As Boolean()) As BooleanVector
151             Return New BooleanVector(b)
152         End Operator
153
154         ''' <summary>
155         ''' Or Y
156         ''' </summary>
157         ''' <param name="x"></param>
158         ''' <param name="y"></param>
159         ''' <returns></returns>
160         ''' 
161         <MethodImpl(MethodImplOptions.AggressiveInlining)>
162         Public Shared Operator Or(x As BooleanVector, y As Boolean()) As BooleanVector
163             Return New BooleanVector(x.Select(Function(b, i) b OrElse y(i)))
164         End Operator
165
166         ''' <summary>
167         ''' Or Y
168         ''' </summary>
169         ''' <param name="x"></param>
170         ''' <param name="y"></param>
171         ''' <returns></returns>
172         ''' 
173         <MethodImpl(MethodImplOptions.AggressiveInlining)>
174         Public Shared Operator Or(x As BooleanVector, y As BooleanVector) As BooleanVector
175             Return x Or y.ToArray
176         End Operator
177
178         ''' <summary>
179         ''' <see cref="ToArray"/>
180         ''' </summary>
181         ''' <param name="x"></param>
182         ''' <returns></returns>
183         ''' 
184         <MethodImpl(MethodImplOptions.AggressiveInlining)>
185         Public Overloads Shared Narrowing Operator CType(x As BooleanVector) As Boolean()
186             Return x.ToArray
187         End Operator
188
189         Public Shared Operator IsTrue(b As BooleanVector) As Boolean
190             If b.IsNullOrEmpty Then
191                 Return False
192             Else
193                 Return Not b.Any(Function(x) x = False)
194             End If
195         End Operator
196
197         Public Shared Operator IsFalse(b As BooleanVector) As Boolean
198             If b Then
199                 ' b是True,则不是False,在这里返回False,表明IsFalse不成立
200                 Return False
201             Else
202                 Return True
203             End If
204         End Operator
205     End Class
206 End Namespace