annotate src/parallel_execution/RedBlackTree.agda @ 511:044c25475ed4

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