Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-09

VirtualTreeView, Absoluteindex example on freepascal.org

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jürgen Hestermann

On
http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus there is the following example procedure:

------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
Var
   Data: PTreeData;
   XNode: PVirtualNode;
   Rand: Integer;
Begin
   Randomize;
   Rand := Random(99);
   XNode:=VST.AddChild(nil);
   if VST.AbsoluteIndex(XNode) > -1 then
   Begin
    Data := VST.GetNodeData(Xnode);
    Data^.Column0:= 'One ' + IntToStr(Rand);
    Data^.Column1:= 'Two ' + IntToStr(Rand + 10);
    Data^.Column2:= 'Three ' + IntToStr(Rand - 10);
   End;
End;
------------------------------------------------

Can someone explain what the line

   if VST.AbsoluteIndex(XNode) > -1 then

actually means?
The method AbsoluteIndex gives back a Cardinal so how can it be less than 0? Therefore the if statement is completely useless because it is always true.

BTW: I was pointed to this because Lazarus (or FPC?) showed me a compiling message that this line will cause a type convertion to 64 bit (well done!).

Reply by email

Frederic Da Vitoria
to Jürgen Hestermann

2013/9/11 J?rgen Hestermann

Show quoted text 37 lines
On http://wiki.freepascal.org/**VirtualTreeview_Example_for_**Lazarus<http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus> there is the following example procedure: ------------------------------**------------------ procedure TForm1.Button1Click(Sender: TObject); Var Data: PTreeData; XNode: PVirtualNode; Rand: Integer; Begin Randomize; Rand := Random(99); XNode:=VST.AddChild(nil); if VST.AbsoluteIndex(XNode) > -1 then Begin Data := VST.GetNodeData(Xnode); Data^.Column0:= 'One ' + IntToStr(Rand); Data^.Column1:= 'Two ' + IntToStr(Rand + 10); Data^.Column2:= 'Three ' + IntToStr(Rand - 10); End; End; ------------------------------**------------------ Can someone explain what the line if VST.AbsoluteIndex(XNode) > -1 then actually means? The method AbsoluteIndex gives back a Cardinal so how can it be less than 0? Therefore the if statement is completely useless because it is always true. BTW: I was pointed to this because Lazarus (or FPC?) showed me a compiling message that this line will cause a type convertion to 64 bit (well done!).

I didn't look at the code, but usually index = -1 means that the item was not found. So maybe AbsoluteIndex's result should actually be signed? In this example, I guess the test is to avoid a bug if AddChild does not manage to insert the root node. I don't know if AddChild could create a node without inserting, but this is what the code seems to be testing.

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130912/25845b1a/attachment.html>

Reply by email

Jürgen Hestermann
to Frederic Da Vitoria
Frederic Da Vitoria wrote 7 lines
Can someone explain what the line if VST.AbsoluteIndex(XNode) > -1 then actually means? I didn't look at the code, but usually index = -1 means that the item was not found. So maybe AbsoluteIndex's result should actually be signed?

And how would it ever reach the value -1?
Obviously there is no code that sets it to this value, otherwise it would not compile with Cardinal type.

In this example, I guess the test is to avoid a bug if AddChild does not manage to insert the root node. I don't know if AddChild could create a node without inserting, but this is what the code seems to be testing.

But it is a useless "if" statement that doesn't test anything.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130912/e6c4ed69/attachment.html>

Reply by email

Frederic Da Vitoria
to Jürgen Hestermann

2013/9/12 J?rgen Hestermann

Show quoted text 16 lines
Am 2013-09-12 14:50, schrieb Frederic Da Vitoria: Can someone explain what the line > if VST.AbsoluteIndex(XNode) > -1 then > actually means? > I didn't look at the code, but usually index = -1 means that the item was not found. So maybe AbsoluteIndex's result should actually be signed? And how would it ever reach the value -1? Obviously there is no code that sets it to this value, otherwise it would not compile with Cardinal type.

Once again, I did not look at the code. My remarks only come from my memories of how VirtualTreeView worked with Delphi 6. The code has obviously much changed since. I was only attempting to answer to your question: this is probably the reason. Is it still working? Obviously not. What should be done? I see 2 options: either restore the old functionality if needed or (if indeed this test never had any real use) remove this test from the example. How does code handle when AbsoluteIndex does not find an item? I just took a I read the code correctly, if the parameter is the root, AbsoluteIndex will answer 0. If the parameter is not found, AbsoluteIndex will answer 0 too. Is this deliberate?

Show quoted text 6 lines
In this example, I guess the test is to avoid a bug if AddChild does not manage to insert the root node. I don't know if AddChild could create a node without inserting, but this is what the code seems to be testing. But it is a useless "if" statement that doesn't test anything.

Now yes. Once upon a time maybe not.

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130912/11a2f083/attachment.html>

Reply by email

Jürgen Hestermann
to Frederic Da Vitoria
Frederic Da Vitoria wrote
My remarks only come from my memories of how VirtualTreeView worked with Delphi 6. The code has obviously much changed since.
Are you sure? The function is quite simple:
---------------------------------------------------------------------
function TBaseVirtualTree.AbsoluteIndex(Node: PVirtualNode) : Cardinal;
begin
Result := 0;
while Assigned(Node) and (NodeFRoot) do
    begin
    if not (vsInitialized in Node.States) then
       InitNode(Node);
    if Assigned(Node.PrevSibling) then
       begin // if there's a previous sibling then add its total count to the result
       Node := Node.PrevSibling;
       Inc(Result, Node.TotalCount);
       end
     else
       begin
       Node := Node.Parent;
       if NodeFRoot then
          Inc(Result);
       end;
    end;
end;
---------------------------------------------------------------------
It's hard to believe that there was a version of VirtualTreeView
that used signed intergers *and* had code to set it to -1 *and*
then someone has changed it to a non-signed integer *and*
also removed or changed the code to now set it to zero.
I see 2 options: either restore the old functionality if needed

I don't believe that an "old functionality" exits. It looks as if it was like that from the beginning.

If I read the code correctly, if the parameter is the root, AbsoluteIndex will answer 0. If the parameter is not found, AbsoluteIndex will answer 0 too.

Yes. There are 3 cases where the result is 0:
1.) Node is nil
2.) Node is the (invisible) root node
3.) Node is the first (visible) node

All of them result in zero so these cases cannot be distinguished by the function result.

Is this deliberate?

That's the question.
But I don't think that it was different at any time. And it seems that as long as this is not changed in VirtualTreeView the if statement in the example on the Free Pascal page is nonsense.

Reply by email

Frederic Da Vitoria
to Jürgen Hestermann

2013/9/12 J?rgen Hestermann

Show quoted text 34 lines
Am 2013-09-12 17:51, schrieb Frederic Da Vitoria: > My remarks only come from my memories of how VirtualTreeView worked with Delphi 6. > The code has obviously much changed since. Are you sure? The function is quite simple: ------------------------------**------------------------------**--------- function TBaseVirtualTree.**AbsoluteIndex(Node: PVirtualNode) : Cardinal; begin Result := 0; while Assigned(Node) and (NodeFRoot) do begin if not (vsInitialized in Node.States) then InitNode(Node); if Assigned(Node.PrevSibling) then begin // if there's a previous sibling then add its total count to the result Node := Node.PrevSibling; Inc(Result, Node.TotalCount); end else begin Node := Node.Parent; if NodeFRoot then Inc(Result); end; end; end; ------------------------------**------------------------------**--------- It's hard to believe that there was a version of VirtualTreeView that used signed intergers *and* had code to set it to -1 *and* then someone has changed it to a non-signed integer *and* also removed or changed the code to now set it to zero.

No, I am not sure, as I stated in my first answer, I am answering from memory, all my answers here are mainly guesses. Either my first assumption is right or maybe whoever wrote the the button code example thought that VirtualTreeview behaved like IndexOf (which is what I would have expected too).

If you are interested, I still have an old version from 2001 on a backup , I could check it.

I see 2 options: either restore the old functionality if needed I don't believe that an "old functionality" exits. It looks as if it was like that from the beginning.

Well then, I can add a variant to my first option: create the functionality (since IMO it should be there).

Show quoted text 19 lines
If I read the code correctly, > if the parameter is the root, AbsoluteIndex will answer 0. > If the parameter is not found, AbsoluteIndex will answer 0 too. Yes. There are 3 cases where the result is 0: 1.) Node is nil 2.) Node is the (invisible) root node 3.) Node is the first (visible) node All of them result in zero so these cases cannot be distinguished by the function result. > Is this deliberate? That's the question. But I don't think that it was different at any time. And it seems that as long as this is not changed in VirtualTreeView the if statement in the example on the Free Pascal page is nonsense.

In the current state of the code, I agree it is useless.

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130912/21f42479/attachment.html>

Reply by email