Sign in Join

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

Mailing lists / lazarus / 2010-06

Iterate TStringToStringTree

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Mattias Gaertner
to Juha Manninen: Hi Is there a cleaner way to iterate TStringToStringTree? This is the codetoolsstructs…
Hi Is there a cleaner way to iterate TStringToStringTree? This is the codetoolsstructs version but the LCL version is almost the same.

With fpc 2.5.1 you could write an enumerator:
http://wiki.lazarus.freepascal.org/for-in_loop

Show quoted text 14 lines
var MapToEdit: TStringToStringTree; Node: TAVLTreeNode; Item: PStringToStringTreeItem; begin ... Node:=MapToEdit.Tree.FindLowest; while Nodenil do begin Item:=PStringToStringTreeItem(Node.Data); // *** use Item^.Name and Item^.Value *** Node:=MapToEdit.Tree.FindSuccessor(Node); end;

Mattias

Reply by email

Juha Manninen

Hi

Is there a cleaner way to iterate TStringToStringTree? This is the codetoolsstructs version but the LCL version is almost the same.

var
  MapToEdit: TStringToStringTree;
  Node: TAVLTreeNode;
  Item: PStringToStringTreeItem;
begin
  ...
    Node:=MapToEdit.Tree.FindLowest;
    while Nodenil do begin
      Item:=PStringToStringTreeItem(Node.Data);
      // *** use Item^.Name and Item^.Value ***
      Node:=MapToEdit.Tree.FindSuccessor(Node);
    end;

Regards,
Juha

Reply by email

Graeme Geldenhuys
to Juha Manninen
Juha Manninen wrote
Is there a cleaner way to iterate TStringToStringTree? This is the codetoolsstructs version but the LCL version is almost the same.

With any FPC version your can simply create a new Iterator descendant. See the link below for a description. The code is included in the article mentioned.

http:/msg15082.html

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Juha Manninen
to Graeme Geldenhuys

Hi

With any FPC version your can simply create a new Iterator descendant. See the link below for a description. The code is included in the article mentioned.

Thanks.
Java shines with its container classes. Iterator interface and support for generics types are just cool. It all could be implemented with Object Pascal as well.

Juha

Reply by email

Graeme Geldenhuys
to Juha Manninen
Juha Manninen wrote 5 lines
Thanks. Java shines with its container classes. Iterator interface and support for generics types are just cool. It all could be implemented with Object Pascal as well.

Yes, java is great in that way, but it was decided not to extend the classes in the RTL with such functionality. So I implemented them as external classes that gets created by a Factory Method, using a lazyman's Singleton.

It can now be used as follows... Good news is that any iteration code over any list class (object list, string list, tree view) now looks the same. No need to know if the list class is 1-based or 0-based etc.. The container/list class can now change internally, and no iteration code needs to change either.

var
  itr: IStringIterator
begin
      itr := gIteratorFactory.StringIterator(lst);
      if Assigned(itr) then
      begin
        while itr.HasNext do
          Add(itr.Next);
      end;
      itr := nil;
end;

I think I made some changes to the units included with the article, so I attached the latest ones I have.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/ -------------- next part -------------- A non-text attachment was scrubbed... Name: iterator_intf.pas Type: text/x-pascal Size: 5146 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100615/ddae8c95/attachment-0002.bin> -------------- next part -------------- A non-text attachment was scrubbed... Name: iterator_impl.pas Type: text/x-pascal Size: 11732 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100615/ddae8c95/attachment-0003.bin>

Reply by email