List Push and Pop
|
|
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
|
09-03-2005 14:09
hello again! i am trying to build a fast push/pop function for using with lists. i need to insert 1 element on the begin, and remove the last one on the list. shame on me but, i can't script it simple, is there any geek that made it before?
|
|
Minsk Oud
Registered User
Join date: 12 Jul 2005
Posts: 85
|
09-03-2005 14:17
To do it simply, all you need is: list PopBack(list x) { return llDeleteSubList(x, -1, -1); }
list PushFrontString(list x, string s) { return + x; }
Not sure off the top if "llList2String(x, -1)" will work to get the last element, or if you need "llList2String(x, llGetListLength(x) - 1)". Honestly I would just build it the other way around (add elements to the end of the list and remove them from the front). Be aware that with a large list, or rapid pushes and pops, your performance will be absolutely horrid. <edit>*gasp* No, I didn't notice. It may be educational to discover that not all stack/queue implementations (C++ STL, for example) provide values from the pop. For most applications it makes... absolutely. no. difference.</edit>
_____________________
Ignorance is fleeting, but stupidity is forever. Ego, similar.
|
|
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
|
09-03-2005 16:19
That's not quite it, Minsk. the Push function is fine, but the Pop doesn't return an element. The main problem is that you need to return both an element and a list.
This needs further consideration. LSL is not a good environment for LILO or LIFO type functions.
|
|
Tiger Crossing
The Prim Maker
Join date: 18 Aug 2003
Posts: 1,560
|
09-03-2005 17:13
If the list variable is global and always the same one, you can avoid returning the altered list by changing it directly. Then you can just return the popped value.
_____________________
~ Tiger Crossing ~ (Nonsanity)
|
|
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
|
09-04-2005 02:36
From: Minsk Oud ... Honestly I would just build it the other way around (add elements to the end of the list and remove them from the front). u may be right, i am using this inverstion... well, like i say on every new thing that i try to build: "gonna stop this project, found another LSL limitation"  ... just joking, this project can't stop! yes the list is big, 80 elements. the goal is keep only the last 80 entrys. i am gonna keep the "pull" & "delete" :]
|
|
Kurt Zidane
Just Human
Join date: 1 Apr 2004
Posts: 636
|
09-04-2005 09:13
for performance reasons, I would consider using the ll funtions directly. That way, the sls dosen't have to copy the list, before editing the list.
|
|
Minsk Oud
Registered User
Join date: 12 Jul 2005
Posts: 85
|
09-04-2005 11:59
From: Kurt Zidane for performance reasons, I would consider using the ll funtions directly. That way, the sls dosen't have to copy the list, before editing the list. Try it; when I looked at memory consumption I found no evidence that the lists are copied on calls to or returns from a user function. If fact the system behaves like they are reference counted. Would not expect to see severe problems at 80 entries. I usually start seeing performance problems at a few hundred elements, and it should be clear of 500 before you hit the memory limit.
_____________________
Ignorance is fleeting, but stupidity is forever. Ego, similar.
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
09-04-2005 14:01
From: Minsk Oud I usually start seeing performance problems at a few hundred elements, and it should be clear of 500 before you hit the memory limit. Even storing keys and rotations? That's useful info to file away.
|
|
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
|
09-04-2005 16:32
i can now store up to 100 entrys, i am storing few data on each entry. i need to add a new entry to the list, and remove the last one (older). like u say this takes too much time rearanging the list, and worst, too much memory. the bad thing is that i need to remove 1 record at random, and just reajust everything (there is an integer on this list, from 0 to 99 - if i remove 33, then everything should fall 1 number down...) big problems alot of work. this is the only thing that i am not beeing able to do... push/pop 
|
|
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
|
09-04-2005 17:08
llDeleteSubList( 100elements,33, 33 )
I think that's how it works, anyways.
|
|
Fire Centaur
Creator
Join date: 2 Nov 2006
Posts: 149
|
working pop and push
09-30-2007 08:30
Hi, thanks for your examples! I worked on them a bit and here's what I got working: list x; list test; integer count=0; string Pop() { string r = llList2String(test,0); test = llDeleteSubList(test, 0, 0); return r; } Push(string s) { test+= ; } default { state_entry() { Push("H" ; Push("e" ; Push("l" ; Push("l" ; Push("o" ; integer ii; for (ii=0; ii< llGetListLength(test); ii++) llSay(0, llList2String(test,ii));
}
touch_start(integer total_number) { llSay(0,Pop() + " Length is now "+ (string) llGetListLength(test)); } }
_____________________
Second Life to your Facebook Profile!  http://www.facebook.com/p.php?api_key=00b8153d7f12a4c7228b6c927fb2c537
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
09-30-2007 09:38
couldn't you just do this... list gLstLast100; fPushPop( string vStrAdd ){ gLstLast100 = [vStrAdd] + llList2List( gLstLast100, 0, 98 ); } default{ //uncomment state entry to show it returning correct max list //leave commented to show growth // state_entry(){ // integer i = 0; // do{ // gLstLast100 += ; // }while ((++i) < 100); // } touch( integer vTouched ){ fPushPop( llDetectedKey( 0 ) ); llOwnerSay( llList2CSV( gLstLast100 ) ); } } according to wiki, list2list treats the entrys past the end of a smaller list as if they were there, but removed before the function returns... EDIT: tested and corrected logic glitch it'll only return the valid elements... PoC above
|