caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Stéphane Glondu" <Stephane.Glondu@crans.org>
To: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] generic Hashtbl.to_array
Date: Tue, 15 Aug 2006 17:08:12 +0900	[thread overview]
Message-ID: <44E180EC.2010807@crans.org> (raw)
In-Reply-To: <c1490a380607250820i62aeb5bcse265fc509dae7c9a@mail.gmail.com>

I know I am late.

Tom a écrit :
> The dirtiest solution:
> 
> let to_array t =
>  let a =  Array.make (Hashtbl.length t) (Obj.magic 0)  in
>    ignore
>      (Hashtbl.fold (fun k v i -> a.(i) <- (k, v); i + 1) t 0) ;
>      a

What about:

let to_array h =
  let res = ref [||] in
  let rec assign = ref (fun i x ->
			  res := Array.make (Hashtbl.length h) x;
			  !res.(i) <- x;
			  assign := Array.set !res) in
  ignore (Hashtbl.fold (fun k v i -> !assign i (k, v); i+1) h 0);
  !res;;

Sorry if it has already been proposed (but I have not seen it).

-- 
Stéphane Glondu


  reply	other threads:[~2006-08-15  8:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-25 12:44 AW: " Christoph Bauer
2006-07-25 15:20 ` Tom
2006-08-15  8:08   ` Stéphane Glondu [this message]
2006-07-26  9:46 ` AW: " Damien Doligez
  -- strict thread matches above, loose matches on Subject: below --
2006-07-26 14:41 Christoph Bauer
2006-07-26 14:53 ` Tom
2006-07-26  2:16 oleg
2006-07-26  9:48 ` [Caml-list] " Damien Doligez
2006-07-25 15:53 AW: AW: " Christoph Bauer
2006-07-25 16:35 ` Tom
2006-08-15  8:26   ` Stéphane Glondu
2006-07-25 12:00 Christoph Bauer
2006-07-25 16:19 ` skaller
2006-07-25  8:29 Christoph Bauer
2006-07-25  9:14 ` [Caml-list] " Erick Tryzelaar
2006-07-25 11:45 ` Damien Doligez

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=44E180EC.2010807@crans.org \
    --to=stephane.glondu@crans.org \
    --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).