且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与重新排序顺序属性对象

更新时间:2023-12-05 12:07:28

订单使用浮点

有关,指定对象的顺序等于物体的顺序现在在索引1,减1(列表[0] .order =列表[1] .order - 1

For push, assign the object an order equal to the order of the object that's now at index 1, minus 1 (list[0].order = list[1].order - 1)

有关交换,交换两个对象的订单( TEMP =列表[我]列表[i] =列表[J]列表[J] =列表[我]; TEMP =列表[我] .order;列表[I] .order =列表[J] .order;列表[J] .order =温度);如果这可能会引入一致性问题,那么理想情况下,你可以把一个交通上的元素标记,以表明他们的订单正在修改的过程中,还是最坏的情况下锁定的对象,直到他们是一致的。

For swap, swap the two objects' orders (temp = list[i]; list[i] = list[j]; list[j] = list[i]; temp = list[i].order; list[i].order = list[j].order; list[j].order = temp); if this might introduce consistency problems then ideally you could put a transit flag on the elements to indicate that their order is in the process of being modified, or worst case lock the objects until they're consistent

有关删除,什么也不做 - 在列表中的对象仍然有序的,你刚才介绍的序列中的差距应该不会有问题

For remove, do nothing - the objects in the list are still ordered, you've just introduced a gap in the sequence which shouldn't be a problem

插入是唯一的问题之一。如果要插入索引的元素,那么它的顺序是等于元素的平均订单的指数 I-1 I + 1 列表[I] .order =(名单[I-1] .order +列表[我+1] .order)/ 2 )。确认这一新秩序并没有在指数等于订单 I-1 I + 1 列表[I] .order =列表[I-1] .order和放大器;&放大器;列表[I] .order =列表[我+ 1] .order ) - 这将!表明您已经打机小量。如果发生这种情况(这应该很少,如果发生),你有两个选择:

insert is the only problematic one. If you're inserting an element at index i, then its order is equal to the average of the orders of the elements at indices i-1 and i+1 (list[i].order = (list[i-1].order + list[i+1].order)/2). Verify that this new order doesn't equal the order at index i-1 or i+1 (list[i].order != list[i-1].order && list[i].order != list[i+1].order) - this would indicate that you've hit machine epsilon. When this occurs (and this should rarely if ever occur) you've got two options:

  1. 硬着头皮重新排列你的整个列表,也就是分配 0 的顺序在索引0,下订单 1 索引1,...一个订单 N 的折射率n。
  2. 请当地的重新排序,以尽量减少成本。重新排序相邻元素列表[I-1] .order =(名单[I-2] .order +列表[I-1] .order)/ 2 列表[I + 1] .order =(名单[I + 2] .order +列表[I + 1] .order)/ 2 重排前名单[I] =(名单[I-1] +列表[I + 1])/ 2 ,再次验证你有没有在您的[I-1]和[我伸手机episilon +1]重新排序 - 如果你已经达到机器最小的如[I-1],那么首先重新排列[I-2] 列表[I-2] .order =(名单[I-3] .order +列表[I-2] .order)/ 2 ,然后重新排列[I-1]。如果第[i-2]重新排序命中机小量然后第一重排序[Ⅰ-3],等等。 (如果您到达列表的末尾,然后简单地减少元素的顺序[0]或增加元素[N]的顺序)正如你所看到的,在最坏的情况下,你已经有了一个级联重新排序是比较昂贵的比你可以简单地忍辱负重,重新排序整个列表;然而,在所有的可能性的重新排序将保持地方。一个很好的折衷方法是,如果你已经级联的次数太多(关于太多一个合理的值),然后做一个完整的重新排序。
  1. Bite the bullet and reorder your entire list, i.e. assign an order of 0 at index 0, an order 1 at index 1, ... an order n at index n.
  2. Do a local reorder to try to minimize the cost. Reorder the adjacent elements to list[i-1].order = (list[i-2].order + list[i-1].order)/2 and list[i+1].order = (list[i+2].order + list[i+1].order)/2 before reordering list[i] = (list[i-1] + list[i+1])/2, again verifying that you haven't reached machine episilon at your [i-1] and [i+1] reordering - if you have reached machine epsilon at e.g. [i-1], then first reorder [i-2] to list[i-2].order = (list[i-3].order + list[i-2].order)/2, and then reorder [i-1]. If the [i-2] reorder hits machine epsilon then first reorder [i-3], and so on. (If you reach the end of the list then simply decrement the order of element [0] or increment the order of element [n].) As you can see, in the worst case you've got a cascade reordering that is more expensive than had you simply bit the bullet and reordered the entire list; however, in all likelihood the reordering will remain local. A good compromise is that if you've cascaded too many times (for a reasonable value of "too many") then do a complete reordering.