Delphi VirtualStringTree - 检查重复项?
是的,我知道我发布了很多问题,但那是因为我要么需要保证我做对了,我做错了什么,或者我完全无能为力,并且在文档中找不到任何内容。无论如何,
我正在尝试检查重复的节点。我想要这样做:
循环遍历我的节点,并比较每个节点的文本(记录),但如果我有很多节点,那不是太耗时和消耗内存吗?对此是否有更好的方法?
谢谢! - 杰夫。
编辑:感谢 Deltics,我成功了!如果有人有同样的问题,这里有一些工作代码,在 VST 中使用 2 级节点!
Procedure UncheckDuplicates;
Var
ParentNode,ChildNode : PVirtualNode;
I,J : Integer;
SL : TStringList;
SkypeID : String;
Begin
SL := TStringlist.Create;
try
ParentNode := frmMain.vtSkype.GetFirst;
for I := 0 to frmMain.vtSkype.RootNodeCount - 1 do
begin
ChildNode := ParentNode.FirstChild;
for J := 0 to ParentNode.ChildCount - 1 do
begin
if NodeIsChecked(ChildNode) then
begin
SkypeID := GetData(ChildNode).SkypeID;
if SL.IndexOf(SkypeID) <> -1 then
begin
ChildNode.CheckState := csUncheckedNormal;
end
else
begin
SL.Add(SkypeID);
end;
end;
ChildNode := ChildNode.NextSibling;
end;
ParentNode := ParentNode.NextSibling;
end;
finally
SL.Free;
end;
frmMain.vtSkype.Refresh;
End;
我不害怕分享我的代码,我对社区负有责任。 :)
Yeah, I know I post a lot of questions, but thats because I either need assurance that I am doing it right, what I am doing wrong, or if I am totally clueless, and cant find anything in the documentation. Anyways,
I am trying to check for duplicate nodes. Here is how I would want to do it:
Loop thru my nodes, and compare each single node's text (record), but if I got many nodes, wouldnt that be too time and memory consuming? Would there be a better approach for this?
Thanks!
- Jeff.
EDIT: Thanks to Deltics, I got it working! In case we have some people with the same question, here is some working code, using 2 levels of nodes in VST!
Procedure UncheckDuplicates;
Var
ParentNode,ChildNode : PVirtualNode;
I,J : Integer;
SL : TStringList;
SkypeID : String;
Begin
SL := TStringlist.Create;
try
ParentNode := frmMain.vtSkype.GetFirst;
for I := 0 to frmMain.vtSkype.RootNodeCount - 1 do
begin
ChildNode := ParentNode.FirstChild;
for J := 0 to ParentNode.ChildCount - 1 do
begin
if NodeIsChecked(ChildNode) then
begin
SkypeID := GetData(ChildNode).SkypeID;
if SL.IndexOf(SkypeID) <> -1 then
begin
ChildNode.CheckState := csUncheckedNormal;
end
else
begin
SL.Add(SkypeID);
end;
end;
ChildNode := ChildNode.NextSibling;
end;
ParentNode := ParentNode.NextSibling;
end;
finally
SL.Free;
end;
frmMain.vtSkype.Refresh;
End;
I am not afraid to share my code, I owe it to the community. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您在什么时候检查重复项。
如果是在您添加项目并且同时添加所有项目时,(或者如果可以/适当地将重复检查移动到填充树视图的点,而不是使用已经填充的树)然后维护已添加项目的列表可能是最简单的方法,例如假设您从简单的字符串列表中添加项目(在此插图代码中的字符串中):
It depends at what point you are checking for duplicates.
If it is at the point at which you are adding items and you are adding all items at the same time, (or if it is possible/appropriate to move you duplicate check to point at which the treeview is populated, rather than working with an already populated tree) then maintaining a list of already added items as you go could be the simplest way, e.g. assuming you are adding items from a simple stringlist (in strings in this illustration code):
通常,您会将所有字符串收集到一个列表中,然后对其进行排序。然后,您可以循环并检查相邻项目是否相等。
假设合理的排序算法是 O(n log n),而不是朴素算法,即 O(n^2)。如果你没有大量的物品,那么天真就可以完美地工作。
Normally you'd collect all your strings into a list and then sort it. You can then loop through and check adjacent items for equality.
That's O(n log n) assuming a reasonable sort algorithm as opposed to the naive algorithm which is O(n^2). If you don't have loads of items then the naive will work perfectly well though.
大卫的版本可以工作。如果您有 D2010 或更高版本,您还可以使用 DeHL 中的 Set 集合,它使用哈希来检查重复项,并可以在 O(n) 时间内处理您的列表。
David's version will work. If you have D2010 or later, you could also use a Set collection from DeHL, which uses a hash to check for duplicates and can process your list in O(n) time.