From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA05044; Tue, 23 Mar 2004 11:00:25 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA04905 for ; Tue, 23 Mar 2004 11:00:24 +0100 (MET) Received: from hirsch.in-berlin.de (hirsch.in-berlin.de [192.109.42.6]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i2NA0wKW032214 for ; Tue, 23 Mar 2004 11:00:58 +0100 X-Envelope-From: oliver@first.in-berlin.de X-Envelope-To: Received: from hirsch.in-berlin.de (localhost [127.0.0.1]) by hirsch.in-berlin.de (8.12.11/8.12.11/Debian-3) with ESMTP id i2NA01dp022841 for ; Tue, 23 Mar 2004 11:00:22 +0100 Received: from first.UUCP (uucp@localhost) by hirsch.in-berlin.de (8.12.11/8.12.11/Debian-3) with UUCP id i2N9tRPj022416 for inria.fr!caml-list; Tue, 23 Mar 2004 10:55:27 +0100 Received: by first.in-berlin.de via sendmail from stdin id (Debian Smail3.2.0.114) Tue, 23 Mar 2004 10:45:47 +0100 (CET) From: oliver@first.in-berlin.de (Oliver Bandel) Date: Tue, 23 Mar 2004 10:45:47 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] Hashtbl and destructive operations on keys Message-ID: <20040323094547.GA745@first.in-berlin.de> References: <871xnky9zk.dlv@vanicat.homelinux.org> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; oliver:01 in-berlin:01 oliver:01 bandel:01 caml-list:01 hashtbl:01 2004:99 accum:01 val:01 hashtbl:01 val:01 hash:01 accum:01 imho:01 ciao:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 328 On Tue, Mar 23, 2004 at 12:13:14AM +0100, Thomas Fischbacher wrote: [...] > > let nonconsing_accum f_combine f_copy ht h_key h_val = > if Hashtbl.mem ht h_key > then Hashtbl.replace ht h_key > (f_combine (Hashtbl.find ht h_key) h_val) > else Hashtbl.add ht (f_copy h_key) h_val > ;; > Why don't you sample all values first and afteryou have done it, using a function to apply your f_combine to all stuff, you get for each of the keys? You can get all values for a key with Hashtbl.find_all. So you only have to remember, which keys are in the hashtbl and then apply your f_combine to each of that keys. That also ha sthe advantage that you are free to apply different functions to your hashtbl-values, because you have saved all your values inside of the hash. So you may apply (+) as well as (*) or others, once you have sampled all your data. Because you are testing the existence of the key with each new value you want to insert in your hashtbl, you have unnecessary calls with each insert-operation (with each call to nonconsing_accum). IMHO it makes more sense to sample all data with Hashtbl.add first, and then do your operations on a per-key base with Hashtbl.find_all. (I hope I didn't miss your point.) Ciao, Oliver ------------------- 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