is it possible to possible to create an iterator from a callback interace?

A

aninnymouse

I'm using a library that provides the following function which
traverses the graph and calls a provided callback function on each
visited vertex:
void depth_first_search(Graph *graph, VertexCB callback, void *data);

Can somebody tell me how I can create a function that implements an
interator iterface using the above function. I'm hoping it's possible
to use it like so:
Vertex *v;
while (v = dfs_iterator()) { }
 
J

Jack Klein

I'm using a library that provides the following function which
traverses the graph and calls a provided callback function on each
visited vertex:
void depth_first_search(Graph *graph, VertexCB callback, void *data);

Can somebody tell me how I can create a function that implements an
interator iterface using the above function. I'm hoping it's possible
to use it like so:
Vertex *v;
while (v = dfs_iterator()) { }

The C language doesn't define anything called an "iterator". Are you
looking for comp.lang.c++, down the hall to the left? If not, you had
best define exactly what you mean by "iterator" in this context.
 
A

aninnymouse

I meant iterator as a design pattern (e.g. method of access
controllable by a loop). The callback approach will visit each vertex
and advance without any control. I need to control when to advance to
the next node. The naive approach would be to simply have the original
callback add the elements to a list and iterate over the list, but it's
possible there may be many elements, so that wouldn't be efficient.
Basically I want to have depth_first_search "pause" after calling the
callback, until I explicitly call it to resume the flow.
 
S

suresh

I meant iterator as a design pattern (e.g. method of access
controllable by a loop). The callback approach will visit each vertex
and advance without any control. I need to control when to advance to
the next node. The naive approach would be to simply have the original
callback add the elements to a list and iterate over the list, but it's
possible there may be many elements, so that wouldn't be efficient.
Basically I want to have depth_first_search "pause" after calling the
callback, until I explicitly call it to resume the flow.

That does not look like purely implementable in C. You need some
platform specific functions to accomplish this.
<OT>
The call back should wait on a semaphore and the resume function
should release the semaphore.
</OT>

If you explain your original problem little more (for example - what
do you want to do in the while loop) you might find the pure C solution
here.
 
K

Kenny McCormack

I meant iterator as a design pattern (e.g. method of access
controllable by a loop). The callback approach will visit each vertex
and advance without any control. I need to control when to advance to
the next node. The naive approach would be to simply have the original
callback add the elements to a list and iterate over the list, but it's
possible there may be many elements, so that wouldn't be efficient.
Basically I want to have depth_first_search "pause" after calling the
callback, until I explicitly call it to resume the flow.

Allow me to be the first to say this - and I say it from the deepness of my
heart, with all the kindness and love one has come to associate with the
helpful posts you get in this newsgroup:

Not portable. Can't discuss it here. Blah, blah, blah.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top