简体   繁体   English

在运行时创建具有可引用ID的变量

[英]Create variables at runtime with ID that can be referenced

I am creating a 3D modeling app that will store the vertex points in a bunch of variables, one for each. 我正在创建一个3D建模应用程序,它将顶点存储在一堆变量中,每个变量一个。 I need to be able to dynamically add or remove vertex points which calls for dynamically adding or removing these variables. 我需要能够动态添加或删除顶点,这些顶点要求动态添加或删除这些变量。 Each face will reference these points with an ID linking to the variable containing the point coordinates. 每个面都将使用ID链接到包含点坐标的变量来引用这些点。 Is there a way to create and destroy variables at runtime? 有没有一种在运行时创建和销毁变量的方法? Is there a better way to accomplish this with an existing data type? 有没有更好的方法可以使用现有数据类型来完成此任务? I don't think an array will work because these need to be independent in order to be added or removed easily. 我不认为数组会起作用,因为它们需要独立才能轻松添加或删除。 I also can't directly store the coordinates of the points because a floating point error or other small change would make duplicate points next to each other. 我也不能直接存储点的坐标,因为浮点错误或其他小的更改会使重复的点彼此相邻。

Consider using a Dictionary (as Alexei pointed out in his comment). 考虑使用Dictionary (正如Alexei在其评论中指出的那样)。 A Dictionary is very fast for adding, removing and retrieving values. 字典对于添加,删除和检索值非常快。

As for the key, options include 至于键,选项包括

  • A sequential int 顺序int
  • A long that is a hash based on data in the vertex 一个long ,它是基于顶点中数据的哈希
  • Worst case a Guid 最坏的情况

A sequential int would work well in many use cases, though if you have multiple threads requesting new IDs you would have slight contention getting the next ID (and be sure you follow best practices for threading). 顺序int在许多用例中都可以很好地工作,但是如果您有多个请求新ID的线程,则在争夺下一个ID时会有些争执(并确保遵循线程最佳实践)。

A long hash risks collisions, eg if the hash is derived from the coordinates and you can have multiple vertices under management with the same coordinates (vertices with different coordinates run the normal risk of hash collisions). long哈希会产生冲突风险,例如,如果哈希是从坐标派生的,并且您可以管理多个具有相同坐标的顶点(坐标不同的顶点会发生哈希冲突的正常风险)。

A Guid is pretty large for a key, especially if there are many points, but is guaranteed to be unique. 一个Guid对于一个键来说相当大,特别是在有很多点的情况下,但是可以保证是唯一的。

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

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