1 | #Region "Microsoft.VisualBasic::81224ebc33c10a8bccb9a424a5517a6a, Microsoft.VisualBasic.Core\Language\Language\Java\Arrays.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 Arrays |
35 | ' |
36 | ' Function: copyOfRange, Max, Min, Shuffle, SubList |
37 | ' |
38 | ' Sub: Fill |
39 | ' |
40 | ' |
41 | ' /********************************************************************************/ |
42 | |
43 | #End Region |
44 | |
45 | Imports System.Runtime.CompilerServices |
46 | Imports Microsoft.VisualBasic.Math |
47 | |
48 | Namespace Language.Java |
49 | |
50 | Public Module Arrays |
51 | |
52 | <Extension> |
53 | Public Sub Fill(Of T)(ByRef a As T(), val As T) |
54 | For i% = 0 To a.Length - 1 |
55 | a(i%) = val |
56 | Next |
57 | End Sub |
58 | |
59 | Public Function copyOfRange(Of T)(matrix As T(), start As Integer, length As Integer) As T() |
60 | Dim out As T() = New T(length - 1) {} |
61 | Call Array.Copy(matrix, start, out, Scan0, length) |
62 | Return out |
63 | End Function |
64 | |
65 | <Extension> |
66 | Public Function Shuffle(Of T)(ByRef list As List(Of T)) As List(Of T) |
67 | Dim rnd As New Random |
68 | Call rnd.Shuffle(list) |
69 | Return list |
70 | End Function |
71 | |
72 | ''' <summary> |
73 | ''' Returns a view of the portion of this list between the specified fromIndex, inclusive, |
74 | ''' and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) |
75 | ''' The returned list is backed by this list, so non-structural changes in the returned |
76 | ''' list are reflected in this list, and vice-versa. The returned list supports all of the |
77 | ''' optional list operations supported by this list. |
78 | ''' </summary> |
79 | ''' <typeparam name="T"></typeparam> |
80 | ''' <param name="list"></param> |
81 | ''' <param name="fromIndex%">low endpoint (inclusive) of the subList</param> |
82 | ''' <param name="toIndex%">high endpoint (exclusive) of the subList</param> |
83 | ''' <returns>a view of the specified range within this list</returns> |
84 | ''' |
85 | <Extension> |
86 | Public Function SubList(Of T)(list As List(Of T), fromIndex%, toIndex%) As List(Of T) |
87 | Return list.Skip(fromIndex).Take(toIndex - fromIndex).AsList |
88 | End Function |
89 | |
90 | <Extension> |
91 | Public Function Min(Of T As IComparable(Of T))(source As IEnumerable(Of T)) As T |
92 | Return Enumerable.Min(source) |
93 | End Function |
94 | |
95 | <Extension> |
96 | Public Function Max(Of T As IComparable(Of T))(source As IEnumerable(Of T)) As T |
97 | Return Enumerable.Max(source) |
98 | End Function |
99 | End Module |
100 | End Namespace |