Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Judicael Courant <Judicael.Courant@lri.fr>
To: Mattias Waldau <mattias.waldau@abc.se>
Cc: caml-list@inria.fr
Subject: Re: sorting of list of vectors (array with one dimension)
Date: Thu, 30 Nov 2000 12:33:34 +0100	[thread overview]
Message-ID: <3A263B0E.379265F1@lri.fr> (raw)
In-Reply-To: <HDEEKOMJILGEIHIMAPCDIEBPDLAA.mattias.waldau@abc.se>

Mattias Waldau a écrit :
> 
> I have a list of vectors, I would like to sort according to the first
> vector, and move the corresponding data in the other vectors.
> 
> Is there a function for this (for example, a sort function, where I add a
> function that is called each time data is swapped) or do I have to write a
> quicksort in ocaml.
> 
> /mattias

Maybe you are not using the right data representation ? Things would be
much simpler if you used a vector of lists instead of a list of vectors
; just a matter of

Array.sort (fun l1 l2 -> compare (List.hd l1) (List.hd l2)) lv

Now, you can easily convert your initial representation to this one as
follows (untested code):

(* get : int -> 'a array list -> 'a list *)
let get i vl = List.map (fun t -> t.(i)) vl

(* lv_of_vl : 'a array list -> 'a list array *)
let lv_of_vl vl =
  Array.init (Array.length (List.hd vl))
             (fun i -> get i vl)

(* set : int -> 'a list -> 'a array list -> unit *)
let set i l vl =
  List.iter2 (fun x t -> t.(i) <- x) l vl

(* vl_of_lv : 'a list array -> 'a array list -> unit *)
let vl_of_lv lv vl =
   Array.iteri (fun i l -> set i l vl) lv


Sincerly yours,

Judicaël.
-- 
Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/
(+33) (0)1 69 15 64 85
"Montre moi des morceaux de ton monde, et je te montrerai le mien"
Tim, matricule #929, condamné à mort.
http://rozenn.picard.free.fr/tim.html



      parent reply	other threads:[~2000-12-03 22:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-21  8:23 Jocelyn Serot
2000-11-29 17:36 ` sorting of list of vectors (array with one dimension) Mattias Waldau
2000-11-30  7:37   ` John Max Skaller
2000-11-30 11:47     ` Sven LUTHER
2000-11-30 12:21       ` John Max Skaller
2000-12-01 18:44         ` Mattias Waldau
2000-12-02 16:00           ` John Max Skaller
2000-11-30 11:33   ` Judicael Courant [this message]

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=3A263B0E.379265F1@lri.fr \
    --to=judicael.courant@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=mattias.waldau@abc.se \
    /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