Added amb466 - Random Sorting
--HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401923
This commit is contained in:
parent
1f96e25121
commit
211b0159e5
@ -86,8 +86,26 @@ enum SortOrder
|
||||
{
|
||||
Sort_Ascending = 0,
|
||||
Sort_Descending = 1,
|
||||
Sort_Random = 2,
|
||||
};
|
||||
|
||||
void sort_random(cell_t *array, cell_t size)
|
||||
{
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
for (int i = size-1; i > 0; i--)
|
||||
{
|
||||
int n = (rand() % i) + 1;
|
||||
|
||||
if (array[i] != array[n])
|
||||
{
|
||||
array[i] ^= array[n];
|
||||
array[n] ^= array[i];
|
||||
array[i] ^= array[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int sort_ints_asc(const void *int1, const void *int2)
|
||||
{
|
||||
return (*(int *)int1) - (*(int *)int2);
|
||||
@ -109,9 +127,15 @@ static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
|
||||
if (type == Sort_Ascending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
|
||||
} else {
|
||||
}
|
||||
else if (type == Sort_Descending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
|
||||
}
|
||||
else
|
||||
{
|
||||
sort_random(array, array_size);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -157,9 +181,15 @@ static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
|
||||
if (type == Sort_Ascending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
|
||||
} else {
|
||||
}
|
||||
else if (type == Sort_Descending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
|
||||
}
|
||||
else
|
||||
{
|
||||
sort_random(array, array_size);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -217,9 +247,15 @@ static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params)
|
||||
if (type == Sort_Ascending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
|
||||
} else {
|
||||
}
|
||||
else if (type == Sort_Descending)
|
||||
{
|
||||
qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
|
||||
}
|
||||
else
|
||||
{
|
||||
sort_random(array, array_size);
|
||||
}
|
||||
|
||||
/* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
|
||||
* We'll browse through each index of the array and patch up the distance.
|
||||
@ -392,6 +428,20 @@ int sort_adtarray_strings_desc(const void *str1, const void *str2)
|
||||
return strcmp((char *) str2, (char *) str1);
|
||||
}
|
||||
|
||||
void sort_adt_random(CellArray *cArray)
|
||||
{
|
||||
size_t arraysize = cArray->size();
|
||||
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
for (int i = arraysize-1; i > 0; i--)
|
||||
{
|
||||
int n = (rand() % i) + 1;
|
||||
|
||||
cArray->swap(i, n);
|
||||
}
|
||||
}
|
||||
|
||||
static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
CellArray *cArray;
|
||||
@ -405,6 +455,14 @@ static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params)
|
||||
}
|
||||
|
||||
cell_t order = params[2];
|
||||
|
||||
if (order == Sort_Random)
|
||||
{
|
||||
sort_adt_random(cArray);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
cell_t type = params[3];
|
||||
size_t arraysize = cArray->size();
|
||||
size_t blocksize = cArray->blocksize();
|
||||
|
@ -43,6 +43,7 @@ enum SortOrder
|
||||
{
|
||||
Sort_Ascending = 0, /**< Ascending order */
|
||||
Sort_Descending = 1, /**< Descending order */
|
||||
Sort_Random = 2 /**< Random order */
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -47,6 +47,10 @@ public Action:Command_TestSortInts(args)
|
||||
SortIntegers(array, 10, Sort_Descending)
|
||||
PrintIntegers(array, 10)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortIntegers(array, 10, Sort_Random)
|
||||
PrintIntegers(array, 10)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
@ -74,6 +78,10 @@ public Action:Command_TestSortFloats(args)
|
||||
SortFloats(array, 10, Sort_Descending)
|
||||
PrintFloats(array, 10)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortFloats(array, 10, Sort_Random)
|
||||
PrintFloats(array, 10)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
@ -125,7 +133,7 @@ public Action:Command_TestSortStrings(args)
|
||||
"sidluke",
|
||||
"johnny got his gun",
|
||||
"gabe newell",
|
||||
"hello",
|
||||
"pRED*'s awesome",
|
||||
"WHAT?!"
|
||||
}
|
||||
|
||||
@ -137,6 +145,10 @@ public Action:Command_TestSortStrings(args)
|
||||
SortStrings(strarray, 10, Sort_Descending)
|
||||
PrintStrings(strarray, 10)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortStrings(strarray, 10, Sort_Random)
|
||||
PrintStrings(strarray, 10)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
@ -157,7 +169,7 @@ public Action:Command_TestSort2D(args)
|
||||
"sidluke",
|
||||
"johnny got his gun",
|
||||
"gabe newell",
|
||||
"hello",
|
||||
"pred is a crab",
|
||||
"WHAT?!"
|
||||
}
|
||||
|
||||
@ -204,6 +216,10 @@ public Action:Command_TestSortADTInts(args)
|
||||
SortADTArray(array, Sort_Descending, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_Integer)
|
||||
PrintADTArrayIntegers(array)
|
||||
|
||||
return Plugin_Handled
|
||||
|
||||
}
|
||||
@ -239,6 +255,10 @@ public Action:Command_TestSortADTFloats(args)
|
||||
SortADTArray(array, Sort_Descending, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_Float)
|
||||
PrintADTArrayFloats(array)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
@ -264,7 +284,7 @@ public Action:Command_TestSortADTStrings(args)
|
||||
PushArrayString(array, "sidluke");
|
||||
PushArrayString(array, "johnny got his gun");
|
||||
PushArrayString(array, "gabe newell");
|
||||
PushArrayString(array, "hello");
|
||||
PushArrayString(array, "Hello pRED*");
|
||||
PushArrayString(array, "WHAT?!");
|
||||
|
||||
PrintToServer("Testing ascending sort:")
|
||||
@ -275,6 +295,10 @@ public Action:Command_TestSortADTStrings(args)
|
||||
SortADTArray(array, Sort_Descending, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
PrintToServer("Testing random sort:")
|
||||
SortADTArray(array, Sort_Random, Sort_String)
|
||||
PrintADTArrayStrings(array)
|
||||
|
||||
return Plugin_Handled
|
||||
}
|
||||
|
||||
@ -298,7 +322,7 @@ public Action:Command_TestSortADTCustom(args)
|
||||
PushArrayString(array, "sidluke");
|
||||
PushArrayString(array, "johnny got his gun");
|
||||
PushArrayString(array, "gabe newell");
|
||||
PushArrayString(array, "hello");
|
||||
PushArrayString(array, "pRED*'s running out of ideas");
|
||||
PushArrayString(array, "WHAT?!");
|
||||
|
||||
PrintToServer("Testing custom sort:")
|
||||
|
Loading…
Reference in New Issue
Block a user