Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Should be INSIDE STANDARD-LIB: Hashtbl.keys
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: Re: [Caml-list] Should be INSIDE STANDARD-LIB: Hashtbl.keys
On Wed, 21 Apr 2004, Oliver Bandel wrote:

>       Hashtbl.keys <hash>  returns a list of all keys of the
>       hash "hash", that exists and therefore have values bound
>       to it. If a key has several bindings, it will occur only
>       once in this list.

For that purpose, it is possible instead of a (key, data) Hashtbl.t to use
a (key, data list ref) Hashtbl.t:

let add tbl key data =
  let r =
    try Hashtbl.find tbl key
    with Not_found ->
      let r = ref [] in
      Hashtbl.add tbl key r;
      r in
  r := data :: !r

let fold f tbl init =
  Hashtbl.fold (fun key r accu -> f key (List.hd !r) accu) tbl init

let keys tbl =
  fold (fun key _ accu -> key :: accu) tbl []


Martin

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners