Hacker News
points by Jtsummers 2 days ago | 0 comments
> What's the utility of this?

Constant memory usage. It uses a small amount of additional memory (tracking the current pixel, pixel value, and a couple temp variables used for calculations) over the image itself making it a very low overhead algorithm. You can basically get away with 5 more variables, 20 more bytes if they're all 4-byte values, using this approach compared to the much greater memory needs of a stack-based approach (either a stack data structure or recursive). And actually just 4 variables since the D and P variables don't overlap in lifetime.

EDIT: Light editing of the above for grammar and clarity.

[–] hatthew 2 days ago
But that's only assuming that your original data structure already has space for the algorithm to store metadata. If there's space that can be manipulated freely to store metadata, can't you just remove that space to save memory, and then use the extra space for a stack?