还是宏大师

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'

3
.gitignore vendored
View File

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

View File

@ -3,48 +3,88 @@
"prefix": "define_template", "prefix": "define_template",
"body": [ "body": [
"#define _template_func_def(T, func) T_DEFINE_##func(T)", "#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" "description": "Define template"
}, },
"Define sort": { "Define sort": {
"prefix": "define_sort", "prefix": "define_sort",
"body": [ "body": [
"#define SORT_PREDICATE(type, name) bool name(type a, type b)", "#include <stdio.h>",
"#define SORT_SWAP(type, a, b) \\", "#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; \\", " void *INBLOCK_TMP = malloc(size); \\",
" a = b; \\", " memcpy(INBLOCK_TMP, a, size); \\",
" b = _BLOCK_T; \\", " memcpy(a, b, size); \\",
" memcpy(b, INBLOCK_TMP, size); \\",
" }", " }",
"", "",
"#define T_DEFINE_less(T) \\", "int memcmp_reverse(const void *ptr1, const void *ptr2, const size_t size) {",
" SORT_PREDICATE(T, T_less__##T) { return a < b; }", " size_t i = size;",
"#define T_DEFINE_greater(T) \\", " for (; i > 0; i--) {",
" SORT_PREDICATE(T, T_greater__##T) { return a > b; }", " const unsigned char byte1 = ((const unsigned char *)ptr1)[i - 1];",
"", " const unsigned char byte2 = ((const unsigned char *)ptr2)[i - 1];",
"#define T_DEFINE_sort(T) \\", " if (byte1 != byte2) {",
" void T_sort__##T(T *start, const T *end, bool (*predicate)(T, T)) { \\", " return byte1 - byte2;",
" T *i, *j; \\", " }",
" for (i = start; i < end; i++) { \\",
" for (j = i; j < end; j++) { \\",
" if (predicate(*i, *j)) { \\",
" SORT_SWAP(T, *i, *j); \\",
" } \\",
" } \\",
" } \\",
" }", " }",
" return 0;",
"}",
"", "",
"#define T_DEFINE_sort_default(T) \\", "SORT_PREDICATE(const void, T_less__) {",
" void T_sort_default__##T(T *start, const T *end) { \\", " const int cmp = memcmp_reverse(a, b, size);",
" T_sort__##T(start, end, _template_func(T, less)); \\", " 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_DEFINE_sort_init(T) \\", "#define T_sort(T) T_sort__",
" _template_func_def(T, less); \\", "void T_sort_default__(const size_t size, void *start, const void *end) {",
" _template_func_def(T, greater); \\", " T_sort__(size, start, end, T_less(T));",
" _template_func_def(T, sort); \\", "}",
" _template_func_def(T, sort_default);" "#define T_sort_default(T) T_sort_default__"
], ],
"description": "Define sort" "description": "Define sort"
} }

106
0325/9.c
View File

@ -3,43 +3,83 @@
#define false 0 #define false 0
#define _template_func_def(T, func) T_DEFINE_##func(T) #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) #include <stdio.h>
#define SORT_SWAP(type, a, b) \ #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; \ void *INBLOCK_TMP = malloc(size); \
a = b; \ memcpy(INBLOCK_TMP, a, size); \
b = _BLOCK_T; \ memcpy(a, b, size); \
memcpy(b, INBLOCK_TMP, size); \
} }
#define T_DEFINE_less(T) \ int memcmp_reverse(const void *ptr1, const void *ptr2, const size_t size) {
SORT_PREDICATE(T, T_less__##T) { return a < b; } size_t i = size;
#define T_DEFINE_greater(T) \ for (; i > 0; i--) {
SORT_PREDICATE(T, T_greater__##T) { return a > b; } const unsigned char byte1 = ((const unsigned char *)ptr1)[i - 1];
const unsigned char byte2 = ((const unsigned char *)ptr2)[i - 1];
#define T_DEFINE_sort(T) \ if (byte1 != byte2) {
void T_sort__##T(T *start, const T *end, bool (*predicate)(T, T)) { \ return byte1 - byte2;
T *i, *j; \ }
for (i = start; i < end; i++) { \
for (j = i; j < end; j++) { \
if (predicate(*i, *j)) { \
SORT_SWAP(T, *i, *j); \
} \
} \
} \
} }
return 0;
}
#define T_DEFINE_sort_default(T) \ SORT_PREDICATE(const void, T_less__) {
void T_sort_default__##T(T *start, const T *end) { \ const int cmp = memcmp_reverse(a, b, size);
T_sort__##T(start, end, _template_func(T, less)); \ 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_DEFINE_sort_init(T) \ #define T_sort(T) T_sort__
_template_func_def(T, less); \ void T_sort_default__(const size_t size, void *start, const void *end) {
_template_func_def(T, greater); \ T_sort__(size, start, end, T_less(T));
_template_func_def(T, sort); \ }
_template_func_def(T, sort_default); #define T_sort_default(T) T_sort_default__
#define for_i(i, range, inner) \ #define for_i(i, range, inner) \
{ \ { \
@ -48,15 +88,11 @@
inner; \ inner; \
} }
T_DEFINE_sort_init(int);
#include <stdio.h>
int main() { int main() {
int n, a[301] = {0}; int n, a[301] = {0};
scanf("%d", &n); scanf("%d", &n);
for_i(i, n, scanf("%d", a + i)); 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])); for_i(i, n, printf("%d ", a[i]));
return 0; return 0;
} }