-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigertab.sml
83 lines (70 loc) · 2.06 KB
/
tigertab.sml
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
structure tigertab :> tigertab =
struct
open Polyhash
type ('a, 'b) Tabla = ('a, 'b) hash_table
exception yaExiste of string
exception noExiste
exception noExisteS of string
fun tabNueva() = mkPolyTable(100, noExiste)
fun fromTab t =
let val t' = tabNueva()
in apply (fn x => insert t' x) t; t' end
fun name x = x
fun tabEsta(s, t) =
case peek t s of
SOME _ => true
| NONE => false
fun tabInserta(s, e, t) = let val t' = copy t in (peekInsert t' (s, e); t') end
fun tabRInserta(s, e, t) = let val t' = copy t in (insert t' (s, e); t') end
fun tabBusca(s, t) = peek t s
fun tabSaca(s, t) =
case tabBusca(s, t) of
SOME t => t
| NONE => raise (noExisteS "tabSaca")
fun tabAplica(f, t) = map(fn(_, e) => f e) t
fun tabAAplica(f, g, t) =
let val l' = listItems t
val t' = mkPolyTable(100, noExiste)
in
List.app(fn(k, e) => insert t' (k, e))
(List.map(fn(k, e) => (f k, g e)) l');
t'
end
fun tabRAAplica(f, g, t) =
let val l' = rev(listItems t)
val t' = mkPolyTable(100, noExiste)
in
List.app(fn(k, e) => insert t' (k, e))
(List.map(fn(k, e) => (f k, g e)) l');
t'
end
fun tabInserList(t, l) =
let val t' = copy t in (List.app(fn(s, e) => insert t' (s, e)) l; t') end
fun tabAList t = listItems t
fun tabFiltra(f, t) =
let val l = listItems t
val t' = mkPolyTable(100, noExiste)
in
List.app(fn(k, e) => insert t' (k,e))
(List.filter (fn(a, b) => f b) l);
t'
end
fun tabPrimer(f, t) = hd(List.filter (fn(a, b) => f b) (listItems t))
fun tabClaves t = List.map (fn(x, y) => x) (listItems t)
fun tabApp f t =
let val vals = List.map #2 (tabAList t)
val claves = tabClaves t
val newVals = (List.app f vals; vals)
val newPairs = ListPair.zip (claves, newVals)
in
tabInserList(tabNueva(), newPairs)
end
fun inserta (k, v, t) = t := (tabRInserta(k, v, !t))
fun busca(s, t, e) =
case tabBusca(s, t) of
SOME t => t
| NONE => raise Fail e
fun fromList xs = tabInserList( tabNueva(), xs)
fun tab2Func t x = tabSaca (x, t)
fun printWith t f g = (List.app (fn (k, v) => print("(" ^ (f k) ^ ", " ^ (g v) ^ ") ")) (tabAList t); print("\n"))
end