diff options
Diffstat (limited to 'src/multiset.c')
| -rw-r--r-- | src/multiset.c | 277 |
1 files changed, 277 insertions, 0 deletions
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 | } | ||
