LINKED LIST structure in Max

julien breval's icon

hello,

I need a linked list data structure in Max and cannot find a way to implement it
http://en.wikipedia.org/wiki/Linked_list

actually I only need to perform the following operations on a single list:

* get the current size of the list (possible with zl len)
* get the value of any element of the list (possible with zl nth)
* add a new element at the end of the list (possible with zl join)
* remove the element of index k from the list (probably possible)

I would not want to remove one element of the list using a complicated combination of zl slice, zl ecils and zl join, though I guess it would work

any better idea?

best,
Julien

pid's icon

can all be done with zl as you suspect, or there are the mxj classes of list ops.

if you gonna go crazy over such things you should look into the amazing and awesome bach project. http://www.bachproject.net/bach/home_page.html

as far as i am concerned bach is the 'fifth beatle' to max.

julien breval's icon

Thanks for you answer

Bach project looks really interesting but sorry I am not into computer-aided composition nor automatic composition (some of my music includes a computer as an instrument but it does not go any further).
In this field you may be interested in PWGL
http://www2.siba.fi/PWGL/

Back to linked lists... I'm not crazy at all about such things, and I would not have imagined it would be that complicated (not complex) to implement in Max. It is done in a few minutes in C/C++/C#, and you can always find it on the Internet anyway.
I think I am just losing my time on this one, this is for making something ridiculously simple but in this case Max is simply not the right tool for it, and nor is JavaScript even if it would probably work. Then Java? Why not, but I'm afraid it is too slow (and also way overkill) for this application. And I only used Java for 30 minutes in my life so really far from an expert.

-j

johnpitcairn's icon
andrea agostini's icon

Hi
pid, thank you for your appreciation of bach :) - in fact, julien, you can as well use bach as a simple power-tool for lists, without touching the more strictly musical tools.

But I also wish to point out some things:
- you can easily do your own abstraction for removing the nth element, it's gonna take you 2 minutes
- if is much more efficient to retrieve the nth element of an array (as in zl) than a linked list. The power of linked lists shows up when you want to insert, remove or sort elements.
- the coll object is internally based on a linked list (well, I'm almost sure of this). So if you want an arbitrarily-sized collection of data with really fast insertion and deletion, coll might be your thing.
- linked lists require pointers, which Max don't give you. Just for fun, you might want to try to emulate them with weird operations on a buffer~ (you could think of it as a generic memory pool), and you still would have huge limitations. Apart from this, I don't see a way of implementing a linked list without writing some code... But then, why not use bach? ;)

hth
aa

julien breval's icon

Thanks for your answers
(btw isn't it possible to quote what the others wrote on this forum? copy-paste workaround here sorry)

"Maybe the lobjects might help you out"
Did not see these, I will try it

" in fact, julien, you can as well use bach as a simple power-tool for lists, without touching the more strictly musical tools."
I did not know about that and will also try it

"you can easily do your own abstraction for removing the nth element, it's gonna take you 2 minutes"
I did it, but it was that easy to integrate into the final program. Either I am lazy or don't want to loose too much time with it. See below (about coll).

"if is much more efficient to retrieve the nth element of an array (as in zl) than a linked list. The power of linked lists shows up when you want to insert, remove or sort elements."
Yes, but I do want to insert or remove elements. This is a dynamic list.

"the coll object is internally based on a linked list (well, I'm almost sure of this). So if you want an arbitrarily-sized collection of data with really fast insertion and deletion, coll might be your thing."
Many thanks, that's obviously the best solution for my application. How could I forget it? I have used coll a lot in the past!

"linked lists require pointers, which Max don't give you. Just for fun, you might want to try to emulate them with weird operations on a buffer~ (you could think of it as a generic memory pool), and you still would have huge limitations."
I am not interested in the linked list itself, but it is a generic way for getting dynamic lists. But coll will do it inside Max.

best,
julien

johnpitcairn's icon

Coll may not be as fast as other solutions, depending on the length of the list. Searching for or performing an operation on an index in a coll requires more time for higher indexes - the search is sequential from the first index.