| 1 | #Region "Microsoft.VisualBasic::6baa2251bdfa8c363996c5d46ad5036b, Microsoft.VisualBasic.Core\Scripting\Expressions\ArrayIndex.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 ArrayIndex |
| 35 | ' |
| 36 | ' Function: AsVector, TranslateIndex |
| 37 | ' |
| 38 | ' |
| 39 | ' /********************************************************************************/ |
| 40 | |
| 41 | #End Region |
| 42 | |
| 43 | Imports System.Runtime.CompilerServices |
| 44 | Imports Microsoft.VisualBasic.Language |
| 45 | |
| 46 | Namespace Scripting.Expressions |
| 47 | |
| 48 | Public Module ArrayIndex |
| 49 | |
| 50 | ''' <summary> |
| 51 | ''' 表达式字符串只允许:``1, 2, 3, 4, 5`` |
| 52 | ''' </summary> |
| 53 | ''' <param name="exp$"></param> |
| 54 | ''' <returns></returns> |
| 55 | <Extension> Public Function AsVector(exp$) As Double() |
| 56 | Return exp.Split(","c) _ |
| 57 | .Select(AddressOf Trim) _ |
| 58 | .Select(Function(s) Val(s)) _ |
| 59 | .ToArray |
| 60 | End Function |
| 61 | |
| 62 | ''' <summary> |
| 63 | ''' Translate the vector expression into a intger index vector |
| 64 | ''' </summary> |
| 65 | ''' <param name="exp$"> |
| 66 | ''' + ``1``, index=1 |
| 67 | ''' + ``1:8``, index=1, count=8 |
| 68 | ''' + ``1->8``, index from 1 to 8 |
| 69 | ''' + ``8->1``, index from 8 to 1 |
| 70 | ''' + ``1,2,3,4``, index=1 or 2 or 3 or 4 |
| 71 | ''' </param> |
| 72 | ''' <returns></returns> |
| 73 | <Extension> Public Function TranslateIndex(exp$) As Integer() |
| 74 | If exp.IsPattern("-?\d+") Then |
| 75 | Return { |
| 76 | CInt(exp) |
| 77 | } |
| 78 | ElseIf exp.IndexOf(":"c) > -1 Then |
| 79 | Dim t = exp.Split(":"c) |
| 80 | Dim from = CInt(t(Scan0)) |
| 81 | Dim count = CInt(t(1)) |
| 82 | Dim indcies%() = New Integer(count - 1) {} |
| 83 | |
| 84 | For i As Integer = 0 To count - 1 |
| 85 | indcies(i) = i + from |
| 86 | Next |
| 87 | |
| 88 | Return indcies |
| 89 | ElseIf exp.IndexOf(","c) > -1 Then |
| 90 | Dim t As Integer() = exp _ |
| 91 | .Split(","c) _ |
| 92 | .Select(Function(i) CInt(Val(i.Trim))) _ |
| 93 | .ToArray |
| 94 | Return t |
| 95 | ElseIf InStr(exp, "->") > 0 Then |
| 96 | Dim t As Integer() = Strings.Split(exp, "->") _ |
| 97 | .Select(Function(s) CInt(s.Trim)) _ |
| 98 | .ToArray |
| 99 | Dim from = t(Scan0) |
| 100 | Dim to% = t(1) |
| 101 | Dim delta = If(from < [to], 1, -1) |
| 102 | Dim out As New List(Of Integer) |
| 103 | |
| 104 | For i As Integer = from To [to] Step delta |
| 105 | out += i |
| 106 | Next |
| 107 | |
| 108 | Return out |
| 109 | Else |
| 110 | Throw New SyntaxErrorException($"'{exp}' expression syntax error!") |
| 111 | End If |
| 112 | End Function |
| 113 | End Module |
| 114 | End Namespace |