Initialize array in O(1) -- how is this trick called?
- by user946850
There is this pattern that trades performance of array access against the need to iterate it when clearing it. You keep a generation counter with each entry, and also a global generation counter. The "clear" operation increases the generation counter. On each access, you compare local vs. global generation counters; if they differ, the array has been reset.
This has come up in StackOverflow recently, but I don't remember if this trick has an official name. Does it?
One use case is Dijkstra's algorithm if only a tiny subset of the nodes has to be relaxed, and if this has to be done repeatedly.