HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Finding patterns in a growing collection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
growingcollectionpatternsfinding

Problem

If you look at the condition of If calculated Then in the code below, this is what slowing down the code. While the code provided seem fast with Const initBit = 4 try it with something over 12. I want to be able to use this code (with calculated param as true) with initBit of 20 or more.

Beware that 20 or more might require a gig or more of RAM and/or compiled as x64.

C# code (converted with an online tool from VB.NET):

```
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

static class Module1
{
public static void Main()
{
Console.WindowHeight = 59;
int initBit = 4;
var sw = Stopwatch.StartNew();
initBits(initBit, false);
sw.Stop();
printResult(sw, false);

sw = Stopwatch.StartNew();
initBits(initBit, true);
sw.Stop();
printResult(sw, true);

Console.Read();
}

private static int maxBits;
private static int[] CountBit;
private static int[] CalculatedBit;

private static int[] MapBit;
private static void initBits(int maxBit, bool calculated)
{
int[] calc = null;
bool calcOk = false;
int calcOkPos = 0;
List calcResult = new List();
int findPos = 0;

maxBits = ((1 = 1; i += -1)
{
for (var j = 0; j > 1) & 0x55555555);
bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
return ((bits + (bits >> 4) & 0xf0f0f0f) * 0x1010101) >> 24;
}

private static int getBitValue(int bits, int pos)
{
for (var k = 0; k <= pos - 1; k++)
{
bits = bits & bits - 1;
}
return bits ^ bits & (bits - 1);
}

private static void printResult(Stopwatch sw, bool calculated)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(" Calculated : " + calculated.ToString() + Environment.NewLine + " maxBits : " + maxBits.ToString() + " (" + getBitCount(maxBits) + ")" + Environment.NewLine + " CalculatedBit.Length : " + CalculatedBit.Length.ToString());
//if initBit is 6 or less
if (Calcu

Solution

I found the perfect way! It was right in front of me; I just had to look at the data itself.

Private Sub initBits(ByVal maxBit As Integer)

    maxBits = ((1 > 1)
    Dim StartBit = maxBits
    Dim count As Integer = 0

    For i = StartBit To StopBit + 1 Step -2
        count += CountBit(i)
    Next

    ReDim CalculatedBit(count)
    count = 1
    For i = StartBit To StopBit + 1 Step -2
        MapBit(i) = count
        For j = 0 To CountBit(i) - 1
            CalculatedBit(count) = getBitValue(i, j)
            count += 1
        Next
    Next

    Do
        count = StartBit
        StartBit = StopBit
        StopBit = StopBit >> 1
        For i = StartBit To StopBit + 1 Step -2
            MapBit(i) = MapBit(count)
            count -= 2
        Next
    Loop Until StopBit = 0

    For i = 3 To maxBits Step 2
        MapBit(i - 1) = MapBit(i) + 1
    Next
End Sub


Data:

Number : MapBit : CountBit : Value
0 : 0 : 0 : 0
1 : 1 : 1 : 1
2 : 2 : 1 : 2
3 : 1 : 2 : 1 2
4 : 6 : 1 : 4
5 : 5 : 2 : 1 4
6 : 2 : 2 : 2 4
7 : 1 : 3 : 1 2 4
8 : 12 : 1 : 8
9 : 11 : 2 : 1 8
10 : 9 : 2 : 2 8
11 : 8 : 3 : 1 2 8
12 : 6 : 2 : 4 8
13 : 5 : 3 : 1 4 8
14 : 2 : 3 : 2 4 8
15 : 1 : 4 : 1 2 4 8


The dictionary I made of the odd numbers of the second half in this example: 15, 13, 11 and 9.

The rest is just the same pattern; look at 7, 5, 3 and 1. This mapbit(num) reference are the same as 15, 13, 15 and 15. I do not need to loop through the dictionary like i was doing before; it's a simple logic.

Then the even numbers, which is always mapbit(evennumber) = mapbit(evennumber+1) +1. No need to find the pattern again.

Code Snippets

Private Sub initBits(ByVal maxBit As Integer)

    maxBits = ((1 << maxBit)) - 1
    ReDim CountBit(maxBits)
    ReDim MapBit(maxBits)

    For i = 1 To maxBits
        CountBit(i) = getBitCount(i)
    Next

    Dim StopBit = (maxBits >> 1)
    Dim StartBit = maxBits
    Dim count As Integer = 0

    For i = StartBit To StopBit + 1 Step -2
        count += CountBit(i)
    Next

    ReDim CalculatedBit(count)
    count = 1
    For i = StartBit To StopBit + 1 Step -2
        MapBit(i) = count
        For j = 0 To CountBit(i) - 1
            CalculatedBit(count) = getBitValue(i, j)
            count += 1
        Next
    Next

    Do
        count = StartBit
        StartBit = StopBit
        StopBit = StopBit >> 1
        For i = StartBit To StopBit + 1 Step -2
            MapBit(i) = MapBit(count)
            count -= 2
        Next
    Loop Until StopBit = 0

    For i = 3 To maxBits Step 2
        MapBit(i - 1) = MapBit(i) + 1
    Next
End Sub

Context

StackExchange Code Review Q#13048, answer score: 2

Revisions (0)

No revisions yet.