Docsity

Persistent Data Structures - Programming Languages and Techniques II - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Persistent Data Structures - Programming Languages and Techniques II - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Persistent Data Structures

Definitions

 An immutable data structure is one that, once created, cannot be modified  Immutable data structures can (usually) be copied , with modifications, to create a new version  The modified version takes up as much memory as the original version

 A persistent data structure is one that, when modified, retains both the old and the new values  Persistent data structures are effectively immutable, in that prior references to it do not see any change  Modifying a persistent data structure may copy part of the original, but the new version shares memory with the original

 This definition is unrelated to persistent storage, which means keeping a copy of data on disk between program executions

Lists

 Lists are the original persistent data structures, and are

very heavily used in functional programming

x y z

original

w

insert w delete x

As you can see, persistence is automatic with a

list, and requires no additional effort

Trees and binary trees

 Trees and binary trees can also be implemented in a

persistent fashion, though it takes a bit more work

5

A

B C

D E F G

H I J K L M N

A’

C’

G’

docsity.com

Tries

 A trie is like a binary search tree, only each node may have many children

 Tries are most often used with strings (and have up to 26 children per node)

 Each node of a 32-trie may have 32 children

Vector implementation I

 A persistent vector in Clojure is implemented as an N-level trie (N <= 7), where the root and internal nodes are arrays of 32 references, and the leaves are arrays of 32 values  The depth of the trie (1 to 7) is also kept as an instance value  For example, consider accessing location 5000 in a vector  5000 decimal is 1001110001000 binary

 To acess element 5000 in a trie of depth 4:  The binary number in group 4 (green) says to take the 0th^ reference  The binary number in group 3 (orange) says to take the 5th^ reference  The binary number in group 2 (green) says to take the 28th^ reference  The binary number in group 1 (blue) says to take the 8th^ value

Persistent Hash Map

 Since (in Java and Clojure) a hash code is a 32-bit integer, a hash map could be implemented just like a vector  For a vector, the additional space required for the trie structure is a reasonable proportion of the total space  For a hash map, the additional space required is not reasonable  There will be a large number of 32-element arrays which contain mostly nulls  The hard part is to use only as much space as needed

 Basic approach:  Use arrays size N <= 32, where N is the number of non-null children  Use a 32-bit word to indicate which children are actually present  For example: 000 1 0000000 1 000 1 0000000000 1 0 1 000 indicates 5 children  Find a fast function to map numbers in the range [0, 31] into the range [0, N)  Many processors have an instruction to count the number of 1 bits in a word