Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] sorting Hashtbl.t
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oleg <oleg_inconnu@m...>
Subject: [Caml-list] sorting Hashtbl.t
Hi

Hashtbl.iter does not always iterate in an orderly fashion, for instance

let h = Hashtbl.create 3 in 
for i = 1 to 10 do 
   Hashtbl.add h i i 
done; 
Hashtbl.iter (Printf.printf "%d, %d\n") h;;

Produces:
7, 7
8, 8
1, 1
9, 9
2, 2
10, 10
3, 3
4, 4
5, 5
6, 6

Is there any way to sort Hashtlb.t or otherwise make Hashtbl.iter iterate 
elements in order [1]?

Thanks
Oleg

[1] One could of course copy elements to a list or an array, then sort and 
iterate the latter, but I suppose it is inefficient, as hash table elements 
are "almost sorted".
-------------------
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