快速向量结构,允许在D?中进行[i]和.xyz运算. [英] Fast vector struct that allows [i] and .xyz-operations in D?

查看:102
本文介绍了快速向量结构,允许在D?中进行[i]和.xyz运算.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在D中创建一个矢量结构,如下所示:

I'd like to create a vector struct in D that works like this:

vec u, v;
vec w = [2,6,8];
v.x = 9; // Sets x
v[1] = w.y; // Sets y
u = v; // Should copy data

稍后我也想添加诸如u = v * u等之类的东西.但是以上内容现在就可以了.
这是我走了多远:

Later I'd also like to add stuff like u = v * u etc. But the above will do for now.
This is how far I've come:

struct vec3f
{
    float[3] data;
    alias data this;
    @property
    {
        float x(float f) { return data[0] = f; }
        float y(float f) { return data[1] = f; }
        float z(float f) { return data[2] = f; }
        float x() { return data[0]; }
        float y() { return data[1]; }
        float z() { return data[2]; }
    }
    void opAssign(float[3] v)
    {
        data[0] = v[0];
        data[1] = v[1];
        data[2] = v[2];
    }
}

现在,这几乎可以按我想要的方式工作,但是我不确定这是否正确". opAssign()是否应该返回一些值?

Now this pretty much makes it work like I wanted, but I feel very unsure about if this is "right". Should the opAssign() perhaps return some value?

我还想知道这是否真的能做到尽可能快?我是否尝试添加alias data[0] x;等,但这不起作用.有任何想法吗?还是这是完成方式"?也许编译器很聪明,可以确定属性函数或多或少是别名?

I'm also wondering if this is really as fast as it can be? Is I've tried adding alias data[0] x; etc. but that doesn't work. Any ideas? Or is this "how it's done"? Perhaps the compiler is smart enough to figure out the propery functions are more or less aliases?

推荐答案

总体而言,这看起来很合理.出于分配链接的目的,可以说opAssign应该返回v.但是,在实践中,这经常被忽略,并且可能会导致性能下降(我不知道).与D1不同,您可以从D2中的函数返回静态数组.

Overall, this looks pretty reasonable. For the purpose of assignment chaining, opAssign should arguably return v. However, in practice this is often overlooked and might cause a performance hit (I don't know). Unlike in D1, you can return static arrays from functions in D2.

就性能而言,考虑这一点的最佳方法是在组装级别.假设启用了内联,几乎可以肯定地将内联x().静态数组直接存储在结构中,而无需额外的间接层.指令return data[0];将使编译器生成从结构开头的偏移量读取的代码.该偏移量将在编译时知道.因此,很可能调用x()会生成与x实际上是一个公共成员变量完全相同的汇编指令.

As far as performance, the best way to think of this is at the assembly level. Assuming inlining is enabled, x() will almost certainly be inlined. Static arrays are stored directly in the struct without an extra layer of indirection. The instruction return data[0]; will cause the compiler to generate code to read from an offset from the beginning of the struct. This offset will be known at compile time. Therefore, most likely calling x() will generate exactly the same assembly instructions as if x were actually a public member variable.

不过,另一种可能性是使用匿名联合和结构:

One other possibility, though, would be to use an anonymous union and struct:

struct vec3f
{
    union {
        float[3] vec;

        struct {
            float x;
            float y;
            float z;
        }
    }

    alias vec this;  // For assignment of a float[3].

}

请注意,尽管alias this目前还存在很多问题,除非您愿意提交一些错误报告,否则您可能不应该使用它.

Note, though that alias this is fairly buggy right now, and you probably shouldn't use it yet unless you're willing to file some bug reports.

这篇关于快速向量结构,允许在D?中进行[i]和.xyz运算.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆