annotate src/parallel_execution/RedBlackTree.agda @ 514:f86da73d611e

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