Raised This Month: $32 Target: $400
 8% 

Personal String Management (utilizes trunk's Cellarray)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ts2do
Senior Member
Join Date: Mar 2004
Old 09-15-2007 , 22:33   Personal String Management (utilizes trunk's Cellarray)
Reply With Quote #1

In the following code, g_strings is an array that houses enum arrays that point to arrays that contain characters. This is especially useful for plugins that may store the same string many times. Note that this code should not be used for plugins that utilize the same strings until the end of the map; use EngFunc_AllocString for that.
Code:
new Array:g_strings; enum _:string_t {     Array:s_aStr,     s_iRefCount }; // Do this before using the below functions: //  g_strings = ArrayCreate(string_t); // Call this to add the string to your string array stock alloc_string(const str[], len) {     new buf[256], elem[string_t];     for(new i=0;i<ArraySize(g_strings);i++)     {         ArrayGetArray(g_strings, i, elem);         if(ArraySize(elem[s_aStr])-1==len)         {             array_string(elem[s_aStr], buf, 255);             if(!strcmp(buf, str))             {                 elem[s_iRefCount]++;                 ArraySetArray(g_strings, i, elem);                 return i;             }         }     }     elem[s_aStr] = ArrayCreate();     for(new i=0;i<=len;i++)     {         ArrayPushCell(elem[s_aStr], str[i]);     }     elem[s_iRefCount] = 1;     ArrayPushArray(g_strings, elem);     return ArraySize(g_strings) - 1; } // Call this to retrieve the string from its index stock string(isz, buf[], buflen) {     array_string(ArrayGetCell(g_strings, isz), buf, buflen); } // Do not use this: stock array_string(Array:astr, buf[], buflen) {     buflen = min(ArraySize(astr), buflen);     for(new i=0;i<buflen;i++)     {         buf[i] = ArrayGetCell(astr, i);     }     buf[buflen] = 0; } // Call this for every indexed string when you're destroying // something that contains indexed strings stock unreference_string(isz) {     new elem[string_t];     ArrayGetArray(g_strings, isz, elem);     if(elem[s_iRefCount]==1)     {         ArrayDestroy(elem[s_aStr]);         return;     }     elem[s_iRefCount]--;     ArraySetArray(g_strings, isz, elem); }
__________________

Last edited by ts2do; 09-15-2007 at 22:43.
ts2do is offline
Send a message via AIM to ts2do
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 09-15-2007 , 23:30   Re: Personal String Management (utilizes trunk's Cellarray)
Reply With Quote #2

I imagine this would be very slow. I can't see any real point to it since it's still locked to the size of the array it's returning to, but it's still a nice idea.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
ts2do
Senior Member
Join Date: Mar 2004
Old 09-15-2007 , 23:55   Re: Personal String Management (utilizes trunk's Cellarray)
Reply With Quote #3

it is slow...
it's just an outline for what I want to see in a module eventually
what would also be nice would be function extensions for string.inc and other things that allow you to just use the string handle as parameter rather than []
__________________
ts2do is offline
Send a message via AIM to ts2do
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 09-16-2007 , 15:00   Re: Personal String Management (utilizes trunk's Cellarray)
Reply With Quote #4

Array already has support for this. The only advantage I can see in using this is having it in Pawn so no modules are required.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode