Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,37 @@
#include <stdio.h>
#define s(x) (1U << ((x) - 'A'))
typedef unsigned int bitset;
int consolidate(bitset *x, int len)
{
int i, j;
for (i = len - 2; i >= 0; i--)
for (j = len - 1; j > i; j--)
if (x[i] & x[j])
x[i] |= x[j], x[j] = x[--len];
return len;
}
void show_sets(bitset *x, int len)
{
bitset b;
while(len--) {
for (b = 'A'; b <= 'Z'; b++)
if (x[len] & s(b)) printf("%c ", b);
putchar('\n');
}
}
int main(void)
{
bitset x[] = { s('A') | s('B'), s('C') | s('D'), s('B') | s('D'),
s('F') | s('G') | s('H'), s('H') | s('I') | s('K') };
int len = sizeof(x) / sizeof(x[0]);
puts("Before:"); show_sets(x, len);
puts("\nAfter:"); show_sets(x, consolidate(x, len));
return 0;
}

View file

@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct edge { int to; struct edge *next; };
struct node { int group; struct edge *e; };
int **consolidate(int **x)
{
# define alloc(v, size) v = calloc(size, sizeof(v[0]));
int group, n_groups, n_nodes;
int n_edges = 0;
struct edge *edges, *ep;
struct node *nodes;
int pos, *stack, **ret;
void add_edge(int a, int b) {
ep->to = b;
ep->next = nodes[a].e;
nodes[a].e = ep;
ep++;
}
void traverse(int a) {
if (nodes[a].group) return;
nodes[a].group = group;
stack[pos++] = a;
for (struct edge *e = nodes[a].e; e; e = e->next)
traverse(e->to);
}
n_groups = n_nodes = 0;
for (int i = 0; x[i]; i++, n_groups++)
for (int j = 0; x[i][j]; j++) {
n_edges ++;
if (x[i][j] >= n_nodes)
n_nodes = x[i][j] + 1;
}
alloc(ret, n_nodes);
alloc(nodes, n_nodes);
alloc(stack, n_nodes);
ep = alloc(edges, n_edges);
for (int i = 0; x[i]; i++)
for (int *s = x[i], j = 0; s[j]; j++)
add_edge(s[j], s[j + 1] ? s[j + 1] : s[0]);
group = 0;
for (int i = 1; i < n_nodes; i++) {
if (nodes[i].group) continue;
group++, pos = 0;
traverse(i);
stack[pos++] = 0;
ret[group - 1] = malloc(sizeof(int) * pos);
memcpy(ret[group - 1], stack, sizeof(int) * pos);
}
free(edges);
free(stack);
free(nodes);
// caller is responsible for freeing ret
return realloc(ret, sizeof(ret[0]) * (1 + group));
# undef alloc
}
void show_sets(int **x)
{
for (int i = 0; x[i]; i++) {
printf("%d: ", i);
for (int j = 0; x[i][j]; j++)
printf(" %d", x[i][j]);
putchar('\n');
}
}
int main(void)
{
int *x[] = {
(int[]) {1, 2, 0}, // 0: end of set
(int[]) {3, 4, 0},
(int[]) {3, 1, 0},
(int[]) {0}, // empty set
(int[]) {5, 6, 0},
(int[]) {7, 6, 0},
(int[]) {3, 9, 10, 0},
0 // 0: end of sets
};
puts("input:");
show_sets(x);
puts("components:");
show_sets(consolidate(x));
return 0;
}