在C中思考:我有内存缓冲区(平面3D),有数百万个~/.bash_profile
结构,我想重新计算邻居:
Object
以后可以访问typedef struct Object {
vector_int3 coords;
struct Object *neighbours[6]; // Neighbouring objects in 6 directions
int prop1;
} Object;
我试图用Swift做同样的事情,我试过了:
chunk->neighbours[TOP]->prop1;
我只想在Swift中使用指向struct Object {
var coords: vector_int3
// IMO this is wrong, because it allocates new heap buffer for 6 pointers
var neighbours = UnsafeMutablePointer<Chunk>.allocate(capacity: 6)
var prop1: Int
...
}
的6个指针缓冲区,以便能够通过索引访问它。什么是最接近Swift实现提到的C结构?不,这不是过早的优化。
答案 0 :(得分:2)
C结构的等价物看起来像
struct Object {
var coords: vector_int3
var neighbours: (UnsafeMutablePointer<Chunk>, UnsafeMutablePointer<Chunk>, UnsafeMutablePointer<Chunk>, UnsafeMutablePointer<Chunk>, UnsafeMutablePointer<Chunk>, UnsafeMutablePointer<Chunk>)
var prop1: Int
...
}
是的,它非常笨拙。