caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Christoph Bauer <christoph.bauer@lms-gmbh.de>
Cc: Erick Tryzelaar <erickt@dslextreme.com>, caml-list <caml-list@inria.fr>
Subject: Re: AW: [Caml-list] generic Hashtbl.to_array
Date: Tue, 25 Jul 2006 20:45:44 +1000	[thread overview]
Message-ID: <1153824344.1389.45.camel@rosella.wigram> (raw)
In-Reply-To: <26EB47FDD566A7469FC862DAF373792F0171128D@kaiserslautern1.lmsintl.com>

On Tue, 2006-07-25 at 12:19 +0200, Christoph Bauer wrote:
> Hi,
> 
> > 
> > You could also try inverting the Hashtbl fold into an 
> > iterator+closure and pass the closure into the Array.init 
> > function, but I'm not sure how complicated/efficient that would be.
> 
> Something like:
> 
> let to_array_2 t =
>   let init _ = fun () -> raise Not_found  in
>   let a = Array.init (Hashtbl.length t) init in
>     ignore
>       (Hashtbl.fold (fun k v i -> a.(i) <- (fun () -> (k, v)); i+1) t 0);
>     Array.map (fun f -> f ())  a

ugg .. really need a variable length array here. However the most
efficient solution is:

(1) Use Hashtbl.iter to capture just one value out of the 
Hashtble:

	let get1 h = 
		Hashtbl.iter (fun k v -> raise XSome (k,v)) h;
		raise XNone
	in

(2) Initialise the array with it:

	let a = try get1 h with
	| XSome x -> Array.init (Hashtbl.length h) x
	| XNone -> raise Not_found (* zero length array? *)
	in

(3) Now use Hashtbl.fold or iter to initialise the array.

This 'costs' a write barrier but there is no additional
data structure required, and the whole thing is quite safe.

This leaves open the problem of making a zero length array
of the correct type.. not a problem if you're writing
the code inside the Hashtbl module, but trickier if you're
doing it outside.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2006-07-25 10:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-25 10:19 Christoph Bauer
2006-07-25 10:45 ` skaller [this message]
2006-07-25 12:44 Christoph Bauer
2006-07-26  9:46 ` Damien Doligez
2006-07-25 15:34 Christoph Bauer
2006-07-25 15:53 AW: " Christoph Bauer
2006-07-25 16:35 ` Tom
2006-07-26  9:29 Christoph Bauer
2006-07-26 14:41 Christoph Bauer

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=1153824344.1389.45.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=christoph.bauer@lms-gmbh.de \
    --cc=erickt@dslextreme.com \
    /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).