ForEach and traversal methods in OOP data structures 
Author Message
 ForEach and traversal methods in OOP data structures

Hello World.

        I've got a problem with traversal of data structures. I've implemented
many data types as descendants of the abstract  DataObjectType object.
DataObjectType provides a basic set
of methods, for example Traverse and ForEach:

        procedure ForEach (Task : pointer);
        procedure Traverse (Start, Stop : word; Task : pointer);

For each calls a procedure, method or function (far-declared maybe)
that performs a task. The declaration of the task procedure is;

        procedure MyTask (var Data; var Continue : boolean);

Data contains element data from the data structure. Continue is set
to TRUE if the procedure would like to abort the traversal, for
example when an element has been located.

Turbo Vision uses a strange way of calling an Action pointer that
is assumed to be far declared. I'm not very good at assembler
programming, so I've just guessed what Turbo Vision ForEach does. I
would like a similair procedure that uses the following;

        procedure ElementPointer (Index : word) : pointer;
                Method that returns pointer to element data
        procedure Elements : word;
                Number of elements

It should not be impossible. Could some one help me please? For
some reason so far my own invetions all have crashed my programs,
for example in the code;

        procedure MyTask (var Data : string; var Continue : boolean);
        begin
                GlobalObject.Display (Data);
                if EventHandler.UserAbort then Continue := FALSE;
        end;

        ...



/J




Wed, 18 Jun 1902 08:00:00 GMT  
 ForEach and traversal methods in OOP data structures

Quote:

> Hello World.

>         I've got a problem with traversal of data structures. I've implemented
> many data types as descendants of the abstract  DataObjectType object.
> DataObjectType provides a basic set
> of methods, for example Traverse and ForEach:
>      This may not be a direct answer to your question, but I hope it is helpful anyway.

     It sounds to me like your question can be rephrased as "How do I apply some operation
to each element of a complex data structure?"  I had encountered this problem once with a
structure I'd built as a tree-like set of linked lists, then having to compute some function
on each element of the list.  I basically wrote a routine called "DoOncePerItem" that was
passed the root of my data structure and a Procedure variable.  It was the job of "DoOncePerItem"
to know how to traverse my structure -- as it found each element, it applied the passed procedure
to that element.  [Did that make sense?].  It was all done with Standard Pascal, and worked very
nicely.

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 ForEach and traversal methods in OOP data structures

Hi,

Quote:
> It sounds to me like your question can be rephrased as "How do I apply some operation
> to each element of a complex data structure?"  I had encountered this problem once with a
> structure I'd built as a tree-like set of linked lists, then having to compute some function
> on each element of the list.  I basically wrote a routine called "DoOncePerItem" that was
> passed the root of my data structure and a Procedure variable.  It was the job of "DoOncePerItem"
> to know how to traverse my structure -- as it found each element, it applied the passed procedure
> to that element.  [Did that make sense?].  It was all done with Standard Pascal, and worked very
> nicely.

        The only difference between your procedure and the one I
would like in my program, is that I would like both a "procedure
ForEach (Task : pointer)" and a "procedure Traverse (Start, Stop
: word; Task : pointer)". That is, I would like to perform
tasks for all elements (ForEach) in straight forward order, _or_
I would like to traverse some of the elements backward (5, 1, Task)
or forward (1, 5, Task).

        It seems like you have to know a lot about assembler calls
to procedures and how methods are designed (I guess you'd have to
write an entire method in assembler). Turbo Vision uses a common
"CALL" instruction. But shouldn't it be "FAR CALL"?

Still looking help with this matter!!!

/J




Wed, 18 Jun 1902 08:00:00 GMT  
 ForEach and traversal methods in OOP data structures

Quote:

>> It sounds to me like your question can be rephrased as "How do Iapply some operation
>> to each element of a complex data structure?"  I had encounteredthis problem once with a
>> structure I'd built as a tree-like set of linked lists, then havingto compute some function
>> on each element of the list.  I basically wrote a routine called"DoOncePerItem" that was
>> passed the root of my data structure and a Procedure variable.  Itwas the job of "DoOncePerItem"
>> to know how to traverse my structure -- as it found each element, itapplied the passed procedure
>> to that element.  [Did that make sense?].  It was all done withStandard Pascal, and worked very
>> nicely.

>    The only difference between your procedure and the one I
>would like in my program, is that I would like both a "procedure
>ForEach (Task : pointer)" and a "procedure Traverse (Start, Stop
>: word; Task : pointer)". That is, I would like to perform
>tasks for all elements (ForEach) in straight forward order, _or_
>I would like to traverse some of the elements backward (5, 1, Task)
>or forward (1, 5, Task).

>    It seems like you have to know a lot about assembler calls
>to procedures and how methods are designed (I guess you'd have to
>write an entire method in assembler). Turbo Vision uses a common
>"CALL" instruction. But shouldn't it be "FAR CALL"?

>Still looking help with this matter!!!

Johan, I'm not sure if it will help any (mainly because I'm not
exactly certain what you want), but you might take a look at the
AVL Unit on my home page.

   http://members.gnn.com/rdonais/index.html

or by ftp://members.gnn.com/rdonais/tpascal/avl.zip

The AVL Collection contains the following iterators -

Procedure ForEach(Action: Pointer);
  { (Sub)Procedure Action(TheItem: Pointer); FAR;          }
  { Entire tree is traversed in order of LEFT, RIGHT, NODE }
  { Item and Index are *NOT* valid on any call to Action   }
  { and are not changed by this method                     }

Procedure Traverse(Fwd: Boolean; Action: Pointer);
  { (Sub)Procedure Action(Node: tpAVLnode; Depth: Word); FAR; }
  { Tree is traversed in order: from LEFT, NODE, RIGHT     }
  { if Fwd is TRUE, and from RIGHT, NODE, LEFT when FALSE. }
  { Initially Item is set to NIL and Index is set to zero. }
  { Item and Index are reset before each call to action.   }

Function  FirstThat(Action: Pointer): Boolean;
  { (Sub)Function Action(TheItem: Pointer); Boolean; FAR;  }
  { Tree is traversed starting with the left-most node     }
  { in order from LEFT, NODE, RIGHT until action is TRUE   }
  { or the entire tree has been processed                  }
  { Initially Item is set to nil and Index is set to Zero. }
  { Item and Index are set before each call to action      }

Function  LastThat(Action: Pointer): Boolean;
  { (Sub)Function Action(TheItem: Pointer); Boolean; FAR;  }
  { Tree is traversed starting with the right-most node    }
  { in order from RIGHT, NODE, LEFT until action is TRUE   }
  { or the entire tree has been processed                  }
  { Initially Item is set to nil and Index is set to Zero. }
  { Item and Index are set before each call to action      }

Function  NextThat(Subscript: Longint; Action: Pointer): Boolean;
  { (Sub)Function Action(TheItem: Pointer); Boolean; FAR;  }
  { tree is traversed starting with next higher Subscript  }
  { in order from LEFT, NODE, RIGHT until action is TRUE   }
  { or the entire tree has been processed                  }
  { Initially Item is set to nil and Index is set to Zero. }
  { Item and Index are set before each call to action      }

Function  PrevThat(Subscript: Longint; Action: Pointer): Boolean;
  { (Sub)Function Action(TheItem: Pointer); Boolean; FAR;  }
  { tree is traversed starting with next lower Subscript   }
  { in order from RIGHT, NODE, LEFT until action is TRUE   }
  { or the entire tree has been processed                  }
  { Initially Item is set to nil and Index is set to Zero. }
  { Item and Index are set before each call to action      }

You'll find that the ForEach iterator passes the item to the
user's action procedure and the node to its own "private"
procedures.  You'll also find a local procedure that acts as
a sub-procedure to two or more parent procedures.  Other than
these you shouldn't find much more out of the ordinary.  If
you have specific questions about the routines don't hesitate
to ask.

Note that Traversal() is passed a parameter that determines if
the tree is to be traversed from left-to-right or right-to-left.
Is this some thing like what you want?  Or are you looking to
have the navigation controlled by the action procedure?  

If I wanted a traversal that would start at a specified node
and then according to the result from Action(), would go left,
right, parent, quit, then I would declare an enumerated type
for the various actions and use it as the result type for a
new interator, based somewhat on the current NextThat or
PrevThat iterators.

    ...red



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. ForEach and traversal methods in OOP data structures

2. Define data structure for graph (abstract data structures)

3. HELP! ForEach/FirstThat methods

4. HELP! ForEach/FirstThat methods

5. TASM data structures to Pascal data types

6. Really posting data, what method?

7. Programming tool in OOP featuring data structures, streams ... whatever!!!

8. Programming tool in OOP featuring data structures, streams ... whatever!!!

9. data structure

10. D1: Help - large data structures

11. Data structure corruption.

12. Data Structure source code for all to share?

 

 
Powered by phpBB® Forum Software