annotate src/parallel_execution/RedBlackTree.agda @ 512:95865cab040a

fix RedBlackTree.agda
author mir3636
date Thu, 04 Jan 2018 15:10:24 +0900
parents 044c25475ed4
children f2a3acc766b5
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
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
27 data CompareResult {n : Level } : Set n where
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
28 LT : CompareResult
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
29 GT : CompareResult
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
30 EQ : CompareResult
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
31
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
32 record Node (a k : Set) : Set where
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
33 field
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
34 key : k
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
35 value : a
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
36 right : Maybe (Node a)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
37 left : Maybe (Node a)
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
38 color : Color
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
39 open Node
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
40
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
41 record RedBlackTree (a k si : Set) : Set where
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
42 field
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
43 root : Maybe (Node a k )
511
044c25475ed4 fix stack.agda
mir3636
parents: 478
diff changeset
44 stack : Stack si
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
45 compare : k -> k -> CompareResult
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
46
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
47 open RedBlackTree
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
48
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
49 open Stack
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
50
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
51 insertCase3 : ?
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
52 insertCase3 = ? -- tree datum parent grandparent next
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
53
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
54 insertCase2 : {n m : Level } {t : Set m } {a : Set n} -> RedBlackTree ? ? -> a (Node a) (Node a) ? -> t
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
55 insertCase2 tree datum parent grandparent next with (color parent)
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
56 ... | Red = insertCase3 tree datum parent grandparent next
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
57 ... | Black = next (record { root = ?; stack = createSingleLinkedStack })
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
58
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
59 insertCase1 : {n m : Level } {t : Set m } {a : Set n} -> RedBlackTree ? ? -> a (Maybe (Node a) ) (Node a) ? -> t
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
60 insertCase1 tree datum Nothing grandparent next = next (record { root = ?; stack = createSingleLinkedStack })
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
61 insertCase1 tree datum (Just parent) grandparent next = insertCase2 tree datum parent grandparent next
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
62
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
63 insertNode : ?
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
64 insertNode tree datum next = get2Stack (stack tree) (\ s d1 d2 -> insertCase1 ( record { root = root tree; stack = s }) datum d1 d2 next)
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
65
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
66 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
67 findNode1 tree datum n next with (compare datum n)
428
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
68 ... | 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
69 ... | GT = findNode2 tree datum (right n) next
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
70 ... | LT = findNode2 tree datum (left n) next
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
71 where
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
72 findNode2 tree datum nothing next = insertNode tree datum next
427
07ccd411ad70 fix interface in agda
ryokka
parents: 425
diff changeset
73 findNode2 tree datum (just n) next = findNode (record tree {root = just n}) datum n next
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
74 findNode3 : ?
428
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
75 findNode3 nothing tree next = next tree
ff4ab9add959 fix findNode
ryokka
parents: 427
diff changeset
76 findNode3 (just n) tree next =
478
0223c07c3946 fix stack.agda
ryokka
parents: 428
diff changeset
77 popStack (tree stack) (\s d -> findNode3 d (record { root = record n {right = ? } }))
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
78
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
79 findNode : {Data t : Set} -> RedBlackTree Data -> Data -> Node Data -> (Code : RedBlackTree Data (RedBlackTree Data -> t) -> t) -> t
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
80 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
81
512
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
82 putRedBlackTree : {Data t : Set} -> RedBlackTree Data -> Data -> (Code : RedBlackTree Data -> t) -> t
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
83 putRedBlackTree tree datum next with (root tree)
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
84 ... | Nothing = insertNode tree datum next
95865cab040a fix RedBlackTree.agda
mir3636
parents: 511
diff changeset
85 ... | Just n = findNode tree datum n (\ tree1 -> insertNode tree1 datum next)
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
86
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
87 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
88 getRedBlackTree tree cs with (root tree)
417
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
89 ... | Nothing = cs tree Nothing
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
90 ... | Just d = cs stack1 (Just data1)
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
91 where
24c98ca207f4 add RedBlackTree.agda
mir3636
parents:
diff changeset
92 data1 = datum d
425
ea6353b6c4ef Add findNode to RedBlackTree.agda
innparusu
parents: 417
diff changeset
93 stack1 = record { root = (next d) }