diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 307 | ||||
| -rw-r--r-- | src/multiset.c | 277 | ||||
| -rw-r--r-- | src/multiset.h | 36 | ||||
| -rw-r--r-- | src/utils.c | 47 | ||||
| -rw-r--r-- | src/utils.h | 29 |
5 files changed, 696 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4a0d36f --- /dev/null +++ b/src/main.c | |||
| @@ -0,0 +1,307 @@ | |||
| 1 | /* | ||
| 2 | * Лабораторная работа №1 | ||
| 3 | * | ||
| 4 | * Реализовать программу генерации бинарного кода Грея для заполнения | ||
| 5 | * универсума (заданной пользователем разрядности). На основе универсума | ||
| 6 | * формируются два мультимножества двумя способами заполнения: вручную и | ||
| 7 | * автоматически (выбирает пользователь). Мощность множеств также задает | ||
| 8 | * пользователь. В результате на экран выводятся результаты всех действий | ||
| 9 | * над множествами. Кроме того, требуется реализовать защиту от | ||
| 10 | * некорректного пользовательского ввода. | ||
| 11 | * | ||
| 12 | * trie (radix 32 -> 32 indirections + a lot of memory for pointers + a lot of memory + "fast" insert/delete) | ||
| 13 | * array + binary search (log(32)/log(2) -> max 32 jumps + low amount of memory + "slow" insert/delete) | ||
| 14 | * | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <stdio.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include <time.h> | ||
| 21 | #include <assert.h> | ||
| 22 | |||
| 23 | #include "utils.h" | ||
| 24 | #include "multiset.h" | ||
| 25 | |||
| 26 | #define MULTISET_ELEMENT_MAX_COUNT 99 | ||
| 27 | |||
| 28 | uint | ||
| 29 | bin_to_gray (uint num) | ||
| 30 | { | ||
| 31 | return num ^ (num >> 1); | ||
| 32 | } | ||
| 33 | |||
| 34 | uint | ||
| 35 | gray_to_bin (uint num) | ||
| 36 | { | ||
| 37 | num ^= num >> 16; | ||
| 38 | num ^= num >> 8; | ||
| 39 | num ^= num >> 4; | ||
| 40 | num ^= num >> 2; | ||
| 41 | num ^= num >> 1; | ||
| 42 | return num; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | generate_universum (MULTISET u) | ||
| 47 | { | ||
| 48 | for (uint i = 0; i < u->power; ++i) { | ||
| 49 | u->elements[i].value = i; | ||
| 50 | u->elements[i].count = (rand () % MULTISET_ELEMENT_MAX_COUNT) + 1; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | void | ||
| 55 | generate_random_multiset (MULTISET ms, MULTISET u) | ||
| 56 | { | ||
| 57 | assert (ms); | ||
| 58 | assert (u); | ||
| 59 | |||
| 60 | uint pd = u->power/ms->power; | ||
| 61 | for (uint i = 0; i < ms->power; ++i) { | ||
| 62 | ms->elements[i] = u->elements[i*pd + (rand () % pd)]; | ||
| 63 | ms->elements[i].count = (rand () % ms->elements[i].count) + 1; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | void | ||
| 68 | print_multiset (MULTISET ms, MULTISET u) | ||
| 69 | { | ||
| 70 | assert (u); | ||
| 71 | |||
| 72 | if (!ms || ms->power == 0) { | ||
| 73 | printf ("Пустое множество!\n"); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | for (uint i = 0; i < ms->power; ++i) { | ||
| 78 | printf ("[%0*d] = %0*b x %*d\n", | ||
| 79 | log10ui (ms->power) + 1, i, | ||
| 80 | CTZ (u->power), bin_to_gray (ms->elements[i].value), | ||
| 81 | log10ui (multiset_element_max_count (u)) + 1, ms->elements[i].count); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | uint | ||
| 86 | request_universum_bitdepth_from_user () | ||
| 87 | { | ||
| 88 | uint bitdepth = 0; | ||
| 89 | |||
| 90 | for (;;) { | ||
| 91 | while (!input ("Задайте разрядность универсума U", "^[[:digit:]]+$", "%u", &bitdepth)) | ||
| 92 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 93 | if (bitdepth <= 24) | ||
| 94 | break; | ||
| 95 | puts ("Разрядность должна быть <= 24"); | ||
| 96 | } | ||
| 97 | |||
| 98 | return bitdepth; | ||
| 99 | } | ||
| 100 | |||
| 101 | uint | ||
| 102 | request_multiset_power_from_user (const char *name, MULTISET u) | ||
| 103 | { | ||
| 104 | if (!u) return 0; | ||
| 105 | |||
| 106 | uint power = 0; | ||
| 107 | |||
| 108 | for (;;) { | ||
| 109 | while (!input ("Зададайте мощность мультимножества %s", "^[[:digit:]]+$", "%u", name, &power)) | ||
| 110 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 111 | if (0 < power && power <= u->power) | ||
| 112 | return power; | ||
| 113 | printf ("Мощность мультимножества должна принадлежать (0; %d]!\n", u->power); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | MULTISET | ||
| 118 | request_universum_from_user (void) | ||
| 119 | { | ||
| 120 | MULTISET u; | ||
| 121 | |||
| 122 | for (;;) { | ||
| 123 | uint bitdepth = request_universum_bitdepth_from_user (); | ||
| 124 | if (bitdepth == 0) return NULL; | ||
| 125 | u = init_multiset (1 << bitdepth); | ||
| 126 | if (u) break; | ||
| 127 | else puts ("Недостаточно памяти для универсумма данной разрядности!"); | ||
| 128 | } | ||
| 129 | |||
| 130 | generate_universum (u); | ||
| 131 | return u; | ||
| 132 | } | ||
| 133 | |||
| 134 | struct multiset_element | ||
| 135 | request_multiset_element_from_user (MULTISET ms, MULTISET u, int i) | ||
| 136 | { | ||
| 137 | struct multiset_element mse; | ||
| 138 | |||
| 139 | for (;;) { | ||
| 140 | input: | ||
| 141 | char *bstr; | ||
| 142 | while (!input ("Задайте код Грея %d элемента мультимножества", "^[01]+$", "%ms", | ||
| 143 | i + 1, &bstr)) | ||
| 144 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 145 | mse.value = gray_to_bin (strtoul (bstr, NULL, 2)); | ||
| 146 | free (bstr); | ||
| 147 | |||
| 148 | if (mse.value > (uint) 1 << u->power) { | ||
| 149 | printf ("Код Грея должен принадлежать [%0*d; %0*d]!\n", | ||
| 150 | log10ui (ms->power) + 1, 0, | ||
| 151 | log10ui (ms->power) + 1, bin_to_gray (ms->power)); | ||
| 152 | continue; | ||
| 153 | } | ||
| 154 | for (int j = 0; j < i; ++j) { | ||
| 155 | if (ms->elements[j].value == mse.value) { | ||
| 156 | printf ("Элемент под данным кодом Грея уже в мультимножестве!\n"); | ||
| 157 | goto input; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | break; | ||
| 162 | } | ||
| 163 | |||
| 164 | for (;;) { | ||
| 165 | while (!input ("Задайте кратность %0*b", "^[[:digit:]]+$", "%d", | ||
| 166 | CTZ (u->power), bin_to_gray (mse.value), &mse.count)) | ||
| 167 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 168 | if (0 < mse.count && mse.count <= u->elements[mse.value].count) | ||
| 169 | break; | ||
| 170 | printf ("Введённая кратность должна входить в 1..%d!\n", u->elements[mse.value].count); | ||
| 171 | } | ||
| 172 | |||
| 173 | return mse; | ||
| 174 | } | ||
| 175 | |||
| 176 | MULTISET | ||
| 177 | request_multiset_from_user (const char *name, MULTISET u) | ||
| 178 | { | ||
| 179 | MULTISET ms; | ||
| 180 | |||
| 181 | for (;;) { | ||
| 182 | uint power = request_multiset_power_from_user (name, u); | ||
| 183 | ms = init_multiset (power); | ||
| 184 | if (!ms) return NULL; | ||
| 185 | // if (!ms) { | ||
| 186 | // printf ("Недостаточно памяти для мультимножества данной мощности!\n"); | ||
| 187 | // continue; | ||
| 188 | // } | ||
| 189 | |||
| 190 | for (;;) { | ||
| 191 | int a; | ||
| 192 | while (!input ("Заполнить мультимножество автоматически (1) или вручную (2)", "^[12]$", "%d", &a)) | ||
| 193 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 194 | switch (a) { | ||
| 195 | case 1: | ||
| 196 | generate_random_multiset (ms, u); | ||
| 197 | return ms; | ||
| 198 | case 2: | ||
| 199 | for (uint i = 0; i < ms->power; ++i) | ||
| 200 | ms->elements[i] = request_multiset_element_from_user (ms, u, i); | ||
| 201 | qsort (ms->elements, ms->power, sizeof (struct multiset_element), compare_multiset_elements); | ||
| 202 | return ms; | ||
| 203 | default: | ||
| 204 | printf ("Неверный выбор!\n"); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | int | ||
| 211 | main (void) | ||
| 212 | { | ||
| 213 | srand (time (NULL)); | ||
| 214 | |||
| 215 | MULTISET u = request_universum_from_user (); | ||
| 216 | print_multiset (u, u); | ||
| 217 | PAGEBREAK (); | ||
| 218 | |||
| 219 | MULTISET a = request_multiset_from_user ("A", u); | ||
| 220 | print_multiset (a, u); | ||
| 221 | PAGEBREAK (); | ||
| 222 | |||
| 223 | MULTISET b = request_multiset_from_user ("B", u); | ||
| 224 | print_multiset (b, u); | ||
| 225 | PAGEBREAK (); | ||
| 226 | |||
| 227 | MULTISET c; | ||
| 228 | |||
| 229 | puts ("Объединение A и B:"); | ||
| 230 | c = multiset_union (a, b); | ||
| 231 | print_multiset (c, u); | ||
| 232 | PAGEBREAK (); | ||
| 233 | free (c); | ||
| 234 | |||
| 235 | puts ("Пересечение A и B:"); | ||
| 236 | c = multiset_intersection (a, b); | ||
| 237 | print_multiset (c, u); | ||
| 238 | PAGEBREAK (); | ||
| 239 | free (c); | ||
| 240 | |||
| 241 | puts ("Дополнение A:"); | ||
| 242 | c = multiset_complement (a, u); | ||
| 243 | print_multiset (c, u); | ||
| 244 | PAGEBREAK (); | ||
| 245 | free (c); | ||
| 246 | |||
| 247 | puts ("Дополнение B:"); | ||
| 248 | c = multiset_complement (b, u); | ||
| 249 | print_multiset (c, u); | ||
| 250 | PAGEBREAK (); | ||
| 251 | free (c); | ||
| 252 | |||
| 253 | puts ("Разность A\\B:"); | ||
| 254 | c = multiset_difference (a, b); | ||
| 255 | print_multiset (c, u); | ||
| 256 | PAGEBREAK (); | ||
| 257 | free (c); | ||
| 258 | |||
| 259 | puts ("Разность B\\A:"); | ||
| 260 | c = multiset_difference (b, a); | ||
| 261 | print_multiset (c, u); | ||
| 262 | PAGEBREAK (); | ||
| 263 | free (c); | ||
| 264 | |||
| 265 | puts ("Арифметическое сложение A и B:"); | ||
| 266 | c = multiset_arithmetic_sum (a, b, u); | ||
| 267 | print_multiset (c, u); | ||
| 268 | PAGEBREAK (); | ||
| 269 | free (c); | ||
| 270 | |||
| 271 | puts ("Арифметическая разность A - B:"); | ||
| 272 | c = multiset_arithmetic_difference (a, b); | ||
| 273 | print_multiset (c, u); | ||
| 274 | PAGEBREAK (); | ||
| 275 | free (c); | ||
| 276 | |||
| 277 | puts ("Арифметическая разность B - A:"); | ||
| 278 | c = multiset_arithmetic_difference (b, a); | ||
| 279 | print_multiset (c, u); | ||
| 280 | PAGEBREAK (); | ||
| 281 | free (c); | ||
| 282 | |||
| 283 | puts ("Арифметическое произведение A и B:"); | ||
| 284 | c = multiset_arithmetic_multiplication (a, b, u); | ||
| 285 | print_multiset (c, u); | ||
| 286 | PAGEBREAK (); | ||
| 287 | free (c); | ||
| 288 | |||
| 289 | puts ("Арифметическое деление A на B:"); | ||
| 290 | c = multiset_arithmetic_division (a, b, u); | ||
| 291 | print_multiset (c, u); | ||
| 292 | PAGEBREAK (); | ||
| 293 | free (c); | ||
| 294 | |||
| 295 | puts ("Арифметическое деление B на A:"); | ||
| 296 | c = multiset_arithmetic_division (b, a, u); | ||
| 297 | print_multiset (c, u); | ||
| 298 | PAGEBREAK (); | ||
| 299 | free (c); | ||
| 300 | |||
| 301 | puts ("Симетрическая разность A и B:"); | ||
| 302 | c = multiset_symmetrical_difference (a, b); | ||
| 303 | print_multiset (c, u); | ||
| 304 | free (c); | ||
| 305 | |||
| 306 | return EXIT_SUCCESS; | ||
| 307 | } | ||
diff --git a/src/multiset.c b/src/multiset.c new file mode 100644 index 0000000..ca1ea8d --- /dev/null +++ b/src/multiset.c | |||
| @@ -0,0 +1,277 @@ | |||
| 1 | #include "multiset.h" | ||
| 2 | #include <assert.h> | ||
| 3 | |||
| 4 | MULTISET | ||
| 5 | init_multiset (uint power) | ||
| 6 | { | ||
| 7 | if (!power) return NULL; | ||
| 8 | |||
| 9 | MULTISET ms = malloc (sizeof (struct multiset) + | ||
| 10 | sizeof (struct multiset_element)*power); | ||
| 11 | if (!ms) return NULL; | ||
| 12 | |||
| 13 | ms->power = power; | ||
| 14 | memset (ms->elements, 0, power*sizeof (struct multiset_element)); | ||
| 15 | |||
| 16 | return ms; | ||
| 17 | } | ||
| 18 | |||
| 19 | int | ||
| 20 | compare_multiset_elements (const void *lhs, const void *rhs) | ||
| 21 | { | ||
| 22 | if (!lhs && !rhs) return 0; | ||
| 23 | if (!lhs) return -1; | ||
| 24 | if (!rhs) return +1; | ||
| 25 | |||
| 26 | uint lhs_value = *((const uint *) lhs); | ||
| 27 | uint rhs_value = ((const struct multiset_element *) rhs)->value; | ||
| 28 | |||
| 29 | return lhs_value - rhs_value; | ||
| 30 | } | ||
| 31 | |||
| 32 | uint | ||
| 33 | multiset_get (MULTISET ms, uint value) | ||
| 34 | { | ||
| 35 | if (!ms) return 0; | ||
| 36 | |||
| 37 | struct multiset_element *mse = bsearch (&value, ms->elements, ms->power, | ||
| 38 | sizeof (struct multiset_element), | ||
| 39 | compare_multiset_elements); | ||
| 40 | return mse ? mse->count : 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | MULTISET | ||
| 44 | multiset_set (MULTISET ms, uint value, uint count) | ||
| 45 | { | ||
| 46 | if (!ms) | ||
| 47 | ms = init_multiset (1); | ||
| 48 | assert (ms); | ||
| 49 | |||
| 50 | int first = 0; | ||
| 51 | int last = ms->power - 1; | ||
| 52 | int middle = (first + last)/2; | ||
| 53 | while (first <= last) { | ||
| 54 | if (ms->elements[middle].value < value) | ||
| 55 | first = middle + 1; | ||
| 56 | else if (ms->elements[middle].value == value) { | ||
| 57 | if (count) { | ||
| 58 | ms->elements[middle].count = count; | ||
| 59 | } else { | ||
| 60 | ms->power -= 1; | ||
| 61 | memmove (&ms->elements[middle], &ms->elements[middle + 1], | ||
| 62 | sizeof (struct multiset_element)*(ms->power - middle)); | ||
| 63 | // ms = realloc (ms, (sizeof (struct multiset) + | ||
| 64 | // sizeof (struct multiset_element)*(ms->power))); | ||
| 65 | } | ||
| 66 | break; | ||
| 67 | } | ||
| 68 | else last = middle - 1; | ||
| 69 | |||
| 70 | middle = (first + last)/2; | ||
| 71 | } | ||
| 72 | if (first > last && count) { | ||
| 73 | ms = realloc (ms, (sizeof (struct multiset) + | ||
| 74 | sizeof (struct multiset_element)*(ms->power + 1))); | ||
| 75 | assert (ms); | ||
| 76 | memmove (&ms->elements[first + 1], &ms->elements[first], | ||
| 77 | sizeof (struct multiset_element)*(ms->power - first)); | ||
| 78 | ms->power += 1; | ||
| 79 | ms->elements[first].value = value; | ||
| 80 | ms->elements[first].count = count; | ||
| 81 | } | ||
| 82 | return ms; | ||
| 83 | } | ||
| 84 | |||
| 85 | uint | ||
| 86 | multiset_element_max_count (MULTISET ms) | ||
| 87 | { | ||
| 88 | if (!ms) return 0; | ||
| 89 | |||
| 90 | uint max_count = 0; | ||
| 91 | |||
| 92 | for (uint i = 0; i < ms->power; ++i) { | ||
| 93 | uint count_i = ms->elements[i].count; | ||
| 94 | if (count_i > max_count) | ||
| 95 | max_count = count_i; | ||
| 96 | } | ||
| 97 | |||
| 98 | return max_count; | ||
| 99 | } | ||
| 100 | |||
| 101 | MULTISET | ||
| 102 | multiset_copy (MULTISET ms) | ||
| 103 | { | ||
| 104 | if (!ms) return NULL; | ||
| 105 | |||
| 106 | size_t size = (sizeof (struct multiset) + | ||
| 107 | sizeof (struct multiset_element)*ms->power); | ||
| 108 | MULTISET ms_copy = malloc (size); | ||
| 109 | assert (ms_copy); | ||
| 110 | memcpy (ms_copy, ms, size); | ||
| 111 | return ms_copy; | ||
| 112 | } | ||
| 113 | |||
| 114 | MULTISET | ||
| 115 | multiset_union (MULTISET lhs, MULTISET rhs) | ||
| 116 | { | ||
| 117 | if (!lhs && !rhs) return NULL; | ||
| 118 | if (!lhs) return multiset_copy (rhs); | ||
| 119 | if (!rhs) return multiset_copy (lhs); | ||
| 120 | |||
| 121 | MULTISET res = multiset_copy (lhs); | ||
| 122 | |||
| 123 | for (uint i = 0; i < rhs->power; ++i) { | ||
| 124 | struct multiset_element rhse = rhs->elements[i]; | ||
| 125 | uint rese_count = multiset_get (res, rhse.value); | ||
| 126 | res = multiset_set (res, rhse.value, MAX (rese_count, rhse.count)); | ||
| 127 | } | ||
| 128 | |||
| 129 | return res; | ||
| 130 | } | ||
| 131 | |||
| 132 | MULTISET | ||
| 133 | multiset_intersection (MULTISET lhs, MULTISET rhs) | ||
| 134 | { | ||
| 135 | if (!lhs || !rhs) return NULL; | ||
| 136 | |||
| 137 | MULTISET res = init_multiset (0); | ||
| 138 | |||
| 139 | for (uint i = 0; i < lhs->power; ++i) { | ||
| 140 | struct multiset_element lhse = lhs->elements[i]; | ||
| 141 | uint rhs_count = multiset_get (rhs, lhse.value); | ||
| 142 | uint count = MIN (lhse.count, rhs_count); | ||
| 143 | if (count > 0) | ||
| 144 | res = multiset_set (res, lhse.value, count); | ||
| 145 | } | ||
| 146 | |||
| 147 | return res; | ||
| 148 | } | ||
| 149 | |||
| 150 | MULTISET | ||
| 151 | multiset_complement (MULTISET ms, MULTISET u) | ||
| 152 | { | ||
| 153 | if (!ms) return multiset_copy (u); | ||
| 154 | |||
| 155 | MULTISET res = init_multiset (0); | ||
| 156 | |||
| 157 | for (uint i = 0; i < u->power; ++i) { | ||
| 158 | struct multiset_element ue = u->elements[i]; | ||
| 159 | uint mse_count = multiset_get (ms, ue.value); | ||
| 160 | if (ue.count - mse_count) | ||
| 161 | res = multiset_set (res, ue.value, ue.count - mse_count); | ||
| 162 | } | ||
| 163 | |||
| 164 | return res; | ||
| 165 | } | ||
| 166 | |||
| 167 | MULTISET | ||
| 168 | multiset_difference (MULTISET lhs, MULTISET rhs) | ||
| 169 | { | ||
| 170 | if (!lhs) return NULL; | ||
| 171 | if (!rhs) return multiset_copy (lhs); | ||
| 172 | |||
| 173 | MULTISET res = init_multiset (0); | ||
| 174 | |||
| 175 | for (uint i = 0; i < lhs->power; ++i) { | ||
| 176 | struct multiset_element lhse = lhs->elements[i]; | ||
| 177 | uint rhs_count = multiset_get (rhs, lhse.value); | ||
| 178 | if (lhse.count > rhs_count) | ||
| 179 | res = multiset_set (res, lhse.value, lhse.count - rhs_count); | ||
| 180 | } | ||
| 181 | |||
| 182 | return res; | ||
| 183 | } | ||
| 184 | |||
| 185 | MULTISET | ||
| 186 | multiset_arithmetic_sum (MULTISET lhs, MULTISET rhs, MULTISET u) | ||
| 187 | { | ||
| 188 | if (!lhs && !rhs) return NULL; | ||
| 189 | if (!lhs) return multiset_copy (rhs); | ||
| 190 | if (!rhs) return multiset_copy (lhs); | ||
| 191 | |||
| 192 | MULTISET res = multiset_copy (lhs); | ||
| 193 | |||
| 194 | for (uint i = 0; i < rhs->power; ++i) { | ||
| 195 | struct multiset_element rhse = rhs->elements[i]; | ||
| 196 | uint rese_count = multiset_get (res, rhse.value); | ||
| 197 | uint ue_count = multiset_get (u, rhse.value); | ||
| 198 | res = multiset_set (res, rhse.value, MIN (rese_count + rhse.count, ue_count)); | ||
| 199 | } | ||
| 200 | |||
| 201 | return res; | ||
| 202 | } | ||
| 203 | |||
| 204 | MULTISET | ||
| 205 | multiset_arithmetic_difference (MULTISET lhs, MULTISET rhs) | ||
| 206 | { | ||
| 207 | return multiset_difference (lhs, rhs); | ||
| 208 | } | ||
| 209 | |||
| 210 | MULTISET | ||
| 211 | multiset_arithmetic_multiplication (MULTISET lhs, MULTISET rhs, MULTISET u) | ||
| 212 | { | ||
| 213 | if (!lhs || !rhs) return NULL; | ||
| 214 | |||
| 215 | MULTISET res = init_multiset (0); | ||
| 216 | |||
| 217 | for (uint i = 0; i < lhs->power; ++i) { | ||
| 218 | struct multiset_element lhse = lhs->elements[i]; | ||
| 219 | uint rhs_count = multiset_get (rhs, lhse.value); | ||
| 220 | uint ue_count = multiset_get (u, lhse.value); | ||
| 221 | if (lhse.count && rhs_count) | ||
| 222 | res = multiset_set (res, lhse.value, MIN (lhse.count * rhs_count, ue_count)); | ||
| 223 | } | ||
| 224 | |||
| 225 | return res; | ||
| 226 | } | ||
| 227 | |||
| 228 | MULTISET | ||
| 229 | multiset_arithmetic_division (MULTISET lhs, MULTISET rhs, MULTISET u) | ||
| 230 | { | ||
| 231 | if (!lhs) return NULL; | ||
| 232 | if (!rhs) return multiset_copy (lhs); | ||
| 233 | |||
| 234 | MULTISET res = init_multiset (0); | ||
| 235 | |||
| 236 | for (uint i = 0; i < lhs->power; ++i) { | ||
| 237 | struct multiset_element lhse = lhs->elements[i]; | ||
| 238 | uint rhs_count = multiset_get (rhs, lhse.value); | ||
| 239 | uint ue_count = multiset_get (u, lhse.value); | ||
| 240 | if (!lhse.count) | ||
| 241 | continue; | ||
| 242 | if (rhs_count && lhse.count / rhs_count) | ||
| 243 | res = multiset_set (res, lhse.value, lhse.count / rhs_count); | ||
| 244 | else | ||
| 245 | res = multiset_set (res, lhse.value, ue_count); | ||
| 246 | } | ||
| 247 | |||
| 248 | return res; | ||
| 249 | } | ||
| 250 | |||
| 251 | MULTISET | ||
| 252 | multiset_symmetrical_difference (MULTISET lhs, MULTISET rhs) | ||
| 253 | { | ||
| 254 | if (!lhs || !rhs) return NULL; | ||
| 255 | |||
| 256 | MULTISET res = init_multiset (0); | ||
| 257 | |||
| 258 | for (uint i = 0; i < lhs->power; ++i) { | ||
| 259 | struct multiset_element lhse = lhs->elements[i]; | ||
| 260 | uint rhs_count = multiset_get (rhs, lhse.value); | ||
| 261 | if (!rhs_count) | ||
| 262 | res = multiset_set (res, lhse.value, lhse.count); | ||
| 263 | else if (rhs_count != lhse.count) | ||
| 264 | res = multiset_set (res, lhse.value, (lhse.count > rhs_count | ||
| 265 | ? lhse.count - rhs_count | ||
| 266 | : rhs_count - lhse.count)); | ||
| 267 | } | ||
| 268 | |||
| 269 | for (uint i = 0; i < rhs->power; ++i) { | ||
| 270 | struct multiset_element rhse = rhs->elements[i]; | ||
| 271 | uint lhs_count = multiset_get (lhs, rhse.value); | ||
| 272 | if (!lhs_count) | ||
| 273 | res = multiset_set (res, rhse.value, rhse.count); | ||
| 274 | } | ||
| 275 | |||
| 276 | return res; | ||
| 277 | } | ||
diff --git a/src/multiset.h b/src/multiset.h new file mode 100644 index 0000000..6e1a8c9 --- /dev/null +++ b/src/multiset.h | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | #ifndef MULTISET_H | ||
| 2 | #define MULTISET_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include "utils.h" | ||
| 7 | |||
| 8 | struct multiset_element | ||
| 9 | { | ||
| 10 | uint value; | ||
| 11 | uint count; | ||
| 12 | }; | ||
| 13 | |||
| 14 | typedef struct multiset | ||
| 15 | { | ||
| 16 | uint power; | ||
| 17 | struct multiset_element elements[0]; | ||
| 18 | } *MULTISET; | ||
| 19 | |||
| 20 | MULTISET init_multiset (uint power); | ||
| 21 | int compare_multiset_elements (const void *lhs, const void *rhs); | ||
| 22 | uint multiset_get (MULTISET ms, uint value); | ||
| 23 | MULTISET multiset_set (MULTISET ms, uint value, uint count); | ||
| 24 | uint multiset_element_max_count (MULTISET ms); | ||
| 25 | MULTISET multiset_copy (MULTISET ms); | ||
| 26 | MULTISET multiset_union (MULTISET lhs, MULTISET rhs); | ||
| 27 | MULTISET multiset_intersection (MULTISET lhs, MULTISET rhs); | ||
| 28 | MULTISET multiset_complement (MULTISET ms, MULTISET u); | ||
| 29 | MULTISET multiset_difference (MULTISET lhs, MULTISET rhs); | ||
| 30 | MULTISET multiset_arithmetic_sum (MULTISET lhs, MULTISET rhs, MULTISET u); | ||
| 31 | MULTISET multiset_arithmetic_difference (MULTISET lhs, MULTISET rhs); | ||
| 32 | MULTISET multiset_arithmetic_multiplication (MULTISET lhs, MULTISET rhs, MULTISET u); | ||
| 33 | MULTISET multiset_arithmetic_division (MULTISET lhs, MULTISET rhs, MULTISET u); | ||
| 34 | MULTISET multiset_symmetrical_difference (MULTISET lhs, MULTISET rhs); | ||
| 35 | |||
| 36 | #endif | ||
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..2218754 --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #include "utils.h" | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdarg.h> | ||
| 5 | #include <regex.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | int | ||
| 9 | input (const char *message, const char *regex, const char *template, ...) | ||
| 10 | { | ||
| 11 | char *line = NULL; | ||
| 12 | size_t line_size = 0; | ||
| 13 | regex_t compiled; | ||
| 14 | int matches = 0; | ||
| 15 | va_list ap; | ||
| 16 | |||
| 17 | va_start (ap, template); | ||
| 18 | vprintf (message, ap); | ||
| 19 | printf (" (\"%s\"): ", regex); | ||
| 20 | getline (&line, &line_size, stdin); | ||
| 21 | line[strlen (line) - 1] = '\0'; | ||
| 22 | |||
| 23 | regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB); | ||
| 24 | if (regexec (&compiled, line, 0, NULL, 0)) | ||
| 25 | goto defer; | ||
| 26 | |||
| 27 | matches = vsscanf (line, template, ap); | ||
| 28 | va_end (ap); | ||
| 29 | |||
| 30 | defer: | ||
| 31 | free (line); | ||
| 32 | return matches; | ||
| 33 | } | ||
| 34 | |||
| 35 | uint | ||
| 36 | log10ui (uint ui) | ||
| 37 | { | ||
| 38 | return ((ui >= 1000000000u) ? 9 : | ||
| 39 | (ui >= 100000000u) ? 8 : | ||
| 40 | (ui >= 10000000u) ? 7 : | ||
| 41 | (ui >= 1000000u) ? 6 : | ||
| 42 | (ui >= 100000u) ? 5 : | ||
| 43 | (ui >= 10000u) ? 4 : | ||
| 44 | (ui >= 1000u) ? 3 : | ||
| 45 | (ui >= 100u) ? 2 : | ||
| 46 | (ui >= 10u) ? 1 : 0u); | ||
| 47 | } | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..99765db --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef UTILS_H | ||
| 2 | #define UTILS_H | ||
| 3 | |||
| 4 | #define MAX(a, b) ({ \ | ||
| 5 | __typeof__ (a) _a = (a); \ | ||
| 6 | __typeof__ (b) _b = (b); \ | ||
| 7 | _a > _b ? _a : _b; \ | ||
| 8 | }) | ||
| 9 | |||
| 10 | #define MIN(a, b) ({ \ | ||
| 11 | __typeof__ (a) _a = (a); \ | ||
| 12 | __typeof__ (b) _b = (b); \ | ||
| 13 | _a < _b ? _a : _b; \ | ||
| 14 | }) | ||
| 15 | |||
| 16 | #define CTZ(X) __builtin_ctz (X) | ||
| 17 | |||
| 18 | #define PAGEBREAK() puts ("\n") | ||
| 19 | #define NEWLINE() putchar ('\n'); | ||
| 20 | |||
| 21 | typedef unsigned int uint; | ||
| 22 | |||
| 23 | int | ||
| 24 | input (const char *message, const char *regex, const char *template, ...); | ||
| 25 | |||
| 26 | uint | ||
| 27 | log10ui (uint ui); | ||
| 28 | |||
| 29 | #endif | ||
