int main() { int i, n; do { scanf("%d",&n); }while(n<0 || n>20); #define LEN n int A[LEN], B[LEN]; for(i=0;i<n;i++) scanf("%d",&A[i]); for(i=0;i<n;i++) printf("%c",(A[i]) ? '*' : '.'); for(i=0;i<1000;i++) { printf("\n"); if(i%2) { next_generation(B, A, n); if(check == 0) break; } else { next_generation(A, B, n); if(check == 0) break; } } return 0; }
int main(){ int n,i,j,flag,current[20], next[20]; char c; do{ scanf("%d",&n); getchar(); }while(n<0||n>20); for(i=0;i<n;++i){ c=getchar(); c-='0'; current[i]=c; next[i]=current[i]; c=getchar(); } for(j=0; j<1000;++j){ flag = 0; for(i=0;i<n;++i){ current[i] = next[i]; if(next[i] == 1){ flag = 1; printf("*"); } else printf("."); } printf("\n"); if(!flag) break; next_generation(current, next,n); } return 0;
int main(int argc, char * argv[]) { uint32_t generation = 0; init_gfx(); init_random(); run = true; init_world(); while(run) { SDL_Event sdl_ev; if (SDL_PollEvent(&sdl_ev) != 0) { if (sdl_ev.type == SDL_KEYDOWN) switch (sdl_ev.key.keysym.sym) { case SDLK_q: run = false; break; case SDLK_SPACE: pause = !pause; break; default: break; } } if (!pause) { draw(); SDL_Flip(surface); next_generation(); generation++; /* SDL_Delay(50); */ } } SDL_Quit(); return 0; }
int main() { int array_lenght; do { scanf("%d",&array_lenght); }while(array_lenght < 0 || array_lenght > 20); int current[array_lenght]; int next[array_lenght]; int count,count_print; for(count = 0; count < array_lenght; count++) scanf ("%d",¤t[count]); printf("\n"); for(count_print = 0; count_print < array_lenght; count_print++) { if(current[count_print]) printf("*"); else printf("."); } printf("\n"); for(count = 0; count < 4 && check_generation(current, next, array_lenght); count++) { next_generation(current, next, array_lenght); for(count_print = 0; count_print < array_lenght; count_print++) { if(next[count_print]) printf("*"); else printf("."); } printf("\n"); strcpy_int(current, next, array_lenght); } }
int main() { int current[23]; int next[23]; int length; int index = 0; for (index = 0; index < 23; index++) { current[index] = next[index] = 0; } do { scanf("%d", &length); }while (length < 0 || length > 20); for (index = 0; index < length; index++) { scanf("%d", ¤t[index]); } next_generation(current, next, length); return 0; }
void Source::reproduction(const gsl_rng *rand){ assign_fitness(); gsl_ran_multinomial(rand, pop_size, pop_size, fitness, off_numbers); next_generation(rand); mutate_population(rand); }
/* Generate the robbies for the next generation (the list is sorted * from the best fitting to the worst). * You can choose fixed parameters for every robby. */ void generate_robbies(struct robby **rl, long unsigned int couplenum, long unsigned int robbynum, long unsigned int generation) { unsigned long int i; if(generation>0) for (i = 0; i < couplenum; i++) rl[i][0].genome->fitness = rl[i][0].fitness; #ifdef DEBUG_GENOMES list <Species *>::iterator s_it; vector <Genome *>::iterator g_it; i=0; for (s_it = species_list.begin(); s_it != species_list.end(); ++s_it) { cout << "Species BEFORE " << i++ << endl; for (g_it = (*s_it)->genomes.begin(); g_it != (*s_it)->genomes.end(); ++g_it) { cout <<"fitness is: " <<(*g_it)->fitness <<" >> "<< (*g_it)->id << endl; cout << "----" << endl; } } #endif if (generation == 0) { setup_generations(rl, couplenum, robbynum); #ifdef DEBUG_SPECIES FILE *f; f=fopen("fitvalues", "w"); fclose(f); #endif } else { #ifdef DEBUG_SPECIES FILE *f; f=fopen("fitvalues","a"); for(i=0;i<couplenum;i++) { fprintf(f,"%lu %f %lu\n",i, rl[i][0].fitness,generation); fprintf(f,"0 %f %lu\n\n", rl[i][0].fitness, generation); } fprintf(f,"\n"); fclose(f); #endif next_generation(rl, couplenum, robbynum); } #ifdef DEBUG_GENOMES i=0; for (s_it = species_list.begin(); s_it != species_list.end(); ++s_it) { cout << "Species AFTER " << i++ << endl; for (g_it = (*s_it)->genomes.begin(); g_it != (*s_it)->genomes.end(); ++g_it) { cout <<"fitness is: " <<(*g_it)->fitness <<" >> "<< (*g_it)->id << endl; cout << "----" << endl; } } #endif }
int main(){ int current[20], next[20] , helper; scanf( "%d", &length ); for(helper = 0 ; helper < length ; helper++) scanf( "%d", ¤t[helper] ); while(end != 1){ next_generation( current , next , length); } return 0; }
int main() { int length, current[20], next[20], i; scanf ( "%d", &length ) ; for ( i=0;i<length;i++) scanf("%d",¤t[i]); next_generation(current,next,length); return 0; }
int main(int argc, char** argv){ namespace po = boost::program_options; po::options_description opts; opts.add_options() ("input", po::value<std::vector<int>>()->multitoken(), "sequence to generate a formula for (e.g. 1 2 4 8)") ("help,h", po::bool_switch(), "show this help"); po::positional_options_description file_opts; file_opts.add("input", -1); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(opts).positional(file_opts).run(), vm); po::notify(vm); if(vm["help"].as<bool>()){ std::cout << "Genetic Sequence Formulizer, version " << __DATE__ << "\n"; std::cout << "seq2form [-h] <sequence of non-negative integers>\n"; std::cout << opts << std::endl; return 0; } // TODO: make these program options const auto genome_size = 16; const auto population = 10; const auto generations = 2000001 / population; auto evolver = create_evolver(population, genome_size); evolver.goal = vm["input"].as<std::vector<int>>(); std::cout << "your input:\t"; for(auto&& x : evolver.goal) std::cout << x << ", "; std::cout << std::endl; // GO! (until we find a perfect solution, or we hit the generations-bound) for(int i = 0; i < generations; ++i){ evolver.next_generation(); if(evolver.current_generation[0].second <= 0) { break; } } // SHOW! auto & best_genome = evolver.current_generation[0].first; std::cout << "formula:\t" << best_genome.as_formula() << std::endl; std::cout << "continuation:\n"; static const std::vector<const int> inputs = {2003, 1337, 7877, 5732, 8315, 4466, 0354, 8923, 8888, 0000, 4059, 2403}; static const std::vector<const int> outputs = {2, 4, 3, 4, 3, 0, 2, 2, 0, 0, 1, -1}; for(int i = 0; i < inputs.size(); ++i){ std::cout << inputs[i] << " = " << outputs[i] << " = " << best_genome.evaluate_on(i) << ", " << std::endl; } std::cout << std::endl; }
/* * constructor of the class * initializes the timer and side-length of the board, as well as the controller */ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); // sets up the graphical interface running = false; // the game is not running until Start is pressed size = INITIAL_SIZE; // initialize the side-length of the board // reset the world (empty) controller.reset_world_random(size, 0); QTimer *timer = new QTimer(this); // timer for next generation computation connect (timer, SIGNAL(timeout()), this, SLOT(next_generation())); // set the desired number of ms for the timer's signal timer->start(1000); }
int main() { int lenght, counter, number [20]; scanf("%d", &lenght); if(lenght >= 0 && lenght <= 20) for( counter = 0; counter < lenght; counter++ ) scanf( " %d", &number[ counter ] ); next_generation( number, &lenght ); return 0; }
int main(void) { int i, j, k; double time; FILE *output; struct timeval lt, ll; /* allocate memory and read input data */ read_world(); change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size); next_change_list = (cells**)malloc(sizeof(cells*)* world_size*world_size*world_size); /* set timer */ gettimeofday(<, NULL); /* core part */ init_change_list(); for (i = 0; i < num_of_steps; i++) { next_generation(); if (change_list_size == 0){ break; } } /* set timer and print measured time*/ gettimeofday(&ll, NULL); time = (double)(ll.tv_sec - lt.tv_sec) + (double)(ll.tv_usec - lt.tv_usec) / 1000000.0; fprintf(stderr, "Time : %.6lf\n", time); /* write output file */ output = fopen("output.life", "w"); fprintf(output, "%d %d %d %d %d %d 1\n", world_size, D1, D2, L1, L2, num_of_steps); for (i = 0; i < world_size; i++) { for (j = 0; j < world_size; j++) { for (k = 0; k < world_size; k++) { fprintf(output, "%d ", cube[i][j][k].status); } fprintf(output, "\n"); } } fclose(output); return 0; }
int main(){ int len, i = 0, j; char str[20], nextStr[20], temp; scanf("%d", &len); if(len >= 0 && len <= 20){ while(i < len){ scanf("%c", &temp); if(temp != ' '){ printf("error: Input error! Space expected."); return 1; } scanf("%c", &temp); if(temp == '0' || temp == '1'){ if((i == 0 || i == (len -1)) && temp == '1'){ printf("error: Input error! cell chain must start and end with 0."); return 1; } str[i] = temp; } else{ printf("error: Input error! 0/1 expected."); return 1; } i++; } str[i] = '\0'; for(i = 0; i < 1000; i++){ for(j = 0; j < len; j++){ if(str[j] == '0') printf("."); else printf("*"); } printf("\n"); /*this condition is not in the while loop so that a line with only dots is output*/ if(strchr(str, '1') == NULL) break; next_generation(str, nextStr, len); } } return 0; }
int main(){ int n,j,i,check,current[20],next[20]; scanf("%d", &n); while(n < 0 || n > 20){ scanf("%d", &n); } do{ for(i = 0;i < n;i++){ scanf("%d", ¤t[i]); } }while(current[0] != 0 || current[n-1] != 0); for(j = 0;j < 1000;j++){ check = 0; for(i = 0;i < n-1;i++){ if(current[i] == 0){ printf("."); }else{ printf("*"); } } printf(" \n"); next_generation(&next[0],¤t[0],n); for(i = 0;i < n-1;i++){ if(current[i] != 0){ check = 1; break; } } if(check != 1){ for(i = 0;i < n-1;i++){ if(current[i] == 0){ printf("."); }else{ printf("*"); } }printf(" \n"); break; } } }
int main() { int lenght, index; int current[20], next[20]; scanf("%d", &lenght); for(index = 0; index < lenght; index++){ scanf(" %d", ¤t[index]); } next_generation(current, next, lenght); printf("\n\n"); return 0; }
int main(){ lifegame* game = open_lifegame(); generate_lifegame(game, 5, 5); random_lifegame(game); dump_lifegame(game); int runstate = 1; while(runstate == 1){ Sleep(250); system("cls"); next_generation(game); dump_lifegame(game); } close_lifegame(game); return 0; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), ui->RArea, SLOT(next_generation())); pause = true; settings_dialog = new myDialog(); ui->RArea->setAreaSize(((myDialog*)settings_dialog)->getAreaWidth(), ((myDialog*)settings_dialog)->getAreaHeight()); ui->RArea->setBar(ui->statusBar); ui->RArea->setAreaColor(((myDialog*)settings_dialog)->getAreaColor()); ui->RArea->setCellColor(((myDialog*)settings_dialog)->getCellColor()); timerSpeed = ((myDialog*)settings_dialog)->getAreaSpeed(); connect(ui->RArea, SIGNAL(doubleClick()), this, SLOT(fullScreenArea())); ui->playPauseButton->setIcon(QIcon(":/img/media-playback-start.svg")); fullMode = false; }
int main() { int current[21],next[21],lenght,counter,counter2; scanf("%d",&lenght); next[0]=0; next[lenght-1]=0; for (counter=0;counter<lenght;counter++) scanf("%d", ¤t[counter]); for (counter2=0; counter2 < lenght ; counter2++) { if(current[counter2]==1)printf("*"); if(current[counter2]==0)printf("."); } printf("\n"); next_generation(current, next, lenght); return 0; }
void Colony::reproduction(const gsl_rng *rand){ int k,j,new_pop_size; double pop_fitness; assign_fitness(); pop_fitness = 0.; for(j=0;j<pop_size;j++) { pop_fitness += fitness[j]; } pop_fitness = pop_fitness*max_fitness; new_pop_size = gsl_ran_poisson(rand,pop_fitness); // printf("fitness: %lf\tnew pop size: %i\n", pop_fitness, new_pop_size); if (new_pop_size==0) { printf("extinct\n"); exit(0); } if (new_pop_size>limit) { printf("established\n"); exit(0); } gsl_ran_multinomial(rand, pop_size, new_pop_size, fitness, off_numbers); next_generation(rand); pop_size = new_pop_size; mutate_population(rand); for(j=pop_size;j<limit;j++) { for(k=0; k<n_del; k++) { del_matrix[j][k] = 0; } for(k=0; k<n_env; k++) { env_matrix[j][k] = 0; } } };
int main(){ int n, current[20], next[20]; do{ scanf("%d", &n); }while(n < 0 || n > 20 ); do { for(int i=0; i < n; i++) do scanf("%d", ¤t[i]); while(current[i] < 0 || current[i] > 1 ); }while(current[0]!=0 || current[n-1]!=0 ); for(int i=0; i < 1000; i++){ for(int j = 0; j < n; j++) printf("%c", (current[j] == 0) ? '.' : '*'); printf("\n"); next_generation( ¤t[0], &next[0], n); if(checkAlive(¤t[0], n)){ for(int j = 0; j < n; j++) printf("%c", (current[j] == 0) ? '.' : '*'); printf("\n"); break; } } return 0; }
int main() { int current[ 20 ]; int next[ 20 ]; int lenght , lenght_helper , cell_current , i , error = 0; for ( i = 0 ; i < 20 ; i++ ) { *( current + i ) = 0; *( next + i ) = 0; } do { scanf( "%d" , &lenght ); } while( lenght > 20 || lenght < 0 ); for ( lenght_helper = lenght , cell_current = 0 ; lenght_helper > 0 ; lenght_helper -- , cell_current ++ ) { scanf ( "%d" , ¤t[ cell_current ] ); if ( cell_current == 0 || cell_current == lenght - 1) { if ( current [ cell_current ] == 1 ) { printf ( " Error : String must start and end with 0. "); error = 1; break; } } } if ( error == 0 ) next_generation( current , next , lenght ); return 0; }
void next_generation( int *current , int *next , int lenght ) { int i , alive = 0 , start, end; for ( i = 0 ; i <= lenght; i++ ) { if( *( current + i ) == 1 ) { alive ++ ; } } for ( i = 0 ; i < lenght ; i++ ) { if( *( current + i ) == 0 ) printf( " ." ); else printf( " *" ); } if( alive > 0 && repeated < 1000 ) { for ( i = 1 ; i < lenght - 1 ; i++ ) { if ( *( current + i + 1 ) == *( current + i - 1 ) ) { *( next + i ) = 0; } else { *( next + i ) = 1; } } printf( "\n" ); repeated ++; next_generation( next , current , lenght ); } }
int main(int argc, char **argv) { FILE *f1, *f2; PHYLTREE tree1, tree2; long g1, g2, ntr1, ntr2, i, d; char buf[80]; tree1.root = NULL; tree1.num_leaves = 0; tree1.leaf = NULL; tree2.root = NULL; tree2.num_leaves = 0; tree2.leaf = NULL; if (argc > 2) { if ((f1 = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "failed to open %s -- exit.\n", argv[1]); exit (EXIT_FAILURE); } if ((f2 = fopen(argv[2], "r")) == NULL) { fprintf(stderr, "failed to open %s -- exit.\n", argv[2]); exit (EXIT_FAILURE); } while ((!feof(f1)) && (!feof(f2))) { g1 = next_generation(f1); g2 = next_generation(f2); while (g1 < g2) { fprintf(stderr, "generation %ld is missing in %s\n", g1, argv[2]); g1 = next_generation(f1); if (feof(f1) || ferror(f1)) break; } while (g2 < g1) { fprintf(stderr, "generation %ld is missing in %s\n", g2, argv[1]); g2 = next_generation(f2); if (feof(f2) || ferror(f2)) break; } if (ferror(f1)) { fprintf(stderr, "error reading from %s\n", argv[1]); break; } if (ferror(f2)) { fprintf(stderr, "error reading from %s\n", argv[2]); break; } if (feof(f1) || feof(f2)) { if (!feof(f2)) fprintf(stderr, "generation %ld and subsequent ones missing from %s\n", g2, argv[1]); else if (!feof(f1)) fprintf(stderr, "generation %ld and subsequent ones missing from %s\n", g1, argv[2]); break; } getline(f1, buf, 80); ntr1 = strtol(buf, NULL, 10); getline(f2, buf, 80); ntr2 = strtol(buf, NULL, 10); if (ntr1 != ntr2) { fprintf(stderr, "generation %ld: %ld trees in %s, %ld trees in %s\n", g1, ntr1, argv[1], ntr2, argv[2]); continue; } /* printf("g = %ld, n = %ld\n", g1, ntr1); */ for (i = 0; i < ntr1; i++) { if (phyl_read_tree(f1, &tree1) < 0) { fprintf(stderr, "error reading tree from %s, g = %ld, n = %ld\n", argv[1], g1, i); continue; } /* if (tree1.lengthinfo_complete) printf("complete branch length info found in %s\n", argv[1]); else printf("no branch length info found in %s\n", argv[1]); printf("***** tree from %s @ g = %ld, n = %ld *****\n", argv[1], g1, i); print_tree(&tree1); */ if (phyl_read_tree(f2, &tree2) < 0) { phyl_free_tree(&tree1); fprintf(stderr, "error reading tree from %s, g = %ld, n = %ld\n", argv[2], g2, i); continue; } /* if (tree2.lengthinfo_complete) printf("complete branch length info found in %s\n", argv[2]); else printf("no branch length info found in %s\n", argv[2]); printf("***** tree from %s @ g = %ld, n = %ld *****\n", argv[2], g2, i); print_tree(&tree2); */ d = phyl_topotreedist(&tree1, &tree2); if (d >= 0) printf("%ld %ld\n", g1, d); else { switch (d) { case PHYLERR_DIFFNUMLEAVES: fprintf(stderr, "different number of leaves, generation %ld, tree #%ld\n", g1, i); break; case PHYLERR_INCOMPATLEAVES: fprintf(stderr, "different sets of leaves: generation %ld, tree #%ld\n", g1, i); break; default: fprintf(stderr, "error code %ld during tree distance computation\n", d); break; } } /* printf("topological tree distance @ g = %ld, n = %ld: %ld\n", g1, i, d); */ phyl_free_tree(&tree1); phyl_free_tree(&tree2); } } fclose(f1); fclose(f2); } return (0); }
main(int argc, char *argv[]) { /* Declare vars : */ /* - these for the getopt stuff... */ int c; int lflg = 0, mflg = 0, errflg = 0; extern char *optarg; extern int optind, optopt; /* - and these for the main program... */ int i , code_length, pop_size = 200, generations = 10, rand_seed = 123; pop_member *head_of_pop; /* figure out all the cmd line requests... */ while ((c = getopt(argc, argv, ":l:p:g:r:m")) != -1) switch (c) { case 'l': lflg++; code_length = atoi(optarg); break; case 'p': pop_size = atoi(optarg); break; case 'g': generations = atoi(optarg); break; case 'r': rand_seed = atoi(optarg); break; case 'm': mflg++; break; case ':': /* -l, -p, -g, or -r without arguments */ fprintf(stderr, "Option -%c requires an argument!\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); errflg++; } if (!lflg) errflg++; if (errflg || argc == 1) { fprintf(stderr, "usage: \n"); fprintf(stderr, " ga_codes -l <code_length> "); fprintf(stderr, "[ -p <no. of pop members, default=200> ] \\ \n"); fprintf(stderr, " [ -g <no. of generations, default=10> ] \\ \n"); fprintf(stderr, " [ -r <integer random no. seed, default=123> ] \\ \n"); fprintf(stderr, " [ -m [for matlab compatible output] ]\n\n"); exit(2); } /* use random seed to initiate random number generator */ srand48(rand_seed); /* initialize population with randomly generated codes */ head_of_pop = initialize_pop(pop_size, code_length); /* evaluate population */ head_of_pop = apply_evals_to_pop(head_of_pop, code_length); /* sort population by evals */ head_of_pop = sort_pop_by_evals(head_of_pop, code_length); /* print out all codes and evals in pop for diagnostic use... */ /* printout_pop(head_of_pop, code_length); printf("\n"); */ /* loop thru iterations of reproduction and replacing pop members that * have poor evals... */ for (i = 1; i <= generations; i++) { /* run a reproduction cycle */ head_of_pop = next_generation(head_of_pop, pop_size, code_length); /* evaluate population */ head_of_pop = apply_evals_to_pop(head_of_pop, code_length); /* sort population by evals */ head_of_pop = sort_pop_by_evals(head_of_pop, code_length); /* print out all codes and evals in pop for diagnostic use... * printout_pop(head_of_pop, code_length); printf("\n"); */ } /* print output info... */ printout_info(mflg, code_length, pop_size, generations, rand_seed, head_of_pop->code, head_of_pop->eval, head_of_pop->max_sl_height); } /* end main */
// TODO - old pickle - remove in "six months". int old_record_apply_dim(as_remote_record *rr, as_storage_rd *rd, bool skip_sindex, bool *is_delete) { as_namespace* ns = rr->rsv->ns; as_record* r = rd->r; // Set rd->n_bins! as_storage_rd_load_n_bins(rd); // Set rd->bins! as_storage_rd_load_bins(rd, NULL); // For memory accounting, note current usage. uint64_t memory_bytes = as_storage_record_get_n_bytes_memory(rd); // Keep old bins intact for sindex adjustment and unwinding. uint16_t n_old_bins = rd->n_bins; as_bin* old_bins = rd->bins; uint16_t n_new_bins = cf_swap_from_be16(*(uint16_t *)rr->pickle); as_bin new_bins[n_new_bins]; memset(new_bins, 0, sizeof(new_bins)); rd->n_bins = n_new_bins; rd->bins = new_bins; // Fill the new bins and particles. int result = unpickle_bins(rr, rd, NULL); if (result != 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed unpickle bins ", ns->name); destroy_stack_bins(new_bins, n_new_bins); return result; } // Apply changes to metadata in as_index needed for and writing. index_metadata old_metadata; update_index_metadata(rr, &old_metadata, r); // Prepare to store or drop key, as determined by message. rd->key = rr->key; rd->key_size = rr->key_size; // Write the record to storage. if ((result = as_record_write_from_pickle(rd)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed write ", ns->name); unwind_index_metadata(&old_metadata, r); destroy_stack_bins(new_bins, n_new_bins); return -result; } // Success - adjust sindex, looking at old and new bins. if (! (skip_sindex && next_generation(r->generation, (uint16_t)rr->generation, ns)) && record_has_sindex(r, ns)) { write_sindex_update(ns, as_index_get_set_name(r, ns), rr->keyd, old_bins, n_old_bins, new_bins, n_new_bins); } // Cleanup - destroy relevant bins, can't unwind after. destroy_stack_bins(old_bins, n_old_bins); // Fill out new_bin_space. as_bin_space* new_bin_space = NULL; if (n_new_bins != 0) { new_bin_space = (as_bin_space*) cf_malloc_ns(sizeof(as_bin_space) + sizeof(new_bins)); new_bin_space->n_bins = rd->n_bins; memcpy((void*)new_bin_space->bins, new_bins, sizeof(new_bins)); } // Swizzle the index element's as_bin_space pointer. as_bin_space* old_bin_space = as_index_get_bin_space(r); if (old_bin_space) { cf_free(old_bin_space); } as_index_set_bin_space(r, new_bin_space); // Now ok to store or drop key, as determined by message. as_record_finalize_key(r, ns, rd->key, rd->key_size); as_storage_record_adjust_mem_stats(rd, memory_bytes); *is_delete = n_new_bins == 0; return AS_OK; }
int record_apply_ssd(as_remote_record *rr, as_storage_rd *rd, bool skip_sindex, bool *is_delete) { // TODO - old pickle - remove in "six months". if (rr->is_old_pickle) { return old_record_apply_ssd(rr, rd, skip_sindex, is_delete); } as_namespace* ns = rr->rsv->ns; as_record* r = rd->r; bool has_sindex = ! (skip_sindex && next_generation(r->generation, (uint16_t)rr->generation, ns)) && record_has_sindex(r, ns); int result; uint16_t n_old_bins = 0; as_bin *old_bins = NULL; uint16_t n_new_bins = rr->n_bins; as_bin *new_bins = NULL; if (has_sindex) { // TODO - separate function? if ((result = as_storage_rd_load_n_bins(rd)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed load n-bins ", ns->name); return -result; } n_old_bins = rd->n_bins; old_bins = alloca(n_old_bins * sizeof(as_bin)); if ((result = as_storage_rd_load_bins(rd, old_bins)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed load bins ", ns->name); return -result; } // Won't use to flatten. rd->bins = NULL; if (n_new_bins != 0) { new_bins = alloca(n_new_bins * sizeof(as_bin)); memset(new_bins, 0, n_new_bins * sizeof(as_bin)); if ((result = as_flat_unpack_remote_bins(rr, new_bins)) != 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed unpickle bins ", ns->name); return -result; } } } // Won't use to flatten, but needed to know if bins are in use. rd->n_bins = n_new_bins; // Apply changes to metadata in as_index needed for and writing. index_metadata old_metadata; update_index_metadata(rr, &old_metadata, r); // Write the record to storage. if ((result = as_record_write_from_pickle(rd)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed write ", ns->name); unwind_index_metadata(&old_metadata, r); return -result; } // Success - adjust sindex, looking at old and new bins. if (has_sindex) { write_sindex_update(ns, as_index_get_set_name(r, ns), rr->keyd, old_bins, n_old_bins, new_bins, n_new_bins); } // Now ok to store or drop key, as determined by message. as_record_finalize_key(r, ns, rr->key, rr->key_size); *is_delete = n_new_bins == 0; return AS_OK; }
// TODO - old pickle - remove in "six months". int old_record_apply_ssd(as_remote_record *rr, as_storage_rd *rd, bool skip_sindex, bool *is_delete) { as_namespace* ns = rr->rsv->ns; as_record* r = rd->r; bool has_sindex = ! (skip_sindex && next_generation(r->generation, (uint16_t)rr->generation, ns)) && record_has_sindex(r, ns); uint16_t n_old_bins = 0; int result; if (has_sindex) { // Set rd->n_bins! if ((result = as_storage_rd_load_n_bins(rd)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed load n-bins ", ns->name); return -result; } n_old_bins = rd->n_bins; } as_bin old_bins[n_old_bins]; if (has_sindex) { // Set rd->bins! if ((result = as_storage_rd_load_bins(rd, old_bins)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed load bins ", ns->name); return -result; } } // Stack space for resulting record's bins. uint16_t n_new_bins = cf_swap_from_be16(*(uint16_t *)rr->pickle); as_bin new_bins[n_new_bins]; memset(new_bins, 0, sizeof(new_bins)); rd->n_bins = n_new_bins; rd->bins = new_bins; // Fill the new bins and particles. cf_ll_buf_define(particles_llb, STACK_PARTICLES_SIZE); if ((result = unpickle_bins(rr, rd, &particles_llb)) != 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed unpickle bins ", ns->name); cf_ll_buf_free(&particles_llb); return result; } // Apply changes to metadata in as_index needed for and writing. index_metadata old_metadata; update_index_metadata(rr, &old_metadata, r); // Prepare to store or drop key, as determined by message. rd->key = rr->key; rd->key_size = rr->key_size; // Write the record to storage. if ((result = as_record_write_from_pickle(rd)) < 0) { cf_warning_digest(AS_RECORD, rr->keyd, "{%s} record replace: failed write ", ns->name); unwind_index_metadata(&old_metadata, r); cf_ll_buf_free(&particles_llb); return -result; } // Success - adjust sindex, looking at old and new bins. if (has_sindex) { write_sindex_update(ns, as_index_get_set_name(r, ns), rr->keyd, old_bins, n_old_bins, new_bins, n_new_bins); } // Now ok to store or drop key, as determined by message. as_record_finalize_key(r, ns, rd->key, rd->key_size); *is_delete = n_new_bins == 0; cf_ll_buf_free(&particles_llb); return AS_OK; }
int main() { const std::string gol_name = "Game of Life Mini V0.2"; const int delaz = 100; assert(delaz > 0); const std::chrono::milliseconds delay(delaz); const int x_size = 80, y_size = 80; const bool term = false, sfml = true; assert(x_size > 2); assert(x_size <= 100); assert(y_size > 2); assert(y_size <= 100); assert(generation_loop > 0); const int dot_size = 8; const int window_x = x_size*dot_size, window_y = y_size*dot_size; assert(x_size > 0); assert(y_size > 0); std::vector < std::vector<int>> life_grid(y_size, std::vector<int>(x_size, 0)); std::vector < std::vector<int>> change_grid(y_size, std::vector<int>(x_size, 0)); initialize_grid(life_grid); for (int pos = 0; pos < y_size; ++pos) { life_grid[pos][pos] = 1; life_grid[y_size - 1 - pos][pos] = 1; } const std::string life_dot_img = "Life_Dot.png"; sf::Texture life_dot_tex; if (!life_dot_tex.loadFromFile(life_dot_img)) { std::cout << life_dot_img << " not found!\n"; } sf::Sprite life_dot_sprite; life_dot_sprite.setTexture(life_dot_tex); sf::RenderWindow window(sf::VideoMode(window_x, window_y), gol_name, sf::Style::Default); int cur_gen = 0; while (window.isOpen()) { if (sfml) { show_grid_sfml(life_grid, window, life_dot_sprite, dot_size); } if (term) { show_grid_term(life_grid, cur_gen); } next_generation(life_grid, change_grid); life_grid = change_grid; ++cur_gen; std::this_thread::sleep_for(delay); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { window.close(); return 1; } } return 0; }