pfds 10.1.2
TRANSCRIPT
![Page 1: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/1.jpg)
PFDS 10.1.2Binary Random-Access
List Revisited
@rf0444
![Page 2: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/2.jpg)
a Seq = NIL | CONS of a * (a * a) Seq
NIL
CONS 1 NIL
CONS 1 (CONS (2, 3) NIL)
CONS 1 (CONS (2,3) (CONS ((4,5),(6,7)) NIL))
![Page 3: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/3.jpg)
a Seq = NIL | CONS of a * (a * a) Seq
NIL
CONS 1 NIL
CONS 1 (CONS (2, 3) NIL)
CONS 1 (CONS (2,3) (CONS ((4,5),(6,7)) NIL))
0個
1個
3個
7個
![Page 4: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/4.jpg)
a Seq = NIL | CONS of a * (a * a) Seq
CONS 1 (CONS (2,3) (CONS ((4,5),(6,7)) NIL))
1個a
2個a * a
4個(a * a) * (a * a)
![Page 5: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/5.jpg)
a Tree = LEAF of a | NODE of a Tree * a Tree
a Digit = ZERO | ONE of a Tree
a RList = a Digit list
9.2.1 Binary Random-Access Lists
ZERO ONE ONE
31 2 4 5
![Page 6: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/6.jpg)
型だけ見ると下の構成もできるように見える
9.2.1 Binary Random-Access Lists
ZERO ONE ONE
43
5
21
サイズがおかしい
完全じゃない
![Page 7: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/7.jpg)
a Seq = NIL | ZERO of (a * a) Seq
| ONE of a * (a * a) Seq
10.1.2 Binary Random-AccessLists Revisited
ZERO
ONE1
ONE((2, 3), (4,5))
aa * a (a * a) * (a * a)
((a * a) * (a * a)) * ((a * a) * (a * a)) Seq
![Page 8: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/8.jpg)
head, tail
大体 9.2.1 と同じ
![Page 9: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/9.jpg)
lookup
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 5
![Page 10: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/10.jpg)
lookup
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 5
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 4ZERO
![Page 11: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/11.jpg)
lookup
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 4ZERO
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 2 の左
![Page 12: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/12.jpg)
lookup
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 2 の左
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 1 の左ZERO
![Page 13: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/13.jpg)
lookup
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 0 の右 の左
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 1 の左ZERO
![Page 14: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/14.jpg)
lookup
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 0 の左 の右 の左
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 0 の右 の左
![Page 15: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/15.jpg)
lookup
(((4, 5), (6, 7)), ((8, 9), (10, 11))) の左 の右 の左
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
lookup 0 の左 の右 の左
((4, 5), (6, 7)) の右 の左 (6, 7) の左
6
![Page 16: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/16.jpg)
update
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
update 5 20
![Page 17: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/17.jpg)
update
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . update 4 20ZERO
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
update 5 20
![Page 18: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/18.jpg)
update
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . update 4 20ZERO
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . update 2 (20, 7)
![Page 19: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/19.jpg)
update
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . update 2 (20, 7)
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . update 1 (20, 7)ZERO
![Page 20: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/20.jpg)
update
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . update 1 (20, 7)ZERO
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO .update 0 ((4, 5), (20, 7))
![Page 21: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/21.jpg)
update
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO .update 0 ((4, 5), (20, 7))
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO .update 0 (((4, 5), (20, 7)), ((8, 9), (10, 11)))
![Page 22: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/22.jpg)
update
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO .update 0 (((4, 5), (20, 7)), ((8, 9), (10, 11)))
![Page 23: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/23.jpg)
update
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))
![Page 24: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/24.jpg)
update
先頭要素が ZERO の時に毎回 lookup が走る
結果、update は O(log^2 n)
変更する値ではなく、変更する関数を渡すようにする
![Page 25: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/25.jpg)
update
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
update 5 20
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
fupdate (fn _ -> 20) 5
![Page 26: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/26.jpg)
update
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
fupdate (fn _ -> 20) 5
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . fupdate (fn _ -> 20) 4ZERO
![Page 27: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/27.jpg)
update
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . fupdate (fn (x, y) -> (20, y)) 2
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . fupdate (fn x -> 20) 4ZERO
![Page 28: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/28.jpg)
update
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) .fupdate (fn (x, y) -> (20, y)) 1
ZERO
ONE(2, 3)
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . fupdate (fn (x, y) -> (20, y)) 2
ZERO
![Page 29: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/29.jpg)
update
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) .fupdate (fn (x, y) -> (20, y)) 1
ZERO
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO .fupdate (fn (w, (x, y)) -> (w, (20, y))) 0
![Page 30: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/30.jpg)
update
ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO .fupdate (fn (w, (x, y)) -> (w, (20, y))) 0
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO .fupdate (fn ((w, (x, y)), z) -> ((w, (20, y)), z)) 0
![Page 31: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/31.jpg)
update
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO
ONE(((4, 5), (6, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO .fupdate (fn ((w, (x, y)), z) -> ((w, (20, y)), z)) 0
![Page 32: PFDS 10.1.2](https://reader033.vdocuments.us/reader033/viewer/2022052302/55d55b9fbb61eb044d8b46b2/html5/thumbnails/32.jpg)
update
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))
cons 1 . ZERO . cons (2, 3) . ZERO . ZERO
ZERO
ONE1
ONE(2, 3)
ONE(((4, 5), (20, 7)), ((8, 9), (10, 11)))