1 #Region "Microsoft.VisualBasic::de99f877192f15b393c35879ea83067e, Microsoft.VisualBasic.Core\Language\Linq\Linq.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 LinqAPI
35     
36     '         Constructor: (+1 OverloadsSub New
37     '         Function: (+3 Overloads) BuildHash, DefaultFirst, (+2 Overloads) Exec, IsEquals, LQuery
38     '                   MakeList, Takes
39     
40     '         Structure CountHelper
41     
42     '             Operators: <=, <>, =, >=
43     
44     '         Structure LQueryHelper
45     
46     '             Operators: (+2 Overloads) <=, (+2 Overloads) >=
47     
48     '         Structure TakeHelper
49     
50     '             Properties: n
51     
52     '             Constructor: (+1 OverloadsSub New
53     '             FunctionToString
54     '             Operators: <=, >=
55     
56     '         Structure BuildHashHelper
57     
58     '             Constructor: (+1 OverloadsSub New
59     '             Operators: <=, >=
60     
61     '         Structure ListHelper
62     
63     '             Operators: (+3 Overloads) <=, (+3 Overloads) >=
64     
65     '         Structure ExecHelper
66     
67     '             Operators: (+2 Overloads) <=, (+2 Overloads) >=
68     
69     '         Structure FirstOrDefaultHelper
70     
71     '             Constructor: (+1 OverloadsSub New
72     '             Operators: <=, >=
73     
74     '         Structure ToArrayHelper
75     
76     '             Constructor: (+1 OverloadsSub New
77     '             Operators: (+2 Overloads) <=, (+2 Overloads) >=
78     
79     
80     ' /********************************************************************************/
81
82 #End Region
83
84 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
85 Imports Microsoft.VisualBasic.Language.LinqAPIHelpers
86 Imports Microsoft.VisualBasic.Linq
87 Imports Microsoft.VisualBasic.Parallel.Linq
88 Imports Microsoft.VisualBasic.Serialization
89 Imports Microsoft.VisualBasic.Serialization.JSON
90
91 Namespace Language
92
93     ''' <summary>
94     ''' Language syntax extension for the Linq expression in VisualBasic language
95     ''' </summary>
96     Public NotInheritable Class LinqAPI
97
98         ''' <summary>
99         ''' 2016-10-21
100         ''' 在这里被设计成Class而不是Module是为了防止和Linq拓展之中的函数产生冲突
101         ''' </summary>
102         Private Sub New()
103         End Sub
104
105         ''' <summary>
106         ''' Initializes a new instance of the <see cref="List"/>`1 class that
107         ''' contains elements copied from the specified collection and has sufficient capacity
108         ''' to accommodate the number of elements copied.
109         ''' </summary>
110         Public Shared Function MakeList(Of T)() As ListHelper(Of T)
111             Return New ListHelper(Of T)
112         End Function
113
114         ''' <summary>
115         ''' Execute a linq expression. Creates an array from a <see cref="IEnumerable(Of T)"/>.
116         ''' </summary>
117         ''' <typeparam name="T"></typeparam>
118         ''' <returns>An array that contains the elements from the input sequence.</returns>
119         Public Shared Function Exec(Of T)() As ExecHelper(Of T)
120             Return New ExecHelper(Of T)
121         End Function
122
123         Public Shared Function DefaultFirst(Of T)(Optional [default] As T = NothingAs FirstOrDefaultHelper(Of T)
124             Return New FirstOrDefaultHelper(Of T)([default])
125         End Function
126
127         ''' <summary>
128         ''' 
129         ''' </summary>
130         ''' <typeparam name="T">Is the type of linq source</typeparam>
131         ''' <typeparam name="V">Is the type of value output</typeparam>
132         ''' <param name="source"></param>
133         ''' <returns></returns>
134         Public Shared Function Exec(Of T, V)(source As IEnumerable(Of T)) As ToArrayHelper(Of T, V)
135             Return New ToArrayHelper(Of T, V)(source)
136         End Function
137
138         Public Shared Function BuildHash(Of T, V, [In])(keys As Func(Of [In], T), values As Func(Of [In], V)) As BuildHashHelper(Of T, V, [In])
139             Return New BuildHashHelper(Of T, V, [In])(keys, values)
140         End Function
141
142         Public Shared Function BuildHash(Of T, [In])(keys As Func(Of [In], T)) As BuildHashHelper(Of T, [In], [In])
143             Return New BuildHashHelper(Of T, [In], [In])(keys, Function(x) x)
144         End Function
145
146         Public Shared Function BuildHash(Of T As INamedValue)() As BuildHashHelper(Of String, T, T)
147             Return New BuildHashHelper(Of String, T, T)(Function(x) x.Key, Function(x) x)
148         End Function
149
150         Public Shared Function Takes(Of T)(n As IntegerAs TakeHelper(Of T)
151             Return New TakeHelper(Of T)(n)
152         End Function
153
154         Public Shared Function LQuery(Of T, out)(task As Func(Of T, out), Optional partTokens As Integer = 20000) As LQueryHelper(Of T, out)
155             Return New LQueryHelper(Of T, out) With {
156                 .task = task,
157                 .partTokens = partTokens
158             }
159         End Function
160
161         Public Shared Function IsEquals(Of T)(Optional c% = 0) As CountHelper(Of T)
162             Return New CountHelper(Of T) With {
163                 .count = c
164             }
165         End Function
166     End Class
167
168     Namespace LinqAPIHelpers
169
170         Public Structure CountHelper(Of T)
171
172             Public count%
173
174             ''' <summary>
175             ''' 判断序列计数是否相等
176             ''' </summary>
177             ''' <param name="h"></param>
178             ''' <param name="source"></param>
179             ''' <returns></returns>
180             Public Shared Operator <=(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
181                 Return source.Count = h.count
182             End Operator
183
184             Public Shared Operator >=(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
185                 Throw New NotSupportedException
186             End Operator
187
188             Public Shared Operator =(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
189                 Return h.count = source.Count
190             End Operator
191
192             Public Shared Operator <>(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
193                 Return Not h = source
194             End Operator
195         End Structure
196
197         Public Structure LQueryHelper(Of T, out)
198
199             Dim task As Func(Of T, out)
200             Dim partTokens As Integer
201
202             Public Overloads Shared Operator <=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of T)) As out()
203                 Return LQuerySchedule.LQuery(source, helper.task, helper.partTokens).ToArray
204             End Operator
205
206             Public Overloads Shared Operator >=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of T)) As out()
207                 Throw New NotSupportedException
208             End Operator
209
210             Public Overloads Shared Operator <=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of IEnumerable(Of T))) As out()
211                 Return helper <= source.IteratesALL
212             End Operator
213
214             Public Overloads Shared Operator >=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of IEnumerable(Of T))) As out()
215                 Throw New NotSupportedException
216             End Operator
217         End Structure
218
219         Public Structure TakeHelper(Of T)
220
221             Public ReadOnly Property n As Integer
222
223             Sub New(n As Integer)
224                 Me.n = n
225             End Sub
226
227             Public Overrides Function ToString() As String
228                 Return Me.GetJson
229             End Function
230
231             Public Overloads Shared Operator <=(num As TakeHelper(Of T), source As IEnumerable(Of T)) As IEnumerable(Of T)
232                 Return source.Take(num.n)
233             End Operator
234
235             Public Overloads Shared Operator >=(num As TakeHelper(Of T), source As IEnumerable(Of T)) As IEnumerable(Of T)
236                 Throw New NotSupportedException
237             End Operator
238         End Structure
239
240         ' Summary:
241         '     Creates a System.Collections.Generic.Dictionary`2 from an System.Collections.Generic.IEnumerable`1
242         '     according to specified key selector and element selector functions.
243         '         ' Parameters:
244         '   source:
245         '     An System.Collections.Generic.IEnumerable`1 to create a System.Collections.Generic.Dictionary`2
246         '     from.
247         '         '   keySelector:
248         '     A function to extract a key from each element.
249         '         '   elementSelector:
250         '     A transform function to produce a result element value from each element.
251         '         ' Type parameters:
252         '   TSource:
253         '     The type of the elements of source.
254         '         '   TKey:
255         '     The type of the key returned by keySelector.
256         '         '   TElement:
257         '     The type of the value returned by elementSelector.
258         '         ' Returns:
259         '     A System.Collections.Generic.Dictionary`2 that contains values of type TElement
260         '     selected from the input sequence.
261         '         ' Exceptions:
262         '   T:System.ArgumentNullException:
263         '     source or keySelector or elementSelector is null.-or-keySelector produces a key
264         '     that is null.
265         '         '   T:System.ArgumentException:
266         '     keySelector produces duplicate keys for two elements.
267         Public Structure BuildHashHelper(Of T, V, [In])
268
269             ReadOnly __keys As Func(Of [In], T)
270             ReadOnly __values As Func(Of [In], V)
271
272             Sub New(k As Func(Of [In], T), v As Func(Of [In], V))
273                 __keys = k
274                 __values = v
275             End Sub
276
277             Public Shared Operator <=(cls As BuildHashHelper(Of T, V, [In]), linq As IEnumerable(Of [In])) As Dictionary(Of T, V)
278                 Return linq.ToDictionary(cls.__keys, cls.__values)
279             End Operator
280
281             Public Shared Operator >=(cls As BuildHashHelper(Of T, V, [In]), linq As IEnumerable(Of [In])) As Dictionary(Of T, V)
282                 Throw New NotSupportedException
283             End Operator
284         End Structure
285
286         Public Structure ListHelper(Of T)
287
288             ''' <summary>
289             ''' Initializes a new instance of the <see cref="List"/>`1 class that
290             ''' contains elements copied from the specified collection and has sufficient capacity
291             ''' to accommodate the number of elements copied.
292             ''' </summary>
293             ''' <param name="linq">The collection whose elements are copied to the new list.</param>
294             ''' <returns></returns>
295             Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of T)) As List(Of T)
296                 Return linq.AsList
297             End Operator
298
299             Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of T)) As List(Of T)
300                 Throw New NotSupportedException
301             End Operator
302
303             ''' <summary>
304             ''' Initializes a new instance of the <see cref="List"/>`1 class that
305             ''' contains elements copied from the specified collection and has sufficient capacity
306             ''' to accommodate the number of elements copied.
307             ''' </summary>
308             ''' <param name="linq">The collection whose elements are copied to the new list.</param>
309             ''' <returns></returns>
310             Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As List(Of T)
311                 Return linq.Unlist
312             End Operator
313
314             Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As List(Of T)
315                 Throw New NotSupportedException
316             End Operator
317
318             ''' <summary>
319             ''' Initializes a new instance of the <see cref="List"/>`1 class that
320             ''' contains elements copied from the specified collection and has sufficient capacity
321             ''' to accommodate the number of elements copied.
322             ''' </summary>
323             ''' <param name="linq">The collection whose elements are copied to the new list.</param>
324             ''' <returns></returns>
325             Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of IEnumerable(Of T)))) As List(Of T)
326                 Return linq.IteratesALL.Unlist
327             End Operator
328
329             Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of IEnumerable(Of T)))) As List(Of T)
330                 Throw New NotSupportedException
331             End Operator
332         End Structure
333
334         ''' <summary>
335         ''' Execute a linq expression. Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
336         ''' </summary>
337         ''' <typeparam name="T"></typeparam>
338         Public Structure ExecHelper(Of T)
339
340             ''' <summary>
341             ''' Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
342             ''' </summary>
343             ''' <param name="cls"></param>
344             ''' <param name="linq">
345             ''' An <see cref="System.Collections.Generic.IEnumerable(Of T)"/> to create an array from.
346             ''' </param>
347             ''' <returns>An array that contains the elements from the input sequence.</returns>
348             Public Shared Operator <=(cls As ExecHelper(Of T), linq As IEnumerable(Of T)) As T()
349                 Return linq.ToArray
350             End Operator
351
352             Public Shared Operator >=(cls As ExecHelper(Of T), linq As IEnumerable(Of T)) As T()
353                 Throw New NotSupportedException
354             End Operator
355
356             ''' <summary>
357             ''' Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
358             ''' </summary>
359             ''' <param name="cls"></param>
360             ''' <param name="linq">
361             ''' An <see cref="System.Collections.Generic.IEnumerable(Of T)"/> to create an array from.
362             ''' </param>
363             ''' <returns>An array that contains the elements from the input sequence.</returns>
364             Public Shared Operator <=(cls As ExecHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As T()
365                 Return linq.ToVector
366             End Operator
367
368             Public Shared Operator >=(cls As ExecHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As T()
369                 Throw New NotSupportedException
370             End Operator
371         End Structure
372
373         Public Structure FirstOrDefaultHelper(Of T)
374
375             ReadOnly __default As T
376
377             Sub New(__default As T)
378                 Me.__default = __default
379             End Sub
380
381             ''' <summary>
382             ''' Exec <see cref="IEnumerable(Of T)"/>.DefaultFirst extension method.
383             ''' </summary>
384             ''' <param name="cls"></param>
385             ''' <param name="linq"></param>
386             ''' <returns></returns>
387             Public Shared Operator <=(cls As FirstOrDefaultHelper(Of T), linq As IEnumerable(Of T)) As T
388                 Return linq.DefaultFirst(cls.__default)
389             End Operator
390
391             Public Shared Operator >=(cls As FirstOrDefaultHelper(Of T), linq As IEnumerable(Of T)) As T
392                 Throw New NotSupportedException
393             End Operator
394         End Structure
395
396         ''' <summary>
397         ''' Execute a linq expression. Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
398         ''' </summary>
399         ''' <typeparam name="T"></typeparam>
400         Public Structure ToArrayHelper(Of T, V)
401
402             Private __source As IEnumerable(Of T)
403
404             Sub New(source As IEnumerable(Of T))
405                 __source = source
406             End Sub
407
408             ''' <summary>
409             ''' Exec ToArray
410             ''' </summary>
411             ''' <param name="cls"></param>
412             ''' <param name="linq"></param>
413             ''' <returns></returns>
414             Public Shared Operator <=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, V)) As V()
415                 Return (From x As T In cls.__source Select linq(x)).ToArray
416             End Operator
417
418             Public Shared Operator >=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, V)) As V()
419                 Throw New NotSupportedException
420             End Operator
421
422             ''' <summary>
423             ''' Exec ToArray
424             ''' </summary>
425             ''' <param name="cls"></param>
426             ''' <param name="linq"></param>
427             ''' <returns></returns>
428             Public Shared Operator <=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, Integer, V)) As V()
429                 Return (From x As SeqValue(Of T)
430                     In cls.__source.SeqIterator
431                         Select linq(x.value, x.i)).ToArray
432             End Operator
433
434             Public Shared Operator >=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, Integer, V)) As V()
435                 Throw New NotSupportedException
436             End Operator
437         End Structure
438     End Namespace
439 End Namespace