Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] The invert Benchmark
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-18 (21:04)
From: Nadji.Gauthier@l...
Subject: Re: [Caml-list] The invert Benchmark
May I propose this program ? It uses a list of strings attached to
a string in a hashtbl as proposed by Xavier Leroy, uses the very useful
Pcre binding of Markus Mottl, doesn't need a join facility, and is
slightly faster than the one in perl.
Of course, any comment from the gurus are appreciated ...

exception Impossible

let process_input () = 
  let h = Hashtbl.create 127 in
  let foldadd k d =
      let y = Hashtbl.find h k in
      Hashtbl.replace h k (d::y)
    with _ -> Hashtbl.add h k [d]
    let r = Pcre.regexp "\t" in
    while true do
      let l = input_line stdin in
      (match Pcre.split ~rex:r l with
	 [a;b] -> foldadd b a
       | _ -> failwith "bad input")
    raise Impossible
  with _ -> h

let post_process h = 
  let l = Hashtbl.fold (fun k d l -> (k, List.sort compare d)::l) h [] in
  List.sort (fun (a,_) (b, _) -> compare a b) l

let pprint = 
  List.iter (fun (k, d) -> 
	       Printf.printf "%s" k; 
	       List.iter (fun s -> Printf.printf "\t%s" s) d;
	       Printf.printf "\n";

let _ =
  let h = process_input () in
  let l = post_process h in
  pprint l
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: