Added amb466 - Random Sorting
authorMatt Woodrow <pred@alliedmods.net>
Mon Mar 03 06:28:41 2008 +0000 (2008-03-03)
changeset 19173b9449e1cb98
parent 1916 e2c263855b2c
child 1918 372a721066bd
Added amb466 - Random Sorting
core/smn_sorting.cpp
plugins/include/sorting.inc
plugins/testsuite/sorttest.sp
     1.1 --- a/core/smn_sorting.cpp	Sun Mar 02 23:58:33 2008 +0000
     1.2 +++ b/core/smn_sorting.cpp	Mon Mar 03 06:28:41 2008 +0000
     1.3 @@ -86,8 +86,26 @@
     1.4  {
     1.5  	Sort_Ascending = 0,
     1.6  	Sort_Descending = 1,
     1.7 +	Sort_Random = 2,
     1.8  };
     1.9  
    1.10 +void sort_random(cell_t *array, cell_t size)
    1.11 +{
    1.12 +	srand((unsigned int)time(NULL));
    1.13 +
    1.14 +	for (int i = size-1; i > 0; i--)
    1.15 +	{
    1.16 +        int n = (rand() % i) + 1;
    1.17 +
    1.18 +		if (array[i] != array[n]) 
    1.19 +		{
    1.20 +			array[i] ^= array[n];
    1.21 +			array[n] ^= array[i];
    1.22 +			array[i] ^= array[n];
    1.23 +		}
    1.24 +	}
    1.25 +}
    1.26 +
    1.27  int sort_ints_asc(const void *int1, const void *int2)
    1.28  {
    1.29  	return (*(int *)int1) - (*(int *)int2);
    1.30 @@ -109,9 +127,15 @@
    1.31  	if (type == Sort_Ascending)
    1.32  	{
    1.33  		qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
    1.34 -	} else {
    1.35 +	} 
    1.36 +	else if (type == Sort_Descending)
    1.37 +	{
    1.38  		qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
    1.39  	}
    1.40 +	else
    1.41 +	{
    1.42 +		sort_random(array, array_size);
    1.43 +	}
    1.44  
    1.45  	return 1;
    1.46  }
    1.47 @@ -157,9 +181,15 @@
    1.48  	if (type == Sort_Ascending)
    1.49  	{
    1.50  		qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
    1.51 -	} else {
    1.52 +	}
    1.53 +	else if (type == Sort_Descending)
    1.54 +	{
    1.55  		qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
    1.56  	}
    1.57 +	else
    1.58 +	{
    1.59 +		sort_random(array, array_size);
    1.60 +	}
    1.61  
    1.62  	return 1;
    1.63  }
    1.64 @@ -217,9 +247,15 @@
    1.65  	if (type == Sort_Ascending)
    1.66  	{
    1.67  		qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
    1.68 -	} else {
    1.69 +	} 
    1.70 +	else if (type == Sort_Descending)
    1.71 +	{
    1.72  		qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
    1.73  	}
    1.74 +	else
    1.75 +	{
    1.76 +		sort_random(array, array_size);
    1.77 +	}
    1.78  
    1.79  	/* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
    1.80  	 * We'll browse through each index of the array and patch up the distance.
    1.81 @@ -392,6 +428,20 @@
    1.82  	return strcmp((char *) str2, (char *) str1);
    1.83  }
    1.84  
    1.85 +void sort_adt_random(CellArray *cArray)
    1.86 +{
    1.87 +	size_t arraysize = cArray->size();
    1.88 +
    1.89 +	srand((unsigned int)time(NULL));
    1.90 +
    1.91 +	for (int i = arraysize-1; i > 0; i--)
    1.92 +	{
    1.93 +        int n = (rand() % i) + 1;
    1.94 +
    1.95 +		cArray->swap(i, n);
    1.96 +	}
    1.97 +}
    1.98 +
    1.99  static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params)
   1.100  {
   1.101  	CellArray *cArray;
   1.102 @@ -405,6 +455,14 @@
   1.103  	}
   1.104  
   1.105  	cell_t order = params[2];
   1.106 +	
   1.107 +	if (order == Sort_Random)
   1.108 +	{
   1.109 +		sort_adt_random(cArray);
   1.110 +
   1.111 +		return 1;
   1.112 +	}
   1.113 +
   1.114  	cell_t type = params[3];
   1.115  	size_t arraysize = cArray->size();
   1.116  	size_t blocksize = cArray->blocksize();
     2.1 --- a/plugins/include/sorting.inc	Sun Mar 02 23:58:33 2008 +0000
     2.2 +++ b/plugins/include/sorting.inc	Mon Mar 03 06:28:41 2008 +0000
     2.3 @@ -43,6 +43,7 @@
     2.4  {
     2.5  	Sort_Ascending = 0,		/**< Ascending order */
     2.6  	Sort_Descending = 1,	/**< Descending order */
     2.7 +	Sort_Random = 2			/**< Random order */
     2.8  };
     2.9  
    2.10  /**
     3.1 --- a/plugins/testsuite/sorttest.sp	Sun Mar 02 23:58:33 2008 +0000
     3.2 +++ b/plugins/testsuite/sorttest.sp	Mon Mar 03 06:28:41 2008 +0000
     3.3 @@ -47,6 +47,10 @@
     3.4  	SortIntegers(array, 10, Sort_Descending)
     3.5  	PrintIntegers(array, 10)
     3.6  	
     3.7 +	PrintToServer("Testing random sort:")
     3.8 +	SortIntegers(array, 10, Sort_Random)
     3.9 +	PrintIntegers(array, 10)
    3.10 +	
    3.11  	return Plugin_Handled
    3.12  }
    3.13  
    3.14 @@ -74,6 +78,10 @@
    3.15  	SortFloats(array, 10, Sort_Descending)
    3.16  	PrintFloats(array, 10)
    3.17  	
    3.18 +	PrintToServer("Testing random sort:")
    3.19 +	SortFloats(array, 10, Sort_Random)
    3.20 +	PrintFloats(array, 10)
    3.21 +	
    3.22  	return Plugin_Handled
    3.23  }
    3.24  
    3.25 @@ -125,7 +133,7 @@
    3.26  			"sidluke",
    3.27  			"johnny got his gun",
    3.28  			"gabe newell",
    3.29 -			"hello",
    3.30 +			"pRED*'s awesome",
    3.31  			"WHAT?!"
    3.32  		}
    3.33  		
    3.34 @@ -137,6 +145,10 @@
    3.35  	SortStrings(strarray, 10, Sort_Descending)
    3.36  	PrintStrings(strarray, 10)
    3.37  	
    3.38 +	PrintToServer("Testing random sort:")
    3.39 +	SortStrings(strarray, 10, Sort_Random)
    3.40 +	PrintStrings(strarray, 10)
    3.41 +	
    3.42  	return Plugin_Handled
    3.43  }
    3.44  
    3.45 @@ -157,7 +169,7 @@
    3.46  			"sidluke",
    3.47  			"johnny got his gun",
    3.48  			"gabe newell",
    3.49 -			"hello",
    3.50 +			"pred is a crab",
    3.51  			"WHAT?!"
    3.52  		}
    3.53  	
    3.54 @@ -204,6 +216,10 @@
    3.55  	SortADTArray(array, Sort_Descending, Sort_Integer)
    3.56  	PrintADTArrayIntegers(array)
    3.57  	
    3.58 +	PrintToServer("Testing random sort:")
    3.59 +	SortADTArray(array, Sort_Random, Sort_Integer)
    3.60 +	PrintADTArrayIntegers(array)
    3.61 +	
    3.62  	return Plugin_Handled
    3.63  
    3.64  }
    3.65 @@ -239,6 +255,10 @@
    3.66  	SortADTArray(array, Sort_Descending, Sort_Float)
    3.67  	PrintADTArrayFloats(array)
    3.68  	
    3.69 +	PrintToServer("Testing random sort:")
    3.70 +	SortADTArray(array, Sort_Random, Sort_Float)
    3.71 +	PrintADTArrayFloats(array)
    3.72 +	
    3.73  	return Plugin_Handled
    3.74  }
    3.75  
    3.76 @@ -264,7 +284,7 @@
    3.77  	PushArrayString(array, "sidluke");
    3.78  	PushArrayString(array, "johnny got his gun");
    3.79  	PushArrayString(array, "gabe newell");
    3.80 -	PushArrayString(array, "hello");
    3.81 +	PushArrayString(array, "Hello pRED*");
    3.82  	PushArrayString(array, "WHAT?!");
    3.83  	
    3.84  	PrintToServer("Testing ascending sort:")
    3.85 @@ -275,6 +295,10 @@
    3.86  	SortADTArray(array, Sort_Descending, Sort_String)
    3.87  	PrintADTArrayStrings(array)
    3.88  	
    3.89 +	PrintToServer("Testing random sort:")
    3.90 +	SortADTArray(array, Sort_Random, Sort_String)
    3.91 +	PrintADTArrayStrings(array)
    3.92 +	
    3.93  	return Plugin_Handled
    3.94  }
    3.95  
    3.96 @@ -298,7 +322,7 @@
    3.97  	PushArrayString(array, "sidluke");
    3.98  	PushArrayString(array, "johnny got his gun");
    3.99  	PushArrayString(array, "gabe newell");
   3.100 -	PushArrayString(array, "hello");
   3.101 +	PushArrayString(array, "pRED*'s running out of ideas");
   3.102  	PushArrayString(array, "WHAT?!");
   3.103  	
   3.104  	PrintToServer("Testing custom sort:")