/
main.cpp
132 lines (118 loc) · 3.07 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//Avoid problems with the compiler
#define HAVE_STDINT_H
#include "SDK/plugin.h"
#define NULL 0
typedef void(*logprintf_t)(const char* format, ...);
logprintf_t
logprintf
;
void
**ppPluginData
;
extern void
*pAMXFunctions
;
PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
{
pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
logprintf("** quicksort was successfully loaded! ** ");
return 1;
}
PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
}
PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}
PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
{
return AMX_ERR_NONE;
}
// quickSort
//
// This public-domain C implementation by Darel Rex Finley.
//
// * This function assumes it is called with valid parameters.
//
// * Example calls:
// quickSort(&myArray[0],5); // sorts elements 0, 1, 2, 3, and 4
// quickSort(&myArray[3],5); // sorts elements 3, 4, 5, 6, and 7
// Available at: http://alienryderflex.com/quicksort/
bool quickSort_asc(int *arr, int elements)
{
#define MAX_LEVELS 1000
int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i = 0, L, R;
beg[0] = 0; end[0] = elements;
while (i >= 0)
{
L = beg[i]; R = end[i] - 1;
if (L < R)
{
piv = arr[L];
if (i == MAX_LEVELS - 1) return 0;
while (L < R)
{
while (arr[R] >= piv && L<R) R--; if (L<R) arr[L++] = arr[R];
while (arr[L] <= piv && L<R) L++; if (L<R) arr[R--] = arr[L];
}
arr[L] = piv; beg[i + 1] = L + 1; end[i + 1] = end[i]; end[i++] = L;
}
else
{
i--;
}
}
return 1;
}
bool quickSort_desc(int *arr, int elements)
{
#define MAX_LEVELS 1000
int piv, beg[MAX_LEVELS], end[MAX_LEVELS], i = 0, L, R;
beg[0] = 0; end[0] = elements;
while (i >= 0)
{
L = beg[i]; R = end[i] - 1;
if (L < R)
{
piv = arr[L];
if (i == MAX_LEVELS - 1) return 0;
while (L < R)
{
while (arr[R] <= piv && L<R) R--; if (L<R) arr[L++] = arr[R]; //just swap > for < for descending order
while (arr[L] >= piv && L<R) L++; if (L<R) arr[R--] = arr[L]; //just swap < for > for descending order
}
arr[L] = piv; beg[i + 1] = L + 1; end[i + 1] = end[i]; end[i++] = L;
}
else
{
i--;
}
}
return 1;
}
//native quicksort_asc(array[], elements = sizeof(array));
static cell AMX_NATIVE_CALL n_quicksort_asc(AMX *amx, cell *params)
{
cell* addr = NULL;
amx_GetAddr(amx, params[1], &addr);
return quickSort_asc(static_cast<int*>(addr), static_cast<int>(params[2]));
}
//native quicksort_desc(array[], elements = sizeof(array));
static cell AMX_NATIVE_CALL n_quicksort_desc(AMX *amx, cell *params)
{
cell* addr = NULL;
amx_GetAddr(amx, params[1], &addr);
return quickSort_desc(static_cast<int*>(addr), static_cast<int>(params[2]));
}
AMX_NATIVE_INFO projectNatives[] =
{
{ "quicksort_asc", n_quicksort_asc },
{ "quicksort_desc", n_quicksort_desc },
{ NULL, NULL }
};
PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
{
return amx_Register(amx, projectNatives, -1);
}