还是宏大师

This commit is contained in:
Bluemangoo 2025-03-28 08:07:24 +08:00
parent ffc6eea779
commit ebab65979f
Signed by: Bluemangoo
GPG Key ID: F2F7E46880A1C4CF
4 changed files with 145 additions and 66 deletions

2
.clang-tidy Normal file
View File

@ -0,0 +1,2 @@
---
Checks: '-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling'

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.exe
.idea
cmake*
build

View File

@ -3,48 +3,88 @@
"prefix": "define_template",
"body": [
"#define _template_func_def(T, func) T_DEFINE_##func(T)",
"#define _template_func(T, func) T_##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"
],
"description": "Define template"
},
"Define sort": {
"prefix": "define_sort",
"body": [
"#define SORT_PREDICATE(type, name) bool name(type a, type b)",
"#define SORT_SWAP(type, a, b) \\",
"#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) \\",
" { \\",
" const type _BLOCK_T = a; \\",
" a = b; \\",
" b = _BLOCK_T; \\",
" void *INBLOCK_TMP = malloc(size); \\",
" memcpy(INBLOCK_TMP, a, size); \\",
" memcpy(a, b, size); \\",
" memcpy(b, INBLOCK_TMP, size); \\",
" }",
"",
"#define T_DEFINE_less(T) \\",
" SORT_PREDICATE(T, T_less__##T) { return a < b; }",
"#define T_DEFINE_greater(T) \\",
" SORT_PREDICATE(T, T_greater__##T) { return a > b; }",
"",
"#define T_DEFINE_sort(T) \\",
" void T_sort__##T(T *start, const T *end, bool (*predicate)(T, T)) { \\",
" T *i, *j; \\",
" for (i = start; i < end; i++) { \\",
" for (j = i; j < end; j++) { \\",
" if (predicate(*i, *j)) { \\",
" SORT_SWAP(T, *i, *j); \\",
" } \\",
" } \\",
" } \\",
"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;",
"}",
"",
"#define T_DEFINE_sort_default(T) \\",
" void T_sort_default__##T(T *start, const T *end) { \\",
" T_sort__##T(start, end, _template_func(T, less)); \\",
"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__",
"",
"#define T_DEFINE_sort_init(T) \\",
" _template_func_def(T, less); \\",
" _template_func_def(T, greater); \\",
" _template_func_def(T, sort); \\",
" _template_func_def(T, sort_default);"
"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__"
],
"description": "Define sort"
}

104
0325/9.c
View File

@ -3,43 +3,83 @@
#define false 0
#define _template_func_def(T, func) T_DEFINE_##func(T)
#define _template_func(T, func) T_##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
#define SORT_PREDICATE(type, name) bool name(type a, type b)
#define SORT_SWAP(type, a, b) \
#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) \
{ \
const type _BLOCK_T = a; \
a = b; \
b = _BLOCK_T; \
void *INBLOCK_TMP = malloc(size); \
memcpy(INBLOCK_TMP, a, size); \
memcpy(a, b, size); \
memcpy(b, INBLOCK_TMP, size); \
}
#define T_DEFINE_less(T) \
SORT_PREDICATE(T, T_less__##T) { return a < b; }
#define T_DEFINE_greater(T) \
SORT_PREDICATE(T, T_greater__##T) { return a > b; }
#define T_DEFINE_sort(T) \
void T_sort__##T(T *start, const T *end, bool (*predicate)(T, T)) { \
T *i, *j; \
for (i = start; i < end; i++) { \
for (j = i; j < end; j++) { \
if (predicate(*i, *j)) { \
SORT_SWAP(T, *i, *j); \
} \
} \
} \
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;
}
#define T_DEFINE_sort_default(T) \
void T_sort_default__##T(T *start, const T *end) { \
T_sort__##T(start, end, _template_func(T, less)); \
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__
#define T_DEFINE_sort_init(T) \
_template_func_def(T, less); \
_template_func_def(T, greater); \
_template_func_def(T, sort); \
_template_func_def(T, sort_default);
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) \
{ \
@ -48,15 +88,11 @@
inner; \
}
T_DEFINE_sort_init(int);
#include <stdio.h>
int main() {
int n, a[301] = {0};
scanf("%d", &n);
for_i(i, n, scanf("%d", a + i));
_template_func(int, sort)(a, a + n, _template_func(int, greater));
$fc(int, sort, a, a + n, $f(_, greater));
for_i(i, n, printf("%d ", a[i]));
return 0;
}