Advertisement
Kaelygon

circular list

Jul 2nd, 2024
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. // ./main > out.txt && grep -o "$(head -c 10000 ./out.txt | tail -c 16)" ./out.txt | wc -l
  2. // ./main | dieharder -B -g 200 -a
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <limits.h>
  8. #include <string.h>
  9.  
  10. typedef uint8_t cygonUI;
  11.  
  12.  
  13. cygonUI cgRor(cygonUI num, cygonUI shift){
  14.     cygonUI invShift=sizeof(num)*CHAR_BIT-shift;
  15.     return ((num >> shift) | (num << invShift ));
  16. }
  17.  
  18. typedef struct cygonNode{
  19.     cygonUI num;
  20.     struct cygonNode* next;
  21. }cygonNode;
  22.  
  23. typedef struct cygonList{
  24.     size_t bits;        //bit width of cygonUI
  25.     size_t size;
  26.     cygonNode* node;    //list of nodes
  27.     cygonNode** mem;    //Mallocated node list adresses
  28.    
  29. }cygonList;
  30.  
  31.  
  32. void nextNode(cygonList* list) {
  33.     list->node=list->node->next;
  34. }
  35.  
  36. void cygonInit(cygonList* list, size_t nodeCount) {
  37.     if (nodeCount < 1) {
  38.         fprintf(stderr, "List can't have 0 nodes! Using size 1\n");
  39.         nodeCount = 1;
  40.     }
  41.    
  42.     list->bits = sizeof(cygonUI) * CHAR_BIT;
  43.     list->size = nodeCount;
  44.     list->node = (cygonNode*) malloc(sizeof(cygonNode) * nodeCount);
  45.     list->mem = (cygonNode**) malloc(sizeof(cygonNode*) * nodeCount); // allocate node pointers
  46.  
  47.     if (list->node == NULL || list->mem == NULL) {
  48.         fprintf(stderr, "Memory allocation failed\n");
  49.         return;
  50.     }
  51.  
  52.     // Link circular list
  53.     for (size_t i = 0; i < nodeCount; ++i) {
  54.         list->mem[i] = &list->node[i]; //Store mallocated address of node
  55.         list->node[i].num = 0;
  56.         list->node[i].next = &list->node[(i + 1) % nodeCount];
  57.     }
  58. }
  59.  
  60. void cygonFree(cygonList* list) {
  61.     free(*list->mem);
  62.     free(list->mem);
  63. }
  64.  
  65. void cygonSeed(cygonList* list, cygonUI seed) {
  66.     cygonNode* start = list->node; //store starting point
  67.  
  68.     do{
  69.         list->node->num=seed;
  70.         nextNode(list);
  71.            
  72.         {//placeholder
  73.             seed++;
  74.         }
  75.  
  76.     }while(list->node != start); //Reaching starting point means we have looped through circular list
  77. }
  78.  
  79. cygonUI cygonRand(cygonList* list) {
  80.     cygonUI a = list->node->num;
  81.     cygonUI b = list->node->next->num;
  82.  
  83.     {//placeholder
  84.         a++;
  85.     }
  86.  
  87.     //update and increment circular list
  88.     list->node->num=a;
  89.     list->node=list->node->next;
  90.  
  91.     return a;
  92. }
  93.  
  94. //print all nodes
  95. void cygonPrint(cygonList* list) {
  96.     cygonNode* start = list->node;
  97.  
  98.     do{
  99.         printf("%u ",list->node->num);
  100.         list->node=list->node->next;
  101.     }while(list->node != start);
  102.     printf("\n");
  103. }
  104.  
  105. size_t randCountArg(int argc, char *argv) {
  106.     if (argc > 1) {
  107.         if(!strcmp(argv,"default")){
  108.             return 512;
  109.         }else{
  110.             char isString=1;
  111.             char *x = argv;
  112.             while (*x) {
  113.                 if (!isdigit(*x)) {
  114.                     isString=0;
  115.                     break;
  116.                 }
  117.                 x++;
  118.             }
  119.  
  120.             if(isString){
  121.                 return strtoul(argv, NULL, 10);
  122.             }
  123.         }
  124.     }else{
  125.         return -1; //generate indefinitely
  126.     }
  127. }
  128.  
  129. int main(int argc, char *argv[]) {
  130.     size_t count = randCountArg(argc, argv[1]);
  131.  
  132.     cygonList list;
  133.  
  134.     cygonInit(&list, 4);
  135.     cygonSeed(&list, 0);
  136.  
  137.     for(size_t i=0;i<count;i++){
  138.         cygonUI num = cygonRand(&list);
  139.         //fwrite(&num, sizeof(cygonUI), 1, stdout); //write raw
  140.         printf("%u ",num);
  141.     }
  142.     printf("\n");
  143.  
  144.     cygonFree(&list);
  145.  
  146.     return 0;
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement