简体   繁体   English

什么数据结构用于多维网格? (C ++)

[英]what data structure to use for multidimensional mesh grid? (c++)

I need to code a test platform for testing a specific algorithm. 我需要编写一个测试平台来测试特定的算法。 The system is supposed to be definable by input and 3 dimensional - similar to network on chip it has both node and link elements connecting to connect them. 该系统应该可以通过输入和三维来定义 - 类似于片上网络,它具有连接它们的节点和链接元件。 Due to the dimensions and links between nodes i have had some hardships on figuring out what data structures to use - a 3 dimensional array like array[x][y][z] is hard to handle as the pointer with drawbacks when adding links to connect the nodes (leaves several null value holes in the structure). 由于节点之间的维度和链接,我在确定要使用的数据结构时遇到了一些困难 - 像数组[x] [y] [z]这样的三维数组很难处理,因为指针在添加链接时有缺点连接节点(在结构中留下几个空值孔)。 Binary search trees are hard to realize since its a grid type. 二进制搜索树很难实现,因为它是网格类型。 For this reason i have thought about doing a linked list where links are easier to implement. 出于这个原因,我考虑过做一个链接更容易实现的链表。 (the final testing platform should look something like the presentation below) where every link is also mapped down as they contain communication schedules (最终的测试平台应该类似于下面的演示文稿),其中每个链接也会映射下来,因为它们包含通信计划

01-------02-------03
| \      | \      | \
|  10----|--11----|--12
|  | \   |  | \   |  | \
|  |  19-|--|--20-|--|--21
04-------05-------06 |  |
| \   |  | \|  |  | \|  |
|  13----|--14----|--15 |
|  | \|  |  | \|  |  | \|
|  |  22-|--|--23-|--|--24
07-------08-------09 |  |
  \|  |    \|  |    \|  |
   16-------17-------18 |
     \|       \|       \|
      25-------26-------27

Can any of you offer some help as to what type of structure in c++ would suit this kind of task. 你们中的任何人都可以提供一些帮助,了解c ++中哪种类型的结构适合这种任务。 The finished program should be able to generate such a structure, given the dimension parameters of x, y and z. 在给定x,y和z的尺寸参数的情况下,完成的程序应该能够生成这样的结构。

currently a the rough outline should look like this 目前粗略的轮廓应该是这样的

>class Node{
>  public:
>   Link* north;
>   Link* east;
>   Link* south;
>   Link* west;
>   Link* up;
>   Link* down;
>   //will contain a node specific scheduler
>}
>
>class Link{
>
>   Node* A;
>   Node* B;
>   //will contain a link specific scheduler
>}

EDIT 22.01.2013 编辑22.01.2013

well first of all as its a testing platform simulating a three dimensional network on chip type multiprocessor system. 首先,它是一个模拟三维网络片上多处理器系统的测试平台。 The task this system has to fill is to enable testing certain algorithms to help map tasks onto these nodes (where each node is connected to a processor core). 该系统必须完成的任务是启用测试某些算法以帮助将任务映射到这些节点(每个节点连接到处理器核心)。 In the light of this, the memory consumption might not be a problem as it is only for testing as i stated, for this reason it is imperative that the system would have both nodes and links as neither of them can be used by two event at the same time (communication on a link will block all other communications etc, this is the reason why i wrote that the class type node/link will have a scheduler in it) 鉴于此,内存消耗可能不是问题,因为它仅用于我所说的测试,因此系统必须同时具有节点和链接,因为它们都不能被两个事件用于同时(链接上的通信将阻止所有其他通信等,这就是为什么我写的类型节点/链接将有一个调度程序)

It depends very much on what kind of operations you are going to perform on that structure. 这在很大程度上取决于您将在该结构上执行哪种操作 Will you have to do searches ? 你需要做搜索吗? If so, what kind: value-based or position-based? 如果是这样,那么:基于价值还是基于头寸? Will you have to perform transformations on it? 你需要对它进行转换吗? Will you have to access all of its elements repeatedly and in a sequence? 您是否必须按顺序重复访问其所有元素? Will you have to access elements based on their position in the grid? 您是否必须根据其在网格中的位置来访问元素? Is your structure sparse or dense ? 你的结构稀疏还是密集

Moreover, do you want to minimize creation time, search time, navigation time, or transformation time? 此外,您想最小化创建时间, 搜索时间, 导航时间或转换时间吗? This all gives fundamental information to make a decision. 这一切都为做出决定提供了基本信息。

I would not go for a solution based on nodes and links, because it is: 不会寻求基于节点和链接的解决方案,因为它是:

  1. expensive in terms of memory consumption (due to the extra link pointers); 内存消耗方面很昂贵(由于额外的链接指针);
  2. expensive in terms of complexity (navigating all the nodes require following all the links, so no random access), and; 昂贵的复杂性 (导航所有节点需要跟随所有链接,因此没有随机访问),和;
  3. expensive in terms of data locality (nodes will be allocated individually and spread all over the heap at separate addresses, which will generate many cache misses and slow down your program); 数据局部性方面是昂贵的(节点将被单独分配并在整个堆中分布在不同的地址,这将产生许多缓存未命中并减慢程序速度);
  4. error-prone (messing up with links is easy, especially if you use raw pointers and don't want to pay for smart pointers' memory overhead) 容易出错 (搞乱链接很容易,特别是如果你使用原始指针并且不想支付智能指针的内存开销)

Without knowing too much about your requirements, I would probably suggest you having a look at Boost.MultiArray . 在不了解您的要求的情况下,我可能会建议您查看Boost.MultiArray

If you want to do things on your own, then (again, without knowing too much about your requirements) I would suggest you using a vector<vector<vector<double>>> , which is relatively simple, does not have fixed size and allows you resizing at run-time, guarantees you O(1) access through the subscripting operator and also some locality of the data (you have several vectors here, so as long as you access data from one vector performance will be fine). 如果你想自己做事,那么(同样,不太了解你的要求)我会建议你使用vector<vector<vector<double>>> ,它相对简单,没有固定的大小和允许你在运行时调整大小,保证你通过下标操作符O(1)访问以及数据的某些位置(这里有几个向量,所以只要你从一个向量性能访问数据就可以了)。

A plain C-style multi-dimensional array is also a possibility, but it's inherently unsafe, which would make it a last-resort choice if I were you (also, allocating a contiguous block of memory might be impossible if your structure is huge). 一个简单的C风格的多维数组也是一种可能性,但它本身就是不安全的,如果我是你的话,这将使它成为最后的选择(如果你的结构很大,也可能无法分配连续的内存块) 。

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

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