annotate final_main/src/AgdaTree.agda.replaced @ 4:12204a2c2eda

add .pdf and some section.
author ryokka
date Sun, 18 Feb 2018 21:43:41 +0900
parents
children eafc166804f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
1 record TreeMethods {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.@$\sqcup$@ n) where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
2 field
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
3 putImpl : treeImpl @$\rightarrow$@ a @$\rightarrow$@ (treeImpl @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
4 getImpl : treeImpl @$\rightarrow$@ (treeImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
5 open TreeMethods
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
6
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
7 record Tree {n m : Level } {a : Set n } {t : Set m } (treeImpl : Set n ) : Set (m Level.@$\sqcup$@ n) where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
8 field
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
9 tree : treeImpl
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
10 treeMethods : TreeMethods {n} {m} {a} {t} treeImpl
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
11 putTree : a @$\rightarrow$@ (Tree treeImpl @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
12 putTree d next = putImpl (treeMethods ) tree d (\t1 @$\rightarrow$@ next (record {tree = t1 ; treeMethods = treeMethods} ))
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
13 getTree : (Tree treeImpl @$\rightarrow$@ Maybe a @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
14 getTree next = getImpl (treeMethods ) tree (\t1 d @$\rightarrow$@ next (record {tree = t1 ; treeMethods = treeMethods} ) d )
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
15
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
16 open Tree
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
17
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
18 data Color {n : Level } : Set n where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
19 Red : Color
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
20 Black : Color
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
21
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
22 data CompareResult {n : Level } : Set n where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
23 LT : CompareResult
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
24 GT : CompareResult
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
25 EQ : CompareResult
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
26
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
27 record Node {n : Level } (a k : Set n) : Set n where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
28 inductive
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
29 field
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
30 key : k
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
31 value : a
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
32 right : Maybe (Node a k)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
33 left : Maybe (Node a k)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
34 color : Color {n}
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
35 open Node
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
36
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
37 record RedBlackTree {n m : Level } {t : Set m} (a k : Set n) : Set (m Level.@$\sqcup$@ n) where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
38 field
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
39 root : Maybe (Node a k)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
40 nodeStack : SingleLinkedStack (Node a k)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
41 compare : k @$\rightarrow$@ k @$\rightarrow$@ CompareResult {n}
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
42
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
43 open RedBlackTree
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
44
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
45
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
46 leafNode : {n : Level } {a k : Set n} @$\rightarrow$@ k @$\rightarrow$@ a @$\rightarrow$@ Node a k
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
47 leafNode k1 value = record {
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
48 key = k1 ;
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
49 value = value ;
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
50 right = Nothing ;
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
51 left = Nothing ;
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
52 color = Red
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
53 }
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
54
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
55 putRedBlackTree : {n m : Level } {a k : Set n} {t : Set m} @$\rightarrow$@ RedBlackTree {n} {m} {t} a k @$\rightarrow$@ k @$\rightarrow$@ a @$\rightarrow$@ (RedBlackTree {n} {m} {t} a k @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
56 putRedBlackTree {n} {m} {a} {k} {t} tree k1 value next with (root tree)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
57 ... | Nothing = next (record tree {root = Just (leafNode k1 value) })
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
58 ... | Just n2 = clearSingleLinkedStack (nodeStack tree) (\ s @$\rightarrow$@ findNode tree s (leafNode k1 value) n2 (\ tree1 s n1 @$\rightarrow$@ insertNode tree1 s n1 next))
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
59
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
60 getRedBlackTree : {n m : Level } {a k : Set n} {t : Set m} @$\rightarrow$@ RedBlackTree {n} {m} {t} a k @$\rightarrow$@ k @$\rightarrow$@ (RedBlackTree {n} {m} {t} a k @$\rightarrow$@ (Maybe (Node a k)) @$\rightarrow$@ t) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
61 getRedBlackTree {_} {_} {a} {k} {t} tree k1 cs = checkNode (root tree)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
62 module GetRedBlackTree where
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
63 search : Node a k @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
64 checkNode : Maybe (Node a k) @$\rightarrow$@ t
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
65 checkNode Nothing = cs tree Nothing
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
66 checkNode (Just n) = search n
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
67 search n with compare tree k1 (key n)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
68 search n | LT = checkNode (left n)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
69 search n | GT = checkNode (right n)
12204a2c2eda add .pdf and some section.
ryokka
parents:
diff changeset
70 search n | EQ = cs tree (Just n)