Skip to content

Commit

Permalink
New method: tabNueva'()
Browse files Browse the repository at this point in the history
  • Loading branch information
perezzini committed Nov 8, 2017
1 parent 4931220 commit b77571f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 63 deletions.
21 changes: 10 additions & 11 deletions tigertab.sig
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ signature tigertab =
sig

type ('a, 'b) Tabla

exception yaExiste of string
exception noExiste
exception noExisteS of string

val tabNueva : unit -> (''a, 'b) Tabla
val tabNueva' : ('a * 'a -> bool) -> ('a, 'b) Tabla
val fromTab : (''a, 'b) Tabla -> (''a, 'b) Tabla
val name : 'a -> 'a
val tabEsta : ''a * (''a, 'b) Tabla -> bool
(* se usa cuando se quiere insertar un par sin importar si la clave existe o no,
o si quiero sobreescribir *)
val tabInserta : ''a * 'b * (''a, 'b) Tabla -> (''a, 'b) Tabla
(* se debe usar este... R de "reemplaza"*)
val tabRInserta : ''a * 'b * (''a, 'b) Tabla -> (''a, 'b) Tabla
val tabBusca : ''a * (''a, 'b) Tabla -> 'b option
val tabSaca : ''a * (''a, 'b) Tabla -> 'b
val tabAplica : ('a -> 'b) * (''c, 'a) Tabla -> (''c, 'b) Tabla
val tabEsta : 'a * ('a, 'b) Tabla -> bool
val tabInserta : 'a * 'b * ('a, 'b) Tabla -> ('a, 'b) Tabla
val tabRInserta : 'a * 'b * ('a, 'b) Tabla -> ('a, 'b) Tabla
val tabBusca : 'a * ('a, 'b) Tabla -> 'b option
val tabSaca : 'a * ('a, 'b) Tabla -> 'b
val tabAplica : ('a -> 'b) * ('c, 'a) Tabla -> ('c, 'b) Tabla
val tabAAplica : ('a -> ''c) * ('b -> 'd) * ('a, 'b) Tabla -> (''c, 'd) Tabla
val tabRAAplica : ('a -> ''b) * ('c -> 'd) * ('a, 'c) Tabla -> (''b, 'd) Tabla
val tabInserList : ('a, 'b) Tabla * ('a * 'b) list -> ('a, 'b) Tabla
val tabAList : ('a, 'b) Tabla -> ('a * 'b) list
val tabFiltra : ('b -> bool) * (''a, 'b) Tabla -> (''a, 'b) Tabla
val tabPrimer : ('b -> bool) * ('a, 'b) Tabla -> ('a * 'b)
val tabClaves : ('a, 'b) Tabla -> 'a list
val crearTabla : ('a * 'a -> bool) -> ('a, 'b) Tabla

end
end
127 changes: 75 additions & 52 deletions tigertab.sml
Original file line number Diff line number Diff line change
@@ -1,60 +1,83 @@
structure tigertab :> tigertab =
struct
open Polyhash

type ('a, 'b) Tabla = ('a, 'b) hash_table
open Polyhash

exception yaExiste of string
exception noExiste
exception noExisteS of string
type ('a, 'b) Tabla = ('a, 'b) hash_table

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 noExiste
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)
exception yaExiste of string
exception noExiste
exception noExisteS of string

fun crearTabla eqFn = mkTable (hash, eqFn) (100, noExiste)
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 (* [peekInsert htbl (k, d)] inserts data d for key k, if k is not
already in the table, returning NONE. If k is already in the
table, and the associated data value is d', then returns SOME d'
and leaves the table unmodified.) *)

fun tabRInserta(s, e, t) = let val t' = copy t in (insert t' (s, e); t') end (* [insert htbl (k, d)] inserts data d for key k. If k already had an
item associated with it, then the old item is overwritten.) *)

fun tabBusca(s, t) = peek t s

fun tabSaca(s, t) =
case tabBusca(s, t) of
SOME t => t
| NONE => raise noExiste

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 tabNueva' eqFn = mkTable (hash, eqFn) (100, noExiste) (* [mkTable (hashVal, sameKey) (sz, exc)] returns a new hashtable,
using hash function hashVal and equality predicate sameKey. The sz
is a size hint, and exc is the exception raised by function find.
It must be the case that sameKey(k1, k2) implies hashVal(k1) =
hashVal(k2) for all k1,k2.) *)
end

0 comments on commit b77571f

Please sign in to comment.