annotate src/parallel_execution/RedBlackTree.agda @ 478:0223c07c3946

fix stack.agda
author ryokka
date Thu, 28 Dec 2017 19:08:04 +0900
parents ff4ab9add959
children 044c25475ed4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
1 module RedBlackTree where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
2
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
3 open import stack
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
4
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
5 record Tree {a t : Set} (treeImpl : Set) : Set where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
6 field
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
7 tree : treeImpl
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
8 putImpl : treeImpl -> a -> (treeImpl -> t) -> t
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
9 getImpl : treeImpl -> (treeImpl -> Maybe a -> t) -> t
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
10
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
11 open Tree
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
12
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
13
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
14 putTree : {a t : Set} -> Tree -> a -> (Tree -> t) -> t
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
15 putTree {a} {t} t0 d next = (putImpl t0) (tree t0) d (\t1 -> next (record t0 {tree = t1} ))
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
16
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
17 getTree : {a t : Set} -> Tree -> (Tree -> t) -> t
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
18 getTree {a} {t} t0 next = (getImpl t0) (tree t0) (\t1 -> next t0)
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
19
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
20
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
21 data Color : Set where
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
22 Red : Color
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
23 Black : Color
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
24
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
25 record Node (a : Set) : Set where
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
26 field
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
27 node : Element a
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
28 right : Maybe (Node a)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
29 left : Maybe (Node a)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
30 color : Color
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
31
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
32 record RedBlackTree (a : Set) : Set where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
33 field
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
34 root : Maybe (Node a)
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
35 stack : Stack
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
36
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
37 open RedBlackTree
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
38
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
39 insertNode : ?
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
40 insertNode tree datum next = get2 (stack tree) (\ s d1 d2 -> insertCase1 ( record { root = root tree; stack = s }) datum d1 d2 next)
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
41
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
42 putRedBlackTree : {Data t : Set} -> RedBlackTree Data -> Data -> (Code : RedBlackTree Data -> t) -> t
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
43 putRedBlackTree tree datum next with (root tree)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
44 ... | Nothing = insertNode tree datum next
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
45 ... | Just n = findNode tree datum n (\ tree1 -> insertNode tree1 datum next)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
46
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
47 findNode : {Data t : Set} -> RedBlackTree Data -> Data -> Node Data -> (Code : RedBlackTree Data (RedBlackTree Data -> t) -> t) -> t
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
48 findNode tree datum n next = pushStack (stack tree) n (\ s -> findNode1 (record tree {stack = s }) datum n next)
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
49
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
50 findNode1 : {Data t : Set} -> RedBlackTree Data -> Data -> Data -> (Code : RedBlackTree Data (RedBlackTree Data -> t) -> t) -> t
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
51 findNode1 tree datum n next with (compare datum n)
428
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
52 ... | EQ = popStack (tree stack) (\s d -> findNode3 d (record tree { root = just (record n {node = datum}); stack = s }) next)
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
53 ... | GT = findNode2 tree datum (right n) next
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
54 ... | LT = findNode2 tree datum (left n) next
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
55 where
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
56 findNode2 tree datum nothing next = insertNode tree datum next
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
57 findNode2 tree datum (just n) next = findNode (record tree {root = just n}) datum n next
428
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
58 findNode3 nothing tree next = next tree
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
59 findNode3 (just n) tree next =
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
60 popStack (tree stack) (\s d -> findNode3 d (record { root = record n {right = ? } }))
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
61
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
62
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
63 insertCase1 tree datum nothing grandparent next = next (record { root = ?; stack = createSingleLinkedStack })
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
64 insertCase1 tree datum (just parent) grandparent next = insertCase2 tree datum parent grandparent next
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
65
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
66 insertCase2 tree datum parent grandparent next with (color parent)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
67 ... | Red = insertCase3 tree datum parent grandparent next
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
68 ... | Black = next (record { root = ?; stack = createSingleLinkedStack })
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
69
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
70 insertCase3 tree datum parent grandparent next
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
71
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
72 getRedBlackTree : {a t : Set} -> RedBlackTree a -> (Code : RedBlackTree a -> (Maybe a) -> t) -> t
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
73 getRedBlackTree tree cs with (root tree)
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
74 ... | Nothing = cs tree Nothing
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
75 ... | Just d = cs stack1 (Just data1)
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
76 where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
77 data1 = datum d
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
78 stack1 = record { root = (next d) }