0

我已经开始为 n 维向量编写一个库。

我想在这个库中支持几个设计功能:

  • 内存布局是连续的。这样共享内存、通过网络发送和保存到文件只需要传递一个指针。
  • 不同的维度可以支持不同的元素大小(即[0][0]可能是1字节,[1][0]也可能是2字节)。

在绘制了某些维度向量的内存布局的一些表示之后,我意识到有两种布局结构的方法:

这是第一种方法。两种表示都是 [2][4][3] 数组的可视化。

 ---------------------------
| index             | 0:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
 ---------------------------
| index             | 1:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 0:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 0:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
     ---------------------------
    | index             | 1:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:2 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:2 |
        | Element           |       |
         ---------------------------

这里是第二种方式。

 ---------------------------
| index             | 0:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 0:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:0:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:1:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:2:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 0:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 0:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 0:3:2 |
        | Element           |       |
         ---------------------------
 ---------------------------
| index             | 1:0:0 |
| elementSize       |       |
| elementCount      |       |
| expansionBoundary |       |
| slotCount         |       |
 ---------------------------
     ---------------------------
    | index             | 1:0:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:0:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:0:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:1:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:1:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:1:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:2:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:2:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:2:2 |
        | Element           |       |
         ---------------------------
     ---------------------------
    | index             | 1:3:0 |
    | elementSize       |       |
    | elementCount      |       |
    | expansionBoundary |       |
    | slotCount         |       |
     ---------------------------
         ---------------------------
        | Index             | 1:3:0 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:1 |
        | Element           |       |
         ---------------------------
         ---------------------------
        | Index             | 1:3:2 |
        | Element           |       |
         ---------------------------

选择一种布局而不是另一种布局是否有某些优点/缺点?

4

0 回答 0