Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom sort natives and callback use optional data #1124

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

FortyTwoFortyTwo
Copy link
Contributor

@FortyTwoFortyTwo FortyTwoFortyTwo commented Nov 17, 2019

3 2 main things done with SortCustom1D, SortCustom2D, and ArrayList.SortCustom natives:

- Allow any variable to custom sort rather than limited to int. Meaning floats, enums, methodmap etc can be used to pass and custom sort. Merged from #2146

  • Replace optional Handle hndl pass to any data, and not required in callback params. I don't see anywhere on why it should be limited to handles, so allow any without needing to create new handles. Also updated core to use cell_t data, doesn't make any difference but may well then do it.
  • Use ArrayList instead of Handle in SortFuncADTArray callback. Handle variable can still be used from old plugins.

Only thing missing is any for regular 1D sorting. I'm not sure if it a good idea to allow any in SortIntegers native to sort enums, but i left it untouched.
SortADTArrayCustom also left untouched because we have ArrayList.SortCustom traditional syntax instead.

Hopefully i didn't miss anything that would break old plugins, but please do let me know if i did break one.
Compiled with this, no warnings and tested.

public void OnPluginStart()
{
	// New sortings
	
	int Data = 322;
	Action Array1D[3] = {Plugin_Handled, Plugin_Continue, Plugin_Stop};
	SortCustom1D(Array1D, sizeof(Array1D), Sort1D, Data);
	
	float Array2D[3][2] = {{3.7, 1.1}, {7.4, -4.7}, {1.0, 2.3}};
	SortCustom2D(Array2D, sizeof(Array2D), Sort2D);
	
	ArrayList ArrayADT = new ArrayList();
	ArrayADT.Push(2);
	ArrayADT.Push(7);
	ArrayADT.Push(3);
	ArrayADT.SortCustom(SortADT);
	delete ArrayADT;
	
	
	// Old sortings
	
	int OldArray1D[3] = {5, 1, 3};
	SortCustom1D(OldArray1D, sizeof(OldArray1D), OldSort1D);
	
	Handle Trie = CreateTrie();
	int OldArray2D[3][2] = {{2, 4}, {7, 1}, {2, 6}};
	SortCustom2D(OldArray2D, sizeof(OldArray2D), OldSort2D, Trie);
	CloseHandle(Trie);
	
	Handle OldArrayADT = CreateArray();
	PushArrayCell(OldArrayADT, 1);
	PushArrayCell(OldArrayADT, 8);
	PushArrayCell(OldArrayADT, 2);
	SortADTArrayCustom(OldArrayADT, OldSortADT);
	CloseHandle(OldArrayADT);
}

public int Sort1D(Action elem1, Action elem2, const Action[] array, any data)
{
	PrintToServer("Sort1D - %d %d - data %d", elem1, elem2, data);
	return 0;
}

public int Sort2D(float[] elem1, float[] elem2, const float[][] array)
{
	PrintToServer("Sort2D - %.2f %.2f", elem1[0], elem2[0]);
	return 0;
}

public int SortADT(int index1, int index2, ArrayList array)
{
	PrintToServer("SortADT - %d %d", array.Get(index1), array.Get(index2));
	return 0;
}

public int OldSort1D(int elem1, int elem2, const int[] array, Handle hndl)
{
	PrintToServer("OldSort1D - %d %d", elem1, elem2);
	return 0;
}

public int OldSort2D(int[] elem1, int[] elem2, const int[][] array, Handle hndl)
{
	PrintToServer("OldSort2D - %d %d - hndl %x", elem1[0], elem2[0], hndl);
	return 0;
}

public int OldSortADT(int index1, int index2, Handle array, Handle hndl)
{
	PrintToServer("OldSortADT - %d %d", GetArrayCell(array, index1), GetArrayCell(array, index2));
	return 0;
}

@Headline Headline added the Feature Request user requested feature label Feb 9, 2020
@FortyTwoFortyTwo
Copy link
Contributor Author

This PR will break older plugins because of SP bug (alliedmodders/sourcepawn#684)

@FortyTwoFortyTwo
Copy link
Contributor Author

Updated as int to any change was merged from #2146. This PR now only deals with optional any data and replacing Handle to ArrayList in SortFuncADTArray.

Using char for any in SortFunc2D also does seems to no longer work for latest SP version, so I've bring the char callback back in.

@FortyTwoFortyTwo FortyTwoFortyTwo changed the title Allow custom sort natives and callback use any types and optional data Allow custom sort natives and callback use optional data Jun 5, 2024
@FortyTwoFortyTwo
Copy link
Contributor Author

Egh pull request reverted, don't even know myself if it's worth for me to update this to bring back the int to any change...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request user requested feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants