D语言:初始化动态多维数组最佳实践?

发布于 2024-12-20 22:52:53 字数 652 浏览 0 评论 0原文

只是好奇这是否是在 D 中初始化动态多维数组的最佳实践。他们的语言参考中有一个关于数组的部分,但我不太确定它是否超出了我想要完成的任务。

class Map {
    Tile[][] tiles;

    this(uint width, uint height) {
        tiles.length = height;
        foreach (ref tilerow; tiles)
            tilerow.length = width;
    }
}

Map map1 = new Map(5000, 3000); // values determined at runtime

(或者类似典型的 for (y=0;y

我对此的担忧是,它单独重新分配数组的每一行,而不是一次性重新分配整个块,所以我不知道这是否会导致过多的内存洗牌。另外,我相信它不能保证是连续的(因为在这种情况下 tiles 只是一个指针数组)。有没有“更好”的方法来做到这一点(不涉及使用一维数组并自己计算索引)?据我从文档中可以看出,连续的多维数组只能在编译时用不可变的维度声明,只是想知道我是否遗漏了一些东西......

Just curious if this is the best practice for initializing a dynamic, multidimensional array in D. There is a section on arrays in their language reference, but I'm not quite sure if it goes over what I'm trying to accomplish.

class Map {
    Tile[][] tiles;

    this(uint width, uint height) {
        tiles.length = height;
        foreach (ref tilerow; tiles)
            tilerow.length = width;
    }
}

Map map1 = new Map(5000, 3000); // values determined at runtime

(or an equivalent alternative like a typical for (y=0;y<height;y++) loop).

My concern with this is that it reallocates each row of the array separately rather than the whole chunk all at once, so I don't know if this will lead to too much memory shuffling. Also, I believe it's not guaranteed to be contiguous (since tiles is just an array of pointers in this case). Is there any "better" way to do this (that doesn't involve using a single-dimensional array and computing the index myself)? As far as I can tell from the docs a contiguous multidimensional array can only be declared with immutable dimensions at compile time, just wondering if I'm missing something...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-12-27 22:52:53

您可以新建数组,至少在 D2 中:

Tile[][] tiles = new Tile[][](height, width);

我相信这是最佳实践。

You can new the array, at least in D2:

Tile[][] tiles = new Tile[][](height, width);

I believe this is the best practice.

别理我 2024-12-27 22:52:53

你可以通过malloc预先编辑你需要的所有东西来捏造它

this(uint width, uint height) {
    void* p = enforce(GC.malloc(Tile.sizeof*width*height),new OutOfMemoryException);
          //allocate all rows at once, throw on returned null
    tiles.length = height;
    foreach (i,ref tilerow; tiles)
        tilerow = cast(Tile[])p[Tile.sizeof*width*i..Tile.sizeof*width*(i+1)];
                //slice it into the multidimensional array
}

,或者使用临时数组来保留下摆以获得更干净/更少错误的代码(即隐藏malloc)

this(uint width, uint height) {
    Tile[] p = new Tile[height*width]
    tiles.length = height;
    foreach (i,ref tilerow; tiles)
        tilerow = p[width*i..width*(i+1)];
                //slice it into the multidimensional array
}

you can fudge it by mallocing every thing you need upfront

this(uint width, uint height) {
    void* p = enforce(GC.malloc(Tile.sizeof*width*height),new OutOfMemoryException);
          //allocate all rows at once, throw on returned null
    tiles.length = height;
    foreach (i,ref tilerow; tiles)
        tilerow = cast(Tile[])p[Tile.sizeof*width*i..Tile.sizeof*width*(i+1)];
                //slice it into the multidimensional array
}

EDIT or use a temporary array to keep hem in for cleaner/less bugprone code (i.e. hide the malloc)

this(uint width, uint height) {
    Tile[] p = new Tile[height*width]
    tiles.length = height;
    foreach (i,ref tilerow; tiles)
        tilerow = p[width*i..width*(i+1)];
                //slice it into the multidimensional array
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文