From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 16F05BBAF for ; Fri, 8 Aug 2008 17:46:50 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av8AAMsHnEjRVcbmlGdsb2JhbACRAD4BAQEBCQMKBxEDlGmFdg X-IronPort-AV: E=Sophos;i="4.31,327,1215381600"; d="scan'208";a="13834145" Received: from rv-out-0506.google.com ([209.85.198.230]) by mail2-smtp-roc.national.inria.fr with ESMTP; 08 Aug 2008 17:46:43 +0200 Received: by rv-out-0506.google.com with SMTP id f6so931311rvb.3 for ; Fri, 08 Aug 2008 08:46:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=fEsVec9nSeup9sCLkcdnx0uVByNLlpV8YER1Yqc0cCE=; b=RjpDokyJORP+Pf2oPfZtb33n2lKEfBu6L8AIpjOP8X6BCFbNnCpoODz+rPEyrbf5n5 S0J4cET/c88X+yOyRxydNILW2aWcuT5bX490B9eOSrueDNALkNlBIL2lV6/DBt4juu5d Io1Uwt9hUyWBXsngugcG4vx4DMWAx/4lnQNdM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=uOPFdVT+Bix3tK2FbEeYHSpG/+6gULpFzpQPywQJ/dXFMnG0k0F5moIGkLehtcSOfV Lh7pzlYWbTyfeyr9hE9dctZ3qsT3TWm9ytRIqK3kFHnBdHIMMrlXQhkO1Nt5XqWZvUw/ Re6qAAmgrvRwgFcnkejAzkHJAFtIZM35Y2UQA= Received: by 10.140.201.8 with SMTP id y8mr1520925rvf.28.1218210401629; Fri, 08 Aug 2008 08:46:41 -0700 (PDT) Received: by 10.140.143.19 with HTTP; Fri, 8 Aug 2008 08:46:41 -0700 (PDT) Message-ID: Date: Fri, 8 Aug 2008 23:46:41 +0800 From: "Ludovic Coquelle" To: "Brighten Godfrey" Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it? Cc: "blue storm" , "Chris Kauffman" , peng.zang@gmail.com, caml-list@yquem.inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <94C5D65F-7CA4-48FF-B4AA-E88E82F6C8E1@cs.berkeley.edu> <527cf6bc0808050525x5d574e5ek4b7acbc4e114307e@mail.gmail.com> X-Spam: no; 0.00; hashtable:01 hashtbl:01 hashtbl:01 functor:01 iter:01 enum:01 enum:01 val:01 iter:01 implements:01 hash:01 iterating:01 pseudocode:01 nodes:01 node:01 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 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 >