获得独特的数字和了解时,他们获释独特、数字

2023-09-11 04:47:58 作者:-为了你我会坚强

我有一个物理模拟(使用Box2D的),其中用相同的整数ID机构不会发生碰撞,例如,属于相同的字符机构。我有一个问题,但在我需要能够获得一个唯一的编号为每个可能的实体,所以没有两个字符意外得到相同的ID。有机构的数量有限,但它们的创建和销毁仿真使然,所以有必要一旦身体他们属于没有了自由的唯一ID。

一个类全球负责创建和销毁所有机构,并且也是管理的唯一编号生成实体,和任何其他地方的物理模拟而言。

我想到了两种方法,到目前为止,但我不知道这将是更好的,如果其中任何一个都:

请在矢量<短> ,以数据为参考漂浮的数量,并在向量作为ID本身的位置。这种方法有可能会造成不必要的复杂性编码操纵组ID的实体的时候,因为他们需要确保他们告诉了全球他们是如何多次提到取出 公式中的这些特殊数字,你都了解吗

请在矢量<布尔> ,与数据是,如果该ID是免费的或没有,向量中的位置作为ID本身。矢量将增长,每一个新的呼叫一个唯一的ID,如果存在没有免费的插槽。的缺点是,一旦该载体达到一定规模,整个仿真的审计将需要完成的,但具有能够抓住唯一号码实体的优点,而不必以帮助管理引用计数

你是什么人认为,有没有更好的办法?

解决方案

您可以保留未使用的ID的免费列表作为你的主内单向链表全球对象。

当一个对象被全球破坏(使得它的ID未使用的),你可能会推动该ID到空闲链表的头。

当你创建一个新的对象,你可以做到以下几点:

 如果空闲列表是非空:弹出头项目,并采取该ID。
否则增加一个全球性的ID计数器,并为其分配的当前值。
 

虽然你仍然可以用完的ID(如果你同时拥有了比你的计数器的最大值更多的对象),这个策略将让你回收的ID,并尽一切与 O(1 )运行的复杂性。

编辑:按@马蒂厄下面的评论中,的std :: deque的容器还可以用于维护自由之列。这个容器也支持 push_front,pop_front 业务与 O(1)的复杂性。

希望这有助于。

I have a physics simulation (using Box2D) where bodies with identical integer IDs do not collide, for instance, bodies that belong to the same character. I have a problem though in that I need to be able to get a unique number for each possible entity, so that no two characters accidentally get the same ID. There's a finite number of bodies, but they are created and destroyed as the simulation dictates, so it's necessary to free unique IDs once the body they belonged to is gone.

A class World is responsible for creating and destroying all bodies, and is also the entity that manages the unique number generation, and anything else where physics simulation is concerned.

I thought of two methods so far but I'm not sure which would be better, if either of them at all:

Keep a vector<short>, with the data being the number of references floating around, and the position in the vector being the ID itself. This method has the disadvantage of creating unneeded complexity when coding entities that manipulate group IDs, since they would need to ensure they tell the World how many references they're taking out.

Keep a vector<bool>, with the data being if that ID is free or not, and the position in the vector being the ID itself. The vector would grow with every new call for a unique ID, if there exist no free slots. The disadvantage is that once the vector reaches a certain size, an audit of the entire simulation would need to be done, but has the advantage of entities being able to grab unique numbers without having to help manage reference counting.

What do you folks think, is there a better way?

解决方案

You could maintain a "free" list of unused IDs as a singly linked list inside your master World object.

When an object is destroyed by World (making its ID unused) you could push that ID onto the head of the free list.

When you are creating a new object you could do the following:

If the free list is non-empty: pop the head item and take that ID.
Else increment a global ID counter and assign it's current value.

While you could still run out of IDs (if you simultaneously had more objects than the max value of your counter), this strategy will allow you to recycle IDs, and to do everything with O(1) runtime complexity.

EDIT: As per @Matthieu's comments below, a std::deque container could also be used to maintain the "free" list. This container also supports the push_front, pop_front operations with O(1) complexity .

Hope this helps.