ListUnique: Difference between revisions

From Second Life Wiki
Jump to navigation Jump to search
Lachesis Ethereal (talk | contribs)
 
(4 intermediate revisions by 2 users not shown)
Line 9: Line 9:




<lsl>
<source lang="lsl2">
list ListUnique( list lAll ) {
list ListUnique( list lAll ) {
     integer i;
     integer i;
Line 22: Line 22:
}
}


</lsl>
</source>




Line 38: Line 38:
Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html
Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html


<div>
</div>


A *possibly* faster replacement (at least in my experiments in removing duplicate Agent UUIDs from multiple scanners on a sim):
<div id="box">
== Function: [[list]] ListSortedAndUnique([[list]] {{LSL Param|input}}, [[integer]] {{LSL Param|ascending}}) ==
<div style="padding: 0.5em;">


<lsl>
If you don't need to maintain the order of items (or want them sorted anyway), the following code is consistently about 2x faster on short lists, and 3-5x faster on lists with hundreds of items.
list nvStripDupes(list DupedList)
 
{
<source lang="lsl2">list ListSortedAndUnique(list input, integer ascending) {
     integer pos;
    input = llListSort(input, 1, ascending);
     list Element;
     integer index;
     while (llGetListLength(DupedList) != 0)
     list   last  = llList2List(input, 0, 0);
    {
     list    result = last;
         Element = llList2List(DupedList, 0, 0);
    for (index = 1 - llGetListLength(input); index < 0; ++index) {
         NoDupes += Element;
         list current = llList2List(input, index, index);
        pos = llListFindList(DupedList, Element);
         if (llListFindList(current, last) < 0) {
        while ( pos != -1)
             last    = current;
        {
             result += last;  
             DupedList = llDeleteSubList(DupedList, pos, pos);
             pos = llListFindList(DupedList, Element);
         }
         }
     }
     }
     return NoDupes;
     return result;
}
}
</lsl>
</source>
</div>
 
The reason is that [[llListFindList]] performs a linear scan through the entire list, thus ListUnique compares every element with every other element. [[llListSort]] however compares each element only to a few others (the exact number depending on the sorting algorithm), and after sorting ListSortedAndUnique only compares neighboring elements.
 
</div>
{{LSLC|Examples|ListUnique}}
{{LSLC|Examples|ListUnique}}
{{LSLC|Examples|ListSortedAndUnique}}
[[Category:LSL_User-Defined Functions]]
[[Category:LSL_User-Defined Functions]]

Latest revision as of 13:29, 13 October 2025

Function: list ListUnique(list lAll);

Given a list of elements, strips out any duplicates in that list, and returns the de-duped list.

See also: Lists


list ListUnique( list lAll ) {
    integer i;
    list lFiltered = llList2List(lAll, 0, 0);
    integer iAll = llGetListLength( lAll );
    for (i = 1; i < iAll; ++i) {
        if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) {
            lFiltered += llList2List(lAll, i, i);
        }
    }
    return lFiltered;
}


Example:

list mylist = ListUnique(["A", "A", "B", "C", "C", "B"])

would return the list:

["A", "B", "C"]


Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html

Function: list ListSortedAndUnique(list input, integer ascending)

If you don't need to maintain the order of items (or want them sorted anyway), the following code is consistently about 2x faster on short lists, and 3-5x faster on lists with hundreds of items.

list ListSortedAndUnique(list input, integer ascending) {
    input = llListSort(input, 1, ascending);
    integer index;
    list    last   = llList2List(input, 0, 0);
    list    result = last;
    for (index = 1 - llGetListLength(input); index < 0; ++index) {
        list current =  llList2List(input, index, index);
        if (llListFindList(current, last) < 0) {
            last    = current;
            result += last; 
        }
    }
    return result;
}

The reason is that llListFindList performs a linear scan through the entire list, thus ListUnique compares every element with every other element. llListSort however compares each element only to a few others (the exact number depending on the sorting algorithm), and after sorting ListSortedAndUnique only compares neighboring elements.