User:Acheron Gloom

From Second Life Wiki
Jump to: navigation, search

Contributions elsewhere (that I need to convert to wiki-style): http://newjessie.org/viewtopic.php?f=18&t=179


GetSubInteger

Two functions for getting a single digit out of an integer. Faster than (integer)llGetSubString((string)num, place, place).

<lsl> integer getSubIntegerLowMem(integer x, integer place){

   integer tenExp = (integer)llPow(10.0, place);
   return (x/(tenExp/10)) - (x/tenExp)*10;

}

integer getSubIntegerFast(integer x, integer place){

   integer tenExp;
   if(place & 1){//1, 3, 5, 7, 9
       if(place & 2){//3, 7
           if(place & 4) tenExp = 10000000;
           else tenExp = 1000;
       }
       else{//1, 5, 9
           if(place & 4) tenExp = 100000;
           else if(place & 8) tenExp = 1000000000;
           else tenExp = 10;
       }
   }
   else{//2, 4, 6, 8, 10
       if(place & 2){//2, 6, 10
           if(place & 4) tenExp = 1000000;
           else{//2, 10
               if(place & 8) tenExp = 10000000000;
               else tenExp = 100;
           }
       }
       else{//4, 8
           if(place & 4) tenExp = 10000;
           else tenExp = 100000000;
       }
   }
   return (x/(tenExp/10)) - (x/tenExp)*10;

} </lsl>


IntegerLength

A few functions for getting the number of digits in an integer, with strengths/weaknesses for each. All are faster than llStringLength method. <lsl> integer getDigitsStrLen(integer x){//Low memory usage, but horrible performance. Don't use this, its just an example.

   return llStringLength((string)x);

}

integer getDigitsLog(integer x){//Low memory usage (35 bytes), good performance. Use this if memory is your main concern. You can probably in-line it for a small performance gain.

   return (integer)llLog10(x);

}

//The two functions below only work up to 8 digits. I use them for text prims. integer get8DigitsNoBranch(integer x){ //Decent memory usage (123 bytes), Better performance. Use this if you want a balanced method.

   return -~((x >= 10) + (x >= 100) + (x >= 1000) + (x >= 10000) + (x >= 100000) + (x >= 1000000) + (x >= 10000000));

}

integer get8DigitsTree(integer x){//High memory usage (239 bytes), best performance. Use this if you need speed. Inline if you need even more speed.

   if (x >= 10000) {
       if (x >= 10000000) return 8;
       if (x >= 100000){
           if (x >= 1000000) return 7;
           return 6;
       }
       return 5;
   }
   if (x >= 100){
       if (x >= 1000) return 4;
       return 3;
   }
   if (x >= 10) return 2;
   return 1;

}

//The two functions below work up to the integer max. I don't need them, but someone might. They're just minor modifications. integer getDigitsNoBranch(integer x){ //Decent memory usage (123 bytes), Better performance. Use this if you want a balanced method.

   return -~((x >= 10) + (x >= 100) + (x >= 1000) + (x >= 10000) + (x >= 100000) + (x >= 1000000) + (x >= 10000000) + (x >= 100000000) + (x >= 1000000000));

}

integer getDigitsTree(integer x){//High memory usage (239 bytes), best performance. Use this if you need speed. Inline if you need even more speed.

   if(x >= 10000) {
       if(x >= 10000000){
           if(x >= 100000000){
               if(x >= 1000000000) return 10;
               return 9;
           }
           return 8;
       }
       if(x >= 100000){
           if(x >= 1000000) return 7;
           return 6;
       }
       return 5;
   }
   if(x >= 100){
       if(x >= 1000) return 4;
       return 3;
   }
   if(x >= 10) return 2;
   return 1;

} </lsl>

Int2Prim

A few functions for putting an integer on a prim using a texture with numeric glyphs. The following functions make use of a texture that I keep inside of the prim named 'n' to save memory. Each list-UUID otherwise would be tens of bytes per face. The texture is located at: http://imgur.com/5NXIN

<lsl> list Int2PrimLeft(integer p, integer text, vector color){//left aligned

   list faces=[];
   //i = getDigitsTree(text); OR i = getDigitsNoBranch(text);
   integer i = -~((text >= 10) + (text >= 100) + (text >= 1000) + (text >= 10000) + (text >=  100000) + (text >=  1000000) + (text >= 10000000));
   integer tenExp = 1;
   while(i) faces = [PRIM_TEXTURE, i=~-i, "n", <1.0, 0.1, 0.0>, <0.0, 0.55 + 0.1*((text/(tenExp*=10))*10 - text/tenExp), 0.0>, 0.0] + faces;
   return [PRIM_LINK_TARGET, p, PRIM_COLOR, ALL_SIDES, color, 1.0] + faces;

}

list Int2PrimRight(integer p, integer text, vector color){//Right aligned

   list faces=[];
   ///i = getDigitsTree(text); OR i = getDigitsNoBranch(text);
   integer i = (counter = 8) + ~((text >= 10) + (text >= 100) + (text >= 1000) + (text >= 10000) + (text >=  100000) + (text >=  1000000) + (text >= 10000000));
   integer tenExp = 1;
   while(counter != i) faces = [PRIM_TEXTURE, counter=~-counter, "n", <1.0, 0.1, 0.0>, <0.0, 0.55 + 0.1*((text/(tenExp*=10))*10 - text/tenExp), 0.0>, 0.0] + faces;
   return [PRIM_LINK_TARGET, p, PRIM_COLOR, ALL_SIDES, color, 1.0] + faces;

} </lsl>