什么是好的,CRUD交感神经算法排序列表中的项目?交感神经、算法、项目、列表中

2023-09-11 03:01:12 作者:花心尐铯魇

我想一个简单的方法来重新present对象列表的顺序。当一个对象改变位置,在该列表中,我想更新的只是一个记录的。我不知道这是可以做到的,但我很感兴趣地问SO蜂巢...

I would like a simple way to represent the order of a list of objects. When an object changes position in that list I would like to update just one record. I don't know if this can be done but I'm interested to ask the SO hive...

算法(或数据结构)应该允许各色通过更新单个项目的属性重新定位在列表 的算法(或数据结构)应该不需要看家维护该列表的完整性 的算法(或数据结构)应允许对新项目的插入或移除现有的项目的

为什么我关心的只是更新一个项目的时间...

[更新,以澄清问题]

[UPDATED to clarify question]

用例的这种算法是一个Web应用程序与CRUDy,足智多谋的服务器设置和一个干净的(角度)客户端。

The use-case for this algorithm is a web application with a CRUDy, resourceful server setup and a clean (Angular) client.

这是很好的做法,坚持纯CRUD操作在可能的情况,并就清洁code全面。如果我能做到这一点的操作在一个单一的资源#更新请求,那么我不需要任何额外的服务器端code来处理重新排序,它都可以完成使用CRUD未被更改。

It's good practice to keep to the pure CRUD actions where possible and makes for cleaner code all round. If I can do this operation in a single resource#update request then I don't need any additional serverside code to handle the re-ordering and it can all be done using CRUD with no alterations.

如果在列表中有多个项目需要更新的每个举动,然后我需要在我的控制器新的动作来处理它。这不是一个搅局者,但它开始蔓延到角,一切都变得比理想情况下应该是不干净的。

If more than one item in the list needs to be updated for each move then I need a new action on my controller to handle it. It's not a showstopper but it starts spilling over into Angular and everything becomes less clean than it ideally should be.

让我们说我们有一本杂志,该杂志有许多网页:

Let's say we have a magazine and the magazine has a number of pages :

Original magazine
- double page advert for Ford    (page=1)
- article about Jeremy Clarkson  (page=2)
- double page advert for Audi    (page=3)
- article by James May           (page=4)
- article by Richard Hammond     (page=5)
- advert for Volkswagen          (page=6)

选项1:存储整数页码

...在我们更新高达每招N条记录

如果我想拉从第5页理查德·哈蒙德的网页多达2页我可以这样做,通过改变它的页码。不过我也有修改的所有的的它,然后取代网页:

If I want to pull Richard Hammond's page up from page 5 to page 2 I can do so by altering its page number. However I also have to alter all the pages which it then displaces:

Updated magazine
- double page advert for Ford    (page=1)
- article by Richard Hammond     (page=2)(old_value=5)*
- article about Jeremy Clarkson  (page=3)(old_value=2)*
- double page advert for Audi    (page=4)(old_value=3)*
- article by James May           (page=5)(old_value=4)*
- advert for Volkswagen          (page=6)

*属性更新

* properties updated

- 它不适合我的建筑

让我们说这个正在使用通过Angular.js JavaScript的拖放正下降重新排序完成的。我想理想情况下只更新已移动页面上的一个值,独自离开了其他页面。我想发送一个HTTP请求到CRUD资源理查德·哈蒙德的网页说,它现在被移动到第二页。

Let's say this is being done using javascript drag-n-drop re-ordering via Angular.js. I would ideally like to just update a value on the page which has been moved and leave the other pages alone. I want to send an http request to the CRUD resource for Richard Hammond's page saying that it's now been moved to the second page.

- 它不能扩展

这不是一个问题,我还没有,但在某些时候我可能有10000页。我宁愿不更新它们的9,999当我移动了新的一页的头版。

It's not a problem for me yet but at some point I may have 10,000 pages. I'd rather not update 9,999 of them when I move a new page to the front page.

...我们在其中更新3条每移动

而不是存储页面的位置。如果,我不是存储来之前它,然后我从最大N动作的次数减少到3页

If instead of storing the page's position, I instead store the page that comes before it then I reduce the number of actions from a maximum of N to 3.

Original magazine
- double page advert for Ford    (id = ford,         page_before = nil)
- article about Jeremy Clarkson  (id = clarkson,     page_before = ford)
- article by James May           (id = captain_slow, page_before = clarkson)
- double page advert for Audi    (id = audi,         page_before = captain_slow)
- article by Richard Hammond     (id = hamster,      page_before = audi)
- advert for Volkswagen          (id = vw,           page_before = hamster)

我们再次将面露仓鼠起来...... 的

again we move the cheeky hamster up...

Updated magazine
- double page advert for Ford    (id = ford,         page_before = nil)
- article by Richard Hammond     (id = hamster,      page_before = ford)*
- article about Jeremy Clarkson  (id = clarkson,     page_before = hamster)*
- article by James May           (id = captain_slow, page_before = clarkson)
- double page advert for Audi    (id = audi,         page_before = captain_slow)
- advert for volkswagen          (id = vw,           page_before = audi)*

*属性更新

* properties updated

这需要在数据库中更新三行:我们搬到了网页,仅低于它的老位置,仅低于新的位置页的页面

This requires updating three rows in the database: the page we moved, the page just below its old position and the page just below its new position.

这是更好,但它仍然涉及到更新三条记录,不给我足智多谋的CRUD行为,我在寻找。

It's better but it still involves updating three records and doesn't give me the resourceful CRUD behaviour I'm looking for.

...中,我们更新每移动1只记录(但需housekeep)

不过要记住,我还是要更新只为每个重新定位记录。在我的追求,为此,我采取不同的方法。存储页面位置作为一个整数,而不是我保存它作为一个浮动。这让我通过的移动项目之间滑动它的两个人:

Remember though, I still want to update only one record for each repositioning. In my quest to do this I take a different approach. Instead of storing the page position as an integer I store it as a float. This allows me to move an item by slipping it between two others:

Original magazine
- double page advert for Ford    (page=1.0)
- article about Jeremy Clarkson  (page=2.0)
- double page advert for Audi    (page=3.0)
- article by James May           (page=4.0)
- article by Richard Hammond     (page=5.0)
- advert for Volkswagen          (page=6.0)

,然后我们再次移动仓鼠:

and then we move Hamster again:

Updated magazine
- double page advert for Ford    (page=1.0)
- article by Richard Hammond     (page=1.5)*
- article about Jeremy Clarkson  (page=2.0)
- double page advert for Audi    (page=3.0)
- article by James May           (page=4.0)
- advert for Volkswagen          (page=6.0)

*属性更新

* properties updated

每当我们进入一个项目,我们选择了一个值某处的项目之间的上面和下面的(比方说,采取这两个项目我们之间滑动的平均值)。

Each time we move an item, we chose a value somewhere between the item above and below it (say by taking the average of the two items we're slipping between).

最后,虽然你需要重新设置...

无论算法,使用插入页面到对方的小数位将最终耗尽,因为你不得不继续使用更小的数字。当你移动的物品越来越多的时候,你慢慢向下移动浮点链,并最终需要一个新的位置,这比什么都可以变小。

Whatever algorithm you use for inserting the pages into each other will eventually run out of decimal places since you have to keep using smaller numbers. As you move items more and more times you gradually move down the floating point chain and eventually need a new position which is smaller than anything available.

每一个现在,然后你因此必须做一个复位重新建立索引列表,并在范围内把它都回来了。这是确定的,但我很感兴趣,看看是否有一种方法可以连接code不要求这个看家的顺序。

Every now and then you therefore have to do a reset to re-index the list and bring it all back within range. This is ok but I'm interested to see whether there is a way to encode the ordering which doesn't require this housekeeping.

难道一个算法(或者更准确的说,数据编码)存在这个问题,这仅需要一个更新,没有看家?如果是这样你能的的解释简单的英语的运行方式(IG没有提及向图或顶点)? Muchos格拉西亚斯。

Does an algorithm (or perhaps more accurately, a data encoding) exist for this problem which requires only one update and no housekeeping? If so can you explain it in plain english how it works (i.g. no reference to directed graphs or vertices...)? Muchos gracias.

我已经颁发给我感觉过的最有趣的回答这个赏金。没有人能够提供一个解决方案(因为从事物的外表没有一个),所以我还没有打上任何特别的问题是正确的。

I've awarded the bounty on this to the question I feel had the most interesting answer. Nobody was able to offer a solution (since from the looks of things there isn't one) so I've not marked any particular question as correct.

调整没有看家的标准

在花了更多的时间思考这个问题,它发生,我认为家政标准实际上应该进行调整。看家的真正的危险不在于它是件麻烦的事,但它最好应稳健谁拥有pre-housekept一套杰出的副本客户端。

After having spent even more time thinking about this problem, it occurs to me that the housekeeping criterion should actually be adjusted. The real danger with housekeeping is not that it's a hassle to do but that it should ideally be robust to a client who has an outstanding copy of a pre-housekept set.

让我们说,乔加载了包含列表(使用角)的网页,然后熄灭,使一杯茶。他只是下载后,房管发生和重新索引的所有项目(1000,2000,3000等)。经过他来自他喝茶回来,他移动从1010 1011一个项目有一个在这一点上的风险,重新索引将会把他的项目到位置时,它的目的不是为了去。

Let's say that Joe loads up a page containing a list (using Angular) and then goes off to make a cup of tea. Just after he downloads it the housekeeping happens and re-indexes all items (1000, 2000, 3000 etc).. After he comes back from his cup of tea, he moves an item from 1010 1011. There is a risk at this point that the re-indexing will place his item into a position it wasn't intended to go.

至于以后的说明 - 任何看家算法理论上应稳健跨越列表的不同housekept版本也提交了项目的或者你应该版的内务管理和创建一个错误,如果有人试图。跨版本更新。

As a note for the future - any housekeeping algorithm should ideally be robust to items submitted across different housekept versions of the list too. Alternatively you should version the housekeeping and create an error if someone tries to update across versions.

与链表问题

虽然链表只需要几个更新,它有一些缺点太多:

While the linked list requires only a few updates it's got some drawbacks too:

在这不平凡的处理删除从列表中(你可能需要相应地调整你的#destroy方法 这是不容易的命令列表检索

的方法,我会选择

我认为,在看到所有的讨论中,我想我会选择非整数(或字符串)定位:

I think that having seen all the discussion, I think I would choose the non-integer (or string) positioning:

在它的强大,以插入和删除 在它的工作原理一个更新的

但它确实需要家政服务和如上所述,如果你要完成,你还需要将每个版本的内务管理和提高一个错误,如果有人试图更新基于一个previous清单版本。

It does however need housekeeping and as mentioned above, if you're going to be complete you will also need to version each housekeeping and raise an error if someone tries to update based on a previous list version.

推荐答案

@tmyklebu有答案,但他从来没有完全到了妙语:在回答你的问题是没有,除非你愿意接受一个最坏正1位情况下的密钥长度来存储n项。

@tmyklebu has the answer, but he never quite got to the punch line: The answer to your question is "no" unless you are willing to accept a worst case key length of n-1 bits to store n items.

这意味着,总密钥存储的n项是O(n ^ 2)。

This means that total key storage for n items is O(n^2).

还有一个对手信息理论的说法,说不管什么方案来分配密钥您选择的n项的数据库,我总能想出了一系列的n个项目重新定位运动(移动项目ķ到位置P),这将迫使用户使用具有n-1个位的密钥。或者推而广之,如果我们先建立一个空数据库,你给我的物品插入,我可以选择一个序列的插入位置,将要求您使用至少零位为先,一为第二等下去

There is an "adversary" information-theoretic argument that says no matter what scheme for assigning keys you choose for a database of n items, I can always come up with a series of n item re-positionings ("Move item k to position p.") that will force you to use a key with n-1 bits. Or by extension, if we start with an empty database, and you give me items to insert, I can choose a sequence of insertion positions that will require you to use at least zero bits for the first, one for the second, etc. indefinitely.

修改

我前面有一个想法,在这里关于使用有理数的关键。但它比只增加长度的一个位来分割对相差1键之间的间隙更昂贵。所以我删除它。

I earlier had an idea here about using rational numbers for keys. But it was more expensive than just adding one bit of length to split the gap between pairs of keys that differ by one. So I've removed it.