Add new CellArray natives: Clone/Resize/FindString/FindValue (bug 6104, r=ds)

This commit is contained in:
Arkshine
2014-04-15 10:34:48 +02:00
parent d6d4badbda
commit f78fda6d9c
4 changed files with 323 additions and 10 deletions

View File

@ -23,14 +23,23 @@ enum Array
*
* @param cellsize How many cells each entry in the array is.
* @param reserved How many blank entries are created immediately when the array is created. These entries are not valid to read from until called with ArraySet.
* @return Handle to the array.
* @return Handle to the array.
*/
native Array:ArrayCreate(cellsize=1, reserved=32);
/**
* Clones an array, returning a new handle with the same size and data.
* You must close it.
*
* @param which Array handle to be cloned.
* @return New handle to the cloned array object on success, 0 on failure.
*/
native Array:ArrayClone(Array:which);
/**
* Clears all entries from the array.
*
* @param which The array to clear.
* @param which The array to clear.
* @return 1 on success, 0 on failure.
*/
native ArrayClear(Array:which);
@ -38,16 +47,26 @@ native ArrayClear(Array:which);
/**
* Returns the number of elements in the array.
*
* @param which The array to check.
* @param which The array to check.
* @return How many elements are in the array.
*/
native ArraySize(Array:which);
/**
* Resizes an array. If the size is smaller than the current size,
* the array is truncated.
*
* @param which Array Handle.
* @param newsize New size.
* @return 1 on success, 0 on failure.
*/
native bool:ArrayResize(Array:which, newsize);
/**
* Returns data within an array.
* Make sure the output buffer matches the size the array was created with!
*
* @param which The array to retrieve the item from.
* @param which The array to retrieve the item from.
* @param item The item to retrieve (zero-based).
* @param output The output buffer to write.
*/
@ -57,7 +76,7 @@ native ArrayGetArray(Array:which, item, any:output[]);
* Returns a single cell of data from an array.
* Use this only with arrays that were created with a cellsize of 1!
*
* @param which The array to retrieve the item from.
* @param which The array to retrieve the item from.
* @param item The item to retrieve (zero-based).
* @return The value of the cell.
*/
@ -209,6 +228,26 @@ native ArraySwap(Array:which, item1, item2);
*/
native ArrayDeleteItem(Array:which, item);
/**
* Returns the index for the first occurance of the provided string. If the string
* cannot be located, -1 will be returned.
*
* @param which Array Handle.
* @param item String to search for.
* @return Array index, or -1 on failure.
*/
native ArrayFindString(Array:which, const item[]);
/**
* Returns the index for the first occurance of the provided value. If the value
* cannot be located, -1 will be returned.
*
* @param which Array Handle.
* @param item Value to search for.
* @return Array index, or -1 on failure.
*/
native ArrayFindValue(Array:which, any:item);
/**
* Creates a handle that is passable to a format compliant routine for printing as a string (with the %a format option).
* It is suggested to pass the function directly as a parameter to the format routine.

View File

@ -1,6 +1,5 @@
#include <amxmodx>
new __testnumber;
new errcount;
new __testfunc[32];
@ -25,9 +24,7 @@ new TestWords[6][] = {
"!="
};
stock test(A,B=0,TestType:Type=TT_Equal)
stock test(any:A,any:B=0,TestType:Type=TT_Equal)
{
++__testnumber;
@ -521,6 +518,8 @@ public arraytest9()
test(buff[0],start++);
}
ArrayDestroy(a);
showres();
}
@ -558,5 +557,112 @@ public arraytest10()
test(ArrayGetCell(a, i),i+1);
}
ArrayDestroy(a);
showres();
}
public arraytest11()
{
server_print("Testing cloning function...");
new Array:a = ArrayCreate(1);
ArrayPushCell(a, 42);
ArrayPushCell(a, 9);
ArrayPushCell(a, -1);
ArrayPushCell(a, 0);
ArrayPushCell(a, 5);
ArrayPushCell(a, 10);
ArrayPushCell(a, 15);
ArrayPushCell(a, 6.5);
new Array:b = ArrayClone(a);
ArrayPushCell(b, 48);
ArrayPushCell(b, 3.14);
test(a, b, TT_NotEqual);
test(ArraySize(a), ArraySize(b) - 2);
test(ArrayGetCell(b, 0), 42);
test(ArrayGetCell(b, 2), -1);
test(ArrayGetCell(b, 7), 6.5);
test(ArrayGetCell(b, 9), 3.14);
ArrayDestroy(a);
ArrayDestroy(b);
showres();
}
public arraytest12()
{
server_print("Testing resizing function...");
new Array:a = ArrayCreate(16);
ArrayPushString(a, "egg");
ArrayResize(a, 50);
ArrayPushString(a, "boileregg");
ArraySetString(a, 50, "no more egg v2");
new buffer[16];
ArrayGetString(a, 50, buffer, charsmax(buffer));
test(ArraySize(a), 50 + 1);
test(strcmp(buffer, "no more egg v2"), 0);
ArrayDestroy(a);
showres();
}
public arraytest13()
{
server_print("Testing finding string in array...");
new Array:a = ArrayCreate(16);
ArrayPushString(a, "z");
ArrayPushString(a, "egg");
ArrayPushString(a, "boilerplate");
ArrayPushString(a, "amxmodx");
ArrayPushString(a, "something");
ArrayPushString(a, "");
ArrayPushString(a, "eggeggeggeggeggeggegg");
test(ArrayFindString(a, "egg"), 1);
test(ArrayFindString(a, "doh"), -1);
test(ArrayFindString(a, "something"), 4);
test(ArrayFindString(a, "eggeggeggeggegg"), 6);
test(ArrayFindString(a, ""), 5);
test(ArrayFindString(a, "zz"), -1);
ArrayDestroy(a);
showres();
}
public arraytest14()
{
server_print("Testing finding value in array...");
new Array:a = ArrayCreate(1);
ArrayPushCell(a, 2);
ArrayPushCell(a, 1);
ArrayPushCell(a, 5);
ArrayPushCell(a, 3.14);
ArrayPushCell(a, -1);
test(ArrayFindValue(a, -1), 4);
test(ArrayFindValue(a, 2), 0);
test(ArrayFindValue(a, 3), -1);
test(ArrayFindValue(a, 3.14), 3);
ArrayDestroy(a);
showres();
}