[Networkit] const and non-const iterator methods in Graph

Marvin Ritter marvin.ritter at gmail.com
Wed May 14 15:10:11 CEST 2014


I will change it in my dgraph branch which will be merged when the
DirectedGraph class is done. While creating the DirectedGraph class Klara
and I are going to refactor the Graph class anyway.

If anyone finds an example where the non-const method is needed mail me,
please.



On Wed, May 14, 2014 at 12:57 PM, Christian Staudt <christian.staudt at kit.edu
> wrote:

> Hi Marvin,
> I would definitely like to get rid of the annoying code redundancy of
> having const and non-const iterator methods, so I hope your argument is
> correct. And I believe it is.
>
> Even if the iterator method executes a lambda function which modifies the
> graph, the modification does not apply to the "this" pointer but the graph
> variable captured from the surrounding scope.
>
> I cannot think of a case in which we really need the non-const version.
> (Anybody?) I guess in the early days of the graph data structure we thought
> that we need const- and non-const versions of iterators without really
> thinking it through.
>
> So thank you for pointing this out, and go ahead. I suggest you make
> changes to the graph data structure in a separate branch so you can test
> them without much interference. When it's time to merge send me a pull
> request and an email.
>
> Christian
>
>
>
> Am 14.05.2014 um 12:37 schrieb Marvin Ritter <marvin.ritter at gmail.com>:
>
> > Hi,
> >
> > I have a short question regarding the iterator methods in the Graph
> class. There are all those nice methods called forNodes, forEdges,
> forEdgesOf ... and for all of them there are 2 definition, one normal one
> and another const one.
> >
> > I really like the use of const, but hy do we need the non-const
> versions?? All those iterator methods, do not change the graph, they just
> loop over something.
> >
> > So what happens if we remove the non-const versions? So that we only have
> > Graph.forNodes(L handle) const;
> > but not
> > Graph.forNodes(L handle);
> >
> > Then we still could do the following:
> >
> > Graph G(3);
> > G.forNodes([&](node v) { // we will use the const version of forNodes,
> because it is the only version available and non-const instances can use
> the const methods as well
> > if (v == 2) G.addNode(); // forNodes is const, but we use our G which
> isn't so we can still change G
> > });
> >
> > const Graph T(3);
> > T.forNodes([&](node v) { // we use the const version of forNodes
> > if (v == 2) T.addNode(); // would not work because T is const and
> addNode isn't
> > });
> >
> > So what case did I miss where we need the non-const version of forNodes?
> >
> > Thanks and best regards,
> > Marvin
> > <ATT00001.c>
>
>
> _______________________________________________
> NetworKit mailing list
> NetworKit at ira.uni-karlsruhe.de
> https://lists.ira.uni-karlsruhe.de/mailman/listinfo/networkit
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ira.uni-karlsruhe.de/mailman/private/networkit/attachments/20140514/d557901a/attachment.html>


More information about the NetworKit mailing list