module Hashtblutil:Hash table utilitiessig
..end
This module provides various functions to simplify working with OCaml standard
hash tables (standard library module Hashtbl). For additional features
and convenience operators, see the Hashtbloper
module.
These functions are used to extract information from hash tables in various
ways.
val keys : ('a, 'b) Hashtbl.t -> 'a list
keys hash
will return a list of all the keys contained in
that hash.val values : ('a, 'b) Hashtbl.t -> 'b list
values hash
will return a list of all the valies contained
in that hash.val length : ('a, 'b) Hashtbl.t -> int
length hash
will return the number of keys present in the
hash.val items : ('a, 'b) Hashtbl.t -> ('a * 'b) list
items hash
will return a list of pairs representing all
the (key, value) pairs present in the hash. This list is suitable for use
with the association list functions in the standard module List
or the
Missinglib module Listutil
.val map : ('a -> 'b -> 'c) -> ('a, 'b) Hashtbl.t -> 'c list
map func hash
will call func key value
for each key/value
pair represented in the hash, and return a list of the return values from
func. As an example, here is the implementation of the
Hashtblutil.keys
function:
let keys hash = map (fun key value -> key) hash;;
These functions alter a hash table in various ways.
Please note that they modify the table in-place; that is, they do not
return a new hash table but rather modify the one passed in as an argument.
val merge : ('a, 'b) Hashtbl.t -> ('a, 'b) Hashtbl.t -> unit
merge oldhash newhash
will iterate over the newhash. Each
key/value pair present in it will be added to the oldhash using
Hashtbl.replace
. Therefore, the entire contents of the new hash will
be added to the old one, replacing any key with the same name that is already
present.val convkeys : ('a, 'b) Hashtbl.t -> ('a -> 'a) -> unit
convkeys hash func
will call func for every key in the
hash. If func
returns a key different than the key passed to it, the
relevant key in the hash table will be renamed, overwriting any key with
the same name as the new key. It is not necessarily possible to predict what
which key/value pair will "win" in this case.
Your func
must be such that it returns its argument unmodified if passed an
already-converted value.
Here is an example:
convkeys myhash String.lowercase
This is used, for instance, when you wish the keys of your hash to be
case-insensitive; you can then use String.lowercase before any call to hash
lookup/modification functions.
These functions are used to store (string, string) hash tables in files,
and load them back. The file format is versatile enough to handle binary
data in strings, yet simple enough to be parsed by line-oriented parsers in
most languages.
val strhash_to_ochan : (string, string) Hashtbl.t -> Pervasives.out_channel -> unit
val ichan_to_strhash : Pervasives.in_channel -> (string, string) Hashtbl.t
val str_of_stritem : string -> string -> string
val stritem_of_str : string -> string * string