thocevar / orca

ORbit Counting Algorithm
GNU General Public License v3.0
35 stars 3 forks source link

Preprocessor macro: unclear behavior #1

Closed Becheler closed 2 years ago

Becheler commented 2 years ago

Hi! Thanks for this cool algorithm! I'm trying to understand what it does, and specifically the following line:

#define common3_get(x) (((common3_it = common3.find(x)) != common3.end()) ? (common3_it->second) : 0)

Would it be possible to have a bit of context/explanation around this particular line of code? I don't understand why an iterator on a std::map would be affected the value 0?

Thanks!

thocevar commented 2 years ago

The common3 is a map that stores the number of nodes that are adjacent to some triplets of nodes. Macro common3_get looks up the key (triplet of nodes) in the map and stores it along the way to avoid another lookup later. The returned value is either the stored value in the map or 0 if the triplet is not present in the map.

A simple indexing common3[x] could be used, which would return a default 0 initialized element, but it would also create a new entry in the map. The goal of this macro is to avoid such inflation of the data structure due to lookups of absent elements.

Becheler commented 2 years ago

Ok, thank you for your explanation, that helps a lot!