octave-maintainers
[Top][All Lists]

## Re: benchmarks - sort

 From: David Bateman Subject: Re: benchmarks - sort Date: Thu, 22 Jan 2004 00:06:46 +0100 User-agent: Mutt/1.4.1i

```Daprès address@hidden <address@hidden> (le 21/01/2004):
> The Scheme Library has a nice solution for merge-sort functionality:
>
> SLIB
> http://www.swiss.ai.mit.edu/~jaffer/slib_7.html#SEC199
>
> You use it like this:
>
> guile> (sort (list 3 3 8 1 9 4) <)
> (1 3 3 4 8 9)

At the moment I have the constructors

octave_sort::octave_sort (void) : compare (NULL) { merge_init ( ); }

octave_sort::octave_sort (bool (*comp) (T, T)) : compare (comp) { merge_init
( ); }

So you can define a function like

bool
double_compare (double a, double b)
{
return (xisnan(b) || (a < b));
}

and then call

octave_sort<double> mysort (compare);

if you don't define the compare function then it is assumed to be "<". So
this is exactly like what you are talking about in scheme. Connecting this
to the user interface might not even be that difficult.

>
> So, 'sort' is a function that accepts two arguments: the first one is
> the list ( or, in Octave's case, a one-dimensional array/cell-array ),
> and a function which tells the sorting function how to compare two
> objects.
>
> By passing the comparison function to the sort algorithm, it allows
> sort to be generic; any data structure that an end-user might have to
> work with can be sorted, provided that they make a simple predicate
> function that can compare any two of them.
>
> I imagine that this can be accomplished in Octave via function
> handles, no?

Yeah, but it might be limited to only sorting cell arrays, as I'd need
to instantiate the octave_sort class in advance for all of the possible
types of the first argument.

Cheers
David

--
Motorola CRM                                 +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax)
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: