Merge branch 'dev'
This commit is contained in:
commit
65f2b79157
6 files changed
+1692
No files matched your search
@@ -0,0 +1,3 @@
|
||||
# Multimap examples
|
||||
|
||||
* multimap.c - Basic examples of a multimap.
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "multimap.h"
|
||||
#include "foreach.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
size_t inthash(int t)
|
||||
{
|
||||
size_t a = t;
|
||||
a += ~(a << 15);
|
||||
a ^= (a >> 10);
|
||||
a += (a << 3);
|
||||
a ^= (a >> 6);
|
||||
a += ~(a << 11);
|
||||
a ^= (a >> 16);
|
||||
return a;
|
||||
}
|
||||
|
||||
MULTIMAP_GENERATE(mm, multi_map, , int, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
const int min = 1, max = 201;
|
||||
multi_map *map = mm_new(50, 0.8, intcmp, inthash);
|
||||
|
||||
printf("Multimap actual capacity: %" PRIu64 "\n", map->capacity);
|
||||
printf("Adding items from %d to %d twice\n", min, max);
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_insert(map, i, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_insert(map, i, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
printf("Multimap count : %" PRIu64 "\n", map->count);
|
||||
printf("Multimap capacity : %" PRIu64 "\n", map->capacity);
|
||||
printf("Multimap load : %lf\n", (double)map->count / (double)map->capacity);
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
sum += mm_get(map, i);
|
||||
|
||||
printf("Sum (unique): %" PRId64 "\n", sum);
|
||||
|
||||
FOR_EACH_MAP(mm, multi_map, int, int, map, {
|
||||
printf("[%3" PRIu64 "] = { %3d : %3d }\n", index, key, value);
|
||||
})
|
||||
|
||||
printf("Removing all items\n");
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_remove_all(map, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
sum = 0;
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
sum += mm_get(map, i);
|
||||
|
||||
printf("Sum: %" PRId64 "\n", sum);
|
||||
|
||||
FOR_EACH_MAP(mm, multi_map, int, int, map, {
|
||||
printf("[%2" PRIu64 "] = { %2d : %5d }\n", index, key, value);
|
||||
})
|
||||
|
||||
mm_free(map);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Multiset examples
|
||||
|
||||
* prime_cof.c - Prime factorization of a number.
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <stdio.h>
|
||||
#include "multiset.h"
|
||||
|
||||
MULTISET_GENERATE(ms, multi_set, /* FMOD */, size_t)
|
||||
|
||||
int cmp(size_t a, size_t b)
|
||||
{
|
||||
if (a > b)
|
||||
return 1;
|
||||
else if (a < b)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void prime_cof(const size_t number)
|
||||
{
|
||||
multi_set *mset = ms_new(cmp);
|
||||
|
||||
size_t num = number;
|
||||
|
||||
for (size_t i = 2; num != 1; i++)
|
||||
{
|
||||
while (num % i == 0)
|
||||
{
|
||||
ms_insert(mset, i);
|
||||
num /= i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("The factorization of %llu is given by:\n", number);
|
||||
|
||||
multi_set_iter iter;
|
||||
size_t result, mul, index;
|
||||
for (ms_iter_new(&iter, mset); !ms_iter_end(&iter);)
|
||||
{
|
||||
if (!ms_iter_next(&iter, &result, &mul, &index))
|
||||
{
|
||||
printf("ERROR\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ms_count(mset) == 1)
|
||||
printf("(%llu^%llu)\n", result, mul);
|
||||
else if (index == 0)
|
||||
printf("(%llu^%llu ", result, mul);
|
||||
else if (index == ms_count(mset) - 1)
|
||||
printf("* %llu^%llu)\n", result, mul);
|
||||
else
|
||||
printf("* %llu^%llu ", result, mul);
|
||||
}
|
||||
|
||||
printf("Set Cardinality: %llu\n\n", ms_cardinality(mset));
|
||||
|
||||
ms_free(mset);
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
prime_cof(360);
|
||||
prime_cof(55777680);
|
||||
prime_cof(4294967296);
|
||||
prime_cof(31415926535);
|
||||
prime_cof(27182818284);
|
||||
prime_cof(16180339887);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user