Mercurial > hg > Gears > GearsAgda
annotate RedBlackTree.agda @ 545:b180dc78abcf
add someTest
author | ryokka |
---|---|
date | Fri, 12 Jan 2018 18:30:05 +0900 |
parents | 1595dd84fc3e |
children | bc3208d510cd |
rev | line source |
---|---|
417 | 1 module RedBlackTree where |
2 | |
3 open import stack | |
533 | 4 open import Level hiding (zero) |
511 | 5 record TreeMethods {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where |
6 field | |
7 putImpl : treeImpl -> a -> (treeImpl -> t) -> t | |
8 getImpl : treeImpl -> (treeImpl -> Maybe a -> t) -> t | |
9 open TreeMethods | |
10 | |
11 record Tree {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.⊔ n) where | |
417 | 12 field |
13 tree : treeImpl | |
513 | 14 treeMethods : TreeMethods {n} {m} {a} {t} treeImpl |
15 putTree : a -> (Tree treeImpl -> t) -> t | |
511 | 16 putTree d next = putImpl (treeMethods ) tree d (\t1 -> next (record {tree = t1 ; treeMethods = treeMethods} )) |
513 | 17 getTree : (Tree treeImpl -> Maybe a -> t) -> t |
511 | 18 getTree next = getImpl (treeMethods ) tree (\t1 d -> next (record {tree = t1 ; treeMethods = treeMethods} ) d ) |
427 | 19 |
478 | 20 open Tree |
21 | |
513 | 22 data Color {n : Level } : Set n where |
425 | 23 Red : Color |
24 Black : Color | |
25 | |
512 | 26 data CompareResult {n : Level } : Set n where |
27 LT : CompareResult | |
28 GT : CompareResult | |
29 EQ : CompareResult | |
30 | |
513 | 31 record Node {n : Level } (a k : Set n) : Set n where |
32 inductive | |
425 | 33 field |
512 | 34 key : k |
35 value : a | |
513 | 36 right : Maybe (Node a k) |
37 left : Maybe (Node a k) | |
514 | 38 color : Color {n} |
512 | 39 open Node |
425 | 40 |
543 | 41 record RedBlackTree {n m : Level } {t : Set m} (a k : Set n) : Set (m Level.⊔ n) where |
417 | 42 field |
514 | 43 root : Maybe (Node a k) |
543 | 44 nodeStack : SingleLinkedStack (Node a k) |
514 | 45 compare : k -> k -> CompareResult {n} |
425 | 46 |
417 | 47 open RedBlackTree |
48 | |
543 | 49 open SingleLinkedStack |
512 | 50 |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
51 -- |
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
52 -- put new node at parent node, and rebuild tree to the top |
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
53 -- |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
54 {-# TERMINATING #-} -- https://agda.readthedocs.io/en/v2.5.3/language/termination-checking.html |
543 | 55 replaceNode : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
56 replaceNode {n} {m} {t} {a} {k} tree s n0 next = popSingleLinkedStack s ( | |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
57 \s parent -> replaceNode1 s parent) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
58 where |
543 | 59 replaceNode1 : SingleLinkedStack (Node a k) -> Maybe ( Node a k ) -> t |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
60 replaceNode1 s Nothing = next ( record tree { root = Just (record n0 { color = Black}) } ) |
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
61 replaceNode1 s (Just n1) with compare tree (key n1) (key n0) |
540 | 62 ... | EQ = next tree |
541 | 63 ... | GT = replaceNode tree s ( record n1 { left = Just n0 } ) next |
64 ... | LT = replaceNode tree s ( record n1 { right = Just n0 } ) next | |
478 | 65 |
525 | 66 |
543 | 67 rotateRight : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> |
68 (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> t) -> t | |
69 rotateRight {n} {m} {t} {a} {k} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 -> rotateRight1 tree s n0 parent rotateNext) | |
530 | 70 where |
543 | 71 rotateRight1 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> |
72 (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> t) -> t | |
73 rotateRight1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext with n0 | |
532 | 74 ... | Nothing = rotateNext tree s Nothing n0 |
530 | 75 ... | Just n1 with parent |
532 | 76 ... | Nothing = rotateNext tree s (Just n1 ) n0 |
530 | 77 ... | Just parent1 with left parent1 |
532 | 78 ... | Nothing = rotateNext tree s (Just n1) Nothing |
530 | 79 ... | Just leftParent with compare tree (key n1) (key leftParent) |
532 | 80 ... | EQ = rotateNext tree s (Just n1) parent |
81 ... | _ = rotateNext tree s (Just n1) parent | |
530 | 82 |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
83 |
543 | 84 rotateLeft : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> |
85 (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> t) -> t | |
86 rotateLeft {n} {m} {t} {a} {k} tree s n0 parent rotateNext = getSingleLinkedStack s (\ s n0 -> rotateLeft1 tree s n0 parent rotateNext) | |
530 | 87 where |
543 | 88 rotateLeft1 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> |
89 (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> t) -> t | |
90 rotateLeft1 {n} {m} {t} {a} {k} tree s n0 parent rotateNext with n0 | |
532 | 91 ... | Nothing = rotateNext tree s Nothing n0 |
530 | 92 ... | Just n1 with parent |
532 | 93 ... | Nothing = rotateNext tree s (Just n1) Nothing |
530 | 94 ... | Just parent1 with right parent1 |
532 | 95 ... | Nothing = rotateNext tree s (Just n1) Nothing |
530 | 96 ... | Just rightParent with compare tree (key n1) (key rightParent) |
532 | 97 ... | EQ = rotateNext tree s (Just n1) parent |
98 ... | _ = rotateNext tree s (Just n1) parent | |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
99 |
530 | 100 {-# TERMINATING #-} |
543 | 101 insertCase5 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Node a k -> Node a k -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
102 insertCase5 {n} {m} {t} {a} {k} tree s n0 parent grandParent next = pop2SingleLinkedStack s (\ s parent grandParent -> insertCase51 tree s n0 parent grandParent next) | |
530 | 103 where |
543 | 104 insertCase51 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
105 insertCase51 {n} {m} {t} {a} {k} tree s n0 parent grandParent next with n0 | |
532 | 106 ... | Nothing = next tree |
107 ... | Just n1 with parent | grandParent | |
108 ... | Nothing | _ = next tree | |
109 ... | _ | Nothing = next tree | |
110 ... | Just parent1 | Just grandParent1 with left parent1 | left grandParent1 | |
111 ... | Nothing | _ = next tree | |
112 ... | _ | Nothing = next tree | |
113 ... | Just leftParent1 | Just leftGrandParent1 | |
114 with compare tree (key n1) (key leftParent1) | compare tree (key leftParent1) (key leftGrandParent1) | |
115 ... | EQ | EQ = rotateRight tree s n0 parent | |
116 (\ tree s n0 parent -> insertCase5 tree s n0 parent1 grandParent1 next) | |
117 ... | _ | _ = rotateLeft tree s n0 parent | |
118 (\ tree s n0 parent -> insertCase5 tree s n0 parent1 grandParent1 next) | |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
119 |
543 | 120 insertCase4 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> Node a k -> Node a k -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
121 insertCase4 {n} {m} {t} {a} {k} tree s n0 parent grandParent next | |
528 | 122 with (right parent) | (left grandParent) |
532 | 123 ... | Nothing | _ = insertCase5 tree s (Just n0) parent grandParent next |
124 ... | _ | Nothing = insertCase5 tree s (Just n0) parent grandParent next | |
528 | 125 ... | Just rightParent | Just leftGrandParent with compare tree (key n0) (key rightParent) | compare tree (key parent) (key leftGrandParent) |
543 | 126 ... | EQ | EQ = popSingleLinkedStack s (\ s n1 -> rotateLeft tree s (left n0) (Just grandParent) |
532 | 127 (\ tree s n0 parent -> insertCase5 tree s n0 rightParent grandParent next)) |
530 | 128 ... | _ | _ = insertCase41 tree s n0 parent grandParent next |
129 where | |
543 | 130 insertCase41 : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> Node a k -> Node a k -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
131 insertCase41 {n} {m} {t} {a} {k} tree s n0 parent grandParent next | |
530 | 132 with (left parent) | (right grandParent) |
532 | 133 ... | Nothing | _ = insertCase5 tree s (Just n0) parent grandParent next |
134 ... | _ | Nothing = insertCase5 tree s (Just n0) parent grandParent next | |
530 | 135 ... | Just leftParent | Just rightGrandParent with compare tree (key n0) (key leftParent) | compare tree (key parent) (key rightGrandParent) |
543 | 136 ... | EQ | EQ = popSingleLinkedStack s (\ s n1 -> rotateRight tree s (right n0) (Just grandParent) |
532 | 137 (\ tree s n0 parent -> insertCase5 tree s n0 leftParent grandParent next)) |
138 ... | _ | _ = insertCase5 tree s (Just n0) parent grandParent next | |
527 | 139 |
532 | 140 colorNode : {n : Level } {a k : Set n} -> Node a k -> Color -> Node a k |
141 colorNode old c = record old { color = c } | |
527 | 142 |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
143 {-# TERMINATING #-} |
543 | 144 insertNode : {n m : Level } {t : Set m } {a k : Set n} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
145 insertNode {n} {m} {t} {a} {k} tree s n0 next = get2SingleLinkedStack s (insertCase1 n0) | |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
146 where |
543 | 147 insertCase1 : Node a k -> SingleLinkedStack (Node a k) -> Maybe (Node a k) -> Maybe (Node a k) -> t -- placed here to allow mutual recursion |
519
0a723e418b2a
add some more directives in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
518
diff
changeset
|
148 -- http://agda.readthedocs.io/en/v2.5.2/language/mutual-recursion.html |
543 | 149 insertCase3 : SingleLinkedStack (Node a k) -> Node a k -> Node a k -> Node a k -> t |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
150 insertCase3 s n0 parent grandParent with left grandParent | right grandParent |
528 | 151 ... | Nothing | Nothing = insertCase4 tree s n0 parent grandParent next |
152 ... | Nothing | Just uncle = insertCase4 tree s n0 parent grandParent next | |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
153 ... | Just uncle | _ with compare tree ( key uncle ) ( key parent ) |
528 | 154 ... | EQ = insertCase4 tree s n0 parent grandParent next |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
155 ... | _ with color uncle |
543 | 156 ... | Red = pop2SingleLinkedStack s ( \s p0 p1 -> insertCase1 ( |
532 | 157 record grandParent { color = Red ; left = Just ( record parent { color = Black } ) ; right = Just ( record uncle { color = Black } ) }) s p0 p1 ) |
528 | 158 ... | Black = insertCase4 tree s n0 parent grandParent next |
543 | 159 insertCase2 : SingleLinkedStack (Node a k) -> Node a k -> Node a k -> Node a k -> t |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
160 insertCase2 s n0 parent grandParent with color parent |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
161 ... | Black = replaceNode tree s n0 next |
532 | 162 ... | Red = insertCase3 s n0 parent grandParent |
163 insertCase1 n0 s Nothing Nothing = next tree | |
164 insertCase1 n0 s Nothing (Just grandParent) = next tree | |
538
5c001e8ba0d5
add redBlackTreeTest.agda test5,test51. but not work
ryokka
parents:
537
diff
changeset
|
165 insertCase1 n0 s (Just parent) Nothing = replaceNode tree s (colorNode n0 Black) next |
532 | 166 insertCase1 n0 s (Just parent) (Just grandParent) = insertCase2 s n0 parent grandParent |
528 | 167 |
531 | 168 ---- |
169 -- find node potition to insert or to delete, the pass will be in the stack | |
170 -- | |
543 | 171 findNode : {n m : Level } {a k : Set n} {t : Set m} -> RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> (Node a k) -> (Node a k) -> (RedBlackTree {n} {m} {t} a k -> SingleLinkedStack (Node a k) -> Node a k -> t) -> t |
172 findNode {n} {m} {a} {k} {t} tree s n0 n1 next = pushSingleLinkedStack s n1 (\ s -> findNode1 s n1) | |
417 | 173 where |
543 | 174 findNode2 : SingleLinkedStack (Node a k) -> (Maybe (Node a k)) -> t |
515 | 175 findNode2 s Nothing = next tree s n0 |
176 findNode2 s (Just n) = findNode tree s n0 n next | |
543 | 177 findNode1 : SingleLinkedStack (Node a k) -> (Node a k) -> t |
515 | 178 findNode1 s n1 with (compare tree (key n0) (key n1)) |
543 | 179 ... | EQ = popSingleLinkedStack s ( \s _ -> next tree s (record n1 { key = key n1 ; value = value n1 } ) ) |
515 | 180 ... | GT = findNode2 s (right n1) |
181 ... | LT = findNode2 s (left n1) | |
425 | 182 |
183 | |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
184 leafNode : {n : Level } {a k : Set n} -> k -> a -> Node a k |
515 | 185 leafNode k1 value = record { |
186 key = k1 ; | |
187 value = value ; | |
188 right = Nothing ; | |
189 left = Nothing ; | |
532 | 190 color = Red |
518
c9f90f573efe
add more reblack tree in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
515
diff
changeset
|
191 } |
417 | 192 |
543 | 193 putRedBlackTree : {n m : Level } {a k : Set n} {t : Set m} -> RedBlackTree {n} {m} {t} a k -> k -> a -> (RedBlackTree {n} {m} {t} a k -> t) -> t |
194 putRedBlackTree {n} {m} {a} {k} {t} tree k1 value next with (root tree) | |
515 | 195 ... | Nothing = next (record tree {root = Just (leafNode k1 value) }) |
543 | 196 ... | Just n2 = clearSingleLinkedStack (nodeStack tree) (\ s -> findNode tree s (leafNode k1 value) n2 (\ tree1 s n1 -> insertNode tree1 s n1 next)) |
515 | 197 |
543 | 198 getRedBlackTree : {n m : Level } {a k : Set n} {t : Set m} -> RedBlackTree {n} {m} {t} a k -> k -> (RedBlackTree {n} {m} {t} a k -> (Maybe (Node a k)) -> t) -> t |
199 getRedBlackTree {_} {_} {a} {k} {t} tree k1 cs = checkNode (root tree) | |
417 | 200 where |
542 | 201 search : Node a k -> t |
515 | 202 checkNode : Maybe (Node a k) -> t |
203 checkNode Nothing = cs tree Nothing | |
204 checkNode (Just n) = search n | |
542 | 205 search n with compare tree k1 (key n) |
206 search n | LT = checkNode (left n) | |
207 search n | GT = checkNode (right n) | |
208 search n | EQ = cs tree (Just n) | |
533 | 209 |
210 open import Data.Nat hiding (compare) | |
211 | |
545 | 212 compareℕ : ℕ → ℕ → CompareResult {Level.zero} |
213 compareℕ x y with Data.Nat.compare x y | |
214 ... | less _ _ = LT | |
215 ... | equal _ = EQ | |
216 ... | greater _ _ = GT | |
217 | |
218 | |
543 | 219 createEmptyRedBlackTreeℕ : { m : Level } (a : Set Level.zero) {t : Set m} -> RedBlackTree {Level.zero} {m} {t} a ℕ |
533 | 220 createEmptyRedBlackTreeℕ {m} a {t} = record { |
221 root = Nothing | |
543 | 222 ; nodeStack = emptySingleLinkedStack |
545 | 223 ; compare = compareℕ |
224 } | |
225 |