Difference between revisions of "User talk:ANSI Soderstrom/Simple LSL Array (Associative)"
Jump to navigation
Jump to search
m (→Thoughts) |
(→A quick implementation: new section) |
||
Line 7: | Line 7: | ||
# This is not what I think of when i think of "array" nor a simple one at that, what you have here is an "{{Wikipedia|associative array|associative array}}". | # This is not what I think of when i think of "array" nor a simple one at that, what you have here is an "{{Wikipedia|associative array|associative array}}". | ||
-- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 06:24, 22 April 2010 (UTC) | -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 06:24, 22 April 2010 (UTC) | ||
== A quick implementation == | |||
It's possible that it's not the best way to implement the loop condition. But it's clever. I've also added support for setting and removing multiple keys at a time. This is for convenience and there is little to no performance gain from doing this vs individual calls. I've checked the logic, it should work, I haven't tested it though. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 07:14, 22 April 2010 (UTC) | |||
<lsl>list GET(list ARRAY, list KEY) { | |||
if(llGetListLength(KEY) == 1) { | |||
integer position = llListFindList(ARRAY, KEY) + 1; | |||
if(position) { | |||
return llList2List(ARRAY, position, position); | |||
} | |||
} | |||
return []; | |||
} | |||
list PUT(list ARRAY, list KEY, list VALUE){ | |||
integer len = -llGetListLength(KEY); | |||
if(len == -llGetListLength(VALUE) && len) { | |||
integer position; | |||
do { | |||
list temp = []; | |||
list key = llList2List(KEY, len, len); | |||
while((position & 1) == !(position = llListFindList(ARRAY, key) + 1)) { | |||
temp = temp + llList2List(ARRAY, 0, position); | |||
ARRAY = llDeleteSubList(ARRAY, 0, position); | |||
} | |||
if(position) { | |||
ARRAY = temp + llListReplaceList(ARRAY, llList2List(VALUE, len, len), position, position); | |||
} else { | |||
ARRAY = temp + ARRAY + llList2List(KEY, len, len) + llList2List(VALUE, len, len); | |||
} | |||
} while( ++len ); | |||
} | |||
return ARRAY; | |||
} | |||
list REMOVE(list ARRAY, list KEY){ | |||
integer len = -llGetListLength(KEY); | |||
if(len) { | |||
do { | |||
list temp = []; | |||
integer position; | |||
list key = llList2List(KEY, len, len); | |||
while((position & 1) == !(position = llListFindList(ARRAY, key) + 1)) { | |||
temp += llList2List(ARRAY, 0, position); | |||
ARRAY = llDeleteSubList(ARRAY, 0, position); | |||
} | |||
if(position) { | |||
ARRAY = temp + llDeleteSubList(ARRAY, position - 1, position); | |||
} else { | |||
ARRAY = temp + ARRAY; | |||
} | |||
} while( ++len ); | |||
} | |||
return ARRAY; | |||
} | |||
</lsl> | |||
-- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 07:14, 22 April 2010 (UTC) |
Revision as of 23:14, 21 April 2010
Thoughts
- Why are you supporting multiple values for a key? It looks like you only return the last one with GET. PUT updates all of them which sorta makes having them pointless.
- Where is the REMOVE function?
- This is not what I think of when i think of "array" nor a simple one at that, what you have here is an " associative array".
-- Strife (talk|contribs) 06:24, 22 April 2010 (UTC)
A quick implementation
It's possible that it's not the best way to implement the loop condition. But it's clever. I've also added support for setting and removing multiple keys at a time. This is for convenience and there is little to no performance gain from doing this vs individual calls. I've checked the logic, it should work, I haven't tested it though. -- Strife (talk|contribs) 07:14, 22 April 2010 (UTC)
<lsl>list GET(list ARRAY, list KEY) {
if(llGetListLength(KEY) == 1) { integer position = llListFindList(ARRAY, KEY) + 1; if(position) { return llList2List(ARRAY, position, position); } } return [];
}
list PUT(list ARRAY, list KEY, list VALUE){
integer len = -llGetListLength(KEY); if(len == -llGetListLength(VALUE) && len) { integer position; do { list temp = []; list key = llList2List(KEY, len, len); while((position & 1) == !(position = llListFindList(ARRAY, key) + 1)) { temp = temp + llList2List(ARRAY, 0, position); ARRAY = llDeleteSubList(ARRAY, 0, position); } if(position) { ARRAY = temp + llListReplaceList(ARRAY, llList2List(VALUE, len, len), position, position); } else { ARRAY = temp + ARRAY + llList2List(KEY, len, len) + llList2List(VALUE, len, len); } } while( ++len ); } return ARRAY;
}
list REMOVE(list ARRAY, list KEY){
integer len = -llGetListLength(KEY); if(len) { do { list temp = []; integer position; list key = llList2List(KEY, len, len); while((position & 1) == !(position = llListFindList(ARRAY, key) + 1)) { temp += llList2List(ARRAY, 0, position); ARRAY = llDeleteSubList(ARRAY, 0, position); } if(position) { ARRAY = temp + llDeleteSubList(ARRAY, position - 1, position); } else { ARRAY = temp + ARRAY; } } while( ++len ); } return ARRAY;
} </lsl> -- Strife (talk|contribs) 07:14, 22 April 2010 (UTC)