Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- typedef struct data {
- int (*get_rank)(const struct data*);
- int rank;
- } data;
- int get_rank(const data* d) {
- return (d->rank);
- }
- int strcmp(const void * x1, const void * x2)
- {
- data* t1 = (data*)x1;
- data* t2 = (data*)x2;
- return (t1->get_rank(t1) - t2->get_rank(t2));
- }
- typedef int (*comparator_t)(const void *, const void *);
- comparator_t comp = strcmp;
- void rank_sort(data* elements[], size_t size) {
- qsort(elements, size, sizeof(data*), comp);
- }
- enum {
- syze = 3
- };
- int main() {
- data* a = malloc(sizeof(data));
- a->get_rank = get_rank;
- a->rank = 1;
- data* b = malloc(sizeof(data));
- b->get_rank = get_rank;
- b->rank = 2;
- data* c = malloc(sizeof(data));
- c->get_rank = get_rank;
- c->rank = 3;
- data* els[] = {c, a, b};
- for (int i = 0; i < syze; ++i) {
- printf("%d ", els[i]->get_rank(els[i]));
- fflush(stdout);
- }
- printf("\n");
- rank_sort(els, syze);
- // for (int i = 0; i < syze; ++i) {
- // printf("%d ", els[i]->get_rank(els[i]));
- // }
- // printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement