-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftable.rkt
65 lines (50 loc) · 1.77 KB
/
ftable.rkt
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
57
58
59
60
61
62
63
64
65
#lang typed/racket
(require typed-racket-datatype)
(provide FTable
ft-ins-all
ft-get
ft-rst
mk-empty-ftable
ft-set-nullable
ft-changed?
ft-is-nullable?
)
(define-datatype FEntry (E [chg : Boolean] [null : Boolean] [ set : (Listof Char)]))
(define-type FTable (Immutable-HashTable String FEntry) )
(define (f-ins [ x : FEntry ] [y : Char]) : FEntry
(cond
[(member y (E-set x)) (E (E-chg x) (E-null x) (E-set x))]
[else (E #t (E-null x) (cons y (E-set x)) )]
)
)
(define (f-rst [ x : FEntry ]) : FEntry
(E #f (E-null x) (E-set x))
)
(define (f-empty) : FEntry
(E #f #f null)
)
(define (f-ins-all [ x : FEntry ] [l : (Listof Char)] ) : FEntry
(foldr (lambda ([c : Char] [z : FEntry]) (f-ins z c) ) x l)
)
(define (ft-ins-all [ t : FTable ] [s : String] [l : (Listof Char)] ) : FTable
(hash-update t s (lambda ([e : FEntry]) (f-ins-all e l)) (lambda () (E #t #f null)))
)
(define (ft-get [t : FTable] [s : String] ) : (Listof Char)
(E-set (hash-ref t s))
)
(define (ft-is-nullable? [t : FTable] [s : String] ) : Boolean
(E-null (hash-ref t s))
)
(define (ft-set-nullable [t : FTable] [s : String] [b : Boolean] ) : FTable
(hash-update t s (lambda ([e : FEntry]) (E (E-chg e) b (E-set e))) (lambda () (E #t b null)) )
)
(define (ft-rst [t : FTable] ) : FTable
(make-immutable-hash (hash-map t (lambda ([k : String] [e : FEntry]) (cons k (f-rst e))) ))
)
(define (ft-changed? [t : FTable] ) : Boolean
(not (andmap (lambda ([b : Boolean]) (not b))
(hash-map t (lambda ([k : String] [e : FEntry]) (E-chg e)) ) ))
)
(define (mk-empty-ftable [v : (Listof String)] ) : FTable
(make-immutable-hash (map (lambda ([ x : String]) (cons x (f-empty) ) ) v) )
)