2
\$\begingroup\$

This piece of code does a linear generation with two 'tweaks':

  1. It sets a global loop as an index to reference variables that are holding values that need to be exclusive to that value of loop. These variable are those using pointers and double pointers. That way we can play on the loop value with each word's generation and the final output are the words, looping in chunks starting with the remainings shortests words to the longests ones.
  2. The sequence is modified, you can check the readme on github

It can restore sessions and needs to be parallelized.

#include <cstdlib>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <omp.h>
#include <math.h>
#include <signal.h>
#include "zhou.h"

bool one = 0; //needed for option parsing and in generate1/2/3()
int min, max;
int length;//length of each character sets
char *arrayofchars;//the character sets
char *tmp;
int **arrayofindex;
unsigned long int *counters;
unsigned long int total, A, B;
unsigned long int *rain;
int loop;
int mmm;//max-min

inline int accu(int value) {
    int i;
    int sum = 0;
    for(i=1; i<=value; ++i) {
        sum+=i;
    }
    return sum;
}
void gen_next() {
    int i;
    for(i=0; i<min+loop; ++i) {
        tmp[i] = arrayofchars[(arrayofindex[loop][i]+rain[loop])%length];
        rain[loop]+=i+1;
    }
    if(length%10 == 0)
        rain[loop]-=accu(min+loop)-2;
    else if(length%2 == 0)
        rain[loop]-=accu(min+loop)-4;
    else if(length%2)
        rain[loop]-=accu(min+loop)-1;
    //hydra's bfg method by Jan Dlabal
    int pos = min + loop - 1;
    while(pos >= 0 && ++arrayofindex[loop][pos] >= length) {
            arrayofindex[loop][pos] = 0;
            pos--;
    }
    tmp[min+loop]='\n';
    tmp[min+loop+1]='\0';
}

void gen() {
    //int nt = omp_get_max_threads();
    int loop2 = B;
    tmp = (char *) malloc(max);
    
    for(unsigned long int a=A; a<total; ++a) {
        loop = B = loop2;
        while(loop <= mmm) {
            if(counters[loop] >= (unsigned long int) pow((double) length, (double) min+loop)) {     
                ++loop2;
            }
            else {
                gen_next();
                printf("%s", tmp);
                ++counters[loop];
            }
            ++loop;
        }
    }
}

void showhelp() {
    printf(\
        "Minimum arguments:\n"
        "./zhou [ min max --set | -set <string> ] | [ -r | -restore | --restore ]\n"
        "./zhou 4 8 -set ABCD\n"
    );
}

void signalHandler(int sig) {
    FILE* fd = fopen("restore", "w");
    fprintf(fd, "%d:", max);
    fprintf(fd, "%d:", min);
    fprintf(fd, "%d:", length);
    fprintf(fd, "%s:%d:", arrayofchars, loop);
    for(int a=0; a<=mmm; ++a) {
        for(int b=0; b<max; ++b)
            fprintf(fd, "%d:", arrayofindex[a][b]);
        
        fprintf(fd, "%ld:%ld:", rain[a], counters[a]);
    }
    fprintf(fd, "%ld:", A);
    fprintf(fd, "%ld:", B);
    exit(0);
}
int main(int argc, char *argv[]) {
    int arg;
    total = 0;
    signal(SIGKILL, signalHandler);
    signal(SIGINT, signalHandler);
    if(argc > 2) {
        for(arg=1; arg<argc; arg++) {
            if(strcmp(argv[arg], "--set") == 0 || strcmp(argv[arg], "-set") == 0 || strcmp(argv[arg], "-s") == 0) {
                length  = strlen(argv[arg+1]);
                arrayofchars = (char*) malloc(length);
                strcpy(arrayofchars, argv[arg+1]);
                one = 1;
            }
            if(strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "--help") == 0 || strcmp(argv[arg], "-help") == 0) {
                showhelp();
                exit(0);
            }
        }
        if(!(min = atoi(argv[1]))) { printf("invalid minimum length; ./zhou min max ...\n"); exit(-1); }
        if(!(max = atoi(argv[2]))) { printf("invalid minimum length; ./zhou min max ...\n"); exit(-1); }
        if(!one) { printf("need input characters: --set option.\n"); exit(-1); }
        mmm = max-min;
        rain = (long unsigned int*)malloc((mmm+1)*sizeof(long unsigned int));
        counters = (long unsigned int*)malloc((mmm+1)*sizeof(long unsigned int));
        arrayofindex = (int**)malloc(max*sizeof(int**));
        for(int a=0; a<=mmm; ++a) {
            total += (long int)pow((double)length,(double)min+a);   
            arrayofindex[a] = (int*) malloc(max*sizeof(int));
            for(int b=0; b<max; ++b)
                arrayofindex[a][b] = 0;
            rain[a] = 0;
            counters[a] = 0;
        }
        A=0;
        B=0;
        loop = 0;
    }
    else if(argc == 2 && (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--restore") == 0 || strcmp(argv[1], "-restore") == 0)) {
        FILE *fd = fopen("restore", "r+");
        int filesize=0;
        do {
            ++filesize;
            getc(fd);
        } while(!feof(fd));
        char *buff = (char*)malloc(filesize);
        fseek(fd, 0, SEEK_SET);

        max = (int)strtol(strtok(buff, ":"), NULL, 10);
        min = (int)strtol(strtok(NULL, ":"), NULL, 10);
        mmm = max-min;
        length = (int)strtol(strtok(NULL, ":"), NULL, 10);
        rain = (long unsigned int*)malloc((max-min+1)*sizeof(long unsigned int));
        counters = (long unsigned int*)malloc((max-min+1)*sizeof(long unsigned int));
        arrayofindex = (int**)malloc(mmm*sizeof(int*));
        arrayofchars = (char*)malloc(length);
        arrayofchars = strtok(NULL, ":");
        loop = (int)strtol(strtok(NULL, ":"), NULL, 10);

        for(int a=0; a<=max-min; ++a) {
            arrayofindex[a] = (int*) malloc(max*sizeof(int));   
            for(int b=0; b<max; ++b)
                arrayofindex[a][b] = (int)strtol(strtok(NULL, ":"), NULL, 10);
            total += (long int)pow((double)length,(double)min+a);
            rain[a] = (unsigned long int)strtol(strtok(NULL, ":"), NULL, 10);
            counters[a] = (unsigned long int)strtol(strtok(NULL, ":"), NULL, 10);
        }
        A = (unsigned long int)strtol(strtok(NULL, ":"), NULL, 10);
        B = (unsigned long int)strtol(strtok(NULL, ":"), NULL, 10);
    }
    else {
        showhelp();
        exit(0);
    }
    gen();
} 
\$\endgroup\$
  • 1
    \$\begingroup\$ On Code Review we only review code that is working as intended. Since you're talking about a bug this is clearly not the case. \$\endgroup\$ – Peilonrayz Jun 25 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.