Difference between revisions of "User:Strife Onizuka/String Tree"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "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 probl…")
 
Line 8: Line 8:
     integer size = llStringLength(last);
     integer size = llStringLength(last);
     if(size > chunk) {
     if(size > chunk) {
         //The advantage of keeping the tree unbalanced this way is that we don't need to get the string length again.
         //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;
         integer split = (llCeil(llLog(size / chunk) * 1.4426950408889634073599246810019) - 1) * chunk;
         do {
         do {
Line 14: Line 14:
             last = llDeleteSubString(last, split, -1);
             last = llDeleteSubString(last, split, -1);
             size = split;
             size = split;
             split = split / 2;
             split = split >> 1;
         } while(size > chunk);
         } while(size > chunk);
     }
     }

Revision as of 10:44, 14 March 2014

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>