开发者

How not to add duplicate into TVirtualStringTree?

I have two VirtualStringTrees, first VST has been populated with data and I want to check the second VST and add nodes that are not already in the first VST. Or I want to add those nodes from second VST that are not duplicates of first VST.

procedure Tcreatevtform.copy2tosimvt(vt: Tvirtualstringtree);
var
  data: PMyRec;
  simvtdata: PMyRectF;
  rootnode, simvtnode: PVirtualNode;
  ty: string;
begin
  rootnode := vt.GetFirst;开发者_如何学编程 //vt is second virtualstringtree
  while Assigned(rootnode) do 
  begin
     data := vt.GetNodeData(rootnode);
     ty := data^.caption;
     if checksimduplicate(ty)=false then 
     begin
        simvtnode := similarvt.AddChild(nil); //similarvt is the first virtualstringtree
        simvtdata := similarvt.GetNodeData(simvtnode);
        simvtdata^.caption := data^.caption;
     end;
     rootnode := vt.GetNext(rootnode,false);
  end;
end;

function Tcreatevtform.checksimduplicate(t: string): boolean;
var
  data: PMyRectf;
  rootnode: PVirtualNode;
  typew: string;
begin
  Result := False;
  rootnode := similarvt.GetFirst;
  while Assigned(rootnode) do 
  begin
     data := similarvt.GetNodeData(rootnode);
     typew := data^.caption; // problem here, typew is always a constant or it is always the first
     if t=typew then 
     begin  
        // node's caption of vt (letter 'a' is the first node's caption in my
        // app. So this function is always false.
        Result := True;
        Break;
     end;
     similarvt.GetNext(rootnode, False);
  end;
end;

I am using D7.


Finding all items that are in one list but not a second can be done easily with a list comparison algorithm. Here's the basic idea:

  • Sort both lists
  • Take 2 index variables, one for each list, and start at the start of each list
  • Compare the two indexed items from each list according to the sort order.
    • If they're equal, then the same item is in both lists. Increment both indexes.
    • If the first item is less than the second one, then it's not in the second list. Increment the first index.
    • If the second item is less than the first one, then it's not in the first list. Increment the second index.
  • Repeat until you reach the end of one of the lists. All remaining items in the other list are not in the first list.

You can add actions to the equals case or either the first-unique or second-unique case to determine what to do in these cases. In your specific case, you'll want to make the second-unique case add an item to the VST. It might get more complicated if you have to preserve tree structure, but that's the basic idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜