DATA STRUCTURE IN ELIXIR
PRESENTED BY MASATAM
ABOUT ME
▶ Name: MasaTam
▶ twitter: @masatam81
▶ Company: Retrieva, Inc.
▶ Academic: M.Eng in CS
at Oregon State Univ.
▶ Area(now): Speech Recognition, NLP
▶ Area(past): System dev, Game dev
DATA STRUCTURE
▶ Algorithm + Data Structure = programs
(Niklaus Wirth, 1976)
▶ “Purely Functional Data Structures” by Chris Okasaki
QUEUE
•FIFO data structure
QUEUE
•FIFO data structure
• push
QUEUE
•FIFO data structure
• push
1
QUEUE
•FIFO data structure
• push
1
QUEUE
•FIFO data structure
• push
1
2
QUEUE
•FIFO data structure
• push
1 2
3
QUEUE
•FIFO data structure
• push
1 2 3
QUEUE
•FIFO data structure
• pop
1 2 3
QUEUE
•FIFO data structure
• pop
2 3
1
QUEUE
•Adding a new item into the back of list
QUEUE
•Adding a new item into the back of list
QUEUE
•Adding a new item into the back of list
•Reverse the list?
QUEUE
•Adding a new item into the back of list
•Reverse the list?
• Not too bad if poped only after pushing all
items
QUEUE
•Solution: use two lists
OUT
IN
QUEUE
•Solution: use two lists
• push
OUT
IN
QUEUE
•Solution: use two lists
• push
OUT
IN
1
QUEUE
•Solution: use two lists
• push
OUT
IN
12
QUEUE
•Solution: use two lists
• push
OUT
IN
2 1
QUEUE
•Solution: use two lists
• pop
OUT
IN
2 1
QUEUE
•Solution: use two lists
• pop
OUT
IN
1 2
reverse
QUEUE
•Solution: use two lists
• pop
OUT
IN
21
QUEUE
•Solution: use two lists
• push
OUT
IN
2
3
QUEUE
•Solution: use two lists
• pop
OUT
IN
3
2
QUEUE
•Performance
QUEUE
•Performance
• Big-O
Push Pop
List
Queue
QUEUE
•Performance
• Big-O
Push Pop
List O(n)
Queue O(1)
QUEUE
•Performance
• Big-O
Push Pop
List O(n) O(1)
Queue O(1) O(1)*
* amortized
QUEUE
•Performance
• Execution time
Push Pop
List
Queue
QUEUE
•Performance
• Execution time
Push Pop
List 18.753sec
(100_000)
0.25sec
(10_000_000)
Queue 1.011sec
(10_000_000)
1.198sec
(10_000_000)
QUEUE
•Actually, Erlang employs this way!
So, using :queue is the easiest way
ERLANG QUEUE
otp/lib/stdlib/src/queue.erl
:queue
ERLANG QUEUE
otp/lib/stdlib/src/queue.erl
:queue
ERLANG QUEUE REVERSE
OTHER DATA STRUCTURE PREPARED
• Red-Black tree
• SplayHeap
TIPS
• BenchFella
• to mesure the performance,
especially executuin times.
TIPS
• Avoid to use structure if possible
TIPS
• Tail call
TIPS
•How to add comparator
CONCLUSION
• Lists are not always the best choice.
• Use appropriate one deppending on your needs.
• check otp/stdlib/src to find the best data structure.
• Source code
https://github.com/muramasa8191/dataStructureEx

Data Structure in Elixir