🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #5356

Re: From Modula to Oberon

#5356
From: Glenn Bruns
Date: Wed, 23 Mar 1988 19:59
52 lines
2382 bytes
In article <1130@PT.CS.CMU.EDU> edw@IUS1.CS.CMU.EDU (Eddie Wyatt) writes:
>  In particular if you want generalized iteration say in C, its
>a simple matter of defining an iteration function for that particular
>data type.  Provide it a reset flag to start iteration,
>either a static var or extra field in the data type representing
>the current object and some termination object.
>
>    for (object = iter_func(data_struct,START); 
>	 object != TERMINATOR;
>	 object = iter_func(data_struct,NEXT)) {....}
>

I have also wrestled with implementing generalized iteration in C.
Eddie's solution is, I think, too restrictive, in that it prevents
one iterator to begin while another is in progress.  For example,
consider an iterator over a tree, used in a C for loop.  The loop
body may call some function that also needs to iterate over all
nodes in the tree.  

My solution (not original, I'm sure) is to provide the following
iterator functions for a data abstraction:

    x_ptr = first_x(abs, int_ptr)
    x_ptr =  next_x(abs, int_ptr)

Where 
    abs      is a data abstraction
    int_ptr  is a pointer to an integer, modified only by first_x() and next_x()
    x_ptr    is a pointer to an element of abs

A null object is returned by first_x() (or next_x()) when there is no
first (or next) object.  The variable 'int_ptr' is used by the iterator
to indicate a position in the data abstraction.

For example:

    for (node = first_node(tree, &i); node != NULL_NODE; node = next_node(tree, &i)) {
	    node_func(node);
    }

Sometimes it is easier to provide a function that traverses all
elements of a data abstraction, applying a given function to each
element.  So the previous example would become:

    tree_apply(tree, node_func);


-- 
Glenn Bruns 
MCC, Software Technology Program
arpa: bruns@mcc.com    
uucp: {seismo,harvard,gatech,pyramid}!ut-sally!im4u!milano!bruns
Technical Details
Message-ID: <396@catalina.SW.MCC.COM>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!ut-sally!im4u!milano!catalina!bruns
References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <3764@bloom-beacon.MIT.EDU> <1130@PT.CS.CMU.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!ut-sally!im4u!milano!catalina!bruns
From: bruns@catalina.SW.MCC.COM (Glenn Bruns)
Newsgroups: comp.lang.misc
Subject: Re: From Modula to Oberon
Message-ID: <396@catalina.SW.MCC.COM>
Date: 23 Mar 88 19:59:48 GMT
References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> <3764@bloom-beacon.MIT.EDU> <1130@PT.CS.CMU.EDU>
Reply-To: bruns@catalina.UUCP (Glenn Bruns)
Organization: MCC, Austin, Texas
Lines: 52