[10 Feb 2009] Enhanced list handling - is this something that could be implemented?

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

[10 Feb 2009] Enhanced list handling - is this something that could be implemented?

Post by TheMartian »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 10 Feb 2009 20:18:13 -0000

Hi

A wish for enhanced functionality when handling lists. Could something like this be implemented? (Pls. see included code for a practical example of what I have in mind).

Recently I found myself in need of some added functionality for handling lists of items beyond what is currently available as built- in functions in Hollywood 3.x. In effect I needed a linked list where each link knew the link before and the link after in a closed loop (very handy for managing taborders among user controls for one thing). The start of the list would then simply be declared by a pointer to one item in the list that could be changed at any time. Further ther would be a counter for the number of items in the list. Finally there would be a way to store the current list being worked on for use as defaultvalue. Each item in the list has a sublist with a 'previous' and a 'next' value and indexed by the name of the object. These values refers to the indexnames of the actual objects, which are stored in another 'objects' list.

I created a number of functions for managing such lists as follows:

Code: Select all

linklists.new (linklistname$) - this creates a new list

linklist.addlink (id$,oldid$,linklistname$) - adds a link to a list before object(oldid$)

linklist.removelink(id$,linklistname$) - removes a link (and thus references to an object) from the list.

linklist.movelink(id$,targetid$,linklistname$) - moves a link to location just before link identified by targetid$

linklist.swaplinks(id1$,id2$,linklistname$) - swaps location of two links

linklists.getnextlink (id$,linklistname$) - Returns next link after link identified by id$ (or last link by default)

linklists.getpreviouslink(id$,linklistname$) - Returns previouslink before link identified by id$ (or first link by default)

linklists.getlinkcount(linklistname$) - Returns number of links in  list.

linklists.setfirstlink(id$,linklistname$) - Sets pointer to first link in the list.
The main trick as I see it for doing it this way is that you have absolute control over the order of items and may reference them by stringnames. Also you can shift the relative order of the elements around easily. By providing sensible defaults populating, managing and depopulating the lists can be done with little code.

A small example:

Assume you have three objects as in Object={"No1","No2","No3"}

Code: Select all

linklists.new("MyList") This creates a new (empty) list identified by "MyList" and sets the currentlistname$ to "MyList"
linklist.addlink("No2") Adds an entry for object["No2"] in our current list as its last item.
linklist.addlink("No3") Adds another entry at the end.
linklist.addlink("No1","No2") Adds and entry for object["No1"] before the entry for object["No2"] in our current list.
To delete all objects in the list we would simply do this:

Code: Select all

While linklists.getlinkcount > 0
    linklists.removelink() /* Removes the last link in the current list */
Wend
If you need the object next to object["No2"] in the current list you just ask for:

Code: Select all

linklists.getnextlink("no2")
To get the name of the first object you say:

Code: Select all

linklists["MyList"].first$ /* (since 'first$' is the pointer to the first link) */
linklists.getpreviouslink() /* another way of getting the first link using a default */
The last object in the list can be had by:

Code: Select all

linklists["MyList"][first$].prev$ (Remember it is a closed loop so last and first are next to each other)
linklists.getnextlink() /* Another way of getting the last link using a default */
I have put up the code I have used to implement it including some example code for use in a file called 'linklist_include.hws' in the folder 'JSPGui v1.0x'. It should run directly.
User avatar
airsoftsoftwair
Posts: 5914
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[13 Feb 2009] Re: Enhanced list handling - is this something that could be implemented?

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 13 Feb 2009 22:55:03 +0100
Hi

A wish for enhanced functionality when handling lists. Could something like this be implemented? (Pls. see included code for a practical example of what I have in mind).
Sure, double-linked lists could be implemented in Hollywood. However, I currently don't see enough practical use for them because with Lua power inside of Hollywood, there's usually an easier way to store your data than in those lists.

So I'd advise everyone who really needs double-linked lists to simply use your implementation which should be enough. If you don't have any huge databases, a native Hollywood implementation wouldn't make a big difference anyway.
Locked