btree

a balk tree implementation ;-)
Log | Files | Refs | LICENSE

gencoupons.c (1116B)


      1 /**
      2  * btree -- a balk tree implementation ;-)
      3  * Copyright 2020, 2021 Matthias Balk
      4  *
      5  * Example program that generates random codes and uses `btree` to store and
      6  * sort them.
      7  */
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <time.h>
     13 
     14 #include "btree.h"
     15 
     16 
     17 #define SYMBOLS "ABCDEFGHKLMNPQRSTUVWXYZ123456789"
     18 #define CODELEN 8
     19 
     20 
     21 int main(int argc, char **argv)
     22 {
     23   node_t *root;
     24   if ((root = bt_new()) == NULL)
     25     return EXIT_FAILURE;
     26 
     27   srand(time(NULL));
     28 
     29   int symbols_cnt = strlen(SYMBOLS);
     30   int duplicate_cnt = 0;
     31   int i = 0;
     32   while (i < 13000000)
     33   {
     34     char* code = malloc((CODELEN + 1) * sizeof(char));
     35     if (code == NULL)
     36     {
     37       perror("malloc");
     38       return EXIT_FAILURE;
     39     }
     40 
     41     for (int j = 0; j < CODELEN; j++)
     42     {
     43       code[j] = SYMBOLS[rand() % symbols_cnt];
     44     }
     45     code[CODELEN] = '\0';
     46 
     47     /* code already exists */
     48     if (!bt_add(root, code)) {
     49       free(code);
     50       duplicate_cnt++;
     51     }
     52     else
     53     {
     54       i++;
     55     }
     56   }
     57 
     58   bt_print_sorted(root);
     59   printf("\nDuplicates: %d\n", duplicate_cnt);
     60 
     61   bt_free(root, 1);
     62 
     63   return EXIT_SUCCESS;
     64 }