Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 =
    try 
      let y = Hashtbl.find h k in
      Hashtbl.replace h k (d::y)
    with _ -> Hashtbl.add h k [d]
  in    
  try
    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")
    done;
    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 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