This could simplify and speed things up significantly:
- No need for the
FOR_EACH_TOGGLE/TOGGLE_IF_MORE_TO_COME/UNDO_PREVIOUS_TOGGLES construct.
- No need to call
Calloc()/Free() each call: can allocate on initialisation and free on finalisation.
- Probable algorithmic improvements by being able to cache useful information that persists between calls and tracks the network as it changes.