98 lines
3.6 KiB
C
98 lines
3.6 KiB
C
#define bool int
|
|
#define true 1
|
|
#define false 0
|
|
|
|
#define _template_func_def(T, func) T_DEFINE_##func(T)
|
|
#define _template_func(T, func) T_##func(T)
|
|
#define _template_func_call(T, func, ...) \
|
|
_template_func(T, func)(sizeof(T), __VA_ARGS__)
|
|
#define $fd _template_func_def
|
|
#define $f _template_func
|
|
#define $fc _template_func_call
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define SORT_PREDICATE(type, name) \
|
|
bool name(const size_t size, type *a, type *b)
|
|
#define SORT_SWAP(size, a, b) \
|
|
{ \
|
|
void *INBLOCK_TMP = malloc(size); \
|
|
memcpy(INBLOCK_TMP, a, size); \
|
|
memcpy(a, b, size); \
|
|
memcpy(b, INBLOCK_TMP, size); \
|
|
}
|
|
|
|
int memcmp_reverse(const void *ptr1, const void *ptr2, const size_t size) {
|
|
size_t i = size;
|
|
for (; i > 0; i--) {
|
|
const unsigned char byte1 = ((const unsigned char *)ptr1)[i - 1];
|
|
const unsigned char byte2 = ((const unsigned char *)ptr2)[i - 1];
|
|
if (byte1 != byte2) {
|
|
return byte1 - byte2;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
SORT_PREDICATE(const void, T_less__) {
|
|
const int cmp = memcmp_reverse(a, b, size);
|
|
const bool a_negative = (((const unsigned char *)a)[size - 1] & 0x80) != 0;
|
|
const bool b_negative = (((const unsigned char *)b)[size - 1] & 0x80) != 0;
|
|
if (a_negative && !b_negative)
|
|
return true;
|
|
if (!a_negative && b_negative)
|
|
return false;
|
|
return cmp < 0;
|
|
}
|
|
#define T_less(T) T_less__
|
|
SORT_PREDICATE(const void, T_greater__) {
|
|
const int cmp = memcmp_reverse(a, b, size);
|
|
const bool a_negative = (((const unsigned char *)a)[size - 1] & 0x80) != 0;
|
|
const bool b_negative = (((const unsigned char *)b)[size - 1] & 0x80) != 0;
|
|
if (a_negative && !b_negative)
|
|
return false;
|
|
if (!a_negative && b_negative)
|
|
return true;
|
|
return cmp > 0;
|
|
}
|
|
#define T_greater(T) T_greater__
|
|
SORT_PREDICATE(const void *, T_less_binary__) { return memcmp(a, b, size) < 0; }
|
|
#define T_less_binary(T) T_less_binary__
|
|
SORT_PREDICATE(const void *, T_greater_binary__) {
|
|
return memcmp(a, b, size) > 0;
|
|
}
|
|
#define T_greater_binary(T) T_greater_binary__
|
|
|
|
void T_sort__(const size_t size, void *start, const void *end,
|
|
bool (*predicate)(size_t, const void *, const void *)) {
|
|
size_t i, j;
|
|
for (i = (size_t)start; i < (size_t)end; i += size) {
|
|
for (j = i + size; j < (size_t)end; j += size) {
|
|
if (predicate(size, (void *)i, (void *)j)) {
|
|
SORT_SWAP(size, (void *)i, (void *)j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#define T_sort(T) T_sort__
|
|
void T_sort_default__(const size_t size, void *start, const void *end) {
|
|
T_sort__(size, start, end, T_less(T));
|
|
}
|
|
#define T_sort_default(T) T_sort_default__
|
|
|
|
#define for_i(i, range, inner) \
|
|
{ \
|
|
int i; \
|
|
for (i = 0; i < range; i++) \
|
|
inner; \
|
|
}
|
|
|
|
int main() {
|
|
int n, a[301] = {0};
|
|
scanf("%d", &n);
|
|
for_i(i, n, scanf("%d", a + i));
|
|
$fc(int, sort, a, a + n, $f(_, greater));
|
|
for_i(i, n, printf("%d ", a[i]));
|
|
return 0;
|
|
} |