From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 45FE4BBB7 for ; Tue, 25 Jul 2006 17:53:10 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k6PFr919008745 for ; Tue, 25 Jul 2006 17:53:09 +0200 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 RAA18628 for ; Tue, 25 Jul 2006 17:53:08 +0200 (MET DST) Received: from mail-outfwd.lms.be (Kaiserslautern1.lms-gmbh.de [213.68.136.230]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k6PFr8sB008738 for ; Tue, 25 Jul 2006 17:53:08 +0200 Received: from localhost (unknown [127.0.0.1]) by mail-outfwd.lms.be (Postfix) with ESMTP id 3A6137F441; Tue, 25 Jul 2006 18:13:09 +0200 (CEST) Received: from mail-kl.lmsintl.com ([127.0.0.1]) by localhost (kl-ftp [127.0.0.1]) (amavisd-new, port 20024) with ESMTP id 32506-05; Tue, 25 Jul 2006 18:13:09 +0200 (CEST) Received: from kaiserslautern1.lmsintl.com (unknown [10.2.0.100]) by mail-kl.lmsintl.com (Postfix) with ESMTP id 1B68CB6EC6; Tue, 25 Jul 2006 18:13:09 +0200 (CEST) Received: by kaiserslautern1.lmsintl.com with Internet Mail Service (5.5.2653.19) id <32PZS81C>; Tue, 25 Jul 2006 17:53:07 +0200 Message-ID: <26EB47FDD566A7469FC862DAF373792F017112F4@kaiserslautern1.lmsintl.com> From: Christoph Bauer To: Brian Hurt , caml-list Subject: AW: AW: [Caml-list] generic Hashtbl.to_array Date: Tue, 25 Jul 2006 17:53:01 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C6B002.6856BC88" X-Virus-Scanned: by IT Services X-j-chkmail-Score: MSGID : 44C63E65.000 on nez-perce : j-chkmail score : XX : 5/20 0 X-j-chkmail-Score: MSGID : 44C63E64.000 on nez-perce : j-chkmail score : XX : 5/20 0 X-Miltered: at nez-perce with ID 44C63E65.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 44C63E64.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; hashtbl:01 hashtbl:01 val:01 val:01 abstr:01 pointer:01 abstr:01 pointer:01 1.0:98 2.0:98 W6:98 W6:98 1.0:98 2.0:98 caml-list:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO,HTML_MESSAGE, HTML_TITLE_EMPTY autolearn=disabled version=3.0.3 This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C6B002.6856BC88 Content-Type: text/plain 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 Does it work correctly for floats? Looks good for floats. # 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 ;; val to_array : ('a, 'b) Hashtbl.t -> ('a * 'b) array = # let h = Hashtbl.create 0;; val h : ('_a, '_b) Hashtbl.t = # Hashtbl.add h 1.0 2.0;; - : unit = () # to_array h;; - : (float * float) array = [|(1., 2.)|] # Gc.compact ();; - : unit = () # BTW, the array should store a pointer to a tuple of two floats, so I thinkt float or ints doesn't matter. I won't use this solution, because it isn't better than others. Christoph Bauer ------_=_NextPart_001_01C6B002.6856BC88 Content-Type: text/html

 
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
  
 
 

Does it work correctly for floats? 
 
Looks good for floats.
 
# 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
 
  ;;
val to_array : ('a, 'b) Hashtbl.t -> ('a * 'b) array = <fun>
# let h = Hashtbl.create 0;;
val h : ('_a, '_b) Hashtbl.t = <abstr>
# Hashtbl.add h 1.0 2.0;;
- : unit = ()
# to_array h;;
- : (float * float) array = [|(1., 2.)|]
# Gc.compact ();;
- : unit = ()
#
 
 BTW, the array should store a pointer to a tuple of two floats, so
I thinkt float or ints doesn't matter. I won't use this solution, because
 it isn't better than others. 

 Christoph Bauer 

------_=_NextPart_001_01C6B002.6856BC88--