【问题标题】:How to order array in lexicographical order with mapped file vb.net如何使用映射文件 vb.net 按字典顺序排列数组
【发布时间】:2019-05-09 01:25:44
【问题描述】:

这对我来说有点复杂

    Dim test() As Byte = New Byte() {50, 40, 30, 10, 10}
    Dim answer() As UInteger = SortLexicoGraphicallyArrayMappedFile(test)

答案是每个 Rotation 从最低数组值到最高数组值排序。

Rotation 0 = 50, 40, 30, 10, 10
Rotation 1 = 10, 50, 40, 30, 10
Rotation 2 = 10, 10, 50, 40, 30
Rotation 3 = 30, 10, 10, 50, 40
Rotation 4 = 40, 30, 10, 10, 50

当我手动对上面的这个数组进行排序时,我应该得到

Rotation 2 = 10, 10, 50, 40, 30
Rotation 1 = 10, 50, 40, 30, 10
Rotation 3 = 30, 10, 10, 50, 40
Rotation 4 = 40, 30, 10, 10, 50
Rotation 0 = 50, 40, 30, 10, 10

所以答案应该是2, 1, 3, 4, 0

我陷入了一个无限循环,我无法将手指放在上面
我的上一个问题有效,因为这里的数据始终是静态的为了以后节省cpu时间,这就是为什么我没有从下面的页面中选择答案。

How to order array in lexicographical order vb.net

这是我的代码

Public Function GetRotation(Data As Byte(), rotation As UInteger) As Byte()
   'Rotation Left
    Dim rotationData As New List(Of Byte)

    Dim start As UInteger = Data.Length - rotation Mod Data.Length

    For i = 0 To Data.Length - 1
        rotationData.Add(Data((start + i) Mod (Data.Length)))
    Next

    Return rotationData.ToArray()
End Function

   Public Function SortLexicoGraphicallyArrayMappedFile(ByRef data As Byte()) As UInteger()
        Dim OrderedRotations As New List(Of UInteger)
        Dim rotatedData As Byte()
        Dim rotation As UInteger = 0


        Dim mmF As MemoryMappedFile
        mmF = MemoryMappedFile.CreateFromFile(My.Application.Info.DirectoryPath & "\outFile", FileMode.CreateNew, "test", CLng(data.LongLength * data.LongLength))
        Dim mmVA As MemoryMappedViewAccessor
        mmVA = mmF.CreateViewAccessor(0, data.LongLength * data.LongLength)

        Dim pos As Long = 0

        For rotation = 0 To data.Length - 1
            rotatedData = GetRotation(data, rotation)
            mmVA.WriteArray(Of Byte)(pos, rotatedData, 0, rotatedData.Length)
            pos += rotatedData.Length

        Next

        For rotation = 0 To data.Length - 1
            OrderedRotations.Add(rotation)
        Next

        Dim eachRotation As Integer = 0
        Dim data1() As Byte
        ReDim data1(data.Length - 1)
        Dim data2() As Byte
        ReDim data2(data.Length - 1)
        Dim index As Long
        For rotation = 0 To data.Length - 1
            Dim flag As Boolean
            Do
                flag = False
                For eachRotation = OrderedRotations.Count - 1 To 0 Step -1
                    mmVA.ReadArray(Of Byte)((OrderedRotations(rotation) * data.Length), data1, 0, data.Length)
                    If OrderedRotations(eachRotation) = OrderedRotations(rotation) Then Continue For
                    mmVA.ReadArray(Of Byte)((OrderedRotations(eachRotation) * data.Length), data2, 0, data.Length)

                    For index = 0 To data.Length - 1
                        If data1(index) > data2(index) Then
                            Exit For
                        ElseIf data1(index) < data2(index) Then
                            mmVA.WriteArray(Of Byte)((OrderedRotations(eachRotation) * data.Length), data1, 0, data1.Length)
                            mmVA.WriteArray(Of Byte)((OrderedRotations(rotation) * data.Length), data2, 0, data2.Length)

                            Dim tmpFirst As UInteger = OrderedRotations(rotation)
                            OrderedRotations(rotation) = OrderedRotations(eachRotation)
                            OrderedRotations(eachRotation) = tmpFirst
                            flag = True
                            Exit For
                        End If
                    Next
                Next
            Loop While flag
        Next

        Return OrderedRotations.ToArray()
    End Function

【问题讨论】:

    标签: vb.net sorting binary-search memory-mapped-files


    【解决方案1】:

    我不知道这是否正确,但我已为您解决了。

    Public Function SortLexicoGraphicallyArrayMappedFile(ByRef data As Byte()) As UInteger()
        Dim OrderedRotations As New List(Of UInteger)
        Dim rotatedData As Byte()
        Dim rotation As UInteger = 0
    
    
        Dim mmF As MemoryMappedFile
        mmF = MemoryMappedFile.CreateFromFile(My.Application.Info.DirectoryPath & "\outFile296", FileMode.CreateNew, "test", CLng(data.LongLength * data.LongLength))
        Dim mmVA As MemoryMappedViewAccessor
        mmVA = mmF.CreateViewAccessor(0, data.LongLength * data.LongLength)
    
        Dim pos As Long = 0
    
        For rotation = 0 To data.Length - 1
            rotatedData = GetRotation(data, rotation)
            mmVA.WriteArray(Of Byte)(pos, rotatedData, 0, rotatedData.Length)
            pos += rotatedData.Length
    
        Next
    
        For rotation = 0 To data.Length - 1
            OrderedRotations.Add(rotation)
        Next
    
        Dim eachRotation As Integer = 0
        Dim data1() As Byte
        ReDim data1(data.Length - 1)
        Dim data2() As Byte
        ReDim data2(data.Length - 1)
        Dim index As Long
        For rotation = 0 To data.Length - 1
            Dim flag As Boolean
            Do
                flag = False
                For eachRotation = OrderedRotations.Count - 1 To 0 Step -1
                    If rotation = eachRotation Then Exit For
                    mmVA.ReadArray(Of Byte)(rotation * data.Length, data1, 0, data.Length)
                    mmVA.ReadArray(Of Byte)((eachRotation * data.Length), data2, 0, data.Length)
    
                    For index = 0 To data.Length - 1
                        If data1(index) < data2(index) Then
                            Exit For
                        ElseIf data1(index) > data2(index) Then
                            mmVA.WriteArray(Of Byte)((eachRotation * data.Length), data1, 0, data1.Length)
                            mmVA.WriteArray(Of Byte)((rotation * data.Length), data2, 0, data2.Length)
    
                            Dim tmpFirst As UInteger = OrderedRotations(eachRotation)
                            OrderedRotations(eachRotation) = OrderedRotations(rotation)
                            OrderedRotations(rotation) = tmpFirst
                            flag = True
                            Exit For
                        End If
                    Next
                Next
            Loop While flag
        Next
    
        Return OrderedRotations.ToArray()
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2019-03-12
      • 2020-09-26
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多