如何使用映射文件vb.net以字典顺序对数组进行排序

编程入门 行业动态 更新时间:2024-10-24 06:26:24
本文介绍了如何使用映射文件vb以字典顺序对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这对我来说有点复杂

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

答案是从最小数组值到最大数组值排序的每个Rotation.

The answer is the each Rotation sorted from lowest array value to highest array value.

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

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

When I sort this array above by hand I should get

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时间,这就是为什么我没有从下面的页面中选择答案的原因.

I get stuck in a infinite loop and I can't put my finger on it My Previous question works because the data is always static here I try to move all the data around all over the place which is probably why it gets stuck I can't figure out a workaround around that.. i need to move all the data around to save cpu time later that's why I didn't pick the answer from the page below.

如何按字典顺序vb排序数组

这是我的代码

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

推荐答案

我不知道这是否正确,但我已为您修复了该问题.

I don't know if this is right but I fixed it for you.

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

更多推荐

如何使用映射文件vb.net以字典顺序对数组进行排序

本文发布于:2023-11-30 00:53:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何使用   字典   顺序   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!