added sorting natives (phew)

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40541
This commit is contained in:
David Anderson
2007-02-26 08:21:09 +00:00
parent fd32916b6e
commit 5cb299014a
6 changed files with 653 additions and 1 deletions
+113
View File
@@ -0,0 +1,113 @@
/**
* vim: set ts=4 :
* ===============================================================
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
* ===============================================================
*
* This file is part of the SourceMod/SourcePawn SDK. This file may only be used
* or modified under the Terms and Conditions of its License Agreement, which is found
* in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins
* may change at any time. To view the latest information, see:
* http://www.sourcemod.net/license.php
*
* Version: $Id$
*/
#if defined _sorting_included
#endinput
#endif
#define _sorting_included
/**
* @brief Contains sorting orders.
*/
enum SortOrder
{
Sort_Ascending = 0, /**< Ascending order */
Sort_Descending = 1, /**< Descending order */
};
/**
* Sorts an array of integers.
*
* @param array Array of integers to sort in-place.
* @param array_size Size of the array.
* @param order Sorting order to use.
* @noreturn
*/
native SortIntegers(array[], array_size, SortOrder:order = Sort_Ascending);
/**
* Sorts an array of float point numbers.
*
* @param array Array of floating point numbers to sort in-place.
* @param array_size Size of the array.
* @param order Sorting order to use.
* @noreturn
*/
native SortFloats(Float:array[], array_size, SortOrder:order = Sort_Ascending);
/**
* Sorts an array of strings.
*
* @param array Array of strings to sort in-place.
* @param array_size Size of the array.
* @param order Sorting order to use.
* @noreturn
*/
native SortStrings(String:array[][], num_strings, SortOrder:order = Sort_Ascending);
/**
* Sort comparison function for 1D array elements.
* @note You may need to use explicit tags in order to use data properly.
*
* @param elem1 First element to compare.
* @param elem2 Second element to compare.
* @param array Array that is being sorted (order is undefined).
* @param hndl Handle optionally passed in while sorting.
* @return -1 if first should go before second
* 0 if first is equal to second
* 1 if first should go after second
*/
functag SortFunc1D public(elem1, elem2, const array[], Handle:hndl);
/**
* Sorts a custom 1D array. You must pass in a comparison function.
*
* @param array Array to sort.
* @param array_size Size of the array to sort.
* @param sortfunc Sort function.
* @param hndl Optional Handle to pass through the comparison calls.
* @noreturn
*/
native SortCustom1D(array[], array_size, SortFunc1D:sortfunc, Handle:hndl=INVALID_HANDLE);
/**
* Sort comparison function for 2D array elements (sub-arrays).
* @note You may need to use explicit tags in order to use data properly.
*
* @param elem1 First array to compare.
* @param elem2 Second array to compare.
* @param array Array that is being sorted (order is undefined).
* @param hndl Handle optionally passed in while sorting.
* @return -1 if first should go before second
* 0 if first is equal to second
* 1 if first should go after second
*/
funcenum SortFunc2D
{
public(array[], array[], const array[][], Handle:hndl),
public(String:array[], String:array[], const String:array[][], Handle:hndl),
};
/**
* Sorts a custom 2D array. You must pass in a comparison function.
*
* @param array Array to sort.
* @param array_size Size of the major array to sort (first index, outermost).
* @param sortfunc Sort comparison function to use.
* @param hndl Optional Handle to pass through the comparison calls.
* @noreturn
*/
native SortCustom2D(array[][], array_size, SortFunc2D:sortfunc, Handle:hndl=INVALID_HANDLE);
+1
View File
@@ -35,6 +35,7 @@ struct Plugin
#include <files>
#include <console>
#include <bitbuffer>
#include <sorting>
/**
* Declare this as a struct in your plugin to expose its information.
+164
View File
@@ -0,0 +1,164 @@
#include <sourcemod>
public Plugin:myinfo =
{
name = "Sorting Test",
author = "AlliedModders LLC",
description = "Tests sorting functions",
version = "1.0.0.0",
url = "http://www.sourcemod.net/"
};
public OnPluginStart(Handle:myself)
{
RegServerCmd("test_sort_ints", Command_TestSortInts)
RegServerCmd("test_sort_floats", Command_TestSortFloats)
RegServerCmd("test_sort_strings", Command_TestSortStrings)
RegServerCmd("test_sort_1d", Command_TestSort1D)
RegServerCmd("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++)
{
PrintToServer("array[%d] = %d", i, array[i])
}
}
public Action:Command_TestSortInts(args)
{
new array[10] = {6, 7, 3, 2, 8, 5, 0, 1, 4, 9}
PrintToServer("Testing ascending sort:")
SortIntegers(array, 10, Sort_Ascending)
PrintIntegers(array, 10)
PrintToServer("Testing descending sort:")
SortIntegers(array, 10, Sort_Descending)
PrintIntegers(array, 10)
return Plugin_Handled
}
/**************************
* Float comparison tests *
**************************/
PrintFloats(const Float:array[], size)
{
for (new i=0; i<size; i++)
{
PrintToServer("array[%d] = %f", i, array[i])
}
}
public Action:Command_TestSortFloats(args)
{
new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2}
PrintToServer("Testing ascending sort:")
SortFloats(array, 10, Sort_Ascending)
PrintFloats(array, 10)
PrintToServer("Testing descending sort:")
SortFloats(array, 10, Sort_Descending)
PrintFloats(array, 10)
return Plugin_Handled
}
public Custom1DSort(elem1, elem2, const array[], Handle:hndl)
{
new Float:f1 = Float:elem1
new Float:f2 = Float:elem2
if (f1 > f2)
{
return -1;
} else if (f1 < f2) {
return 1;
}
return 0;
}
public Action:Command_TestSort1D(args)
{
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 String:array[][], size)
{
for (new i=0; i<size; i++)
{
PrintToServer("array[%d] = %s", i, array[i])
}
}
public Action:Command_TestSortStrings(args)
{
new String:strarray[][] =
{
"faluco",
"bailopan",
"pm onoto",
"damaged soul",
"sniperbeamer",
"sidluke",
"johnny got his gun",
"gabe newell",
"hello",
"WHAT?!"
}
PrintToServer("Testing ascending sort:")
SortStrings(strarray, 10, Sort_Ascending)
PrintStrings(strarray, 10)
PrintToServer("Testing descending sort:")
SortStrings(strarray, 10, Sort_Descending)
PrintStrings(strarray, 10)
return Plugin_Handled
}
public Custom2DSort(String:elem1[], String:elem2[], String:array[][], Handle:hndl)
{
return StrCompare(elem1, elem2)
}
public Action:Command_TestSort2D(args)
{
new String: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
}