从 1 个值数组正确移位到另一个值数组有点难

Bit shifting properly from 1 array of values to another array of values kinda hard question

我正在尝试将位从 old: 移动到 new:,如果第二次遇到 2 个相同的值,那么我猜它应该跳过它们,这就是第一个示例被破坏的原因.

这是我的代码:

        txtUndoPlaintext.Text = Replace(txtUndoPlaintext.Text, "  ", " ")
        txtUndoPlaintext.Text = txtUndoPlaintext.Text.TrimStart(CChar(" "))
        txtUndoPlaintext.Text = txtUndoPlaintext.Text.TrimEnd(CChar(" "))

        Dim UniqueList() As Byte = Split(txtUndoPlaintext.Text, " ").[Select](Function(n) Byte.Parse(n)).ToArray()


        txtPlainText.Text = Replace(txtPlainText.Text, "  ", " ")
        txtPlainText.Text = txtPlainText.Text.TrimStart(CChar(" "))
        txtPlainText.Text = txtPlainText.Text.TrimEnd(CChar(" "))

        Dim OriginalUniqueList() As Byte = Split(txtPlainText.Text, " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        txtUndoBitMask.Text = Replace(txtUndoBitMask.Text, "  ", " ")
        txtUndoBitMask.Text = txtUndoBitMask.Text.TrimStart(CChar(" "))
        txtUndoBitMask.Text = txtUndoBitMask.Text.TrimEnd(CChar(" "))

        bitmask = Split(txtUndoBitMask.Text, " ").[Select](Function(n) Byte.Parse(n)).ToArray()

       Dim newbitmask() As Byte = Nothing
        Array.Resize(newbitmask, bitmask.Length)
        'Array.Copy(bitmask, newbitmask, bitmask.Length)

        bitmaskCounter = 0

        For i = 0 To UniqueList.Length - 1
            For j = (i + 1) To OriginalUniqueList.Length - 1
                If OriginalUniqueList(i) = UniqueList(j) Then
                    Exit For
                End If
            Next j

            'If OriginalUniqueList(i) = UniqueList(i) Then
            ' bitmaskCounter += 1
            ' Continue For
            ' End If

            'If OriginalUniqueList(j) = UniqueList(j) Then
            'bitmaskCounter += 1
            'Continue For
            'End If

            If (j < OriginalUniqueList.Length - 1) AndAlso newbitmask(i) = 1 AndAlso newbitmask(j) = 1 AndAlso bitmask(bitmaskCounter) = 1 Then
                newbitmask(i) = 1
                newbitmask(j) = 1
            ElseIf (j > OriginalUniqueList.Length - 1) AndAlso bitmask(bitmaskCounter) = 1 Then
                newbitmask(i) = 1
            ElseIf (j < OriginalUniqueList.Length - 1) AndAlso OriginalUniqueList(i) = UniqueList(i) Then
                newbitmask(i) = 0
            ElseIf bitmask(bitmaskCounter) = 1 Then
                newbitmask(j) = 1
            End If
            bitmaskCounter += 1
        Next
Broken Example.

value before: 1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2
value after:  1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2

old: 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
new: 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0

2,4 = good
3,8 = bad [this is broken]
7,11 = good

以下是可以正常工作的好例子。

Good Example.
value before: 1 2 3 1 2 3 4 1 2 3 4
value after:  1 2 3 4 1 2 3 4 1 2 3

old: 0 0 0 1 0 0 1 0 0 0 0 
new: 0 0 0 0 1 0 0 1 0 0 0

3,4 = good
6,7 = good

Good Example.

value before: 1 2 1 3 2 3 2 4
value after:  1 2 4 1 2 3 2 3

old: 0 0 1 1 0 1 0 0 
new: 0 0 0 1 0 1 0 1

2,3 = good
3,5 = good
5,7 = good
Good Example.

value before: 1 2 1 3 2
value after:  1 2 1 2 3

old: 0 0 1 1 0
new: 0 0 1 0 1

2,2 = good
3,4 = good

新代码更新

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim bitmaskCounter As Integer = 0


        Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        Dim TheValues = New List(Of Byte)(UniqueList)
        bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        Dim newbitmask() As Byte = Nothing
        Array.Resize(newbitmask, bitmask.Length)
        'Array.Copy(bitmask, newbitmask, bitmask.Length)

        bitmaskCounter = 0
        Dim i As Integer = 0
        Dim j As Integer = 0

        Dim ignoreOldOffsets As New List(Of Short)
        Dim ignoreNewOffsets As New List(Of Short)

        Dim found As Boolean = False

        While i >= 0
            found = False
            Do While j > 0
                If j = OriginalUniqueList.Length Then Exit Do
                If OriginalUniqueList(j) = UniqueList(i) Then
                    found = True
                    Exit Do
                End If
                j += 1
            Loop

            If bitmaskCounter >= bitmask.Length Then Exit While

            If j = i AndAlso OriginalUniqueList(j) = UniqueList(i) OrElse j = OriginalUniqueList.Length Then
                bitmaskCounter += 1
                ignoreOldOffsets.Add(i)
                ignoreNewOffsets.Add(j)
                j = i + 1
                i += 1
                Continue While
            End If

            If ignoreOldOffsets.Contains(i) AndAlso ignoreNewOffsets.Contains(j) Then
                bitmaskCounter += 1
                j = i
                Continue While
            End If

            'If OriginalUniqueList(j) = UniqueList(j) Then
            'bitmaskCounter += 1
            'Continue For
            'End If
            'newbitmask(i) = 1
            'newbitmask(j) = 1
            'ignoreOffsets.Add(i)
            'ignoreOffsets.Add(j)

            If bitmask(i) = 1 Then
                newbitmask(i) = 1
                newbitmask(j) = 1
                ignoreOldOffsets.Add(i)
                ignoreNewOffsets.Add(j)
            End If
            bitmaskCounter += 1
            i += 1
            j = i
        End While

        'Fixed bits output to textbox.
        txtOutput.Text += "New Bits: "
        For i = 0 To newbitmask.Length - 1
            txtOutput.Text += newbitmask(i) & " "
        Next
        txtOutput.Text += vbCrLf

        'Reset bitmaskCounter.
        bitmaskCounter = 0
    End Sub

在上面的代码中

old:      0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
new:      0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0
i get:    0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 

已解决!!!

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim bitmaskCounter As Integer = 0


        'Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim TheValues = New List(Of Byte)(UniqueList)
        'bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        'Dim UniqueList() As Byte = Split("1 2 3 1 2 3 4 1 2 3 4", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim OriginalUniqueList() As Byte = Split("1 2 3 4 1 2 3 4 1 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim TheValues = New List(Of Byte)(UniqueList)
        'bitmask = Split("0 0 0 1 0 0 1 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        'Dim UniqueList() As Byte = Split("1 2 1 3 2 3 2 4", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim OriginalUniqueList() As Byte = Split("1 2 4 1 2 3 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        'Dim TheValues = New List(Of Byte)(UniqueList)
        'bitmask = Split("0 0 1 1 0 1 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()

        Dim UniqueList() As Byte = Split("1 2 1 3 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        Dim OriginalUniqueList() As Byte = Split("1 2 1 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
        Dim TheValues = New List(Of Byte)(UniqueList)
        bitmask = Split("0 0 1 1 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()




        Dim newbitmask() As Byte = Nothing
        Array.Resize(newbitmask, bitmask.Length)
        'Array.Copy(bitmask, newbitmask, bitmask.Length)

        bitmaskCounter = 0
        Dim i As Integer = 0
        Dim j As Integer = 0

        Dim found As Boolean = False

        Dim firstDuplicateIndex As Integer = -1
        Dim uniquesFound As New List(Of Byte)

        For k = 0 To UniqueList.Length - 1
            If uniquesFound.Contains(OriginalUniqueList(k)) = False Then
                uniquesFound.Add(OriginalUniqueList(k))
            Else
                firstDuplicateIndex = k
                Exit For
            End If
        Next

        Dim offsetsComplete As New List(Of Integer)


        While i >= 0
            i = Array.IndexOf(bitmask, CByte(1), i + 1)
            j = i '(i + 1)
            If i = -1 OrElse UniqueList.Length = i Then Exit While
            found = False
            Do While j >= 0
                If j = OriginalUniqueList.Length Then Exit Do

                If j < firstDuplicateIndex OrElse OriginalUniqueList(j) = UniqueList(j) AndAlso bitmask(j) <> 1 OrElse offsetsComplete.Contains(j) Then
                    j += 1
                    Continue Do
                End If

                If OriginalUniqueList(j) = UniqueList(i) Then
                    found = True
                    Exit Do
                End If
                j += 1
            Loop

            If bitmaskCounter >= bitmask.Length Then Exit While

            If j < bitmask.Length AndAlso bitmask(i) = 1 Then
                newbitmask(j) = 1
                offsetsComplete.Add(j)
            End If

            bitmaskCounter += 1
        End While

        'Fixed bits output to textbox.
        txtOutput.Text += "New Bits A: 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0" & vbNewLine
        txtOutput.Text += "New Bits B: "
        For i = 0 To newbitmask.Length - 1
            txtOutput.Text += newbitmask(i) & " "
        Next
        txtOutput.Text += vbCrLf

        'Reset bitmaskCounter.
        bitmaskCounter = 0
    End Sub