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

Christian Staudt christian.staudt at kit.edu
Wed May 14 12:57:16 CEST 2014


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>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.ira.uni-karlsruhe.de/mailman/private/networkit/attachments/20140514/b42c9fb6/attachment.sig>


More information about the NetworKit mailing list