CSE 662 - Database Languages & Runtimes
Functional Data Structures
Sept 1, 2017
1
(Multiple diagrams from ‘Purely Functional Datastructures’ by Chris Okasaki)
Functional Data Structures Sept 1, 2017 (Multiple diagrams from - - PowerPoint PPT Presentation
Functional Data Structures Sept 1, 2017 (Multiple diagrams from Purely Functional Datastructures by Chris Okasaki) CSE 662 - Database Languages & Runtimes 1 Mutable vs Immutable X = [ Alice, Bob, Carol, Dave ] Alice Bob Carol
CSE 662 - Database Languages & Runtimes
Sept 1, 2017
1
(Multiple diagrams from ‘Purely Functional Datastructures’ by Chris Okasaki)
CSE 662 - Database Languages & Runtimes
2
X = [ Alice, Bob, Carol, Dave ]
Alice Bob Carol Dave
X[2]
Carol
X[2] := Eve
Eve
CSE 662 - Database Languages & Runtimes
3
X = [ Alice, Bob, Carol, Dave ]
Alice Bob Carol Dave
X[2] := Eve Thread 1 Thread 2
Eve Carol
? X[2]
CSE 662 - Database Languages & Runtimes
4
Can these problems be avoided?
CSE 662 - Database Languages & Runtimes
5
X = [ Alice, Bob, Carol, Dave ]
Alice Bob Carol Dave
X[2]
Carol
X[2] := Eve Don’t allow writes! But what if we need to update the structure?
CSE 662 - Database Languages & Runtimes
Carol Eve
6
Alice Bob Dave
Key Insight: Immutable components can be re-used!
CSE 662 - Database Languages & Runtimes
Carol Eve
7
Alice Bob Dave
Key Insight: Immutable components can be re-used!
CSE 662 - Database Languages & Runtimes
Carol Eve
8
Alice Bob Dave
Semantics are clearer: Exactly one ‘version’ at any time
CSE 662 - Database Languages & Runtimes
Eve Carol
9
Alice Bob Dave
Data is added, not replaced: No cache coherency problems
CSE 662 - Database Languages & Runtimes
is garbage collected.
a new version of the data structure)
10
(a.k.a. ‘Functional’ or ‘Persistent’ Data Structures)
CSE 662 - Database Languages & Runtimes
11
xs = pop(xs) ys = push(ys,1) Only xs and ys need to change
CSE 662 - Database Languages & Runtimes
12
zs = append(xs,ys)
This entire part needs to be rewritten
CSE 662 - Database Languages & Runtimes
13
CSE 662 - Database Languages & Runtimes
14
How would you implement update(list, index, value)
CSE 662 - Database Languages & Runtimes
15
Implement a set with: set init() boolean member(set, elem) set insert(set, elem)
CSE 662 - Database Languages & Runtimes
16
Can we do better?
CSE 662 - Database Languages & Runtimes
17
x = “expensive()” print x print x
Fast (just saving a ‘todo’) Slow (performing the ‘todo’) Fast (‘todo’ already done)
CSE 662 - Database Languages & Runtimes
18
Make it better!
CSE 662 - Database Languages & Runtimes
19
concatenate(a, b) { a’, front = pop(a) if a’ is empty return (front, b) else return (front, “concatenate(a’,b)”) } What is the time complexity of concatenate? What happens to reads?
CSE 662 - Database Languages & Runtimes
20
CSE 662 - Database Languages & Runtimes
21
CSE 662 - Database Languages & Runtimes
22
Preliminaries: Implement an efficient enqueue()/dequeue()
CSE 662 - Database Languages & Runtimes
23
enqueue(): Push onto ‘todo’ stack ‘current’ queue ‘todo’ stack dequeue(): Pop ‘current’ queue if empty, reverse ‘todo’ stack to make new ‘current’ queue What is the cost? What is the cost?
CSE 662 - Database Languages & Runtimes
24
enqueue(): Push onto ‘todo’ stack ‘current’ queue ‘todo’ stack dequeue(): Pop ‘current’ queue if empty, reverse ‘todo’ stack to make new ‘current’ queue push() is O(1) + 1 credit Pop is O(1); Reverse uses N credits for O(1) amortized