caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Nadji.Gauthier@lip6.fr
To: Xavier Leroy <xavier.leroy@inria.fr>
Cc: Christophe TROESTLER <debian00@tiscalinet.be>,
	"O'Caml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] The invert Benchmark
Date: Wed, 17 Apr 2002 22:28:45 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.21.0204172221160.27387-100000@singha.lip6.fr> (raw)
In-Reply-To: <20020417220454.A4322@pauillac.inria.fr>

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


  reply	other threads:[~2002-04-18 21:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-17 18:26 Christophe TROESTLER
2002-04-17 20:04 ` Xavier Leroy
2002-04-17 20:28   ` Nadji.Gauthier [this message]
2002-04-18 19:07   ` [Caml-list] " Christophe TROESTLER
2002-04-18 19:21     ` Mattias Waldau
2002-04-17 20:13 ` [Caml-list] " Remi VANICAT
2002-04-17 20:30 ` Markus Mottl
2002-04-17 21:19   ` Mattias Waldau
2002-04-18  9:11 ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0204172221160.27387-100000@singha.lip6.fr \
    --to=nadji.gauthier@lip6.fr \
    --cc=caml-list@inria.fr \
    --cc=debian00@tiscalinet.be \
    --cc=xavier.leroy@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).