Hacker News new | ask | show | jobs
by thechao 2237 days ago
> The fix for this was fairly straightforward - I just made the library keep a record of the previously visited IFDs and bail out if it found a loop.

If you just want to detect loops, keep a “+1” pointer that you use to increment through the data; also, keep a “+2” pointer that is advanced twice each time your “+1” pointer advances: either your “+2” pointer hits the end, or it becomes equal to your “+1” pointer — in which case you have a loop.

2 comments

Also known as the "Tortoise and Hare Algorithm!"

https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_Tortoi...

That's quite clever!