Forums > Dev

linklist_sort() help


MIB
June 23, 2009 | 7:48 pm

I am very new to C programming and even newer to the Max SDK.
Could someone explain the linklist_sort() to me?

long myAlphabeticalCmpfn(void *a, void *b)
    {
        t_symbol *s1 = (t_symbol *)a;
        t_symbol *s2 = (t_symbol *)b;

        if(s1->s_name[0] < s2->s_name[0])
            return true;
        else
            return false;
    }

    void mySortMethod(t_myobj *x)
    {
        // the linklist was already created and filled with items previously
        linklist_sort(x->myLinkList, myAlphabeticalCmpfn);
    }

My first problem is that there doesn’t seem to be a function that walks through the linked list. I pass the linked list to the myAlphabeticalCmpfn but there seems to be no equivalent to x->next. So how does it iterate through the whole list?

The myAlphabeticalCmpfn only returns a true or false and doesn’t do any sorting. Where does the sorting take place? What kind of sort is it, swap-sort?

I believe t_symbol is a struct. Is there an equivalent for float (ultimately I want to sort floats).

Sorry if this is all obvious, but I am at a roadblock here. Any input is very much appreciated.



MIB
June 24, 2009 | 5:10 pm

thanks for the reply.
I will have a look at your code later today and report back. thanks again.



MIB
June 24, 2009 | 5:46 pm

Just had a quick look (will dig into it more a little later), but just a question regarding t_linklist. In the doc it says:
"This struct is provided for debugging convenience, but should be considered opaque and is subject to change without notice."
The same goes for t_llelem.
Does this mean that I really should define my own struct?

My struct is going to look something like this:

struct Data{
	int id;
	int depth;
	struct Coord coord1;
	struct Coord coord2;
	float angle;
	float maxDist;
	struct Data *next;
} ;

struct Coord{
	float x_coord;
	float y_coord;
};

And ultimately I would want to try and sort according to coord1.x_coord


June 24, 2009 | 8:03 pm
MIB wrote on Wed, 24 June 2009 19:46
In the doc it says:
"This struct is provided for debugging convenience, but should be considered opaque and is subject to change without notice."
The same goes for t_llelem.
Does this mean that I really should define my own struct?

No, it means you should use t_linklist but you !!MUST!!NOT!! access the struct members directly. Instead use API functions linklist_new() and the other linklist_xxx() functions. linklist_sort() will not work if you use a custom struct. It will probably crash and burn.


June 24, 2009 | 9:28 pm

Just a bit more information. There are a number of things you’re confused about

MIB wrote on Tue, 23 June 2009 21:48
My first problem is that there doesn’t seem to be a function that walks through the linked list. I pass the linked list to the myAlphabeticalCmpfn but there seems to be no equivalent to x->next. So how does it iterate through the whole list?

You don’t need to walk the list to sort it, and you don’t "pass the list" to myAlphabetCmpfn().

linklist_sort() sorts the list. It takes two parameters. The first parameter is your linked list. So far so good. But linklist_sort() doesn’t know what your sort criteria are. That’s what the second parameter is for. The 2nd parameter is a pointer to a function that takes two list elements and returns TRUE if the first is "before" the second according to whatever your criteria are. The myAlphabetCmpfn() code is simply an example of one way to do that. It works on the assumption that your list elements are t_symbols and that you want a (very primitive) ASCII sort. You will need to adapt it to your particular circumstances.

Quote:
Where does the sorting take place?

see above.

Quote:
What kind of sort is it, swap-sort?

Probably some version of qsort(), but you don’t really care.

Quote:
I believe t_symbol is a struct.

Don’t guess: look it up.

Quote:
Is there an equivalent for float (ultimately I want to sort floats).

List items are typically Max objects. They are, in any case, pointers. You could possibly typecast your (32-bit) floats to (void*), although that seems like Truly Vile Idea. It would be cleaner to define your own data type, even if it’s only:

struct myType { float myFloat; };

and work with that. That is basically what Béchamel’s code is doing.


Edited to clarify usage of 2nd param to linklist_sort(). Also corrected an annoying typo


June 26, 2009 | 10:38 am

Take a look at the prototype for atom_setfloat().

No, the typecast isn’t necessary and, in this case, is probably counter-productive.

There was a lot of explicit typecasting in C for the orig. K&R spec, but that’s almost all superfluous since about 1990.


Viewing 6 posts - 1 through 6 (of 6 total)