Delphi TList< T>复制到另一个TList? [英] Delphi TList<T> Copy into another TList?

查看:674
本文介绍了Delphi TList< T>复制到另一个TList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在将TList元素复制到任何其他TList中的任何安全方法 特定位置和特定长度.我应该只将list1的元素分配给list2还是 有没有我不知道的功能可以更准确地处理该问题?

I would like to know if there is any safe way to copy the TList elements into any other TList to a specific position and with a specific length. Should I just assign the elements of the list1 to list2 or is there any functionality out there I'm not aware of that handles that more accurate?

感谢您抽出宝贵的时间.

Thanks for taking your time.

推荐答案

如果您打算替换而不是将它们插入给定位置,则答案是没有直接的机制和迭代分配是使用的方法.

If your intention is to REPLACE items rather than to insert them at a given position, then the answer is that there is no direct mechanism and iterative assignment is the approach to use.

for i := 1 to maxItems do
  dest[ insertPos + i - 1] := src[ i - 1 ];

在这种情况下,您应该考虑以下情况:添加的项目多于目的地列表所能容纳的数量.这是否意味着仅替换适合"的项目,向腾出空间"添加其他项目还是根本不分配任何项目(除非所有项目都适合)是一个只有您的要求才能回答的问题.

In this case you should consider the situation where you are adding more items than the destination list has room for. Whether this means only replacing as many items as will "fit", adding additional items to 'make room' or not assigning any at all (unless all will fit), is a question that only your requirements can answer.

但是,如果您打算将这些项目插入到目标列表中,则可以结合使用 InsertRange() Copy(),以及由源列表维护的内部 T 数组.例如,使用 TList< String> 的两个实例:

However, if your intention is to INSERT the items into the destination list, then you can use a combination of InsertRange() and Copy() together with the internal array of <T> maintained by the source list. For example, using two instances of a TList<String>:

var
  src, dest: TList<String>;
  insertIndex, maxItems: Integer;

dest.InsertRange( insertIndex, Copy( src.List, 0, maxItems ) );

要插入整个 src 列表,您无需使用 Copy(),但可以直接在中引用源列表> InsertRange()方法:

To insert the entire src list you do not need to use Copy() but can reference the source list directly in the InsertRange() method:

dest.InsertRange( insertIndex, src );

性能说明:

如果源列表很大和/或要添加的子项数量很少,则使用 Copy()可能是一项昂贵的操作.但是,将项目实际插入到目标列表中非常有效,因为 InsertRange()方法能够通过一次操作为目标列表中的新项目腾出空间,然后再插入新项目.项目在为他们创建的空间中,因此对于添加的项目数量可能仍然被证明是最有效的.

The use of Copy() is a potentially expensive operation if the source list is large and/or the number of sub-items being added is small. However, the actual insertion of the items into the destination list is very efficient as the InsertRange() method is able to make room for the new items in the destination list in a single operation and then insert the new items in the space created for them, so for larger numbers of items being added it may still prove to be the most efficient.

另一种方法是迭代地分别插入源项目:

The alternative approach would be to iteratively insert the source items individually:

for i := 1 to maxItems do
  dest.Insert( insertIndex, src[i - 1]);

虽然这避免了复制插入的数组项,但是如果目标列表很大并且要插入大量项,则迭代插入本身可能会效率低下,因为必须分别为目标列表中的每个项留出空间插入(尽管通过显式计算并预先分配目标列表的容量,可以显着改善此操作的潜在影响).

Whilst this avoids copying array items being inserted, inserting iteratively is itself potentially inefficient if the destination list is large and a larger number of items are being inserted, as room for each item in the destination list has to be made separately for each insert (although the potential impact of this may be significantly improved by explicitly calculating and pre-allocating the Capacity of the destination list).

例如如果您要将1000个商品列表中的100个商品插入2000个商品列表的(精确)中间,则:

e.g. if you are inserting 100 items from a 1000 item list into the (exact) middle of a 2000 item list:

InsertRange( Copy() )       Copy 100 items into an intermediate array
                            Moves 1000 items in the dest list to make room for 2100 (total)
                            Inserts 100 items into the 'blank' space

Iterative insert            100 repetitions of:
                               Move 1000 items in the dest list to make room for 1 more
                               Inserts 1 item

对于插入100个项目, InsertRange()可能是最有效的.相比之下,如果仅从源列表中插入单个项目,则 InsertRange()方法会产生潜在的过多开销.

For inserting 100 items, InsertRange() is potentially the most efficient. By contrast, if inserting only a single item from the source list then the InsertRange() approach incurs a potentially excessive overhead.

我认为应该很明显,不同的启动条件将决定两种方法中哪一种是最有效的,如果性能是一个重要问题,则应考虑采用这种方法.

As I think should be obvious, different starting conditions would determine which of the two approaches is the most efficient, and should be considered if performance is a significant concern.

这篇关于Delphi TList&lt; T&gt;复制到另一个TList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆