Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persistent vs immutable data structure

Tags:

Is there any difference in a persistent and immutable data structure? Wikipedia refers to immutable data structure when discussing persistence but I have a feeling there might be a subtle difference between the two.

like image 496
Bober02 Avatar asked Apr 05 '12 19:04

Bober02


People also ask

What is an immutable data structure?

Immutable data structure are data structures, like lists, arrays, sets etc., which cannot be changed, meaning that the values inside them can't be added, removed, moved or swapped.

What is persistence in data structure?

The data structure is fully persistent if every version can be both accessed and modified. If there is also a meld or merge operation that can create a new version from two previous versions, the data structure is called confluently persistent. Structures that are not persistent are called ephemeral.

What is persistent and Ephermal data structure?

∎ An ephemeral data structure is one for. which only one version is available at a time: after an update operation, the structure as it existed before the update is lost. ∎ A persistent structure is one where. multiple versions are simultaneously accessible: after an update, both old and new versions can be used.

What is persistent data example?

Persistent storage is any data storage device that retains data after power to that device is shut off. It is also sometimes referred to as non-volatile storage. Magnetic media, such as hard disk drives and tape are common types of persistent storage, as are the various forms of Optical media such as DVD.


2 Answers

Immutability is an implementation technique. Among other things, it provides persistence, which is an interface. The persistence API is something like:

  • version update(operation o, version v) performs operation o on version v, returning a new version. If the data structure is immutable, the new version is a new structure (that may share immutable parts of the old structure). If the data structure isn't immutable, the returned version might just be a version number. The version v remains a valid version, and it shouldn't change in any observe-able way because of this update - the update is only visible in the returned version, not in v.
  • data observe(query q, version v) observes a data structure at version v without changing it or creating a new version.

For more about these differences, see:

  • The chapter "Persistent data structures" by Haim Kaplan in Handbook on Data Structures and Applications
  • "Making Data Structures Persistent" by Driscoll et al.
  • The first lecture of the Spring 2012 incarnation of MIT's 6.851: Advanced Data Structures
like image 144
jbapple Avatar answered Sep 19 '22 12:09

jbapple


Yes, there is a difference. An immutable data structure cannot be modified in any way after its creation. The only way to effective modify it would be to make a mutable copy or something similar (e.g. slightly modifying the parameters you pass to the constructor of the new one). A persistent data structure, on the other hand, is mutable in the sense that the exposed API appears to allow changes to the data structure. In truth, though, any changes will retain a pointer to the existing data structure (and therefore every preceding structure); they only seem to mutate the data structure because the exposed API returns a new pointer that may include pointers to a subset of the previous data structure (in trees, e.g., we will point at the node whose subtree hasn't changed as a result of the operation).

like image 31
Sam Grondahl Avatar answered Sep 21 '22 12:09

Sam Grondahl