Welcome to Web-News
A Web-based News Reader
Subject Re: Randomness in built-in .sort
From bearophile <bearophileHUGS@lycos.com>
Date Mon, 05 Jan 2009 20:18:21 -0500
Newsgroups digitalmars.D

Bill Baxter:

>I use this all the time in NumPy in the form of "argsort" which returns a list of indices giving the sort order.  That can then be used as to index other arrays (thereby permuting them to the sorted order).
order = npy.argsort(keys)
sortedA = A[order]
sortedB = B[order]<

My dlibs already contain an efficient sorting routine, much faster than the built in one.

So to my dlibs I have just added sortedIndexes and remapOrder, you can find their docs here:
http://www.fantascienza.net/leonardo/so/dlibs/func.html

The code:
http://www.fantascienza.net/leonardo/so/libs_d.zip

The usage is very simple, sortedIndexes is similar to sorted():

auto a =  [-5, 2, 3, 1, -11].dup;
auto order1 = a.sortedIndexes();
// now order1 == [4U, 0, 3, 1, 2]

auto order2 = a.sortedIndexes((int x){return x < 0 ? -x : x;});
// Now order2 == [3U, 1, 2, 0, 4]

int[2][] c = [[3, 4], [1,2], [1, 1]];
auto order3 = c.sortedIndexes();
// now order3 == [2U, 1, 0]

auto a =  [-5, 2, 3, 1, -11].dup;

a.remapOrder([4, 0, 3, 1, 2]) ==> [-11, -5, 1, 2, 3]
// a isn't changed here
a.remapOrder([3, 1, 2, 0, 4]) ==> [1, 2, 3, -5, -11]

auto b = ["oranges"[], "for", "apples", "is", "right"].dup;
b.remapOrder([3, 1, 2, 0, 4]) ==>
["is"[], "for", "apples", "oranges", "right"].dup);

(Maybe I have to rename remapOrder as remappedOrder).

--------------------------------

While writing the unittest for those functions, I have seen this isn't accepted by DMD:
int[2][] c = [[3, 4], [1, 2], [1, 1]].dup;

While the compiler accepts this, I don't know why:
int[2][] c0 = [[3, 4], [1, 2], [1, 1]];
int[2][] c2 = c0.dup;

Bye,
bearophile

Recent messages in this thread
 
-# Randomness in built-in .sort Bill Baxter 04-Jan-2009 09:25 pm
.-# Re: Randomness in built-in .sort dsimcha 04-Jan-2009 10:05 pm
.||# Re: Randomness in built-in .sort Bill Baxter 04-Jan-2009 10:23 pm
.|-# Re: Randomness in built-in .sort Andrei Alexandrescu 05-Jan-2009 01:08 am
.|.-# Re: Randomness in built-in .sort dsimcha 05-Jan-2009 10:16 am
.|..-# Re: Randomness in built-in .sort Don 05-Jan-2009 10:44 am
.|..|\# Re: Randomness in built-in .sort dsimcha 05-Jan-2009 11:07 am
.|..-# Re: Randomness in built-in .sort Bill Baxter 05-Jan-2009 03:36 pm
.|...-# Re: Randomness in built-in .sort dsimcha 05-Jan-2009 04:15 pm
.|....-# Re: Randomness in built-in .sort Bill Baxter 05-Jan-2009 04:41 pm
.|....|-# Re: Randomness in built-in .sort (Current message) bearophile 05-Jan-2009 08:18 pm
.|....|.|# Re: Randomness in built-in .sort bearophile 05-Jan-2009 08:22 pm
.|....|.\# Re: Randomness in built-in .sort Bill Baxter 05-Jan-2009 08:55 pm
.|....-# Re: Randomness in built-in .sort Benji Smith 05-Jan-2009 07:41 pm
.|.....-# Re: Randomness in built-in .sort Andrei Alexandrescu 05-Jan-2009 09:43 pm
.|......-# Re: Randomness in built-in .sort Walter Bright 06-Jan-2009 12:03 am
.|.......\# Re: Randomness in built-in .sort Andrei Alexandrescu 06-Jan-2009 02:06 am
.-# Re: Randomness in built-in .sort Stewart Gordon 05-Jan-2009 06:40 am
.|\# Re: Randomness in built-in .sort Bill Baxter 05-Jan-2009 03:58 pm
.-# Re: Randomness in built-in .sort Walter Bright 06-Jan-2009 12:35 pm
..-# Re: Randomness in built-in .sort Sean Kelly 06-Jan-2009 01:56 pm
...-# Re: Randomness in built-in .sort Ary Borenszweig 06-Jan-2009 02:06 pm
....-# Re: Randomness in built-in .sort Walter Bright 06-Jan-2009 02:09 pm
.....\# Re: Randomness in built-in .sort Bill Baxter 06-Jan-2009 05:07 pm