简体   繁体   English

不同大小的元组向量

[英]Vector of tuples with different size

I am currenly implementing marching cubes algorithm using Rust and I faced a problem.我目前正在使用 Rust 实施行进立方体算法,我遇到了一个问题。 This algorithm requires using triangulation table which I copied from GitHub. But which data structure in Rust allow us to store such a data?该算法需要使用我从GitHub复制的三角表。但是Rust中的哪个数据结构允许我们存储这样的数据?

Those tuples mean which edges of cubes have to be connected, so they all are different size.这些元组意味着立方体的哪些边必须连接,所以它们的大小都不同。

pub fn tri_table()-> Vec<???>
{
   return vec![
      (),
      (0, 8, 3),
      (0, 1, 9),
      (1, 8, 3, 9, 8, 1),
      (1, 2, 10),
      (0, 8, 3, 1, 2, 10),
      (9, 2, 10, 0, 2, 9),
      (2, 8, 3, 2, 10, 8, 10, 9, 8),
      (3, 11, 2),
      (0, 11, 2, 8, 11, 0),
      (1, 9, 0, 2, 3, 11),
      (1, 11, 2, 1, 9, 11, 9, 8, 11),
      (3, 10, 1, 11, 10, 3),
      (0, 10, 1, 0, 8, 10, 8, 11, 10),
      (3, 9, 0, 3, 11, 9, 11, 10, 9),
      (9, 8, 10, 10, 8, 11),
      (4, 7, 8),
      ...
   ];
}

It sounds like your triangulation table is known at compile time.听起来你的三角测量表在编译时是已知的。 In that case, use static slices.在这种情况下,使用 static 个切片。 That's the solution with the lowest cost, it has almost zero overhead.这是成本最低的解决方案,它的开销几乎为零。 For the price that you can't modify it anymore afterwards.对于以后不能再修改的价格。

Like this:像这样:

pub fn tri_table() -> &'static [&'static [i32]] {
    &[
        &[],
        &[0, 8, 3],
        &[0, 1, 9],
        &[1, 8, 3, 9, 8, 1],
        &[1, 2, 10],
        &[0, 8, 3, 1, 2, 10],
        &[9, 2, 10, 0, 2, 9],
        &[2, 8, 3, 2, 10, 8, 10, 9, 8],
        &[3, 11, 2],
        // ...
    ]
}

By doing this the data will be baked directly into the data segment of your executable, and calling tri_table() only returns a pointer to it, no allocation will happen and no heap memory will be used.通过这样做,数据将直接烘焙到可执行文件的数据段中,调用tri_table()仅返回指向它的指针,不会发生分配,也不会使用堆 memory。 Calling tri_table() multiple times will always return a reference to the same address, so also no overhead if you call it multiple times.多次调用tri_table()将始终返回对同一地址的引用,因此如果多次调用它也不会产生开销。

Finomnis has already suggested an efficient solution to your problem. Finomnis 已经为您的问题提出了一个有效的解决方案。 But for those who find this question from a search for a way to put different tuples in a vector, the number and type of tuples in that vector might not be known at compile time.但是对于那些通过搜索将不同元组放入一个向量中的方法而发现这个问题的人来说,该向量中元组的数量和类型在编译时可能是未知的。 That's usually the case with vectors.向量通常就是这种情况。

So if you really need a vector , the two main approaches in Rust is to use a vector of trait objects or a vector of enums with values .因此,如果您确实需要向量,Rust 中的两种主要方法是使用特征对象向量或具有值的枚举向量。 Both approaches have their pros and cons.这两种方法各有利弊。 Enums will probably use more memory (depends on the size of the enum variant with the largest value), but a trait object requires an extra (fat) pointer (as explained here ).枚举可能会使用更多的 memory(取决于具有最大值的枚举变体的大小),但是特征 object 需要一个额外的(胖)指针(如此处解释)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM