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: Brighten Godfrey <pbg@c...>
Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it?
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