Module Hashtblutil


module Hashtblutil: sig .. end
Hash table utilities

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.



Hash Table Information

These functions are used to extract information from hash tables in various ways.

val keys : ('a, 'b) Hashtbl.t -> 'a list
Calling keys hash will return a list of all the keys contained in that hash.
val values : ('a, 'b) Hashtbl.t -> 'b list
Calling values hash will return a list of all the valies contained in that hash.
val length : ('a, 'b) Hashtbl.t -> int
Calling length hash will return the number of keys present in the hash.
val items : ('a, 'b) Hashtbl.t -> ('a * 'b) list
Calling 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
Calling 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;; 


Hash Table Conversion

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
Calling 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
This function is used to adjust the keys in a hash table. For example, it may be used to convert all keys to lowercase in a hash table indexed by strings. Calling 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.


Persistent Storage

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
Writes the given hash table out to the specified output channel.
val ichan_to_strhash : Pervasives.in_channel -> (string, string) Hashtbl.t
Reads from the given file, returning a new hash table representing its contents.
val str_of_stritem : string -> string -> string
Converts a single item of a (string, string) Hashtbl.t to a string representation.
val stritem_of_str : string -> string * string
Returns a (key, value) pair generated by parsing a string representation.