コード例 #1
0
ファイル: randcube.c プロジェクト: unixpickle/Rubiks
static int _generate_corners(uint8_t * cornersOut) {
    Perm * p = rand_perm(8);

    srand(time(NULL));

    int i;
    for (i = 0; i < 8; i++) {
        int piece = p->map[i];
        int coset = cube_perm_corner_coset(piece, i);
        int perm = rand() % 3;
        if (perm != 0) perm += 3; // perms are 0, 4, and 5
        int orientation = symmetry_operation_compose(coset, perm);
        cornersOut[i] = piece | (orientation << 4);
    }

    while (!validate_corner_orientation(cornersOut)) {
        int sym = (cornersOut[7] >> 4) & 7;
        sym = symmetry_operation_compose(sym, 4);
        cornersOut[7] &= 0xf;
        cornersOut[7] |= (sym << 4);
    }

    int parity = perm_parity(p);
    perm_free(p);
    return parity;
}
コード例 #2
0
ファイル: randcube.c プロジェクト: unixpickle/Rubiks
static void _generate_edges(uint8_t * edgesOut, int cornerParity) {
    Perm * p = rand_perm(12);
    if (perm_parity(p) != cornerParity) {
        rand_change_parity(p);
    }
    uint16_t edgeOrientations = 0;
    int i;
    for (i = 0; i < 12; i++) {
        int piece = p->map[i];
        int s1, s2;
        cube_perm_edge_symmetries(piece, i, &s1, &s2);
        int sym = rand() % 2 == 0 ? s1 : s2;
        edgesOut[i] = piece | (sym << 4);
        if (i == 11) {
            if (!validate_edges(edgesOut)) {
                int newSym = sym == s1 ? s2 : s1;
                edgesOut[i] = piece | (newSym << 4);
            }
        }
    }

    perm_free(p);
}
コード例 #3
0
int main(int argc, char **argv)
{
    int c;
    size_t i;

    size_t n_phrases = 1;

    size_t out_idx[WORDS_MAX];

    /* words to choose from */
    char *words[WORDS_MAX];
    size_t n_words = 0, n_chars = 0;

    /* minimum lengths to generate */
    size_t chars_min = CHARS_MIN_DEFAULT;
    size_t words_min = WORDS_MIN_DEFAULT;

    /* user-specified wordlists */
    struct wlist *wlists[WLISTS_MAX];
    size_t i_wlists = 0, n_wlists = 0;

    /* user-specified words */
    char *uwords[UWORDS_MAX];
    size_t i_uwords = 0, n_uwords = 0;

    /* sep character between words, -1 -> none */
    int sep = -1;
    int camel = 0;

    /* for strtoul */
    char *endptr;

    /* arguments */

    #ifdef _GNU_SOURCE
    struct option long_opts[] = {
        { "include",    required_argument, NULL, 'i' },
        { "list",       required_argument, NULL, 'l' },
        { "chars",      required_argument, NULL, 'c' },
        { "words",      required_argument, NULL, 'w' },
        { "sep",        required_argument, NULL, 's' },
        { "uppercase",  no_argument, NULL, 'u' },
        { "camelcase",  no_argument, NULL, 'u' },
        { "help",       no_argument, NULL, 'h' },
        { "version",    no_argument, NULL, 'v' },
        { 0,            0,           0,     0  }
    };
    while ((c = getopt_long(argc, argv, "hvi:l:c:w:s:u", long_opts, NULL)) != -1)
    #else
    while ((c = getopt(argc, argv, "hvi:l:c:w:s:u")) != -1)
    #endif
    {
        switch (c)
        {
            case 'h':
                print_usage(argv[0]);
                exit(EXIT_SUCCESS);

            case 'v':
                print_version(argv[0]);
                exit(EXIT_SUCCESS);

            case 'i':
                uwords[i_uwords] = optarg;
                n_uwords = (n_uwords < UWORDS_MAX) ? n_uwords + 1 : UWORDS_MAX;
                i_uwords = (i_uwords + 1) % UWORDS_MAX;
                break;

            case 'l':
                wlists[i_wlists] = wlist_read(optarg);
                if (wlists[i_wlists])
                {
                    n_wlists = (n_wlists < WLISTS_MAX) ? n_wlists + 1 : WLISTS_MAX;
                    i_wlists = (i_wlists + 1) % WLISTS_MAX;
                }
                break;

            case 'c':
                chars_min = strtoul(optarg, &endptr, 10);
                if (*endptr != '\0')
                {
                    fprintf(stderr, "invalid number: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                chars_min = (chars_min > CHARS_MAX) ? CHARS_MAX : chars_min;
                break;

            case 'w':
                words_min = strtoul(optarg, &endptr, 10);
                if (*endptr != '\0')
                {
                    fprintf(stderr, "invalid number: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                words_min = (words_min > WORDS_MAX) ? WORDS_MAX : words_min;
                break;

            case 's':
                sep = *optarg;
                break;

            case 'u':
                camel = 1;
                break;

            case '?':
                exit(EXIT_FAILURE);
        }
    }

    if (optind < argc)
    {
        n_phrases = strtoul(argv[optind], &endptr, 10);
        if (*endptr != '\0')
        {
            fprintf(stderr, "invalid number: %s\n", argv[optind]);
            exit(EXIT_FAILURE);
        }
    }

    /* no user-specified wordlists, use default */
    if (!n_wlists)
    {
        wlists[0] = wlist_read(WORDLIST_DEFAULT);
        if (!wlists[0])
        {
            fprintf(stderr, "couldn't read default wordlist\n");
            exit(EXIT_FAILURE);
        }
        n_wlists = 1;
    }

    while (n_phrases--)
    {
        n_chars = 0;
        n_words = 0;

        for (; n_words < n_uwords; ++n_words)
        {
            words[n_words] = uwords[n_words];
            n_chars += strlen(words[n_words]);
        }

        for (; n_words < words_min || n_chars < chars_min; ++n_words)
        {
            size_t iwl, iw;
            iwl = rand_index(n_wlists);

            iw = rand_index(wlists[iwl]->len);

            words[n_words] = wlist_get(wlists[iwl], iw);
            n_chars += strlen(words[n_words]);
        }

        if (camel)
        {
            for (i = 0; i < n_words; ++i)
                *words[i] = toupper(*words[i]);
        }

        rand_perm(out_idx, n_words);

        for (i = 0; i < n_words; ++i)
        {
            if (i && sep != -1)
                printf("%c", sep);

            printf("%s", words[out_idx[i]]);
        }

        printf("\n");
    }

    for (i = 0; i < n_wlists; ++i)
        wlist_free(wlists[i]);

    return EXIT_SUCCESS;
}
コード例 #4
0
ファイル: vm_slice.cpp プロジェクト: behollis/muViewBranch
int main(int argc, char *argv[]) {
  CommandLine cl;
  MathRandom<MathMersenneTwister> rng;
  Error error;
  ya_check_debug();

  YA_MatD input_matrix;
  YA_MatD output_matrix;

  // Parse the command line
  HandleArgs(cl,argc,argv,&error);

  string outputfile="";
  if (cl.argsize(' ')>0) {
    load(cl.argstring(' ',0),input_matrix);
    if (cl.argsize(' ')>1)
      outputfile=cl.argstring(' ',1);
  } else
    read(cin,input_matrix);

  // Select rows
  if (cl['r']) {
    output_matrix=input_matrix(YA_RowI(cl.argstring('r',0)),":");
    input_matrix=output_matrix;
  }
  
  // Select cols
  if (cl['c']) {
    output_matrix=input_matrix(":",YA_RowI(cl.argstring('c',0)));
    input_matrix=output_matrix;
  }

  // Reorder rows using modulus
  else if (cl['z']) {
    ya_sizet mod=cl.argint('z',0);
    if (mod==0)
      error.generate_error(0,"vm_slice","Cannot specify a mod_num of 0.");
    if (input_matrix.rows()%mod!=0) {
      error.buffer() << "When using -z, the number of rows in the matrix "
                     << "must be evenly divisible by the mod_num.";
      error.addbuf(0,"vm_slice");
    }
    YA_VecI row_order(input_matrix.rows());
    ya_sizet offset=input_matrix.rows()/mod;
    for (ya_sizet i=0; i<input_matrix.rows(); i++) {
      div_t index=div(int(i),int(mod));
      row_order(i)=index.quot+index.rem*offset;
    }
    output_matrix=input_matrix(row_order,":");
  } else    
    output_matrix=input_matrix;

  ya_sizet file_format=YA_DEFAULT_IO;
  if (cl['t'])
    file_format=YA_PRETTY_IO;
  if (cl['b'])
    file_format=YA_BINARY_IO;

  // Random subset
  if (cl['s']) {
    double percent=cl.argdouble('s',0);
    if (percent>1)
      error.generate_error(0,"mat_convert",
        "Random percentage must be between 0 and 1");
    YA_RowI rand_perm(randperm(output_matrix.rows(),rng));
    output_matrix=copy(output_matrix(rand_perm,":"));
    ya_sizet cut_frac=ya_sizet(percent*output_matrix.rows());
    if (cl.argstring('s',1)!="NO_OUTPUT")
      save(cl.argstring('s',1),
           output_matrix(vmcount(cut_frac,":",output_matrix.rows()-1),":"),
           file_format);
    output_matrix=copy(output_matrix(vmcount(cut_frac),":"));
  }

  if (cl['q'])
    ip_transpose(output_matrix);

  if (outputfile=="")
    write(cout,output_matrix,file_format);
  else
    save(outputfile,output_matrix,file_format);
  return 0;
}