| 1 | #Region "Microsoft.VisualBasic::294d3d421637bd5c010bdb8742d4cec3, Microsoft.VisualBasic.Core\Net\Protocol\Streams\VarArray.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 VarArray |
| 35 | ' |
| 36 | ' Constructor: (+2 Overloads) Sub New |
| 37 | ' Function: Serialize |
| 38 | ' |
| 39 | ' |
| 40 | ' /********************************************************************************/ |
| 41 | |
| 42 | #End Region |
| 43 | |
| 44 | Imports Microsoft.VisualBasic.Language |
| 45 | Imports Microsoft.VisualBasic.Linq |
| 46 | Imports Microsoft.VisualBasic.Serialization.BinaryDumping |
| 47 | Imports Buffer = System.Array |
| 48 | |
| 49 | Namespace Net.Protocols.Streams.Array |
| 50 | |
| 51 | ''' <summary> |
| 52 | ''' The bytes length of the element in thee source sequence is not fixed. |
| 53 | ''' (序列里面的元素的长度是不固定的) |
| 54 | ''' </summary> |
| 55 | Public Class VarArray(Of T) : Inherits ArrayAbstract(Of T) |
| 56 | |
| 57 | Sub New(TSerialize As IGetBuffer(Of T), load As IGetObject(Of T)) |
| 58 | Call MyBase.New(TSerialize, load) |
| 59 | End Sub |
| 60 | |
| 61 | Sub New(raw As Byte(), serilize As IGetBuffer(Of T), load As IGetObject(Of T)) |
| 62 | Call Me.New(serilize, load) |
| 63 | |
| 64 | Dim lb As Byte() = New Byte(INT64 - 1) {} |
| 65 | Dim buf As Byte() |
| 66 | Dim i As New Pointer |
| 67 | Dim list As New List(Of T) |
| 68 | Dim l As Long |
| 69 | Dim x As T |
| 70 | |
| 71 | Do While raw.Length > i |
| 72 | |
| 73 | Call Buffer.ConstrainedCopy(raw, i << INT64, lb, Scan0, INT64) |
| 74 | |
| 75 | l = BitConverter.ToInt64(lb, Scan0) |
| 76 | buf = New Byte(l - 1) {} |
| 77 | |
| 78 | Call Buffer.ConstrainedCopy(raw, i << buf.Length, buf, Scan0, buf.Length) |
| 79 | |
| 80 | x = load(buf) |
| 81 | list += x |
| 82 | Loop |
| 83 | |
| 84 | Values = list.ToArray |
| 85 | End Sub |
| 86 | |
| 87 | ''' <summary> |
| 88 | ''' Long + T + Long + T |
| 89 | ''' 其中Long是一个8字节长度的数组,用来指示T的长度 |
| 90 | ''' </summary> |
| 91 | ''' <returns></returns> |
| 92 | Public Overrides Function Serialize() As Byte() |
| 93 | Dim list As New List(Of Byte) |
| 94 | Dim LQuery = From index As SeqValue(Of T) |
| 95 | In Values.SeqIterator.AsParallel |
| 96 | Select buf = New SeqValue(Of Byte()) With { |
| 97 | .i = index.i, |
| 98 | .value = serialization(index.value) |
| 99 | } |
| 100 | Order By buf.i Ascending |
| 101 | |
| 102 | For Each x As SeqValue(Of Byte()) In LQuery |
| 103 | Dim byts As Byte() = x.value |
| 104 | Dim l As Long = byts.Length |
| 105 | Dim lb As Byte() = BitConverter.GetBytes(l) |
| 106 | |
| 107 | Call list.AddRange(lb) |
| 108 | Call list.AddRange(byts) |
| 109 | Next |
| 110 | |
| 111 | Return list.ToArray |
| 112 | End Function |
| 113 | End Class |
| 114 | End Namespace |