문제

I am having an issue trying to port the following code from C to C#. not having much success with using the fixed keyword and a ptr that gets reassigned. Could someone tell me how to represent the following code in c#?

        p = &table[(i = start[k]) >> m];
        i <<= tablebits;
        n = k - tablebits;
    /* make tree (n length) */
        while (--n >= 0) {
            if (*p == 0) {
                right[avail] = left[avail] = 0;
                *p = avail++;
            }
            if (i & 0x8000) p = &right[*p];
            else            p = &left[*p];
            i <<= 1;
        }
        *p = j;
도움이 되었습니까?

해결책

Here goes using no pointers (C# safe)...

var[] curtable = table;
i = start[k];
int idx = i >> m;

i <<= tablebits;
n = k - tablebits;

/* make tree (n length) */
while (--n >= 0) 
{
    if (curtable[idx] == 0) 
    {
        right[avail] = left[avail] = 0;
        curtable[idx] = avail++;
    }
    if (i & 0x8000) 
    {
          idx = curtable[idx];
          curtable = right;
    }
    else  
    {
          idx = curtable[idx];
          curtable = left;
     }
    i <<= 1;
}
curtable[idx] = j;

다른 팁

Looks like I was able to resolve the issue by using fixed pointers for each of the tables, then a changable ptr that I can assign the appropriate fixed pointer to. compiles fine and code appears to operate with the same result as the c code.

    fixed(ushort* p = &table[(i = start[k]) >> m])
    {
        ushort* p4 = p;
        i <<= tablebits;
        n = k - tablebits;
        /* make tree (n length) */
        while (--n >= 0)
        {
            if (*p == 0)
            {
                right[avail] = left[avail] = 0;
                *p = (ushort)avail++;
            }
            if ((i & 0x8000) > 0) fixed(ushort* p2 = &right[*p4]) {p4 = p2;}
            else fixed (ushort* p3 = &left[*p4]) { p4 = p3; }
            i <<= 1;
        }
        *p4 = (ushort)j;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top