I've written some code to manipulate sparse graphs for school work I'm doing, and I have an operation which requires me to loop over all the edges of a graph. The interface I need to implement is some function

int gimme_some_edges(graph* g, int k, int* I, int* J)

which puts the next k edges of the graph g into the arrays I and J, and returns the number of edges actually found in case there are fewer than k left. The iterator pattern seems like the tool for the job.

The biggest consideration I want to take account of is that my program is parallel; each thread is responsible for processing a disjoint subset of the graph's edges. In that case, the current edge of the iterator has to be private to each thread, and the iterator has to be stopped from continuing past some ending point into the edges for which another thread is responsible.

Do I want graph edge iterators to be internal or external to graph objects?

If the graph edge iterator is external and called by the client, there's more to do in the maintenance of this object but the parallelism is straight-forward. Moreover, they'll get deleted at the end of the edge processing function.

On the other hand, if the iterator is a part of the graph, my code looks simpler on the surface. But, I have to make sure I store an array of them, one for each thread. They're also persistent, so their state can get screwy before the next edge processing unless I'm circumspect (don't bet on it).

  • 1
    External, no question Nov 23, 2013 at 14:29
  • Hah evidently it's a question for me ;) are there other reasons why external is preferable than those I mentioned? Nov 23, 2013 at 23:38


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.