www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multisort

reply dsimcha <dsimcha yahoo.com> writes:
I've been working on implementing some statistics functions in D, and noticed
that it would be nice if a multisort function were in Phobos.  Such a function
would take N arrays in and sort them by the first one.  The common way to
handle something like this seems to be to make a struct with the fields, make
opCmp point to whatever field you want to sort by, and then use a regular
sorting algorithm.  This is slow and a pain.  I've implemented a few different
multisort functions, along with documentation and unit testing, and would like
to contribute them to Phobos.  I've attached the source file and the Ddoc
documentation.
Jul 06 2008
next sibling parent bearophile <bearophileHUGS mailas.com> writes:
I haven't tried your code (yet), but I suggest you to not use too much long
lines (90-100 chars is probably enough).

Your code contains both the forms:
    foreach(ti, array; data) temp[ti][index] = array[$-1];
    ...
    foreach(i, array; temp) {
        data[i][0..$] = array[0..$];
    }

I suggest you to use the second form, or the following one if you want to save
a line:
    foreach (i, array; temp)
        data[i][0..$] = array[0..$];

I suggest you to avoid writing a single large unittest, but to add one block of
unittests under each function/class/method, so you can isolate them better,
disable them, etc (even better is having a standard way to give them a name, so
the program/compiler that executes the unit tests knows what
function/class/method are broken. D unittest system needs just small
improvements to become good enough for small/medium size programs).

Bye,
bearophile
Jul 06 2008
prev sibling parent superdan <super dan.org> writes:
dsimcha Wrote:

 I've been working on implementing some statistics functions in D, and noticed
 that it would be nice if a multisort function were in Phobos.  Such a function
 would take N arrays in and sort them by the first one.  The common way to
 handle something like this seems to be to make a struct with the fields, make
 opCmp point to whatever field you want to sort by, and then use a regular
 sorting algorithm.  This is slow and a pain.  I've implemented a few different
 multisort functions, along with documentation and unit testing, and would like
 to contribute them to Phobos.  I've attached the source file and the Ddoc
 documentation.

yarp i needed this 2. sounds good but it's unnecessary. sort does all that shit easily. int[] arr1; float[] arr2; string[] arr3; // sort three arrays as one void mySwap(int* a, int* b) { auto i = a - arr1.ptr, j = b - arr1.ptr; iterSwap(a, b); swap(arr2[i], arr2[j]); swap(arr3[i], arr3[j]); } sort!("a < b", SwapStrategy.unstable, mySwap)(arr1); what happens is that whenever two doods in arr1 get swapped, the corresponding doods in arr2 and arr3 get swapped too. i don't know why the docs don't give such examples. there's just a hint "Possible uses include notifying observers, counting the number of operations, or manipulating multiple collections in lockstep." the lockstep thing gave it away. & of course you deserve shoe heel beating for putting bubblesort in there. no matter how much you warn against its usage.
Jul 06 2008