Don't click here unless you want to be banned.

LSL Wiki : LibraryStridedLists

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org

Strided List Functions


These functions are made for working with strided lists. They are pretty self explanitory so not much commenting is needed. These will give you a good head start in working with your own strided lists.

Strided Lists are lists within lists. Since there is no way to make a multi-dimentional array or have lists contain other lists directly, we use what are called "Strided Lists". This would allow you to keep multiple pieces of data on each of several objects or avatars. For example, if you wanted an answering machine to store messages from people, and wanted to store their name, thier message, and the time they left it, you could use three seperate lists: listA = [Name1, Name2, Name3]; listB = [Message1, Message2, Messag3]; listC = [Time1, Time2, Time3] or you could use a strided list: listA = [Name1, Message1, Time1, Name2, Message2, Time2, Name3, Message3, Time3].

A stride within that list would be one set of parameters, or 1 Name, 1 Message and 1 Time. the Stride Length is how many elements make up a single stride. In the case of the above list, the stride length is 3.

Note: Most people stay away from strided lists due partly to complexity (which I hope to correct here) and partly due to memory usage. While I have yet to test this myself, it seems to be popular concensus that working with strided lists use more memory than working with multiple regular lists. I leave the use of the following up to your discretion.

Examples:
Note: In all of the examples below, the last parameter is the number of elements per stride. in all of the below cases, this number is 3.
Note: The functions that modify the source list now return the original unaltered list on failure.

integer fncStrideCount(list source, integer stride)
Returns the number of strides found within a list.
Source is the list to operate on, and stride is the length of each stride within the list.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

integer Result = fncStrideCount(Source, 3);
// Returns 3

list fncFindStride(list source, list item, integer stride)
Returns the stride number and element number of an element within a stride, within a list. (zero indexed)
Source is the list to search, item is a single element list containing the item to search for, and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
list Item = ["B3"];

list Result = fncFindStride(Source, Item, 3);
// Returns [1, 2].
// The first number is which stride Item is in.
// The second is which element of the stride is Item.

// If item is not found, it will return [-1, -1]

list fncDeleteStride(list source, integer index, integer stride)
Deletes a stride from a list.
Source is the list to operate on, index is which stride to delete (zero indexed), and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

list Result = fncDeleteStride(Source, 1, 3);

// Returns ["A1", "A2", "A3", "C1", "C2", "C3"]
// On Failure, it returns original list

list fncGetStride(list source, integer index, integer stride)
Returns a single stride from the list.
Source is the list to retrieve from, index is which stride to return (zero indexed), and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

list Result = fncGetStride(Source, 1, 3);

// Returns ["B1", "B2", "B3"]
// On failure returns an empty list

list fncReplaceStride(list source, list replacement, integer index, integer stride)
Replaces an entire stride within a list, and returns new list.
Source is the list to operate on, replacement is a list to replace with (Must be an entire stride), index is which stride to replace, and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
list Replace = ["D1", "D2", "D3"]; // Note it's a single and complete stride

list Result = fncReplacestride(Source, Replace, 1, 3);

// Returns ["A1", "A2", "A3", "D1", "D2", "D3", "C1", "C2", "C3"]
// On failure returns original list

list fncGetElement(list source, integer index, integer subindex, integer stride)
Returns a single element from within a stride, within a list.
Source is the list to retrieve from, index is which stride to retrieve from, subindex is which element to retrieve, and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

list Result = fncGetElement(Source, 2, 1, 3);
// Returns ["C2"]
// On failure returns an empty list

list fncReplaceElement(list source, list item, integer index, integer subindex, integer stride)
Replaces a single element within a stride within a list, and returns the new list.
Source is the list to operate on, item is a single element list containing the item to replace with, index is which stride to operate on, subindex is which element in the stride to replace, and stride is the length of each stride.
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
list Replace = ["D3"]; // Note it's a single item

list Result = fncReplaceElement(Source, Replace, 1, 2, 3);

// Returns ["A1", "A2", "A3", "B1", "B2", "D3", "C1", "C2", "C3"]
// On failure returns original list




And here are the functions
// Strided Functions For working with Strided Lists.
// By Aakanaar LaSalle

// the intStride parameter is the length of the strides within the list
// the intIndex is which stride we're working with.
// the intSubIndex is which element of the stride we're working with.

// Returns number of Strides in a List
integer fncStrideCount(list lstSource, integer intStride)
{
  return llGetListLength(lstSource) / intStride;
}

// Find a Stride within a List (returns stride index, and item subindex)
list fncFindStride(list lstSource, list lstItem, integer intStride)
{
  integer intListIndex = llListFindList(lstSource, lstItem);
  
  if (intListIndex == -1) { return [-1, -1]; }
  
  integer intStrideIndex = intListIndex / intStride;
  integer intSubIndex = intListIndex % intStride;
  
  return [intStrideIndex, intSubIndex];
}

// Deletes a Stride from a List
list fncDeleteStride(list lstSource, integer intIndex, integer intStride)
{
  integer intNumStrides = fncStrideCount(lstSource, intStride);
  
  if (intNumStrides != 0 && intIndex < intNumStrides)
  {
    integer intOffset = intIndex * intStride;
    return llDeleteSubList(lstSource, intOffset, intOffset + (intStride - 1));
  }
  return lstSource;
}

// Returns a Stride from a List
list fncGetStride(list lstSource, integer intIndex, integer intStride)
{
  integer intNumStrides = fncStrideCount(lstSource, intStride);
  
  if (intNumStrides != 0 && intIndex < intNumStrides)
  {
    integer intOffset = intIndex * intStride;
    return llList2List(lstSource, intOffset, intOffset + (intStride - 1));
  }
  return [];
}

// Replace a Stride in a List
list fncReplaceStride(list lstSource, list lstStride, integer intIndex, integer intStride)
{
  integer intNumStrides = fncStrideCount(lstSource, intStride);
  
  if (llGetListLength(lstStride) != intStride) { return lstSource; }
  
  if (intNumStrides != 0 && intIndex < intNumStrides)
  {
    integer intOffset = intIndex * intStride;
    return llListReplaceList(lstSource, lstStride, intOffset, intOffset + (intStride - 1));
  }
  return lstSource;
}

// Retrieve a single element from a Stride within a List
list fncGetElement(list lstSource, integer intIndex, integer intSubIndex, integer intStride)
{
  if (intSubIndex >= intStride) { return []; }
  
  integer intNumStrides = fncStrideCount(lstSource, intStride);
  
  if (intNumStrides != 0 && intIndex < intNumStrides)
  {
    integer intOffset = (intIndex * intStride) + intSubIndex;
    return llList2List(lstSource, intOffset, intOffset);
  }
  return [];
}

// Update a single item in a Stride within a List
list fncReplaceElement(list lstSource, list lstItem, integer intIndex, integer intSubIndex, integer intStride)
{
  integer intNumStrides = fncStrideCount(lstSource, intStride);
  
  if (llGetListLength(lstItem) != 1) { return lstSource; }
  
  if (intNumStrides != 0 && intIndex < intNumStrides)
  {
    integer intOffset = (intIndex * intStride) + intSubIndex;
    return llListReplaceList(lstSource, lstItem, intOffset, intOffset);
  }
  return lstSource;
}

list fncGetElementFromAllStrides(list source, integer subindex, integer stride)
Returns a single element from all strides, within a list.
Source is the list to retrieve from, subindex is which element to retrieve, and stride is the length of each stride.
-- Added by GarrMe Dagger (I thought this would be helpful, since I used it alot)
list Source = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

list Result = fncGetElementFromAllStrides(Source, 1, 3);
// Returns ["A2", "B2", "C2"]
// On failure returns an empty list

// Retrieve a single element from all strides within a list
list fncGetElementFromAllStrides(list lstSource, integer intSubIndex, integer intStride)
{
    if (intSubIndex >= intStride) { return []; }
    
    integer intNumStrides = llGetListLength(lstSource) / intStride;
    list lstRetVal = [];
    if (intNumStrides != 0)
    {
        integer x;
        integer intOffset;
        for (x = 0; x < intNumStrides; x++)
        {
            intOffset = (x * intStride) + intSubIndex;
            lstRetVal += llList2List(lstSource, intOffset, intOffset);
            }
    }
    return lstRetVal;
}

HomePage | ScriptLibrary
There is no comment on this page. [Display comments/form]