By Daniel · December 30, 2008
This is a collection of scripts which implement linked lists in GML.
Why use linked lists? Depending on what you're doing, they can sometimes be faster than arrays (or ds_lists, which are array-driven lists). When you remove an element from a ds_list, Game Maker iterates through all proceeding elements and moves them to lower indices -- a potentially expensive operation, if you're dealing with large lists. With linked lists, only one assignment is necessary. The download includes a demonstration of this. This isn't a very good reason to use linked lists, though, as you can get the same efficiency with ds_stacks (which use linked lists internally).
The better reason to use linked lists is, they're nice to work with. Linked lists are completely immutable (with this implementation), so you can pass around references easily without having to do any cloning or worry about losing data. Here are some examples of the things you can do:
ABCD = ds_linkedlist_create(4, 'A', 'B', 'C', 'D'); A = ds_linkedlist_first(ABCD); BCD = ds_linkedlist_rest(ABCD); show_message(ds_linkedlist_repr(ABCD)); // displays "[A, B, C, D]" C = ds_linkedlist_nth(ABCD, 2); FABCD = ds_linkedlist_cons(ABCD, 'F'); ds_linkedlist_destroy(FABCD);
Enjoy,
Daniel
Categories: None
There are no comments to display.
You must be signed in to post comments.