04.06.2011, 13:48
Useful Functions
04.06.2011, 14:11
(
Последний раз редактировалось RyDeR`; 04.06.2011 в 14:41.
)
This is actually quite silly if you know that you can just do the thing that Nero_3D said.
pawn Код:
#define differenceBetweenNumbers(%0,%1) \
floatround(floatabs((%0) - (%1)))
04.06.2011, 14:14
Код:
stock GetDifferenceBetweenNumbers(Float:num1, Float:num2) return printf("%f",((num1) >= (num2)) ? ((num1) - (num2)) : ((num2) - (num1)));
11.06.2011, 21:03
you define return tags as following:
The only useful macro like you posted that I've ever used was:
That allowed me to do this:
When you put the underscore (_) tag, it's the same as not putting a tag at all.
pawn Код:
stock bool:FUnkshion()
stock Float:FUnkshion()
stock bool:FUnkshion()
pawn Код:
#define int _
pawn Код:
stock int:FUnkshion()
12.06.2011, 20:18
pawn Код:
#define DOB:: \
DOB_
#define DOB_GetCells(%0) \
(((%0) + cellbits) / cellbits) // Round up
#define BitArray:%0[%1] \
Bits:%0[DOB_GetCells(%1)]
stock DOB::GetBit (Bits: bitarray [], index)
return _: ((bitarray [index / cellbits] & Bits: (1 << (index % cellbits))) != Bits: 0);
stock DOB::SetBit (Bits: bitarray [], index, bool: set)
{
if (set)
bitarray [index / cellbits] |= Bits: (1 << (index % cellbits));
else
bitarray [index / cellbits] &= Bits: ~(1 << (index % cellbits));
}
stock DOB::SetAllBits (Bits: bitarray [], bool: set, cells = sizeof (bitarray))
{
if (set)
for (new i = 0; i < cells; ++i)
bitarray [i] = Bit: 0xFFFFFFFF; // bitarray [i] |= Bit: 0xFFFFFFFF;
else
for (new i = 0; i < cells; ++i)
bitarray [i] = Bit: 0; // bitarray [i] &= Bit: 0;
}
12.06.2011, 21:13
12.06.2011, 21:45
Nope. It will crash (tested a while back with skin 300 I guess).
12.06.2011, 21:53
I'm not sure is this actually useful but I was bored lol
pawn Код:
stock PrintStack()
{
new stp, stk;
new row[4];
new rowidx = 0;
new stackitem;
// Get the registers
#emit lctrl 3
#emit stor.s.pri stp
#emit lctrl 4
#emit stor.s.pri stk
// Walk through the stack print 4 elements in each row
for (new i = stk; i < stp; i += 4) {
#emit lref.s.pri i
#emit stor.s.pri stackitem
row[rowidx++] = stackitem;
if ((i / 4) % 4 == 0) {
printf("[%04x]: %04x%04x %04x%04x %04x%04x %04x%04x", i - stk,
(row[0] & 0xFFFF0000) >> 16, row[0] & 0x0000FFFF,
(row[1] & 0xFFFF0000) >> 16, row[1] & 0x0000FFFF,
(row[2] & 0xFFFF0000) >> 16, row[2] & 0x0000FFFF,
(row[3] & 0xFFFF0000) >> 16, row[3] & 0x0000FFFF
);
rowidx = 0;
}
}
return (stp - stk) / 4; // Number of items (cells) printed
}
12.06.2011, 23:13
13.06.2011, 12:53
pawn Код:
#define SetAllBits(%1,%2,%3); \
for(new %1aset=(%2),Bit:bbt%1=(!%1aset)?(Bit:0):(Bit:-1),%1i;%1i!=%3;%1i++){%1[%1i]=bbt%1;}
13.06.2011, 12:58
13.06.2011, 13:02
13.06.2011, 13:06
What about Double-O-Seven's post?
26.06.2011, 12:38
Both wrong. If you saw his post, it's better to set the health to -1 so the player immediatly dies.
27.06.2011, 15:20
pawn Code:
stock strreplace (newstr [], oldstr [], srcstr [], deststr [], bool: ignorecase = false, size = sizeof (deststr))
{
new
newlen = strlen (newstr),
oldlen = strlen (oldstr),
srclen = strlen (srcstr),
idx,
rep;
for (new i = 0; i < srclen; ++i)
{
if ((i + oldlen) < srclen)
{
if (!strcmp (srcstr [i], oldstr, ignorecase, oldlen))
{
deststr [idx] = '\0';
strcat (deststr, newstr, size);
++rep;
idx += newlen;
i += oldlen - 1;
}
else
{
if (idx < size)
deststr [idx++] = srcstr [i];
else
return rep;
}
}
else
{
if (idx < size)
deststr [idx++] = srcstr [i];
else
return rep;
}
}
return rep;
}
27.06.2011, 15:33
What about this, Double-O-Seven?
Code:
stock strreplace2(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string)) { if(isnull(find) || isnull(string)) return 0; new matches; for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx)) { strdel(string, pos, pos + flen); if(rlen) strins(string, replace, pos, maxlength); idx = pos + rlen; matches++; } return matches; }
27.06.2011, 16:08
pawn Code:
stock str_replace (newstr [], oldstr [], srcstr [], deststr [], bool: ignorecase = false, size = sizeof (deststr))
{
new
newlen = strlen (newstr),
oldlen = strlen (oldstr),
srclen = strlen (srcstr),
idx,
rep;
for (new i = 0; i < srclen; ++i)
{
if ((i + oldlen) <= srclen)
{
if (!strcmp (srcstr [i], oldstr, ignorecase, oldlen))
{
deststr [idx] = '\0';
strcat (deststr, newstr, size);
++rep;
idx += newlen;
i += oldlen - 1;
}
else
{
if (idx < (size - 1))
deststr [idx++] = srcstr [i];
else
return rep;
}
}
else
{
if (idx < (size - 1))
deststr [idx++] = srcstr [i];
else
return rep;
}
}
deststr [idx] = '\0';
return rep;
}
I've also compared your version (strreplace2) and mine (str_replace) and the strreplace from DUtils.
pawn Code:
for (new i = 0; i < 500; ++i)
{
new
t,
string [256] = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe",
string2 [256];
const MAX_I = 20000;
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
string2 = strreplace ("Affe", "Kuh", string);
}
t = GetTickCount () - t;
printf ("strreplace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
str_replace ("Kuh", "Affe", string, string2);
}
t = GetTickCount () - t;
printf ("str_replace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
strreplace2 (string, "Affe", "Kuh");
}
t = GetTickCount () - t;
printf ("strreplace2: %d ms (%s)", t, string);
}
Code:
[18:09:33] strreplace: 8101 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:34] str_replace: 379 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:34] strreplace2: 547 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:42] strreplace: 7986 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:43] str_replace: 378 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:43] strreplace2: 550 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:51] strreplace: 8017 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:52] str_replace: 376 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:52] strreplace2: 549 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:00] strreplace: 8003 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:00] str_replace: 382 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:01] strreplace2: 546 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
The reason why mine is the fastest is:
My str_replace iterates only once through the whole string because it doesn't use strfind.
strfind is a bad choice here because it also iterates through the whole string in the worst case and for every match it has the iterates once more.
For many matches this is not a good choice.
27.06.2011, 16:13
What about this one?
pawn Code:
stock strReplace(strWhat[], strWith[], strSrc[], strDest[], bool: ignoreCase = false, destSize = sizeof(strDest))
{
new
strPos = -1,
whatLen = strlen(strWhat)
;
memcpy(strDest, strSrc, _, 1 + strlen(strSrc) << 2, destSize);
while((strPos = strfind(strDest, strWhat, ignoreCase, strPos + 1)) != -1)
{
strdel(strDest, strPos, strPos + whatLen);
strins(strDest, strWith, strPos, destSize);
}
return ;
}
27.06.2011, 16:24
pawn Code:
for (new i = 0; i < 500; ++i)
{
new
t,
string [256] = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe",
string2 [256];
const MAX_I = 20000;
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
string2 = strreplace ("Affe", "Kuh", string);
}
t = GetTickCount () - t;
printf ("strreplace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
str_replace ("Kuh", "Affe", string, string2);
}
t = GetTickCount () - t;
printf ("str_replace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
strreplace2 (string, "Affe", "Kuh");
}
t = GetTickCount () - t;
printf ("strreplace2: %d ms (%s)", t, string);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
strReplace ("Affe", "Kuh", string, string2);
}
t = GetTickCount () - t;
printf ("strReplace: %d ms (%s)", t, string2);
}
Code:
[18:27:23] strreplace: 8077 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:24] str_replace: 382 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:24] strreplace2: 543 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:25] strReplace: 484 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:33] strreplace: 8032 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:33] str_replace: 386 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:34] strreplace2: 543 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:34] strReplace: 483 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:42] strreplace: 8023 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:43] str_replace: 381 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:43] strreplace2: 544 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:27:44] strReplace: 489 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
27.06.2011, 16:59
Yes, indeed. I see. Nice function though - Pretty fast.
_______
Here's a VERY fast strCopy (compared to existing ones as far as I know) if someone's interested:
_______
Here's a VERY fast strCopy (compared to existing ones as far as I know) if someone's interested:
pawn Code:
stock strCopy(strSrc[], strDest[], maxLen = -1, destSize = sizeof(strDest))
{
if(maxLen == -1)
maxLen = strlen(strSrc);
return memcpy(strDest, strSrc, _, maxLen << 2, destSize);
}
« Next Oldest | Next Newest »
Users browsing this thread: 45 Guest(s)