1 #Region "Microsoft.VisualBasic::0c122d047d50bdca55d274a682e7e847, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\Set\SetsExtension.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 SetsExtension
35     
36     '     Function: (+2 OverloadsAsSet, Except, Intersection, (+3 OverloadsToArray, Union
37     
38     ' /********************************************************************************/
39
40 #End Region
41
42 Imports System.Runtime.CompilerServices
43 Imports Microsoft.VisualBasic.CommandLine.Reflection
44 Imports Microsoft.VisualBasic.ComponentModel.DataStructures
45
46 Public Module SetsExtension
47
48     <MethodImpl(MethodImplOptions.AggressiveInlining)>
49     <Extension> Public Function ToArray(Of T)(s As [Set], [ctype] As Func(Of Object, T)) As T()
50         Return s.ToArray _
51                 .Select([ctype]) _
52                 .ToArray
53     End Function
54
55     ''' <summary>
56     ''' DirectCast
57     ''' </summary>
58     ''' <typeparam name="T"></typeparam>
59     ''' <returns></returns>
60     ''' 
61     <MethodImpl(MethodImplOptions.AggressiveInlining)>
62     <Extension> Public Function ToArray(Of T)(s As [Set]) As T()
63         Return s.ToArray _
64                 .Select(Function(x) DirectCast(x, T)) _
65                 .ToArray
66     End Function
67
68 #Region "API"
69
70     ''' <summary>
71     ''' 差集,函数会从<paramref name="s1"/>集合之中删除<paramref name="s2"/>中共同的元素
72     ''' </summary>
73     ''' <param name="s1"></param>
74     ''' <param name="s2"></param>
75     ''' <returns></returns>
76     <MethodImpl(MethodImplOptions.AggressiveInlining)>
77     <ExportAPI("Except")>
78     Public Function Except(s1 As [Set], s2 As [Set]) As [Set]
79         Return s1 - s2
80     End Function
81
82     ''' <summary>
83     ''' 就并集
84     ''' </summary>
85     ''' <param name="s1"></param>
86     ''' <param name="s2"></param>
87     ''' <returns></returns>
88     <MethodImpl(MethodImplOptions.AggressiveInlining)>
89     <ExportAPI("Union"Info:="Performs a union of two sets.")>
90     Public Function Union(s1 As [Set], s2 As [Set]) As [Set]
91         Return s1 Or s2
92     End Function
93
94     ''' <summary>
95     ''' 求交集
96     ''' </summary>
97     ''' <param name="s1"></param>
98     ''' <param name="s2"></param>
99     ''' <returns></returns>
100     <MethodImpl(MethodImplOptions.AggressiveInlining)>
101     <ExportAPI("Intersection"Info:="Performs an intersection of two sets.")>
102     Public Function Intersection(s1 As [Set], s2 As [Set]) As [Set]
103         Return s1 And s2
104     End Function
105
106     ''' <summary>
107     ''' 将任意序列转换为集合类型
108     ''' </summary>
109     ''' <param name="source"></param>
110     ''' <returns></returns>
111     <MethodImpl(MethodImplOptions.AggressiveInlining)>
112     <ExportAPI("As.Set")>
113     <Extension>
114     Public Function AsSet(source As IEnumerable) As [Set]
115         Return New [Set](source)
116     End Function
117
118     <ExportAPI("As.Array")>
119     Public Function ToArray([set] As [Set]) As Object()
120         Return [set].ToArray
121     End Function
122
123     ''' <summary>
124     ''' Create a string set
125     ''' </summary>
126     ''' <param name="strings"></param>
127     ''' <returns></returns>
128     <MethodImpl(MethodImplOptions.AggressiveInlining)>
129     <Extension>
130     Public Function AsSet(strings As IEnumerable(Of String)) As StringSet
131         Return New StringSet(strings)
132     End Function
133 #End Region
134 End Module