caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oliver@first.in-berlin.de (Oliver Bandel)
To: caml-list@inria.fr
Subject: Re: [Caml-list] Hashtbl and destructive operations on keys
Date: Tue, 23 Mar 2004 10:45:47 +0100	[thread overview]
Message-ID: <20040323094547.GA745@first.in-berlin.de> (raw)
In-Reply-To: <Pine.LNX.4.58.0403222348530.30579@seekar.cip.physik.uni-muenchen.de>

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


  parent reply	other threads:[~2004-03-23 10:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-22 18:16 Thomas Fischbacher
2004-03-22 19:09 ` Remi Vanicat
2004-03-22 23:13   ` Thomas Fischbacher
2004-03-23  0:59     ` Jacques Garrigue
2004-03-23  9:20     ` Correnson Loïc
2004-03-23 12:12       ` Thomas Fischbacher
2004-03-23  9:45     ` Oliver Bandel [this message]
2004-03-22 23:44 Oliver Bandel
2004-03-22 23:44 Oliver Bandel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040323094547.GA745@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).