Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Mattias Waldau" <mattias.waldau@abc.se>
To: "John Max Skaller" <skaller@ozemail.com.au>,
	"Sven LUTHER" <luther@dpt-info.u-strasbg.fr>,
	<caml-list@inria.fr>
Subject: RE: sorting of list of vectors (array with one dimension)
Date: Fri, 1 Dec 2000 19:44:57 +0100	[thread overview]
Message-ID: <HDEEKOMJILGEIHIMAPCDEEEMDLAA.mattias.waldau@abc.se> (raw)
In-Reply-To: <3A26462C.F8AEFA3F@ozemail.com.au>

Nice suggestions changing the data structure, but I can't change it.

Instead, I implemented the indirection trick by Max Skaller. However instead
of finding how to reorder the data, I just use a temporary data structure
instead.

The algoritm is first to find how to reorder (sort_columns_step_1), and then
l the reordering on each array (sort_columns_step_2).

In this example I like to sort the arrays t1,t2 and t3 according to t1

let t1 = [|2;1;4;3|] in
let t2 = [|true;false;false;true|] in
let t3 = [|"mattias";"magnus";"george";"hugo"|] in
let reorder = sort_columns_step_1 t1 in
   sort_columns_step_2 ~reorder t1;
   sort_columns_step_2 ~reorder t2;
   sort_columns_step_2 ~reorder t3;
   (t1,t2,t3);;
              - : int array * bool array * string array =
[|1; 2; 3; 4|], [|false; true; true; false|],
[|"magnus"; "mattias"; "hugo"; "george"|]

and the code is

let sort_columns_step_1 (a:'a array) : int array =
  let a_i = Array.init (Array.length a) (fun ii -> ii) in
  let compare_a = (fun e1 e2 -> compare a.(e1) a.(e2)) in
  Array.sort compare_a a_i;
  a_i

let sort_columns_step_2 ~(reorder:int array) (a:'a array) =
  let a_temp = Array.init (Array.length a) (fun ii -> a.(reorder.(ii))) in
  Array.iteri (fun ii el -> a.(ii) <- el) a_temp

----
Mattias Waldau, CTO
Tacton AB, Saltmatargatan 7, 11359 Stockholm
Mobile +46 70 549 11 12
http://www.tacton.com mailto:mattias.waldau@tacton.com







  reply	other threads:[~2000-12-03 22:22 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 [this message]
2000-12-02 16:00           ` John Max Skaller
2000-11-30 11:33   ` Judicael Courant

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=HDEEKOMJILGEIHIMAPCDEEEMDLAA.mattias.waldau@abc.se \
    --to=mattias.waldau@abc.se \
    --cc=caml-list@inria.fr \
    --cc=luther@dpt-info.u-strasbg.fr \
    --cc=skaller@ozemail.com.au \
    /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