Added sort functions for ADT Arrays, see sorting.inc
--HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401707
This commit is contained in:
@@ -45,6 +45,16 @@ enum SortOrder
|
||||
Sort_Descending = 1, /**< Descending order */
|
||||
};
|
||||
|
||||
/**
|
||||
* Data types for ADT Array Sorts
|
||||
*/
|
||||
enum SortType
|
||||
{
|
||||
Sort_Integer = 0,
|
||||
Sort_Float,
|
||||
Sort_String,
|
||||
};
|
||||
|
||||
/**
|
||||
* Sorts an array of integers.
|
||||
*
|
||||
@@ -128,3 +138,38 @@ funcenum SortFunc2D
|
||||
* @noreturn
|
||||
*/
|
||||
native SortCustom2D(array[][], array_size, SortFunc2D:sortfunc, Handle:hndl=INVALID_HANDLE);
|
||||
|
||||
/**
|
||||
* 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(Handle:array, SortOrder:order = Sort_Ascending, SortType:type = Sort_Integer);
|
||||
|
||||
/**
|
||||
* Sort comparison function for ADT Array elements. Function provides you with
|
||||
* indexes currently being sorted, use ADT Array functions to retrieve the
|
||||
* index values and compare.
|
||||
*
|
||||
* @param index1 First index to compare.
|
||||
* @param index2 Second index 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 SortFuncADTArray public(index1, index2, Handle:array, Handle:hndl);
|
||||
|
||||
/**
|
||||
* Custom sorts an ADT Array. You must pass in a comparison function.
|
||||
*
|
||||
* @param array Array Handle to sort
|
||||
* @param sortfunc Sort comparision function to use
|
||||
* @param hndl Optional Handle to pass through the comparison calls.
|
||||
* @noreturn
|
||||
*/
|
||||
native SortADTArrayCustom(Handle:array, SortFuncADTArray:sortfunc, Handle:hndl=INVALID_HANDLE);
|
||||
@@ -9,13 +9,17 @@ public Plugin:myinfo =
|
||||
url = "http://www.sourcemod.net/"
|
||||
};
|
||||
|
||||
public OnPluginStart(Handle:myself)
|
||||
public OnPluginStart()
|
||||
{
|
||||
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)
|
||||
RegServerCmd("test_adtsort_ints", Command_TestSortADTInts)
|
||||
RegServerCmd("test_adtsort_floats", Command_TestSortADTFloats)
|
||||
RegServerCmd("test_adtsort_strings", Command_TestSortADTStrings)
|
||||
RegServerCmd("test_adtsort_custom", Command_TestSortADTCustom)
|
||||
}
|
||||
|
||||
/*****************
|
||||
@@ -138,7 +142,7 @@ public Action:Command_TestSortStrings(args)
|
||||
|
||||
public Custom2DSort(String:elem1[], String:elem2[], String:array[][], Handle:hndl)
|
||||
{
|
||||
return StrCompare(elem1, elem2)
|
||||
return strcmp(elem1, elem2)
|
||||
}
|
||||
|
||||
public Action:Command_TestSort2D(args)
|
||||
@@ -162,3 +166,142 @@ public Action:Command_TestSort2D(args)
|
||||
|
||||
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(Handle:array)
|
||||
{
|
||||
new size = GetArraySize(array);
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
PrintToServer("array[%d] = %d", i, GetArrayCell(array, i));
|
||||
}
|
||||
}
|
||||
|
||||
public Action:Command_TestSortADTInts(args)
|
||||
{
|
||||
new Handle:array = CreateArray();
|
||||
PushArrayCell(array, 6);
|
||||
PushArrayCell(array, 7);
|
||||
PushArrayCell(array, 3);
|
||||
PushArrayCell(array, 2);
|
||||
PushArrayCell(array, 8);
|
||||
PushArrayCell(array, 5);
|
||||
PushArrayCell(array, 0);
|
||||
PushArrayCell(array, 1);
|
||||
PushArrayCell(array, 4);
|
||||
PushArrayCell(array, 9);
|
||||
|
||||
PrintToServer("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
PrintToServer("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
return Plugin_Handled
|
||||
|
||||
}
|
||||
|
||||
PrintADTArrayFloats(Handle:array)
|
||||
{
|
||||
new size = GetArraySize(array);
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
PrintToServer("array[%d] = %f", i, float:GetArrayCell(array, i));
|
||||
}
|
||||
}
|
||||
|
||||
public Action:Command_TestSortADTFloats(args)
|
||||
{
|
||||
new Handle:array = CreateArray();
|
||||
PushArrayCell(array, 6.0);
|
||||
PushArrayCell(array, 7.0);
|
||||
PushArrayCell(array, 3.0);
|
||||
PushArrayCell(array, 2.0);
|
||||
PushArrayCell(array, 8.0);
|
||||
PushArrayCell(array, 5.0);
|
||||
PushArrayCell(array, 0.0);
|
||||
PushArrayCell(array, 1.0);
|
||||
PushArrayCell(array, 4.0);
|
||||
PushArrayCell(array, 9.0);
|
||||
|
||||
PrintToServer("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
PrintToServer("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
PrintADTArrayStrings(Handle:array)
|
||||
{
|
||||
new size = GetArraySize(array);
|
||||
decl String:buffer[64];
|
||||
for (new i=0; i<size;i++)
|
||||
{
|
||||
GetArrayString(array, i, buffer, sizeof(buffer));
|
||||
PrintToServer("array[%d] = %s", i, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public Action:Command_TestSortADTStrings(args)
|
||||
{
|
||||
new Handle:array = CreateArray(ByteCountToCells(64));
|
||||
PushArrayString(array, "faluco");
|
||||
PushArrayString(array, "bailopan");
|
||||
PushArrayString(array, "pm onoto");
|
||||
PushArrayString(array, "damaged soul");
|
||||
PushArrayString(array, "sniperbeamer");
|
||||
PushArrayString(array, "sidluke");
|
||||
PushArrayString(array, "johnny got his gun");
|
||||
PushArrayString(array, "gabe newell");
|
||||
PushArrayString(array, "hello");
|
||||
PushArrayString(array, "WHAT?!");
|
||||
|
||||
PrintToServer("Testing ascending sort:")
|
||||
SortADTArray(array, Sort_Ascending, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
PrintToServer("Testing descending sort:")
|
||||
SortADTArray(array, Sort_Descending, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
public ArrayADTCustomCallback(index1, index2, Handle:array, Handle:hndl)
|
||||
{
|
||||
decl String:buffer1[64], String:buffer2[64];
|
||||
GetArrayString(array, index1, buffer1, sizeof(buffer1));
|
||||
GetArrayString(array, index2, buffer2, sizeof(buffer2));
|
||||
|
||||
return strcmp(buffer1, buffer2);
|
||||
}
|
||||
|
||||
public Action:Command_TestSortADTCustom(args)
|
||||
{
|
||||
new Handle:array = CreateArray(ByteCountToCells(64));
|
||||
PushArrayString(array, "faluco");
|
||||
PushArrayString(array, "bailopan");
|
||||
PushArrayString(array, "pm onoto");
|
||||
PushArrayString(array, "damaged soul");
|
||||
PushArrayString(array, "sniperbeamer");
|
||||
PushArrayString(array, "sidluke");
|
||||
PushArrayString(array, "johnny got his gun");
|
||||
PushArrayString(array, "gabe newell");
|
||||
PushArrayString(array, "hello");
|
||||
PushArrayString(array, "WHAT?!");
|
||||
|
||||
PrintToServer("Testing custom sort:")
|
||||
SortADTArrayCustom(array, ArrayADTCustomCallback)
|
||||
PrintADTArrayStrings(array);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user