/************************************************************************* * <Function-name="main" * * * * Description:Searches for a Particualr String and prints the function * * in which the string is found and the Function path * * * * Parameters:argc and argv * * * * Returns:nothing * * * * Added By:SANDEEP * * * * * ************************************************************************/ int main(int argc,char **argv) { int i; for(i=0;i<argc-4;i++) { fun_names(argc,argv,i); search_string(argc,argv,i); path(argc,argv,i); if(occur>0) { printf("\n\n*************Total number of occurences are %d***************",occur); } else printf("\n No Match Found"); s=0; occur=0; } print_path(argc,argv); return 0; }
void DeductionsImpl::print_path(Callee *callee, GraphBuilder *gb) { for(Deduction *d: d_list) { if(d->second == callee) { clang::DiagnosticsEngine *diag = d->TU->getDiagEng(); if(d->CE) { diag->Report(d->CE->getLocStart(), error_realtime_saftey_trace) << d->first->getName(); } else { diag->Report(error_realtime_safety_class) << d->second->name << d->first->name; } print_path(d->first, gb); return; } } clang::DiagnosticsEngine *diag = callee->TU->getDiagEng(); diag->Report(callee->DECL->getLocation(), error_realtime_saftey_trace_end) << callee->getName(); }
/** Algorithm * Receive orders from Coordinator * * Calculate a the cost of the path / find best path given results * */ void slave(int **city_dist, const int num_of_cities, const int my_rank, const int nprocs, const int size_of_work) { MPI_Status stat; int local_lowest_cost = INT32_MAX; int stay_alive = 1; int *local_best_path = malloc((unsigned long) num_of_cities * sizeof(int)); int **my_work = allocate_cells(num_of_cities, size_of_work); while (true) { MPI_Recv(&stay_alive, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat); if (stay_alive == -1) { //printf("Kill the %ith slave\n", my_rank); return; } // Receive work and bound from master MPI_Recv(my_work[0], num_of_cities * size_of_work, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat); MPI_Recv(&local_lowest_cost, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat); for (int i = 0; i < size_of_work; i++) { int *best_dfs_path; best_dfs_path = dfs(my_work[i], num_of_cities, city_dist, 6, local_lowest_cost); int best_dfs_cost = calculate_full_tour_distance(best_dfs_path, city_dist, num_of_cities); if (local_lowest_cost > best_dfs_cost) { local_lowest_cost = best_dfs_cost; memcpy(local_best_path, best_dfs_path, (unsigned long) num_of_cities * sizeof(int)); printf("Lowest cost: %i \t", local_lowest_cost); print_path(num_of_cities, local_best_path); } free(best_dfs_path); } // Finished all work // Send best path and distance to master MPI_Send(local_best_path, num_of_cities, MPI_INT, 0, 0, MPI_COMM_WORLD); MPI_Send(&local_lowest_cost, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); } }
void find_paths(node *root,int path[],int len) { if(root) { //printf("%d ",root->data); path[len]=root->data; len++; } if(root->left||root->right) { find_paths(root->left,path,len); find_paths(root->right,path,len); } if(root->left==NULL && root->right==NULL) { print_path(path,len); return; } }
int main() { /*首先输入起始状态和目标状态*/ memset(q,0,sizeof q); for(i=0;i<9;i++) { scanf("%d",&(q[0].state[i])); } for(i=0;i<9;i++) { scanf("%d",&destination.state[i]); } memset(vis,0,sizeof vis); /*然后进行搜索并输出*/ int founded=bfs(); if(founded) print_path(founded); else printf("-1\n"); system("pause"); return 0; }
int main(int argc, char *argv[]){ int i; get_input(argv[1]); init_best_path(best_path); #pragma omp parallel for for(i = 1; i < numCities;i++){ Path *curr_path; curr_path = malloc( sizeof( Path)); init_path(curr_path); add_city(curr_path, i); compute_path(curr_path ); } print_path(best_path); return 0; }
void launch(t_info *info) { int n_path; int index; index = 0; n_path = amount_path(info->list, info); info->path = (t_room ***)ft_memalloc((n_path + 1) * sizeof(t_room **)); get_addend(info->list)->distance = 0; while (index < n_path) { put_val(info, 0); if ((info->path[index] = find_path(info)) == NULL) break ; reinit(info->list); index++; } info->tab = fill_tab(info, n_path); write(1, "\n", 1); if (info->b_path == 1) print_path(info, n_path, info->t_size); launch2(info, n_path); }
void print_path(int source, int node, std::vector<int> predecessor, Robot* robot, std::vector<Vector2> rand_points, bool& replan, std::vector<Robot *> robots) { if (node == source) { robot->path.push_back(rand_points[source]); std::cout << (char)(node + 97) << ".."; } else if (predecessor[node] == -1) { std::cout << "No path from “<<source<<” to "<< (char)(node + 97) << std::endl; clear_paths(robots); return; } else { print_path(source, predecessor[node], predecessor, robot, rand_points, replan, robots); robot->path.push_back(rand_points[node]); std::cout << (char) (node + 97) << ".."; } replan = false; }
int main(int argc, char **argv) { FILE *fp; Graph *graph; char *filename = "duom.txt"; fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr,"Failed to open file filename\n"); return -1; } graph = initialise_graph(graph); read_file (graph, fp); make_adjacent_list(graph); find_shortest_path(graph, 0); print_path (graph, 0, graph->size - 1); return 0; }
static void show_filemodify(struct diff_queue_struct *q, struct diff_options *options, void *data) { int i; /* * Handle files below a directory first, in case they are all deleted * and the directory changes to a file or symlink. */ qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first); for (i = 0; i < q->nr; i++) { struct diff_filespec *ospec = q->queue[i]->one; struct diff_filespec *spec = q->queue[i]->two; switch (q->queue[i]->status) { case DIFF_STATUS_DELETED: printf("D "); print_path(spec->path); putchar('\n'); break; case DIFF_STATUS_COPIED: case DIFF_STATUS_RENAMED: printf("%c ", q->queue[i]->status); print_path(ospec->path); putchar(' '); print_path(spec->path); putchar('\n'); if (!hashcmp(ospec->sha1, spec->sha1) && ospec->mode == spec->mode) break; /* fallthrough */ case DIFF_STATUS_TYPE_CHANGED: case DIFF_STATUS_MODIFIED: case DIFF_STATUS_ADDED: /* * Links refer to objects in another repositories; * output the SHA-1 verbatim. */ if (no_data || S_ISGITLINK(spec->mode)) printf("M %06o %s ", spec->mode, sha1_to_hex(anonymize ? anonymize_sha1(spec->sha1) : spec->sha1)); else { struct object *object = lookup_object(spec->sha1); printf("M %06o :%d ", spec->mode, get_object_mark(object)); } print_path(spec->path); putchar('\n'); break; default: die("Unexpected comparison status '%c' for %s, %s", q->queue[i]->status, ospec->path ? ospec->path : "none", spec->path ? spec->path : "none"); } } }
/* TODO: append matches to some data structure instead of just printing them out * then there can be sweet summaries of matches/files scanned/time/etc */ int search_dir(const pcre *re, const pcre_extra *re_extra, const char* path, const int depth) { /* TODO: don't just die. also make max depth configurable */ if (depth > MAX_SEARCH_DEPTH) { log_err("Search depth greater than %i, giving up.", depth); exit(1); } struct dirent **dir_list = NULL; struct dirent *dir = NULL; int results = 0; int fd = -1; off_t f_len = 0; char *buf = NULL; int rv = 0; char *dir_full_path = NULL; size_t path_length = 0; int i; results = scandir(path, &dir_list, &ignorefile_filter, &alphasort); if (results > 0) { for (i = 0; i < results; i++) { dir = dir_list[i]; path_length = (size_t)(strlen(path) + strlen(dir->d_name) + 2); /* 2 for slash and null char */ dir_full_path = malloc(path_length); dir_full_path = strncpy(dir_full_path, path, path_length); dir_full_path = strncat(dir_full_path, "/", path_length); dir_full_path = strncat(dir_full_path, dir->d_name, path_length); load_ignore_patterns(dir_full_path); free(dir); dir = NULL; free(dir_full_path); dir_full_path = NULL; } } free(dir_list); dir_list = NULL; results = scandir(path, &dir_list, &filename_filter, &alphasort); if (results == 0) { log_debug("No results found in directory %s", path); free(dir_list); dir_list = NULL; return(0); } else if (results == -1) { log_err("Error opening directory %s", path); return(0); } match matches[MAX_MATCHES_PER_FILE]; int matches_len = 0; int buf_len = 0; int buf_offset = 0; int offset_vector[MAX_MATCHES_PER_FILE * 3]; /* TODO */ int rc = 0; struct stat statbuf; int binary = 0; for (i=0; i<results; i++) { matches_len = 0; buf_offset = 0; binary = 0; dir = dir_list[i]; /* TODO: this is copy-pasted from about 30 lines above */ path_length = (size_t)(strlen(path) + strlen(dir->d_name) + 2); /* 2 for slash and null char */ dir_full_path = malloc(path_length); dir_full_path = strncpy(dir_full_path, path, path_length); dir_full_path = strncat(dir_full_path, "/", path_length); dir_full_path = strncat(dir_full_path, dir->d_name, path_length); log_debug("dir %s type %i", dir_full_path, dir->d_type); /* TODO: scan files in current dir before going deeper */ if (dir->d_type == DT_DIR) { if (opts.recurse_dirs) { log_debug("Searching dir %s", dir_full_path); rv = search_dir(re, re_extra, dir_full_path, depth + 1); } goto cleanup; } if (opts.file_search_regex) { rc = pcre_exec(opts.file_search_regex, NULL, dir_full_path, strlen(dir_full_path), buf_offset, 0, offset_vector, 3); if (rc < 0) { /* no match */ log_debug("Skipping %s due to file_search_regex.", dir_full_path); goto cleanup; } } fd = open(dir_full_path, O_RDONLY); if (fd < 0) { log_err("Error opening file %s. Skipping...", dir_full_path); goto cleanup; } rv = fstat(fd, &statbuf); if (rv != 0) { log_err("Error fstat()ing file %s. Skipping...", dir_full_path); goto cleanup; } f_len = statbuf.st_size; if (f_len == 0) { log_debug("File %s is empty, skipping.", dir_full_path); goto cleanup; } else if (f_len > 1024 * 1024 * 1024) { /* 1 GB */ log_err("File %s is too big. Skipping...", dir_full_path); goto cleanup; } buf = mmap(0, f_len, PROT_READ, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { log_err("File %s failed to load: %s.", dir_full_path, strerror(errno)); goto cleanup; } buf_len = f_len; if (is_binary((void*)buf, buf_len)) { /* Who needs duck typing when you have void cast? :) */ if (opts.search_binary_files) { binary = 1; } else { log_debug("File %s is binary. Skipping...", dir_full_path); goto cleanup; } } if (opts.literal) { char *match_ptr = buf; char *(*ag_strncmp_fp)(const char*, const char*, size_t) = &ag_strnstr; if (opts.casing == CASE_INSENSITIVE) { ag_strncmp_fp = &ag_strncasestr; } while (buf_offset < buf_len) { match_ptr = ag_strncmp_fp(match_ptr, opts.query, buf_len - buf_offset); if (match_ptr == NULL) { break; } matches[matches_len].start = match_ptr - buf; matches[matches_len].end = matches[matches_len].start + opts.query_len; buf_offset = matches[matches_len].end; matches_len++; match_ptr++; /* Don't segfault. TODO: realloc this array */ if (matches_len >= MAX_MATCHES_PER_FILE) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } else { /* In my profiling, most of the execution time is spent in this pcre_exec */ while (buf_offset < buf_len && (rc = pcre_exec(re, re_extra, buf, buf_len, buf_offset, 0, offset_vector, 3)) >= 0) { log_debug("Match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]); buf_offset = offset_vector[1]; matches[matches_len].start = offset_vector[0]; matches[matches_len].end = offset_vector[1]; matches_len++; /* Don't segfault. TODO: realloc this array */ if (matches_len >= MAX_MATCHES_PER_FILE) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } if (opts.stats) { total_file_count++; total_byte_count += buf_len; } if (rc == -1) { log_debug("No match in %s", dir_full_path); } if (matches_len > 0) { if (opts.print_filename_only) { print_path(dir_full_path); } else { if (binary) { printf("Binary file %s matches.\n", dir_full_path); } else { print_file_matches(dir_full_path, buf, buf_len, matches, matches_len); } } } cleanup: if (fd != -1) { munmap(buf, f_len); close(fd); fd = -1; } free(dir); dir = NULL; free(dir_full_path); dir_full_path = NULL; } free(dir_list); dir_list = NULL; return(0); }
vector<vector<node*> > generate_path(vector<node*> tree_out) { vector<vector<node*> > total_path; for(int i = 0 ; i < tree_out.size(); i++) { node* currentNode; stack<node*> stack; if(tree_out[i]->input.size() == 2){ vector<node*> path; path.push_back(tree_out[i]); tree_out[i]->path.push(path); } stack.push(tree_out[i]); vector<node*> path_from_out; while(!stack.empty()) { currentNode = stack.top(); stack.pop(); path_from_out.push_back(currentNode); if(currentNode->input.size() == 2){ vector<node*> path_till_now = path_from_out; currentNode->left->path.push(path_till_now); currentNode->right->path.push(path_till_now); } if(currentNode->input.size() == 0) { vector<node*> single_path = path_from_out; total_path.push_back(single_path); if((currentNode->left == NULL) && (currentNode->right == NULL)) { if(!stack.empty()) { if(!stack.top()->path.empty()) { path_from_out = stack.top()->path.top(); stack.top()->path.pop(); } } } continue; } for(int k = 0; k < currentNode->input.size(); k++) { if((k == 0) && (currentNode->left != NULL)) { stack.push(currentNode->left); } else if((k == 1) && (currentNode->right != NULL)) { stack.push(currentNode->right); } } } } //insert_delay(total_path); print_path(total_path); return total_path; }
void search_buf(const pcre *re, const pcre_extra *re_extra, const char *buf, const int buf_len, const char *dir_full_path) { int binary = 0; int buf_offset = 0; match matches[opts.max_matches_per_file]; int matches_len = 0; int offset_vector[opts.max_matches_per_file * 3]; /* TODO */ int rc = 0; if (is_binary((void*)buf, buf_len)) { /* Who needs duck typing when you have void cast? :) */ if (opts.search_binary_files) { binary = 1; } else { log_debug("File %s is binary. Skipping...", dir_full_path); return; } } if (opts.literal) { const char *match_ptr = buf; char *(*ag_strncmp_fp)(const char*, const char*, const size_t, const size_t, const size_t[]) = &boyer_moore_strnstr; if (opts.casing == CASE_INSENSITIVE) { ag_strncmp_fp = &boyer_moore_strncasestr; } while (buf_offset < buf_len) { match_ptr = ag_strncmp_fp(match_ptr, opts.query, buf_len - buf_offset, opts.query_len, skip_lookup); if (match_ptr == NULL) { break; } matches[matches_len].start = match_ptr - buf; matches[matches_len].end = matches[matches_len].start + opts.query_len; buf_offset = matches[matches_len].end; log_debug("Match found. File %s, offset %i bytes.", dir_full_path, matches[matches_len].start); matches_len++; match_ptr += opts.query_len; /* Don't segfault. TODO: realloc this array */ if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } else { /* In my profiling, most of the execution time is spent in this pcre_exec */ while (buf_offset < buf_len && (rc = pcre_exec(re, re_extra, buf, buf_len, buf_offset, 0, offset_vector, 3)) >= 0) { log_debug("Regex match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]); buf_offset = offset_vector[1]; matches[matches_len].start = offset_vector[0]; matches[matches_len].end = offset_vector[1]; matches_len++; /* Don't segfault. TODO: realloc this array */ if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } if (opts.invert_match) { matches_len = invert_matches(matches, matches_len, buf_len); } if (opts.stats) { stats.total_bytes += buf_len; stats.total_files++; stats.total_matches += matches_len; } if (matches_len > 0) { if (opts.print_filename_only) { print_path(dir_full_path, '\n'); } else { if (binary) { print_binary_file_matches(dir_full_path); } else { print_file_matches(dir_full_path, buf, buf_len, matches, matches_len); } } } else { log_debug("No match in %s", dir_full_path); } }
void BFS(int start, ADJACENT_LIST graph, int vertex_num) { INFO_OF_NODE info; int queue[MAXSIZE]; int head = 0, tail = 1; memset(queue, 0, sizeof(queue)); queue[0] = start; info[start].color = GRAY; info[start].parent = -1; info[start].depth = 0; int i; for(i = 1; i <= vertex_num; ++i) if(i != start) { info[i].color = WHITE; info[i].depth = -1; info[i].parent = -1; } while(head != tail) { int u = queue[head++]; NODE iter = graph[u]; int adjacency = iter->data; while(adjacency != -1) { if(info[adjacency].color == WHITE) { info[adjacency].color = GRAY; info[adjacency].parent = u; info[adjacency].depth = info[u].depth+1; queue[tail++] = adjacency; } iter = iter->next; adjacency = iter->data; } } // print BFS tree. int max_depth = -1; for(i = 1; i <= vertex_num; ++i) { if(info[i].depth > max_depth) max_depth = info[i].depth; } i = 0; int j; while(i <= max_depth) { for(j = 1; j <= vertex_num; ++j) if(info[j].depth == i) printf("%d ", j); printf("\n"); i++; } print_path(start, 8, info); }
void print_file_matches(const char* path, const char* buf, const int buf_len, const match matches[], const int matches_len) { int line = 1; char **context_prev_lines = NULL; int prev_line = 0; int last_prev_line = 0; int prev_line_offset = 0; int cur_match = 0; /* TODO the line below contains a terrible hack */ int lines_since_last_match = 1000000; /* if I initialize this to INT_MAX it'll overflow */ int lines_to_print = 0; int last_printed_match = 0; char sep = '-'; int i, j; int in_a_match = FALSE; int printing_a_match = FALSE; if (opts.ackmate) { sep = ':'; } print_file_separator(); if (opts.print_heading == TRUE) { print_path(path, '\n'); } context_prev_lines = ag_calloc(sizeof(char*), (opts.before + 1)); for (i = 0; i <= buf_len && (cur_match < matches_len || lines_since_last_match <= opts.after); i++) { if (cur_match < matches_len && i == matches[cur_match].end) { /* We found the end of a match. */ cur_match++; in_a_match = FALSE; } if (cur_match < matches_len && i == matches[cur_match].start) { in_a_match = TRUE; /* We found the start of a match */ if (cur_match > 0 && opts.context && lines_since_last_match > (opts.before + opts.after + 1)) { fprintf(out_fd, "--\n"); } if (lines_since_last_match > 0 && opts.before > 0) { /* TODO: better, but still needs work */ /* print the previous line(s) */ lines_to_print = lines_since_last_match - (opts.after + 1); if (lines_to_print < 0) { lines_to_print = 0; } else if (lines_to_print > opts.before) { lines_to_print = opts.before; } for (j = (opts.before - lines_to_print); j < opts.before; j++) { prev_line = (last_prev_line + j) % opts.before; if (context_prev_lines[prev_line] != NULL) { if (opts.print_heading == 0) { print_path(path, ':'); } print_line_number(line - (opts.before - j), sep); fprintf(out_fd, "%s\n", context_prev_lines[prev_line]); } } } lines_since_last_match = 0; } /* We found the end of a line. */ if (buf[i] == '\n' && opts.before > 0) { if (context_prev_lines[last_prev_line] != NULL) { free(context_prev_lines[last_prev_line]); } /* We don't want to strcpy the \n */ context_prev_lines[last_prev_line] = ag_strndup(&buf[prev_line_offset], i - prev_line_offset); last_prev_line = (last_prev_line + 1) % opts.before; } if (buf[i] == '\n' || i == buf_len) { if (lines_since_last_match == 0) { if (opts.print_heading == 0 && !opts.search_stream) { print_path(path, ':'); } if (opts.ackmate) { /* print headers for ackmate to parse */ print_line_number(line, ';'); for (; last_printed_match < cur_match; last_printed_match++) { fprintf(out_fd, "%i %i", (matches[last_printed_match].start - prev_line_offset), (matches[last_printed_match].end - matches[last_printed_match].start) ); last_printed_match == cur_match - 1 ? fputc(':', out_fd) : fputc(',', out_fd); } j = prev_line_offset; /* print up to current char */ for (; j <= i; j++) { fputc(buf[j], out_fd); } } else { print_line_number(line, ':'); if (opts.column) { fprintf(out_fd, "%i:", (matches[last_printed_match].start - prev_line_offset) + 1); } if (printing_a_match && opts.color) { fprintf(out_fd, "%s", opts.color_match); } for (j = prev_line_offset; j <= i; j++) { if (j == matches[last_printed_match].end && last_printed_match < matches_len) { if (opts.color) { fprintf(out_fd, "%s", color_reset); } printing_a_match = FALSE; last_printed_match++; } if (j == matches[last_printed_match].start && last_printed_match < matches_len) { if (opts.color) { fprintf(out_fd, "%s", opts.color_match); } printing_a_match = TRUE; } /* Don't print the null terminator */ if (j < buf_len) { fputc(buf[j], out_fd); } } if (printing_a_match && opts.color) { fprintf(out_fd, "%s", color_reset); } } } else if (lines_since_last_match <= opts.after && i != buf_len) { /* print context after matching line */ if (opts.print_heading == 0) { print_path(path, ':'); } print_line_number(line, sep); for (j = prev_line_offset; j < i; j++) { fputc(buf[j], out_fd); } fputc('\n', out_fd); } prev_line_offset = i + 1; /* skip the newline */ line++; if (!in_a_match) { lines_since_last_match++; } /* File doesn't end with a newline. Print one so the output is pretty. */ if (i == buf_len && buf[i] != '\n') { fputc('\n', out_fd); } } } for (i = 0; i < opts.before; i++) { if (context_prev_lines[i] != NULL) { free(context_prev_lines[i]); } } free(context_prev_lines); }
void visit(Visitor &visitor) const { // Visits unique trophic chains the network represented by adjancency. DEBUG(Rprintf("DUMP GRAPH\n")); DEBUG(print_adjacency()); bool queue_warning = false; for(int n = 0; n < adjacency_.size(); ++n) { if(adjacency_[n].size() == 0) continue; // Node n has no out-edges, it // cannot start a chain if(is_basal_[n] == 0) continue; // Node n has in-edges, so it cannot // start a chain IntVector path(1, n); DEBUG(print_path("INITIAL PATH", path)); Queue queue; queue.push(path); while(!queue.empty()) { path = queue.front(); queue.pop(); R_ProcessEvents(); if(max_queue_>0 && !queue_warning && queue.size()>max_queue_/2) { REprintf("This network has a lot of paths, possibly too many to " "compute\n"); queue_warning = true; } else if(max_queue_>0 && queue.size()>max_queue_) { throw CheddarException("Unable to compute paths - see the help for " "TrophicChains for more information."); } int m = path.back(); DEBUG(Rprintf("AT NODE [%d]\n", m)); if(adjacency_[m].size() == 0) { DEBUG(print_path("", path)); visitor.chain(path); } else { bool cycle = true; for(int j=0; j<adjacency_[m].size(); ++j) { bool found = false; for(IntVector::const_iterator k=path.begin(); k!=path.end(); ++k) { if(*k == adjacency_[m][j]) { found = true; break; } } if(!found) { path.push_back(adjacency_[m][j]); DEBUG(print_path("EXTEND PATH", path)); queue.push(path); path.pop_back(); cycle = false; } } if(cycle) { DEBUG(print_path("", path)); visitor.chain(path); } } } } }
int main() { char s1[5], s2[5]; int n, u, v, s, t; int i, j, cnt; int f = 0; //freopen( "a.in", "r", stdin ); //freopen( "a.out", "w", stdout ); while( 1 ) { if ( scanf( "%d", &n ) == EOF ) { break; } if ( f ) { scanf( "\n" ); printf( "\n" ); } f = 1; memset( a, false, sizeof( a ) ); for ( i = cnt = 0; i < n; ++i ) { scanf( "%s %s", &s1, &s2 ); u = v = -1; for ( j = 0; j < cnt; ++j ) { if ( strcmp( node[j], s1 ) == 0 ) { u = j; } if ( strcmp( node[j], s2 ) == 0 ) { v = j; } } if ( u == -1 ) { u = cnt++; strcpy( node[u], s1 ); } if ( v == -1 ) { v = cnt++; strcpy( node[v], s2 ); } a[u][v] = a[v][u] = true; } nodes = cnt; scanf( "%s %s", s1, s2 ); for ( i = 0; i < nodes; ++i ) { if ( strcmp( node[i], s1 ) == 0 ) { s = i; } if ( strcmp( node[i], s2 ) == 0 ) { t = i; } } bfs( s, t ); if ( visited[t] == false ) { printf( "No route\n" ); } else { print_path( s, t ); } } return 0; }
void search_buf(const char *buf, const int buf_len, const char *dir_full_path) { int binary = -1; /* 1 = yes, 0 = no, -1 = don't know */ int buf_offset = 0; if (opts.search_stream) { binary = 0; } else if (!opts.search_binary_files) { binary = is_binary((void*) buf, buf_len); if (binary) { log_debug("File %s is binary. Skipping...", dir_full_path); return; } } int matches_len = 0; match *matches; size_t matches_size; size_t matches_spare; if (opts.invert_match) { /* If we are going to invert the set of matches at the end, we will need * one extra match struct, even if there are no matches at all. So make * sure we have a nonempty array; and make sure we always have spare * capacity for one extra. */ matches_size = 100; matches = ag_malloc(matches_size * sizeof(match)); matches_spare = 1; } else { matches_size = 0; matches = NULL; matches_spare = 0; } if (opts.literal) { const char *match_ptr = buf; strncmp_fp ag_strnstr_fp = get_strstr(opts); while (buf_offset < buf_len) { match_ptr = ag_strnstr_fp(match_ptr, opts.query, buf_len - buf_offset, opts.query_len, skip_lookup); if (match_ptr == NULL) { break; } if (opts.word_regexp) { const char *start = match_ptr; const char *end = match_ptr + opts.query_len; /* Check whether both start and end of the match lie on a word * boundary */ if ((start == buf || is_wordchar(*(start - 1)) != opts.literal_starts_wordchar) && (end == buf + buf_len || is_wordchar(*end) != opts.literal_ends_wordchar)) { /* It's a match */ } else { /* It's not a match */ match_ptr += opts.query_len; buf_offset = end - buf; continue; } } if ((size_t)matches_len + matches_spare >= matches_size) { matches_size = matches ? matches_size * 2 : 100; log_debug("Too many matches in %s. Reallocating matches to %zu.", dir_full_path, matches_size); matches = ag_realloc(matches, matches_size * sizeof(match)); } matches[matches_len].start = match_ptr - buf; matches[matches_len].end = matches[matches_len].start + opts.query_len; buf_offset = matches[matches_len].end; log_debug("Match found. File %s, offset %i bytes.", dir_full_path, matches[matches_len].start); matches_len++; match_ptr += opts.query_len; if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } else { int rc; int offset_vector[3]; while (buf_offset < buf_len && (rc = pcre_exec(opts.re, opts.re_extra, buf, buf_len, buf_offset, 0, offset_vector, 3)) >= 0) { log_debug("Regex match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]); buf_offset = offset_vector[1]; /* TODO: copy-pasted from above. FIXME */ if ((size_t)matches_len + matches_spare >= matches_size) { matches_size = matches ? matches_size * 2 : 100; log_debug("Too many matches in %s. Reallocating matches to %zu.", dir_full_path, matches_size); matches = ag_realloc(matches, matches_size * sizeof(match)); } matches[matches_len].start = offset_vector[0]; matches[matches_len].end = offset_vector[1]; matches_len++; if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } if (opts.invert_match) { matches_len = invert_matches(matches, matches_len, buf_len); } if (opts.stats) { pthread_mutex_lock(&stats_mtx); stats.total_bytes += buf_len; stats.total_files++; stats.total_matches += matches_len; pthread_mutex_unlock(&stats_mtx); } if (matches_len > 0) { if (binary == -1 && !opts.print_filename_only) { binary = is_binary((void*) buf, buf_len); } pthread_mutex_lock(&print_mtx); if (opts.print_filename_only) { print_path(dir_full_path, '\n'); } else if (binary) { print_binary_file_matches(dir_full_path); } else { print_file_matches(dir_full_path, buf, buf_len, matches, matches_len); } pthread_mutex_unlock(&print_mtx); } else { log_debug("No match in %s", dir_full_path); } if (matches_size > 0) { free(matches); } }
static void do_node(struct AffNode_s *node, void *ptr) { struct arg *arg = ptr; enum AffNodeType_e type = aff_node_type(node); uint32_t size = aff_node_size(node); enum affNodeTypeMask node_mask; if (node == arg->root) printf("/"); else { print_path(arg->root, node); } switch(type) { case affNodeVoid: node_mask = affNodeVoidMask; break; case affNodeChar: node_mask = affNodeCharMask; break; case affNodeInt: node_mask = affNodeIntMask; break; case affNodeDouble: node_mask = affNodeDoubleMask; break; case affNodeComplex:node_mask = affNodeComplexMask; break; default: fprintf(stderr, "lhpc-aff: Internal error: uknown node type %d\n", type); exit(1); } printf(": %s[%d]", type_name(type), size); if (count_subnodes && !recursive) { struct node_count_arg cnt_arg; cnt_arg.cnt = 0; aff_node_foreach(node, node_count, &cnt_arg); printf(" %lld", (long long int)cnt_arg.cnt); } printf("\n"); if (long_format && (node_mask & aff_ls_mask) ) { switch (type) { case affNodeVoid: break; case affNodeChar: { char *ptr = malloc(sizeof (char) * size); int i; if (ptr == 0) { fprintf(stderr, "lhpc-aff: not enough memory\n"); exit(1); } if (aff_node_get_char(arg->r, node, ptr, size)) { fprintf(stderr, "lhpc-aff: error getting data\n"); free(ptr); exit(1); } printf(" \""); for (i = 0; i < size; i++) { unsigned char p = ptr[i]; if (p < 32 || p >= 127 || p == '\"' || p == '\\') printf("\\x%02x", p); else printf("%c", p); } printf("\"\n"); free(ptr); } break; case affNodeInt: { uint32_t *ptr = malloc(sizeof (uint32_t) * size); int i; if (ptr == 0) { fprintf(stderr, "lhpc-aff: not enough memory\n"); exit(1); } if (aff_node_get_int(arg->r, node, ptr, size)) { fprintf(stderr, "lhpc-aff: error getting data\n"); free(ptr); exit(1); } for (i = 0; i < size; i++) printf(" %5d %11d\n", i, ptr[i]); free(ptr); break; } case affNodeDouble: { double *ptr = malloc(sizeof (double) * size); int i; if (ptr == 0) { fprintf(stderr, "lhpc-aff: not enough memory\n"); exit(1); } if (aff_node_get_double(arg->r, node, ptr, size)) { fprintf(stderr, "lhpc-aff: error getting data\n"); free(ptr); exit(1); } for (i = 0; i < size; i++) printf(" %5d %24.16e\n", i, ptr[i]); free(ptr); break; } case affNodeComplex: { double _Complex *ptr = malloc(sizeof (double _Complex) * size); int i; if (ptr == 0) { fprintf(stderr, "lhpc-aff: not enough memory\n"); exit(1); } if (aff_node_get_complex(arg->r, node, ptr, size)) { fprintf(stderr, "lhpc-aff: error getting data\n"); free(ptr); exit(1); } for (i = 0; i < size; i++) printf(" %5d %24.16e %24.16e\n", i, creal(ptr[i]), cimag(ptr[i])); free(ptr); break; } default: fprintf(stderr, "lhpc-aff: Internal error: uknown node type %d\n", type); exit(1); } } if (!directory_only) aff_node_foreach(node, short_list, arg); if (recursive) aff_node_foreach(node, do_node, arg); }
int main() { char buffer[128], z[128]; int num_nodes; struct node *nodes; int i, edge; char name[128]; struct item **adj, *ptr; struct node *u; int source; struct entry *queue = NULL; printf("Enter nuber of nodes: "); fgets(buffer, 127, stdin); sscanf(buffer, "%d", &num_nodes); nodes = (struct node *) malloc(num_nodes * sizeof(struct node)); /*Initialize the vertices*/ for(i=0; i<num_nodes; ++i) { nodes[i].id = i+1; printf("Enter name of node %d: ", i+1); fgets(buffer, 127, stdin); sscanf(buffer, "%[^\n]s", name); nodes[i].name = (char *) (malloc((strlen(name)+1) * sizeof(char))); strcpy(nodes[i].name, name); nodes[i].d = LONG_MAX; nodes[i].c = 'w'; nodes[i].p = NULL; } adj = (struct item **)malloc(num_nodes * sizeof(struct item *)); /*Initialize adjacency list to NULL*/ for(i=0; i<num_nodes; ++i) adj[i] = NULL; /*Add all the edges to adjacency list. No mechanism implemented to prevent multi-edges or self-loop*/ for(i=0; i<num_nodes; ++i) { printf("Enter all edges from %s: (-1 to end)\n", nodes[i].name); while(1) { fgets(buffer, 127, stdin); sscanf(buffer, "%s", z); if(strcmp(z, "exit") == 0) break; edge = find_vertex(nodes, num_nodes, z); if(edge == -1) { printf("Vertex %s does not exist\n", z); break; } add_edge(&adj[i], edge); } } #ifdef DEBUG /*Display all the edges*/ for(i=0; i<num_nodes; ++i) display_edges(adj[i], nodes, i); #endif /*Choose a source vertex*/ printf("Enter the source vertex: "); fgets(buffer, 127, stdin); sscanf(buffer, "%s", z); source = find_vertex(nodes, num_nodes, z); if(source == -1) { printf("Invalid vertex\n"); exit(-1); } #ifdef DEBUG printf("***DEBUG MODE ON***\n"); #endif /*Perform a BFS*/ nodes[source-1].d = 0; nodes[source-1].p = NULL; nodes[source-1].c = 'g'; push(&queue, &nodes[source-1]); #ifdef DEBUG printf("Pushed %s\n", nodes[source-1].name); display_queue(queue); #endif while(!empty(queue)) { u = pop(&queue); #ifdef DEBUG printf("Popped %s\n", u->name); display_queue(queue); #endif i = u->id; ptr = adj[i-1]; while(ptr != NULL) { if(nodes[ptr->vertex-1].c == 'w') { nodes[ptr->vertex-1].c = 'g'; nodes[ptr->vertex-1].d = u->d + 1; nodes[ptr->vertex-1].p = u; push(&queue, &nodes[ptr->vertex-1]); #ifdef DEBUG printf("Pushed %s\n", nodes[ptr->vertex-1].name); display_queue(queue); #endif } ptr = ptr->next; } u->c = 'b'; } #ifdef DEBUG printf("***Printing paths***\n"); #endif for(i=0; i<num_nodes; ++i) { if(i != source-1) { print_path(&nodes[source-1], &nodes[i]); printf("\n"); } } return 1; }
void main() { shortest_path(0, cost, distance, NODES, found); print_path(0,3); print_distance(); }
void print_path(int start, int end) { if( path[end] != start) print_path(start, path[end]); printf("<%d->%d> ", path[end], end); }
void BCubeTopology::print_paths(std::ofstream & p,int src,vector<route_t*>* paths) { for (unsigned int i=0; i<paths->size(); i++) print_path(p, src, paths->at(i)); }
bool test( fastpathfinder::map & m ) { //srand(1); uint32_t width = m.getWidth(); uint32_t height = m.getHeight(); m.clear(); //size_t count = width * height * 0.6f; //for( size_t i = 0; i != count; ++i ) //{ // size_t x = 20 + rand() % (width - 30); // size_t y = 20 + rand() % (height - 30); // m.setCellMask( x, y, 1 ); //} uint32_t x = 1; uint32_t y = 1; uint32_t tx = 24; uint32_t ty = 4; //m.setCellMask( x, y, 0 ); //m.setCellMask( tx, ty, 0 ); m.setCellMask( 10, 0, 9 ); m.setCellMask( 10, 1, 9 ); m.setCellMask( 10, 2, 9 ); m.setCellMask( 10, 3, 9 ); m.setCellMask( 10, 4, 9 ); m.setCellMask( 10, 5, 9 ); m.setCellMask( 10, 6, 9 ); m.setCellMask( 10, 7, 9 ); m.setCellMask( 10, 8, 9 ); //m.setCellMask( tx, ty + 1, 1 ); //m.setCellMask( tx - 1, ty, 1 ); //m.setCellMask( tx, ty - 1, 1 ); print_map( m, 0, 0, width, height ); //bool result = m.findPath( x, y, tx, ty ); fastpathfinder::pathfinder<> pf; pf.initialize( &m ); if( pf.findPathFirst( x, y, tx, ty ) == false ) { return false; } bool found; while( pf.walkerWave( found ) == false ) { //system("CLS"); //print_walk( m, pf, 0, 0, width, height ); //printf("!"); } if( found == false ) { return false; } //system("CLS"); //print_walk( m, pf, 0, 0, width, height ); //if( found == true ) //pf.findFilter(); print_map_cost( m, pf, 0, 0, width, height ); bool s = pf.findPath(); if( s == false ) { return false; } pf.filterPath(); print_path( m, pf, 0, 0, width, height ); return found; }
/*============================*/ int main (int argc, char *argv[]) { int tmp; char *line; loc_min *LM; int *tm; int i; char signal[100]="", what[100]="", stuff[100]=""; /* Parse command line */ program_name = argv[0]; opt.kT = -300; opt.MOVESET = ""; opt.minh = 0.0000001; opt.label = 0; /* normally, use numbers for minima */ GRAPH = NULL; /* Try to parse head to determine graph-type */ decode_switches (argc, argv); if (args_info.inputs_num > 0) { opt.INFILE = fopen(args_info.inputs[0], "r"); if (opt.INFILE==NULL) nrerror("can't open file"); } else { opt.INFILE = stdin; } line = get_line(opt.INFILE); if (line == NULL) { fprintf(stderr,"Error in input file\n"); exit(123); } opt.seq = (char *) space(strlen(line) + 1); sscanf(line,"%s %d %99s %99s %99s", opt.seq, &tmp, signal, what, stuff); if(strcmp(stuff, "\0")!=0 && strncmp(what, "Q", 1)==0) { /* lattice proteins*/ memset(opt.seq, 0, strlen(line)+1); strcpy(opt.seq, stuff); } if ((!opt.poset)&&(strcmp(signal,"::")!=0)) { int r, dim; /* in this case we have a poset file !!!! */ r=sscanf(signal,"P:%d",&dim); if(r<1) { fprintf(stderr, "Warning: obscure headline in input file\n"); dim = 0; } if(dim>0) opt.poset = dim; } if (opt.poset) { /* in this case we have a poset file !!!! */ fprintf(stderr, "!!! Input data are a poset with %d objective functions\n", opt.poset); /* we have a SECIS design file */ if ( ((GRAPH != NULL) && (strstr(GRAPH, "SECIS") != NULL)) ||(strncmp(what, "SECIS", 5) == 0) ) { #if HAVE_SECIS_EXTENSION int len, max_m, min_as; char *sec_structure, *protein_sequence; if (sscanf(what,"SECIS,%d,%d", &max_m, &min_as) < 2) { fprintf(stderr, "Error in input format for SECIS design !" "expected format: SECIS,INT,INT\n" "got: `%s'", what); exit(EXIT_FAILURE); } free(line); line = get_line(opt.INFILE); len = strlen(line); sec_structure = (char*)calloc(len+1, sizeof(char)); protein_sequence = (char*)calloc(len+1, sizeof(char)); sscanf(line,"%s %s", sec_structure, protein_sequence); if (opt.want_verbose) fprintf(stderr, "\nGraph is SECIS design with the following parameters:\n" "Structure: %s\n" "Constraints: %s\n" "Protein sequence: %s\n" "Max. number of mutations : %d\n" "Min. alignment score (aa): %d\n\n", sec_structure, opt.seq, protein_sequence, max_m, min_as); initialize_SECIS(opt.seq, sec_structure, protein_sequence, max_m, min_as); free(sec_structure); free(protein_sequence); #else fprintf(stderr, "You need to reconfigure barriers with the --with-secis" " option\nto use barriers SECIS design extension\n"); exit(EXIT_FAILURE); #endif } } free(line); if (GRAPH==NULL) if(strlen(what)) GRAPH = what; if (GRAPH==NULL) GRAPH="RNA"; opt.GRAPH=GRAPH; LM = barriers(opt); if (opt.INFILE != stdin) fclose(opt.INFILE); tm = make_truemin(LM); if(opt.poset) mark_global(LM); print_results(LM,tm,opt.seq); fflush(stdout); if (!opt.want_quiet) ps_tree(LM,tm,0); if (opt.rates || opt.microrates) { compute_rates(tm,opt.seq); if (!opt.want_quiet) ps_tree(LM,tm,1); print_rates(tm[0], "rates.out"); } if (opt.poset) mark_global(LM); for (i = 0; i < args_info.path_given; ++i) { int L1, L2; sscanf(args_info.path_arg[i], "%d=%d", &L1, &L2); if ((L1>0) && (L2>0)) { FILE *PATH = NULL; char tmp[30]; path_entry *path; path = backtrack_path(L1, L2, LM, tm); (void) sprintf(tmp, "path.%03d.%03d.txt", L1, L2); PATH = fopen (tmp, "w"); if (PATH == NULL) nrerror("couldn't open path file"); print_path(PATH, path, tm); /* fprintf(stderr, "%llu %llu\n", 0, MAXIMUM); */ fclose (PATH); fprintf (stderr, "wrote file %s\n", tmp); free (path); } } /* memory cleanup */ free(opt.seq); free(LM); free(tm); #if WITH_DMALLOC kill_hash(); /* freeing the hash takes unacceptably long */ #endif cmdline_parser_free(&args_info); exit(0); }
void search_buf(const char *buf, const size_t buf_len, const char *dir_full_path) { int binary = -1; /* 1 = yes, 0 = no, -1 = don't know */ size_t buf_offset = 0; if (opts.search_stream) { binary = 0; } else if (!opts.search_binary_files) { binary = is_binary((const void *)buf, buf_len); if (binary) { log_debug("File %s is binary. Skipping...", dir_full_path); return; } } int matches_len = 0; match *matches; size_t matches_size; size_t matches_spare; if (opts.invert_match) { /* If we are going to invert the set of matches at the end, we will need * one extra match struct, even if there are no matches at all. So make * sure we have a nonempty array; and make sure we always have spare * capacity for one extra. */ matches_size = 100; matches = ag_malloc(matches_size * sizeof(match)); matches_spare = 1; } else { matches_size = 0; matches = NULL; matches_spare = 0; } if (opts.query_len == 1 && opts.query[0] == '.') { matches_size = 1; matches = ag_malloc(matches_size * sizeof(match)); matches[0].start = 0; matches[0].end = buf_len; matches_len = 1; } else if (opts.literal) { const char *match_ptr = buf; strncmp_fp ag_strnstr_fp = get_strstr(opts.casing); while (buf_offset < buf_len) { match_ptr = ag_strnstr_fp(match_ptr, opts.query, buf_len - buf_offset, opts.query_len, skip_lookup); if (match_ptr == NULL) { break; } if (opts.word_regexp) { const char *start = match_ptr; const char *end = match_ptr + opts.query_len; /* Check whether both start and end of the match lie on a word * boundary */ if ((start == buf || is_wordchar(*(start - 1)) != opts.literal_starts_wordchar) && (end == buf + buf_len || is_wordchar(*end) != opts.literal_ends_wordchar)) { /* It's a match */ } else { /* It's not a match */ match_ptr += opts.query_len; buf_offset = end - buf; continue; } } if ((size_t)matches_len + matches_spare >= matches_size) { /* TODO: benchmark initial size of matches. 100 may be too small/big */ matches_size = matches ? matches_size * 2 : 100; log_debug("Too many matches in %s. Reallocating matches to %zu.", dir_full_path, matches_size); matches = ag_realloc(matches, matches_size * sizeof(match)); } matches[matches_len].start = match_ptr - buf; matches[matches_len].end = matches[matches_len].start + opts.query_len; buf_offset = matches[matches_len].end; log_debug("Match found. File %s, offset %lu bytes.", dir_full_path, matches[matches_len].start); matches_len++; match_ptr += opts.query_len; if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } else { int offset_vector[3]; while (buf_offset < buf_len && (pcre_exec(opts.re, opts.re_extra, buf, buf_len, buf_offset, 0, offset_vector, 3)) >= 0) { log_debug("Regex match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]); buf_offset = offset_vector[1]; /* TODO: copy-pasted from above. FIXME */ if ((size_t)matches_len + matches_spare >= matches_size) { matches_size = matches ? matches_size * 2 : 100; log_debug("Too many matches in %s. Reallocating matches to %zu.", dir_full_path, matches_size); matches = ag_realloc(matches, matches_size * sizeof(match)); } matches[matches_len].start = offset_vector[0]; matches[matches_len].end = offset_vector[1]; matches_len++; if (matches_len >= opts.max_matches_per_file) { log_err("Too many matches in %s. Skipping the rest of this file.", dir_full_path); break; } } } if (opts.invert_match) { matches_len = invert_matches(buf, buf_len, matches, matches_len); } if (opts.stats) { pthread_mutex_lock(&stats_mtx); stats.total_bytes += buf_len; stats.total_files++; stats.total_matches += matches_len; pthread_mutex_unlock(&stats_mtx); } if (matches_len > 0) { if (binary == -1 && !opts.print_filename_only) { binary = is_binary((const void *)buf, buf_len); } pthread_mutex_lock(&print_mtx); if (opts.print_filename_only) { /* If the --files-without-matches or -L option in passed we should * not print a matching line. This option currently sets * opts.print_filename_only and opts.invert_match. Unfortunately * setting the latter has the side effect of making matches.len = 1 * on a file-without-matches which is not desired behaviour. See * GitHub issue 206 for the consequences if this behaviour is not * checked. */ if (!opts.invert_match || matches_len < 2) { print_path(dir_full_path, '\n'); } } else if (binary) { print_binary_file_matches(dir_full_path); } else { print_file_matches(dir_full_path, buf, buf_len, matches, matches_len); } pthread_mutex_unlock(&print_mtx); } else { log_debug("No match in %s", dir_full_path); } if (matches_size > 0) { free(matches); } }
/* TODO: Append matches to some data structure instead of just printing them out. * Then ag can have sweet summaries of matches/files scanned/time/etc. */ void search_dir(ignores *ig, const char *base_path, const char *path, const int depth) { struct dirent **dir_list = NULL; struct dirent *dir = NULL; scandir_baton_t scandir_baton; int results = 0; char *dir_full_path = NULL; const char *ignore_file = NULL; int i; /* find agignore/gitignore/hgignore/etc files to load ignore patterns from */ for (i = 0; opts.skip_vcs_ignores ? (i == 0) : (ignore_pattern_files[i] != NULL); i++) { ignore_file = ignore_pattern_files[i]; ag_asprintf(&dir_full_path, "%s/%s", path, ignore_file); if (strcmp(SVN_DIR, ignore_file) == 0) { load_svn_ignore_patterns(ig, dir_full_path); } else { load_ignore_patterns(ig, dir_full_path); } free(dir_full_path); dir_full_path = NULL; } if (opts.path_to_agignore) { load_ignore_patterns(ig, opts.path_to_agignore); } scandir_baton.ig = ig; scandir_baton.base_path = base_path; results = ag_scandir(path, &dir_list, &filename_filter, &scandir_baton); if (results == 0) { log_debug("No results found in directory %s", path); goto search_dir_cleanup; } else if (results == -1) { if (errno == ENOTDIR) { /* Not a directory. Probably a file. */ /* If we're only searching one file, don't print the filename header at the top. */ if (depth == 0 && opts.paths_len == 1) { opts.print_heading = -1; } search_file(path); } else { log_err("Error opening directory %s: %s", path, strerror(errno)); } goto search_dir_cleanup; } int offset_vector[3]; int rc = 0; work_queue_t *queue_item; for (i = 0; i < results; i++) { queue_item = NULL; dir = dir_list[i]; ag_asprintf(&dir_full_path, "%s/%s", path, dir->d_name); /* If a link points to a directory then we need to treat it as a directory. */ if (!opts.follow_symlinks && is_symlink(path, dir)) { log_debug("File %s ignored becaused it's a symlink", dir->d_name); goto cleanup; } if (!is_directory(path, dir)) { if (opts.file_search_regex) { rc = pcre_exec(opts.file_search_regex, NULL, dir_full_path, strlen(dir_full_path), 0, 0, offset_vector, 3); if (rc < 0) { /* no match */ log_debug("Skipping %s due to file_search_regex.", dir_full_path); goto cleanup; } else if (opts.match_files) { log_debug("match_files: file_search_regex matched for %s.", dir_full_path); pthread_mutex_lock(&print_mtx); print_path(dir_full_path, '\n'); pthread_mutex_unlock(&print_mtx); goto cleanup; } } queue_item = ag_malloc(sizeof(work_queue_t)); queue_item->path = dir_full_path; queue_item->next = NULL; pthread_mutex_lock(&work_queue_mtx); if (work_queue_tail == NULL) { work_queue = queue_item; } else { work_queue_tail->next = queue_item; } work_queue_tail = queue_item; pthread_mutex_unlock(&work_queue_mtx); pthread_cond_signal(&files_ready); log_debug("%s added to work queue", dir_full_path); } else if (opts.recurse_dirs) { if (depth < opts.max_search_depth) { log_debug("Searching dir %s", dir_full_path); ignores *child_ig = init_ignore(ig); search_dir(child_ig, base_path, dir_full_path, depth + 1); cleanup_ignore(child_ig); } else { log_err("Skipping %s. Use the --depth option to search deeper.", dir_full_path); } } cleanup: ; free(dir); dir = NULL; if (queue_item == NULL) { free(dir_full_path); dir_full_path = NULL; } } search_dir_cleanup: ; free(dir_list); dir_list = NULL; }
static void handle_tree_input(struct regedit *regedit, int c) { struct tree_node *node; switch (c) { case KEY_DOWN: menu_driver(regedit->keys->menu, REQ_DOWN_ITEM); load_values(regedit); break; case KEY_UP: menu_driver(regedit->keys->menu, REQ_UP_ITEM); load_values(regedit); break; case '\n': case KEY_ENTER: case KEY_RIGHT: node = item_userptr(current_item(regedit->keys->menu)); if (node && tree_node_has_children(node)) { tree_node_load_children(node); print_path(regedit, node->child_head); tree_view_update(regedit->keys, node->child_head); value_list_load(regedit->vl, node->child_head->key); } break; case KEY_LEFT: node = item_userptr(current_item(regedit->keys->menu)); if (node && node->parent) { print_path(regedit, node->parent); node = tree_node_first(node->parent); tree_view_update(regedit->keys, node); value_list_load(regedit->vl, node->key); } break; case 'n': case 'N': node = item_userptr(current_item(regedit->keys->menu)); add_reg_key(regedit, node, false); break; case 's': case 'S': node = item_userptr(current_item(regedit->keys->menu)); add_reg_key(regedit, node, true); break; case 'd': case 'D': { int sel; node = item_userptr(current_item(regedit->keys->menu)); if (!node->parent) { break; } sel = dialog_notice(regedit, DIA_CONFIRM, "Delete Key", "Really delete key \"%s\"?", node->name); if (sel == DIALOG_OK) { WERROR rv; struct tree_node *pop; struct tree_node *parent = node->parent; rv = reg_key_del(node, parent->key, node->name); if (W_ERROR_IS_OK(rv)) { tree_view_clear(regedit->keys); pop = tree_node_pop(&node); tree_node_free_recursive(pop); node = parent->child_head; if (node == NULL) { node = tree_node_first(parent); print_path(regedit, node); } tree_view_update(regedit->keys, node); value_list_load(regedit->vl, node->key); } else { dialog_notice(regedit, DIA_ALERT, "Delete Key", "Failed to delete key."); } } break; } } tree_view_show(regedit->keys); value_list_show(regedit->vl); }
int main(int argc, char const *argv[]) { Point coordinates[C]; double distances[C][C]; std::stack<Path> unexplored_paths; // std::priority_queue<Path,std::vector<Path>,ComparePaths> unexplored_paths; Path best_path; best_path.cost = std::numeric_limits<float>::infinity(); Path origin, current_path, new_path; origin.cost = 0.0; origin.order.push_back(0); for(int i = 0; i < C; i++) origin.visited[i] = false; clock_t begin, end; double time_spent; begin = clock(); initialize_coordinates(coordinates); calculate_distances(coordinates,distances); unexplored_paths.push(origin); while(!unexplored_paths.empty()) { current_path = unexplored_paths.top(); unexplored_paths.pop(); if(current_path.cost < best_path.cost) { for(int node = 1 ; node < C ; node++) { if(!current_path.visited[node]) { new_path = current_path; new_path.cost += distances[new_path.order.back()][node]; if(new_path.cost < best_path.cost) { new_path.visited[node] = true; new_path.order.push_back(node); if(new_path.order.size() < C) { unexplored_paths.push(new_path); } else { best_path = new_path; } } } } } } // printf("\n SUM : %lf\n", sumA ); // printf("\n MIN : %lf\n", minA ); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; print_matrix(distances); print_path(best_path); double check_cost = 0.0; for(int i = 0 ; i < C-1 ; i++) { check_cost += distances[best_path.order[i]][best_path.order[i+1]]; } printf("%lf\n",check_cost ); printf("\nTIME : %lf seconds\n", time_spent); return 0; }
int main(void) { walk_path(); print_path(); return 0; }