What/where are the practical uses of the partial_sum
algorithm in STL?
What are some other interesting/non-trivial examples or use-cases?
I used it to reduce memory usage of a simple mark-sweep garbage collector in my toy lambda calculus interpreter.
The GC pool is an array of objects of identical size. The goal is to eliminate objects that aren't linked to other objects, and condense the remaining objects into the beginning of the array. Since the objects are moved in memory, each link needs to be updated. This necessitates an object remapping table.
partial_sum
allows the table to be stored in compressed format (as little as one bit per object) until the sweep is complete and memory has been freed. Since the objects are small, this significantly reduces memory use.
remove_if
to condense the marked objects to the beginning of the pool.partial_sum
over the Boolean values to generate a table of pointers/indexes into the new pool.
It's especially friendly to the data cache to put the remap table in the just-freed, thus still hot, memory.