forked from cedille/ial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie-fast.agda
56 lines (44 loc) · 1.83 KB
/
trie-fast.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module trie-fast where
open import trie-core public
open import string
open import maybe
open import char
open import bool
open import eq
trie-lookup-fast3 : ∀{A : Set} → trie A → string → maybe A
trie-lookup-fast3{A} t s
= extract (stringFoldl f t s)
where
extract : trie A → maybe A
extract (trie.Node x _) = x
-- define an "empty trie" and change this to:
-- (trie A) → char → (trie A)
f : trie A → char → trie A
f (Node _ ts) c with cal-lookup ts c
f (Node _ ts) c | nothing = Node nothing empty-cal
f (Node _ ts) c | just t = t
trie-lookup : ∀{A : Set} → trie A → string → maybe A
trie-lookup = trie-lookup-fast3
trie-insert-fast2 : ∀{A : Set} → trie A → string → A → trie A
trie-insert-fast2{A} t s new-data = (stringFoldr g initial-f s) t
where
initial-f : trie A → trie A
initial-f (Node _ ts) = Node (just new-data) ts
g : char → (trie A → trie A) → (trie A → trie A)
g c f (Node odata ts) with cal-lookup ts c
g c f (Node odata ts) | nothing = Node odata (cal-add ts c (f empty-trie))
g c f (Node odata ts) | just t = Node odata (cal-insert ts c (f t))
trie-insert : ∀{A : Set} → trie A → string → A → trie A
trie-insert = trie-insert-fast2
trie-remove-fast : ∀{A : Set} → trie A → string → trie A
trie-remove-fast{A} t s = (stringFoldr g initial-f s) t
where
initial-f : trie A → trie A
initial-f (Node _ ts) = Node nothing ts
g : char → (trie A → trie A) → (trie A → trie A)
g c f (Node odata ts) with cal-lookup ts c
g c f (Node odata ts) | nothing = Node odata ts
g c f (Node odata ts) | just t = Node odata (cal-insert ts c (f t))
trie-remove : ∀{A : Set} → trie A → string → trie A
trie-remove = trie-remove-fast
open import trie-functions trie-lookup trie-insert trie-remove public