What is the most efficient implementation of arrays with functional updates?

前端 未结 4 1902
别跟我提以往
别跟我提以往 2020-12-28 23:56

I need an array-like data structure with the fastest possible functional update. I\'ve seen a few different implementation of flexible arrays that provide me with this prope

4条回答
  •  时光取名叫无心
    2020-12-29 00:39

    Jean-Cristophe Filliâtre has a very nice implementation of persistent arrays, that is described in the paper linked at the same page (which is about persistent union-find, of which persistent arrays are a core component). The code is directly available there.

    The idea is that "the last version" of the array is represented as an usual array, with O(1) access and update operations, and previous versions are represented as this last version, plus a list of the differences. If you try to access a previous version of the structure, the array is "rerooted" to apply the list of differences and present you again the efficient representation.

    This will of course not be O(1) under all workflows (if you constantly access and modify unrelated versions of the structure, you will pay rerooting costs frequently), but for the common workflow of mainly working with one version, and occasionally backtracking to an older version that becomes the "last version" again and gets the updates, this is very efficient. A very nice use of mutability hidden under a observationally pure interface.

提交回复
热议问题