/*------------------------------------------------------------------ * Function: Iterative_dfs * Purpose: Use a stack variable to implement an iterative version * of depth-first search * In arg: * tour: partial tour of cities visited so far (just city 0) * Globals in: * n: total number of cities in the problem * Notes: * 1 The input tour is modified during execution of search, * but returned to its original state before returning. * 2. The Update_best_tour function will modify the global var * best_tour */ void Iterative_dfs(tour_t tour) { city_t nbr; my_stack_t stack; tour_t curr_tour; stack = Init_stack(); Push(stack, tour); while (!Empty(stack)) { curr_tour = Pop(stack); # ifdef DEBUG printf("Popped tour = %p and %p\n", curr_tour, curr_tour->cities); Print_tour(curr_tour, "Popped"); printf("\n"); # endif if (City_count(curr_tour) == n) { if (Best_tour(curr_tour)) Update_best_tour(curr_tour); } else { for (nbr = n-1; nbr >= 1; nbr--) if (Feasible(curr_tour, nbr)) { Add_city(curr_tour, nbr); Push(stack, curr_tour); Remove_last_city(curr_tour); } } Free_tour(curr_tour); } Free_stack(stack); } /* Iterative_dfs */
static void run() { VALUE stack_start; VALUE code; void Init_stack _((VALUE*)); Init_stack(&stack_start); code = compile(eruby_filename); if (eruby_sync) { print_headers(-1); } else { replace_stdout(); } code = eval(code, eruby_filename); if (eruby_mode == MODE_FILTER && (RTEST(ruby_debug) || RTEST(ruby_verbose))) { print_generated_code(stderr, code, 0); } rb_exec_end_proc(); if (!eruby_sync) { flush_buffer(); } ruby_finalize(); }
void ruby_init(void) { static int initialized = 0; int state; if (initialized) return; initialized = 1; //RHO //rb_origenviron = environ; //RHO Init_stack((void *)&state); Init_BareVM(); Init_heap(); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { rb_call_inits(); ruby_prog_init(); } POP_TAG(); if (state) { error_print(); exit(EXIT_FAILURE); } GET_VM()->running = 1; }
void ruby_init(void) { int state; if (ruby_initialized) return; ruby_initialized = 1; #ifdef __MACOS__ rb_origenviron = 0; #else rb_origenviron = environ; #endif #if WITH_OBJC char *s; s = getenv("MACRUBY_DEBUG"); ruby_dlog_enabled = !(s == NULL || *s == '0'); s = getenv("MACRUBY_DEBUG_FILE"); if (s == NULL) { ruby_dlog_file = stderr; } else { ruby_dlog_file = fopen(s, "w"); if (ruby_dlog_file == NULL) { fprintf(stderr, "cannot open macruby debug file `%s'", strerror(errno)); ruby_dlog_file = stderr; } } #endif Init_stack((void *)&state); Init_PreGC(); Init_BareVM(); Init_heap(); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { rb_call_inits(); #ifdef __MACOS__ _macruby_init(); #elif defined(__VMS) _vmsruby_init(); #endif ruby_prog_init(); ALLOW_INTS; } POP_TAG(); if (state) { error_print(); exit(EXIT_FAILURE); } GET_VM()->running = 1; }
/*------------------------------------------------------------------*/ int main(int argc, char* argv[]) { FILE* digraph_file; tour_t tour; double start, finish; if (argc != 2) Usage(argv[0]); digraph_file = fopen(argv[1], "r"); if (digraph_file == NULL) { fprintf(stderr, "Can't open %s\n", argv[1]); Usage(argv[0]); } Read_digraph(digraph_file); fclose(digraph_file); # ifdef DEBUG Print_digraph(); # endif avail = Init_stack(); best_tour = Alloc_tour(); Init_tour(best_tour, INFINITY); # ifdef DEBUG Print_tour(best_tour, "Best tour"); printf("City count = %d\n", City_count(best_tour)); printf("Cost = %d\n\n", Tour_cost(best_tour)); # endif tour = Alloc_tour(); Init_tour(tour, 0); # ifdef DEBUG Print_tour(tour, "Starting tour"); printf("City count = %d\n", City_count(tour)); printf("Cost = %d\n\n", Tour_cost(tour)); # endif GET_TIME(start); Iterative_dfs(tour); GET_TIME(finish); Free_tour(tour); Print_tour(best_tour, "Best tour"); printf("Cost = %d\n", best_tour->cost); printf("Elapsed time = %e seconds\n", finish-start); free(best_tour->cities); free(best_tour); Free_avail(); free(digraph); return 0; } /* main */
void * ruby_options(int argc, char **argv) { int state; void *tree = 0; Init_stack((void *)&tree); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { SAVE_ROOT_JMPBUF(GET_THREAD(), tree = ruby_process_options(argc, argv)); } else { rb_clear_trace_func(); state = error_handle(state); tree = (void *)INT2FIX(state); } POP_TAG(); return tree; }
void ruby_init(void) { static int initialized = 0; int state; if (initialized) return; initialized = 1; #ifdef __MACOS__ rb_origenviron = 0; #else rb_origenviron = environ; #endif Init_stack((void *)&state); Init_BareVM(); Init_heap(); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { rb_call_inits(); #ifdef __MACOS__ _macruby_init(); #elif defined(__VMS) _vmsruby_init(); #endif ruby_prog_init(); ALLOW_INTS; } POP_TAG(); if (state) { error_print(); exit(EXIT_FAILURE); } GET_VM()->running = 1; }
int main() { stack a; Init_stack(&a,sizeof(int)); blocks buff; int i, b; buff.adress=&b; buff.size=sizeof(int)+sizeof(blocks); for(i=0; i<5; i++) { printf("Ban hay nhap vao mot so nao do: "); scanf("%d", &b); while(getchar()!= '\n'); Push(buff,&a); } for(i=0; i<=6; i++) { printf("Pop!\n"); Pop(&buff,&a); printf("So: %d\n", *(int*)(buff.adress)); } return 0; }
int ruby_cleanup(int ex) { int state; volatile VALUE errs[2]; rb_thread_t *th = GET_THREAD(); int nerr; errs[1] = th->errinfo; th->safe_level = 0; Init_stack((void*)&errs[STACK_UPPER(errs, 0, 1)]); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { SAVE_ROOT_JMPBUF(th, ruby_finalize_0()); } POP_TAG(); errs[0] = th->errinfo; PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { SAVE_ROOT_JMPBUF(th, rb_thread_terminate_all()); } else if (ex == 0) { ex = state; } th->errinfo = errs[1]; ex = error_handle(ex); ruby_finalize_1(); POP_TAG(); rb_thread_stop_timer_thread(); for (nerr = 0; nerr < sizeof(errs) / sizeof(errs[0]); ++nerr) { VALUE err = errs[nerr]; if (!RTEST(err)) continue; /* th->errinfo contains a NODE while break'ing */ if (TYPE(err) == T_NODE) continue; if (rb_obj_is_kind_of(err, rb_eSystemExit)) { return sysexit_status(err); } else if (rb_obj_is_kind_of(err, rb_eSignal)) { VALUE sig = rb_iv_get(err, "signo"); ruby_default_signal(NUM2INT(sig)); } else if (ex == 0) { ex = 1; } } #if EXIT_SUCCESS != 0 || EXIT_FAILURE != 1 switch (ex) { #if EXIT_SUCCESS != 0 case 0: return EXIT_SUCCESS; #endif #if EXIT_FAILURE != 1 case 1: return EXIT_FAILURE; #endif } #endif return ex; }