Add ArraySortEx/SortADTArray natives and new sort method Sort_Random (bug 5494, r=Nextra)
Former-commit-id: 1ff337d9801e2fbd9ad210bc1285d31679b3029e
This commit is contained in:
@ -252,3 +252,44 @@ native ArrayDestroy(&Array:which);
|
||||
* Note that unlike the sorting.inc versions, the array passed to the callback is not in mid-sorted state.
|
||||
*/
|
||||
native ArraySort(Array:array, const comparefunc[], data[]="", data_size=0);
|
||||
|
||||
/**
|
||||
* A faster version of ArraySort.
|
||||
* The sorting algorithm then uses your comparison function to sort the data.
|
||||
*
|
||||
* The advantage of this native is that the Array elements being compared are
|
||||
* directly passed into your function, instead of the item indexes that are passed by ArraySort.
|
||||
* This removes the need for calling ArrayGet[Cell|String|Array] every time before being
|
||||
* able to compare the elements.
|
||||
*
|
||||
* For Arrays with a cellsize of 1 (used for storing integers and floats),
|
||||
* the function is called in the following manner:
|
||||
*
|
||||
* public MySortFunc(Array:array, elem1, elem2, const data[], data_size)
|
||||
*
|
||||
* array - Array handle in its current un-sorted state.
|
||||
* elem1, elem2 - Current element pair being compared
|
||||
* data[] - Extra data array you passed to the sort func.
|
||||
* data_size - Size of extra data you passed to the sort func.
|
||||
*
|
||||
* For Arrays with a cellsize larger than 1 (used for storing arrays and strings),
|
||||
* the function is called in the following manner:
|
||||
*
|
||||
* public MySortFunc(Array:array, elem1[], elem2[], const data[], data_size)
|
||||
*
|
||||
* array - Array handle in its current un-sorted state.
|
||||
* elem1[], elem2[] - Current element pair being compared
|
||||
* data[] - Extra data array you passed to the sort func.
|
||||
* data_size - Size of extra data you passed to the sort func.
|
||||
*
|
||||
*
|
||||
* In both cases your function should return:
|
||||
* -1 if elem1 should go before elem2
|
||||
* 0 if elem1 and elem2 are equal
|
||||
* 1 if elem1 should go after elem2
|
||||
*
|
||||
* Note that the parameters after elem2 are all optional and you do not need to specify them.
|
||||
*
|
||||
* Note that unlike the sorting.inc versions, the array passed to the callback is not in mid-sorted state.
|
||||
*/
|
||||
native ArraySortEx(Array:array, const comparefunc[], data[]="", data_size=0);
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Sorting functions.
|
||||
/* Sorting functions.
|
||||
*
|
||||
* by the AMX Mod X Development Team
|
||||
*
|
||||
@ -14,12 +14,25 @@
|
||||
#endif
|
||||
#define _sorting_included
|
||||
|
||||
/**
|
||||
* Contains sorting orders.
|
||||
*/
|
||||
enum SortMethod
|
||||
{
|
||||
Sort_Ascending = 0,
|
||||
Sort_Descending = 1,
|
||||
Sort_Descending,
|
||||
Sort_Random,
|
||||
};
|
||||
|
||||
/**
|
||||
* Data types for ADT Array Sorts
|
||||
*/
|
||||
enum SortType
|
||||
{
|
||||
Sort_Integer = 0,
|
||||
Sort_Float,
|
||||
Sort_String,
|
||||
};
|
||||
/**
|
||||
* Basic sorting functions below.
|
||||
*/
|
||||
@ -74,3 +87,13 @@ native SortCustom1D(array[], array_size, const comparefunc[], data[]="", data_si
|
||||
* Note that the parameters after elem2[] are all optional and you do not need to specify them.
|
||||
*/
|
||||
native SortCustom2D(array[][], array_size, const comparefunc[], data[]="", data_size=0);
|
||||
|
||||
/**
|
||||
* Sort an ADT Array. Specify the type as Integer, Float, or String.
|
||||
*
|
||||
* @param array Array Handle to sort
|
||||
* @param order Sort order to use, same as other sorts.
|
||||
* @param type Data type stored in the ADT Array
|
||||
* @noreturn
|
||||
*/
|
||||
native SortADTArray(Array:array, SortMethod:order, SortType:type);
|
||||
|
@ -220,7 +220,7 @@ public arraytest3()
|
||||
|
||||
buffb[0]=0;
|
||||
|
||||
formatex(buffb,sizeof(buffb)-1,"%S", ArrayGetStringHandle(a, i));
|
||||
formatex(buffb,sizeof(buffb)-1,"%a", ArrayGetStringHandle(a, i));
|
||||
|
||||
test(strcmp(buffb, "987654321"),0);
|
||||
}
|
||||
@ -457,3 +457,106 @@ public arraytest8()
|
||||
|
||||
showres();
|
||||
}
|
||||
|
||||
public sortcallbackex_string(Array:a, const b[], const c[], d)
|
||||
{
|
||||
return strcmp(b,c);
|
||||
}
|
||||
public arraytest9()
|
||||
{
|
||||
server_print("Testing (new) sorting function with string...");
|
||||
|
||||
new Array:a=ArrayCreate(40);
|
||||
|
||||
ArrayPushString(a, "z");
|
||||
ArrayPushString(a, "yz");
|
||||
ArrayPushString(a, "xyz");
|
||||
ArrayPushString(a, "wxyz");
|
||||
ArrayPushString(a, "vwxyz");
|
||||
ArrayPushString(a, "uvwxyz");
|
||||
ArrayPushString(a, "tuvwxyz");
|
||||
ArrayPushString(a, "stuvwxyz");
|
||||
ArrayPushString(a, "rstuvwxyz");
|
||||
ArrayPushString(a, "qrstuvwxyz");
|
||||
ArrayPushString(a, "pqrstuvwxyz");
|
||||
ArrayPushString(a, "opqrstuvwxyz");
|
||||
ArrayPushString(a, "nopqrstuvwxyz");
|
||||
ArrayPushString(a, "mnopqrstuvwxyz");
|
||||
ArrayPushString(a, "lmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "klmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "jklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "ijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "hijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "ghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "fghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "efghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "defghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "cdefghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "bcdefghijklmnopqrstuvwxyz");
|
||||
ArrayPushString(a, "abcdefghijklmnopqrstuvwxyz");
|
||||
|
||||
new OldSize=ArraySize(a);
|
||||
|
||||
ArraySortEx(a, "sortcallbackex_string");
|
||||
|
||||
test(ArraySize(a),OldSize);
|
||||
|
||||
new buff[40];
|
||||
|
||||
ArrayGetString(a,0,buff,sizeof(buff)-1);
|
||||
|
||||
test(strcmp(buff,"abcdefghijklmnopqrstuvwxyz"),0);
|
||||
|
||||
ArrayGetString(a,25,buff,sizeof(buff)-1);
|
||||
|
||||
test(strcmp(buff,"z"),0);
|
||||
|
||||
|
||||
new start='a';
|
||||
|
||||
for (new i=0;i<OldSize;i++)
|
||||
{
|
||||
ArrayGetString(a,i,buff,sizeof(buff)-1)
|
||||
|
||||
test(buff[0],start++);
|
||||
}
|
||||
|
||||
showres();
|
||||
}
|
||||
|
||||
public sortcallbackex_int(Array:a, const b, const c, d)
|
||||
{
|
||||
return b < c ? -1 : 1;
|
||||
}
|
||||
public arraytest10()
|
||||
{
|
||||
server_print("Testing (new) sorting function with integer...");
|
||||
|
||||
new Array:a=ArrayCreate(1);
|
||||
|
||||
ArrayPushCell(a, 8);
|
||||
ArrayPushCell(a, 1);
|
||||
ArrayPushCell(a, 3);
|
||||
ArrayPushCell(a, 5);
|
||||
ArrayPushCell(a, 7);
|
||||
ArrayPushCell(a, 2);
|
||||
ArrayPushCell(a, 9);
|
||||
ArrayPushCell(a, 4);
|
||||
ArrayPushCell(a, 10);
|
||||
ArrayPushCell(a, 6);
|
||||
|
||||
new OldSize=ArraySize(a);
|
||||
|
||||
ArraySortEx(a, "sortcallbackex_int");
|
||||
|
||||
test(ArraySize(a),OldSize);
|
||||
test(ArrayGetCell(a, 0),1);
|
||||
test(ArrayGetCell(a, 9),10);
|
||||
|
||||
for (new i=0;i<OldSize;i++)
|
||||
{
|
||||
test(ArrayGetCell(a, i),i+1);
|
||||
}
|
||||
|
||||
showres();
|
||||
}
|
||||
|
@ -9,6 +9,9 @@ public plugin_init()
|
||||
register_srvcmd("test_sort_strings", "Command_TestSortStrings")
|
||||
register_srvcmd("test_sort_1d", "Command_TestSort1D")
|
||||
register_srvcmd("test_sort_2d", "Command_TestSort2D")
|
||||
register_srvcmd("test_adtsort_ints", "Command_TestSortADTInts")
|
||||
register_srvcmd("test_adtsort_floats", "Command_TestSortADTFloats")
|
||||
register_srvcmd("test_adtsort_strings", "Command_TestSortADTStrings")
|
||||
}
|
||||
|
||||
/*****************
|
||||
@ -35,6 +38,10 @@ public Command_TestSortInts()
|
||||
server_print("Testing descending sort:")
|
||||
SortIntegers(array, 10, Sort_Descending)
|
||||
PrintIntegers(array, 10)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortIntegers(array, 10, Sort_Random)
|
||||
PrintIntegers(array, 10)
|
||||
}
|
||||
|
||||
/**************************
|
||||
@ -61,6 +68,10 @@ public Command_TestSortFloats()
|
||||
SortFloats(array, 10, Sort_Descending)
|
||||
PrintFloats(array, 10)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortFloats(array, 10, Sort_Random)
|
||||
PrintFloats(array, 10)
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
||||
@ -122,6 +133,10 @@ public Command_TestSortStrings()
|
||||
SortStrings(array, 10, Sort_Descending)
|
||||
PrintStrings(array, 10)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortStrings(array, 10, Sort_Random)
|
||||
PrintStrings(array, 10)
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
||||
@ -151,3 +166,126 @@ public Command_TestSort2D()
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
||||
|
||||
/*******************
|
||||
* ADT ARRAY TESTS *
|
||||
*******************/
|
||||
// Int and floats work the same as normal comparisions. Strings are direct
|
||||
// comparisions with no hacky memory stuff like Pawn arrays.
|
||||
|
||||
PrintADTArrayIntegers(Array:array)
|
||||
{
|
||||
new size = ArraySize(array);
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
server_print("array[%d] = %d", i, ArrayGetCell(array, i));
|
||||
}
|
||||
}
|
||||
|
||||
public Command_TestSortADTInts()
|
||||
{
|
||||
new Array:array = ArrayCreate();
|
||||
ArrayPushCell(array, 6);
|
||||
ArrayPushCell(array, 7);
|
||||
ArrayPushCell(array, 3);
|
||||
ArrayPushCell(array, 2);
|
||||
ArrayPushCell(array, 8);
|
||||
ArrayPushCell(array, 5);
|
||||
ArrayPushCell(array, 0);
|
||||
ArrayPushCell(array, 1);
|
||||
ArrayPushCell(array, 4);
|
||||
ArrayPushCell(array, 9);
|
||||
|
||||
server_print("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
server_print("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
||||
PrintADTArrayFloats(Array:array)
|
||||
{
|
||||
new size = ArraySize(array);
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
server_print("array[%d] = %f", i, Float:ArrayGetCell(array, i));
|
||||
}
|
||||
}
|
||||
|
||||
public Command_TestSortADTFloats()
|
||||
{
|
||||
new Array:array = ArrayCreate();
|
||||
ArrayPushCell(array, 6.0);
|
||||
ArrayPushCell(array, 7.0);
|
||||
ArrayPushCell(array, 3.0);
|
||||
ArrayPushCell(array, 2.0);
|
||||
ArrayPushCell(array, 8.0);
|
||||
ArrayPushCell(array, 5.0);
|
||||
ArrayPushCell(array, 0.0);
|
||||
ArrayPushCell(array, 1.0);
|
||||
ArrayPushCell(array, 4.0);
|
||||
ArrayPushCell(array, 9.0);
|
||||
|
||||
server_print("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
server_print("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
||||
PrintADTArrayStrings(Array:array)
|
||||
{
|
||||
new size = ArraySize(array);
|
||||
new buffer[64];
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
ArrayGetString(array, i, buffer, sizeof(buffer));
|
||||
server_print("array[%d] = %s", i, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public Command_TestSortADTStrings()
|
||||
{
|
||||
new Array:array = ArrayCreate(64);
|
||||
ArrayPushString(array, "faluco");
|
||||
ArrayPushString(array, "bailopan");
|
||||
ArrayPushString(array, "pm onoto");
|
||||
ArrayPushString(array, "damaged soul");
|
||||
ArrayPushString(array, "sniperbeamer");
|
||||
ArrayPushString(array, "sidluke");
|
||||
ArrayPushString(array, "johnny got his gun");
|
||||
ArrayPushString(array, "gabe newell");
|
||||
ArrayPushString(array, "Hello pRED*");
|
||||
ArrayPushString(array, "WHAT?!");
|
||||
|
||||
server_print("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
server_print("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
server_print("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
return PLUGIN_HANDLED
|
||||
}
|
||||
|
Reference in New Issue
Block a user