diff options
| author | 2026-11-07 06:12:48 +0000 | |
|---|---|---|
| committer | 2026-11-07 06:21:22 +0000 | |
| commit | 39dcac2281970febcce36a0151796af2986388b3 (patch) | |
| tree | 7339e54dc942a3ab0d9ba9daae56a2801c1917e9 /src/utils.h | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src/utils.h')
| -rw-r--r-- | src/utils.h | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..ef303a9 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdarg.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <assert.h> | ||
| 9 | #include <stdio.h> | ||
| 10 | #include <string.h> | ||
| 11 | |||
| 12 | #define ASSERT assert | ||
| 13 | #define REALLOC realloc | ||
| 14 | #define FREE free | ||
| 15 | |||
| 16 | #define ERROR(FMT, ...) fprintf (stderr, "ERROR: "FMT"\n", ##__VA_ARGS__) | ||
| 17 | #define INFO(FMT, ...) fprintf (stdout, "INFO: "FMT"\n", ##__VA_ARGS__) | ||
| 18 | #ifdef VERBOSE | ||
| 19 | #define LOG(FMT, ...) fprintf (stderr, "INFO: "FMT"\n", ##__VA_ARGS__) | ||
| 20 | #else | ||
| 21 | #define LOG(...) | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP) | ||
| 25 | |||
| 26 | #define MIN(A,B) \ | ||
| 27 | ({ \ | ||
| 28 | typeof (A) _a = (A); \ | ||
| 29 | typeof (B) _b = (B); \ | ||
| 30 | _a < _b ? _a : _b; \ | ||
| 31 | }) | ||
| 32 | |||
| 33 | #define ARRAY_LEN(array) (sizeof (array)/sizeof (*array)) | ||
| 34 | #define ARRAY_GET(array, index) \ | ||
| 35 | (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index]) | ||
| 36 | |||
| 37 | #ifndef TEMP_CAPACITY | ||
| 38 | #define TEMP_CAPACITY (16*1024*1024) | ||
| 39 | #endif | ||
| 40 | |||
| 41 | static size_t temp_size = 0; | ||
| 42 | static char temp[TEMP_CAPACITY] = { 0 }; | ||
| 43 | |||
| 44 | static inline void * | ||
| 45 | talloc (size_t size) | ||
| 46 | { | ||
| 47 | ASSERT(temp_size + size <= TEMP_CAPACITY); | ||
| 48 | void *result = &temp[temp_size]; | ||
| 49 | temp_size += size; | ||
| 50 | return result; | ||
| 51 | } | ||
| 52 | |||
| 53 | static inline char * | ||
| 54 | tstrdup (const char *cstr) | ||
| 55 | { | ||
| 56 | size_t n = strlen (cstr); | ||
| 57 | char *result = talloc (n + 1); | ||
| 58 | memcpy (result, cstr, n); | ||
| 59 | result[n] = '\0'; | ||
| 60 | return result; | ||
| 61 | } | ||
| 62 | |||
| 63 | static inline char * | ||
| 64 | tprintf(const char *format, ...) | ||
| 65 | { | ||
| 66 | va_list args; | ||
| 67 | |||
| 68 | va_start (args, format); | ||
| 69 | int n = vsnprintf (NULL, 0, format, args); | ||
| 70 | va_end (args); | ||
| 71 | ASSERT(n >= 0); | ||
| 72 | |||
| 73 | char *result = talloc (n + 1); | ||
| 74 | va_start (args, format); | ||
| 75 | vsnprintf (result, n + 1, format, args); | ||
| 76 | va_end (args); | ||
| 77 | |||
| 78 | return result; | ||
| 79 | } | ||
| 80 | |||
| 81 | static inline void | ||
| 82 | temp_reset(void) { temp_size = 0; } | ||
| 83 | |||
| 84 | static inline size_t | ||
| 85 | temp_save(void) { return temp_size; } | ||
| 86 | |||
| 87 | static inline void | ||
| 88 | temp_rewind(size_t checkpoint) { temp_size = checkpoint; } | ||
| 89 | |||
| 90 | static inline int | ||
| 91 | rand_from_to (int min, int max) { return min + rand () % (max + 1 - min); } | ||
| 92 | |||
| 93 | static inline size_t | ||
| 94 | display_width (const char *mbs) | ||
| 95 | { | ||
| 96 | size_t width = 0; | ||
| 97 | while (*mbs) { | ||
| 98 | width += 1; | ||
| 99 | mbs += 1 + ((*mbs & 1 << 7) >> 7); | ||
| 100 | } return width; | ||
| 101 | } | ||
| 102 | |||
| 103 | static inline size_t | ||
| 104 | count_mb (const char *mbs) | ||
| 105 | { | ||
| 106 | size_t count = 0; | ||
| 107 | while (*mbs) { | ||
| 108 | char mb = (*mbs & 1 << 7) >> 7; | ||
| 109 | count += mb; | ||
| 110 | mbs += 1 + mb; | ||
| 111 | } return count; | ||
| 112 | } | ||
| 113 | |||
| 114 | static inline void pause (void) { printf ("Нажмите <RET> чтобы продолжить..."); while (getchar () != '\n'); } | ||
| 115 | |||
| 116 | // NOTE(cmmm): not portable, size_t is expected to be 8 bytes long | ||
| 117 | static inline int | ||
| 118 | log10sz (size_t sz) | ||
| 119 | { | ||
| 120 | return ((sz >= 10000000000000000000lu) ? 19 : | ||
| 121 | (sz >= 1000000000000000000lu) ? 18 : | ||
| 122 | (sz >= 100000000000000000lu) ? 17 : | ||
| 123 | (sz >= 10000000000000000lu) ? 16 : | ||
| 124 | (sz >= 1000000000000000lu) ? 15 : | ||
| 125 | (sz >= 100000000000000lu) ? 14 : | ||
| 126 | (sz >= 10000000000000lu) ? 13 : | ||
| 127 | (sz >= 1000000000000lu) ? 12 : | ||
| 128 | (sz >= 100000000000lu) ? 11 : | ||
| 129 | (sz >= 1000000000lu) ? 9 : | ||
| 130 | (sz >= 100000000lu) ? 8 : | ||
| 131 | (sz >= 10000000lu) ? 7 : | ||
| 132 | (sz >= 1000000lu) ? 6 : | ||
| 133 | (sz >= 100000lu) ? 5 : | ||
| 134 | (sz >= 10000lu) ? 4 : | ||
| 135 | (sz >= 1000lu) ? 3 : | ||
| 136 | (sz >= 100lu) ? 2 : | ||
| 137 | (sz >= 10lu) ? 1 : 0u); | ||
| 138 | } | ||
| 139 | |||
| 140 | extern int levenshtein_distance (const char *a, const char *b); | ||
| 141 | |||
| 142 | #endif | ||
