1

我正在研究 PageRank 问题并有一个协调列表 (Coo) 矩阵。该矩阵有一个源和目标数组,如下所示。每个源点都指向目的地的同一位置

int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };

我正在尝试对边缘进行预处理,以给出像希尔伯特这样的空间填充曲线计算的顺序。在将 (x,y) 转换为 d 转换回 (x,y) 时,我遇到了一些麻烦。

当前代码:

public static void main(String[] args) {
    int n = 2;
    int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
    int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };
    int[] d = new int[destination.length];

    for (int i = 0; i < destination.length; i++) {
        x = source[i];
        y = destination[i];

        d[i] = xy2d(n);
    }

    x = y = 0;
    for (int z = 0; z < 4; z++) {
        System.out.println("Grid:" + x);
        for (int i = 0; i < d.length; i++) {
            if (d[i] == z) {
                d2xy(n, d[i]);

                System.out.println("source: " + source[i] + " dest:" + destination[i] + " --> d:" + d[i]
                        + "--> x:" + x + ":y:" + y);

            }
        }
    }
    System.out.println();
}

static int x, y, rx, ry, s, t, d;

// convert (x,y) to d
static int xy2d(int n) {
    d = 0;
    for (s = n / 2; s > 0; s /= 2) {
        rx = (x & s) >> 0;
        ry = (y & s) >> 0;
        d += s * s * ((3 * rx) ^ ry);
        rot(s);
    }
    return d;
}

// convert d to (x,y)
static void d2xy(int n, int d) {
    t = d;
    x = y = 0;
    for (s = 1; s < n; s *= 2) {
        rx = 1 & (t / 2);
        ry = 1 & (t ^ rx);
        rot(n);
        x += s * rx;
        y += s * ry;
        t /= 4;
    }
}

// rotate/flip a quadrant appropriately
static void rot(int n) {
    if (ry == 0) {
        if (rx == 1) {
            x = n - 1 - x;
            y = n - 1 - y;
        }

        // Swap x and y
        int t = x;
        x = y;
        y = t;
    }
}

代码结果

关于这段代码有什么问题的任何想法?我知道我对全局变量有点过火了

我从这里得到了代码

编辑

我刚刚了解到数组通过引用进入方法,可用于删除 java 中的全局变量。我仍然有相同的结果

public static void main(String[] args) {
    int n = 2;
    int[] destination = new int[] { 5, 0, 5, 0, 5, 0, 5, 0, 2, 3, 5, 0, 3, 4 };
    int[] source = new int[] { 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5 };
    int[] d = new int[destination.length];
    for (int i = 0; i < destination.length; i++) {
        int[] values = new int[] { source[i], destination[i] };
        d[i] = xy2d(n, values);
    }

    for (int z = 0; z < n * n; z++) {
        for (int i = 0; i < d.length; i++) {
            if (d[i] == z) {
                int[] values = new int[2];
                d2xy(n, d[i], values);
                System.out.println("source: " + source[i] + " dest:" + destination[i] + " --. d:" + d[i] + "--> x: "
                        + values[0] + " y:" + values[1]);
            }
        }
    }

}

// convert (x,y) to d
static int xy2d(int n, int[] values) {
    int rx, ry, s, d = 0;
    for (s = n / 2; s > 0; s /= 2) {
        rx = (values[0] & s) >> 0;
        ry = (values[1] & s) >> 0;
        d += s * s * ((3 * rx) ^ ry);
        rot(s, values, rx, ry);
    }
    return d;
}

// convert d to (x,y)
static void d2xy(int n, int d, int[] values) {
    int rx, ry, s, t = d;
    values[0] = 0;
    values[1] = 0;
    for (s = 1; s < n; s *= 2) {
        rx = 1 & (t / 2);
        ry = 1 & (t ^ rx);
        rot(s, values, rx, ry);
        values[0] += s * rx;
        values[1] += s * ry;
        t /= 4;
    }
}

// rotate/flip a quadrant appropriately
static void rot(int n, int[] values, int rx, int ry) {
    if (ry == 0) {
        if (rx == 1) {
            values[0] = n - 1 - values[0];
            values[1] = n - 1 - values[1];
        }

        // Swap x and y
        int t = values[0];
        values[0] = values[1];
        values[1] = t;
    }
}
4

0 回答 0