Committed sorting natives as per request am23838

This commit is contained in:
David Anderson
2006-08-20 04:46:34 +00:00
parent afe7df87d2
commit df7ee94b83
8 changed files with 595 additions and 1 deletions

View File

@ -20,6 +20,7 @@
#include <lang>
#include <messages>
#include <vector>
#include <sorting>
/* Function is called just after server activation.
* Good place for configuration loading, commands and cvars registration. */

View File

@ -0,0 +1,76 @@
/* Sorting functions.
*
* by the AMX Mod X Development Team
*
* This file is provided as is (no warranties).
*
* All sort functions are based off the qsort() function from the
* C standard library, which uses the Quick Sort algorithm.
* For more info, see: http://linux.wku.edu/~lamonml/algor/sort/sort.html
*/
#if defined _time_included
#endinput
#endif
#define _time_included
enum SortMethod
{
Sort_Ascending = 0,
Sort_Descending = 1,
};
/**
* Basic sorting functions below.
*/
native SortIntegers(array[], array_size, SortMethod:order = Sort_Ascending);
native SortFloats(Float:array[], array_size, SortMethod:order = Sort_Ascending);
native SortStrings(array[][], num_strings, SortMethod:order = Sort_Ascending);
/**
* Custom sorting functions below.
*/
/**
* Sorts a custom 1D array. You must pass in a comparison function.
* The sorting algorithm then uses your comparison function to sort the data.
* The function is called in the following manner:
*
* public MySortFunc(elem1, elem2, const array[], const data[], data_size)
*
* elem1, elem2 - Current element pair being compared
* array[] - Array in its current mid-sorted state.
* data[] - Extra data array you passed to the sort func.
* data_size - Size of extra data you passed to the sort func.
*
* 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.
*/
native SortCustom1D(array[], array_size, const comparefunc[], data[]="", data_size=0);
/**
* Sorts a custom 2D array.
* The sorting algorithm then uses your comparison function to sort the data.
* The function is called in the following manner:
*
* public MySortFunc(const elem1[], const elem2[], const array[], data[], data_size)
*
* elem1[], elem2[] - Current element array pairs being compared
* array[][] - Array in its currently being sorted state.
* data[] - Extra data array you passed to the sort func.
* data_size - Size of extra data you passed to the sort func.
*
* 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.
*/
native SortCustom2D(array[][], array_size, const comparefunc[], data[]="", data_size=0);

View File

@ -0,0 +1,153 @@
#include <amxmodx>
public plugin_init()
{
register_plugin("Sort Test", "1.0", "BAILOPAN")
register_srvcmd("test_sort_ints", "Command_TestSortInts")
register_srvcmd("test_sort_floats", "Command_TestSortFloats")
register_srvcmd("test_sort_strings", "Command_TestSortStrings")
register_srvcmd("test_sort_1d", "Command_TestSort1D")
register_srvcmd("test_sort_2d", "Command_TestSort2D")
}
/*****************
* INTEGER TESTS *
*****************/
// Note that integer comparison is just int1-int2 (or a variation therein)
PrintIntegers(const array[], size)
{
for (new i=0; i<size; i++)
{
server_print("array[%d] = %d", i, array[i])
}
}
public Command_TestSortInts()
{
new array[10] = {6, 7, 3, 2, 8, 5, 0, 1, 4, 9}
server_print("Testing ascending sort:")
SortIntegers(array, 10, Sort_Ascending)
PrintIntegers(array, 10)
server_print("Testing descending sort:")
SortIntegers(array, 10, Sort_Descending)
PrintIntegers(array, 10)
}
/**************************
* Float comparison tests *
**************************/
PrintFloats(const Float:array[], size)
{
for (new i=0; i<size; i++)
{
server_print("array[%d] = %f", i, array[i])
}
}
public Command_TestSortFloats()
{
new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
server_print("Testing ascending sort:")
SortFloats(array, 10, Sort_Ascending)
PrintFloats(array, 10)
server_print("Testing descending sort:")
SortFloats(array, 10, Sort_Descending)
PrintFloats(array, 10)
return PLUGIN_HANDLED
}
public Custom1DSort(Float:elem1, Float:elem2)
{
if (elem1 > elem2)
{
return -1;
} else if (elem1 < elem2) {
return 1;
}
return 0;
}
public Command_TestSort1D()
{
new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
SortCustom1D(_:array, 10, "Custom1DSort")
PrintFloats(array, 10)
return PLUGIN_HANDLED
}
/***************************
* String comparison tests *
***************************/
PrintStrings(const array[][], size)
{
for (new i=0; i<size; i++)
{
server_print("array[%d] = %s", i, array[i])
}
}
public Command_TestSortStrings()
{
new array[][] =
{
"faluco",
"bailopan",
"pm onoto",
"damaged soul",
"sniperbeamer",
"sidluke",
"johnny got his gun",
"gabe newell",
"hello",
"WHAT?!"
}
server_print("Testing ascending sort:")
SortStrings(array, 10, Sort_Ascending)
PrintStrings(array, 10)
server_print("Testing descending sort:")
SortStrings(array, 10, Sort_Descending)
PrintStrings(array, 10)
return PLUGIN_HANDLED
}
public Custom2DSort(const elem1[], const elem2[])
{
return strcmp(elem1, elem2)
}
public Command_TestSort2D()
{
new array[][] =
{
"faluco",
"bailopan",
"pm onoto",
"damaged soul",
"sniperbeamer",
"sidluke",
"johnny got his gun",
"gabe newell",
"hello",
"WHAT?!"
}
SortCustom2D(array, 10, "Custom2DSort")
PrintStrings(array, 10)
return PLUGIN_HANDLED
}