User:Strife Onizuka/String Tree

From Second Life Wiki
Jump to navigation Jump to search

This is just a bit of silliness, it's sanity has not been vetted. The purpose of this code is to look at the costs of splitting a string into chunks of a specific size. The problem with getting all sequential chunks of a specific set size is that doing so is O(N2). This script hopes to reduce that. As previously stated, I have no idea if it does. It probably doesn't. I'm hoping for something like O(N*log2(N)). <lsl> integer chunk; list buffer; string getNext() {

   string last = llList2String(buffer, -1);
   buffer = llDeleteSubList(buffer, -1, -1);
   integer size = llStringLength(last);
   if(size > chunk) {
       //The advantage of keeping the tree unbalanced this way is that split only needs to be cleverly calculated once.
       integer split = (llCeil(llLog(size / chunk) * 1.4426950408889634073599246810019) - 1) * chunk;
       do {
           buffer += llGetSubString(last, split, -1);
           last = llDeleteSubString(last, split, -1);
           size = split;
           split = split >> 1;
       } while(size > chunk);
   }
   return last;

}

setup(string str, integer size) {

   buffer = [str];
   chunk = size;

} </lsl>