caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* AW: AW: [Caml-list] generic Hashtbl.to_array
@ 2006-07-25 15:53 Christoph Bauer
  2006-07-25 16:35 ` Tom
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Bauer @ 2006-07-25 15:53 UTC (permalink / raw)
  To: Brian Hurt, caml-list

[-- Attachment #1: Type: text/plain, Size: 887 bytes --]


 

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 



[-- Attachment #2: Type: text/html, Size: 3244 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-08-15  8:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-25 15:53 AW: AW: [Caml-list] generic Hashtbl.to_array Christoph Bauer
2006-07-25 16:35 ` Tom
2006-08-15  8:26   ` Stéphane Glondu

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).