Version française
Home     About     Download     Resources     Contact us    
Browse thread
Getting an element of a hashtable: simple ... or is it?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ludovic Coquelle <lcoquelle@g...>
Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it?
If the type of hashtbl key is known (if the hashtbl module has been
created by the functor),
the same code as initial can avoid a reference and use exception
propagation mechanism:

type key = MyHashtbl.key
exception One of key
let get_one h = try (MyHashtbl.iter (fun k _ -> raise (One k)) h;
raise Not_found) with One x -> x

Can't we have polymorphic exception? like
'a exception One of 'a


On Wed, Aug 6, 2008 at 5:47 AM, Brighten Godfrey <pbg@cs.berkeley.edu> wrote:
> On Aug 5, 2008, at 5:25 AM, blue storm wrote:
>>
>> With Extlib you can use :
>> let get_one hashtbl = Enum.peek (Hashtbl.enum hashtbl)
>> val get_one : ('a, 'b) Hashtbl.t -> ('a * 'b) option
>
> Ah, thanks.
>
>
> On Aug 5, 2008, at 6:21 AM, Peng Zang wrote:
>>
>> I think this is pretty standard.  At least, I see it in ExtLib and I do it
>> on
>> a regular basis.  In fact I have a function to do this for me so I don't
>> have
>> to do it over and over again.  Eg.
>>
>>  let get_one ht = mkGetOne Hashtbl.iter ht
>
> OK -- so you're saying ExtLib also implements it by breaking out of the loop
> with an exception.  Interesting.
>
>
> On Aug 5, 2008, at 2:02 PM, Chris Kauffman wrote:
>>
>> I'm curious what sort of scenario calls for retrieving any single
>> element of a hash table (which is potentially empty?). It seems most
>> of the cases I deal with involve simply storing or iterating over all
>> the elements.
>
> Yes, nearly all cases are like that for me too.  But in this case, I want to
> decompose a graph into its connected components, roughly according to the
> following pseudocode:
>
>    unprocessed_nodes : (node_t, unit) Hashtbl.t = all nodes
>    while unprocessed_nodes not empty do
>        let one_node = choose any one node from unprocessed_nodes
>        let cc = find_connected_component_containing one_node
>        Do some sort of processing on cc. Then:
>        for each node v in cc
>            remove v from unprocessed_nodes
>        done
>
> Thanks,
> ~Brighten Godfrey
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>