有效维护清单

发布于 2024-10-16 07:59:59 字数 1724 浏览 1 评论 0原文

我有一个很大的清单。我可以从此列表中添加/重新排列/删除项目。 由于该列表确实存在错误,因此我不想将整个列表的快照存储在系统中的任何位置。 相反,我维护一个 xml 文件来跟踪添加/移动的项目。

每个项目都与索引和更改顺序相关联。
Def- 更改顺序: 添加/移动项目的顺序。
Def- Index: 添加/移动项目的索引。

所以现在每当我需要显示列表时,我都会有原始列表和 xml 文件。我将使用 xml 文件,根据更改顺序添加/移动项目。

每当我想从列表中移动/删除任何项目时,我都必须更改其他项目的索引。

为此,我使用了简单的代码:

for each item in list: 
    If item's change order > removed/moved item's change order:
        Do Item's CO = item's CO - 1
        If item's index > removed item's index  (This index is not actual index, 
                                                 is the index where we  inserted
                                                 /moved the item. Item's actual 
                                                 index can be different, because
                                                 of the movements of other items 
                                                 in the list)
            Do Item's index = item's index - 1

此代码在很多情况下都会失败。对于前。

原始列表(S、D、G、Y、U、I)

我的 xml 中的

X: (newly added) CO 1, index 5
Giving me: (S, D, G, Y, X, U, I)
Y: (already in list at position 4) CO 2 index 6
Giving me: (S, D, G, X, Y, U, I)

在我的实际列表中:

X: pos: 4 
Y: pos: 5

现在我想根据代码删除 X.
Y:CO 1 索引 5

将其应用于原始列表:(S、D、G、U、Y、I)

这是错误的!

对于此类需求,是否有正确的现有算法?

谢谢...


我无法将文件存储在数据库中。原始列表有可能发生更改。对于前。基本列表是(p,q,r)。 xml 是:(将 x 添加到第 2 个位置,将 y 添加到第 4 个位置,将 Q 移动到第 4 个位置)。但是我的基本列表有可能变成 (w,p,r,g,h),并且我必须使用相同的 xml 文件(将 x 添加到第 2 个位置,将 y 添加到第 4 个位置,将 Q 移动到第 4 个位置) )以便将新元素添加到预期位置。如果某些项目(如 Q)不在基本列表中,但在 xml 中,我必须忽略该 xml 节点。

I have a huge list. I can add/rearrange/remove items from this list.
Since the list is really bug, I don't want to store the snapshot of entire list anywhere in the system.
Instead I maintain a xml file to keep track of items that are added/moved.

Each item is associated with index and change order.
Def- Change order: order in which item is added/moved.
Def- Index: index at which item is added/moved.

So now whenever I need to display the list, I'll have my original list and xml file. I'll use xml files, I'll add/move items according to the change order.

Whenever I want to move/delete any item from list, I have to change the indexes of other items.

for this I used simple code:

for each item in list: 
    If item's change order > removed/moved item's change order:
        Do Item's CO = item's CO - 1
        If item's index > removed item's index  (This index is not actual index, 
                                                 is the index where we  inserted
                                                 /moved the item. Item's actual 
                                                 index can be different, because
                                                 of the movements of other items 
                                                 in the list)
            Do Item's index = item's index - 1

This code is failing in many cases. For ex.

Original List ( S, D, G, Y, U, I)

in my xml I have

X: (newly added) CO 1, index 5
Giving me: (S, D, G, Y, X, U, I)
Y: (already in list at position 4) CO 2 index 6
Giving me: (S, D, G, X, Y, U, I)

after this in my actual list:

X: pos: 4 
Y: pos: 5

Now I want to remove X. according to code
Y: CO 1 index 5

Applying this to original list: (S, D, G, U, Y, I)

which is wrong!

Is there any correct existing algorithm for this type of requirement?

Thanks....


I cant store the file in database. There is a possibility by which the original list can change. For ex. base list is (p,q,r). And xml is: (add x to 2nd pos, add y to 4th pos, move Q to 4th pos). But there is a possibility by which my base list can become (w,p,r,g,h) and I have to use the same xml file (add x to 2nd pos, add y to 4th pos, move Q to 4th pos) so that the new elements get added at there expected position. If some item (like Q) is not in base list, but is in xml, I have to ignore that xml node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-10-23 07:59:59

它没有解决您的算法问题,但是您可以将您的列表存储在数据库中吗?您可以使用基于文件的 DBMS,例如 SQLite,它使用起来非常简单。

在我看来,您所描述的操作可以通过一些简单的 SQL 语句轻松完成。

编辑:MySQL 更改为 SQLite

It doesn't address the question of your algorithm, but could you store your list in a database? You could use a file-based DBMS such as SQLite, which is very simple to use.

It seems to me that the operations you are describing could be easily done with some simple SQL statements.

Edit: MySQL changed to SQLite

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文