distance_label_t *graph_find_path(graph_t *g,char *time, void *from, void *to) { assert(graph_has_node(g, from) && graph_has_node(g, to)); list_t *visited = list_new(); list_t *distanceLabels = list_new(); iter_t *it; for (it = iter(g->nodes); !iter_done(it); iter_next(it)) { distance_label_t *dl = calloc(1, sizeof(distance_label_t)); dl->dist = -1; dl->label = iter_get(it); dl->path = NULL; dl->path_edges = NULL; dl->arrival_time = time; list_add(distanceLabels, dl); } iter_free(it); get_distance_label(distanceLabels, from, g->comp)->path = list_new(); get_distance_label(distanceLabels, from, g->comp)->path_edges = list_new(); dijkstra(g, from, to, visited, distanceLabels); distance_label_t *best = get_distance_label(distanceLabels, to, g->comp); assert(best); free_distancelabels(g->comp,distanceLabels,best); list_free(visited); graph_print_trip(g,time,from,best); return best; }
void *get_min_distance_node(list_t *distanceLabels, comparator_t comp, list_t *visited) { assert(distanceLabels); int minDist = -1; void *minLabel = NULL; iter_t *it; for (it = iter(distanceLabels); !iter_done(it); iter_next(it)) { distance_label_t *itdl = iter_get(it); if (itdl->dist == -1) // inte varit där { continue; } if (list_has(visited, comp, itdl->label)) // finns redan i visited, abort { continue; } if (minDist == -1 || itdl->dist < minDist)// om inte ändrats en enda gång eller nya distansen 'r mindre än förra minsta distans. { minDist = itdl->dist; minLabel = itdl->label; } } iter_free(it); return minLabel; }
VEC *iter_spgmres(SPMAT *A, SPMAT *B, VEC *b, double tol, VEC *x, int k, int limit, int *steps) #endif { ITER *ip; ip = iter_get(0,0); ip->Ax = (Fun_Ax) sp_mv_mlt; ip->A_par = (void *) A; if (B) { ip->Bx = (Fun_Ax) sp_mv_mlt; ip->B_par = (void *) B; } else { ip->Bx = (Fun_Ax) NULL; ip->B_par = NULL; } ip->k = k; ip->limit = limit; ip->info = (Fun_info) NULL; ip->b = b; ip->eps = tol; ip->x = x; iter_gmres(ip); x = ip->x; if (steps) *steps = ip->steps; ip->shared_x = ip->shared_b = TRUE; iter_free(ip); /* release only ITER structure */ return x; }
void graph_foreach_edge(graph_t *g, void_fun3_t f) { iter_t *it; for (it = iter(g->edges); !iter_done(it); iter_next(it)) { edge_t *e = iter_get(it); f(e->from, e->to, e->label); } iter_free(it); }
void dijkstra(graph_t *g, void *current, void *to, list_t *visited, list_t *distanceLabels) { assert(g); assert(current); assert(to); assert(visited); assert(distanceLabels); while (true) { if (!list_has(visited, g->comp, current)) { list_t *unvisited_neighs = unvisited_neighbors(g, current, visited); distance_label_t *here = get_distance_label(distanceLabels, current, g->comp); iter_t *it; for (it = iter(unvisited_neighs); !iter_done(it); iter_next(it)) { void *neigh = iter_get(it); int line = list_quickest_line(g->nodes, current, neigh, here->arrival_time); // printf("med linje:%i i dijkstra\n",line); assert(line); edge_t *edge = graph_get_edge(g,line,current,here->path_edges); assert(edge); list_t *tentativePath = list_clone(here->path); list_t *tentativeEdgePath = list_clone(here->path_edges); list_add(tentativePath, neigh); list_add(tentativeEdgePath, edge); char *bussDepart = list_next_dep_time(g->nodes,current,neigh,line,here->arrival_time); assert(bussDepart); int total_distance = graph_add_penalty(edge, here->arrival_time, bussDepart);// egen rad char *new_arrival_time = add_duration(bussDepart, network_get_dur(edge->label)); //egen rad //free(bussDepart); update_distance(distanceLabels, neigh, g->comp, here->dist + total_distance, tentativePath, tentativeEdgePath, new_arrival_time); //la till new_arrival_tim. ost-bågen borde gå in här!! } iter_free(it); list_free(unvisited_neighs); } list_add(visited, current); if (g->comp(current, to)) { return; } current = get_min_distance_node(distanceLabels, g->comp, visited); assert(current); } }
void graph_print_trip (graph_t *g, char *time,char *from, distance_label_t *dl) { printf("\nTrip start:%s\n",time); char *tmp_t = time; iter_t *it; char*tmp_fr; char*tmp_to; /*iter_t *it2; for (it2 = iter(dl->path_edges); !iter_done(it2); iter_next(it2)) { void *tmp_v = iter_get(it2); edge_t *tmp_e = tmp_v; puts(tmp_e->from); puts(tmp_e->to); } iter_free(it2); */ for (it = iter(dl->path_edges); !iter_done(it); iter_next(it)) { void *tmp_v = iter_get(it); edge_t *tmp_e = tmp_v; int line = network_get_line(tmp_e->label); // printf("\n\n\n%s\n",tmp_e->to); if(!tmp_fr) tmp_fr = from; // printf("linje:%i !%s!",line,tmp_fr); if(g->comp(tmp_fr,tmp_e->from))tmp_to = tmp_e->to; else{tmp_to = tmp_e->from;} //printf(" - !%s! - tid:%s\n",tmp_to,tmp_t); char *buss_dep = list_next_dep_time(g->nodes,tmp_fr,tmp_to,line,tmp_t); char *arr_time = add_duration(buss_dep, network_get_dur(tmp_e->label)); int durr = network_get_dur(tmp_e->label); printf("@ %s: #%i %s --(%i)--> %s\n",buss_dep ,line ,tmp_fr ,durr ,tmp_to ); tmp_fr = tmp_to; tmp_t = arr_time; assert(tmp_fr); assert(tmp_t); } puts("_____________________________________________________________"); printf("this trip took from:%s to:%s and %i minutes to complete.\n",time,dl->arrival_time,dl->dist+1); iter_free(it); }
MAT *iter_sparnoldi(SPMAT *A, VEC *x0, int m, Real *h_rem, MAT *Q, MAT *H) #endif { ITER *ip; ip = iter_get(0,0); ip->Ax = (Fun_Ax) sp_mv_mlt; ip->A_par = (void *) A; ip->x = x0; ip->k = m; iter_arnoldi_iref(ip,h_rem,Q,H); ip->shared_x = ip->shared_b = TRUE; iter_free(ip); /* release only ITER structure */ return H; }
void iter_splanczos(SPMAT *A, int m, VEC *x0, VEC *a, VEC *b, Real *beta2, MAT *Q) #endif { ITER *ip; ip = iter_get(0,0); ip->shared_x = ip->shared_b = TRUE; ip->Ax = (Fun_Ax) sp_mv_mlt; ip->A_par = (void *) A; ip->x = x0; ip->k = m; iter_lanczos(ip,a,b,beta2,Q); iter_free(ip); /* release only ITER structure */ }
bool graph_is_edge_visited(list_t *visited_edges, comparator_t comp, edge_t *edge) { iter_t *it; for (it = iter(visited_edges); !iter_done(it); iter_next(it)) { edge_t *temp_edge = iter_get(it); if (comp(temp_edge, edge)) { iter_free(it); return true; } } iter_free(it); return false; }
void start_find_select(void*) { RandomChoice choice(/*rand_,*/ size()); for(iter_rewind(); !iter_end(); iter_inc(), ++choice) { if(choice.choose()) { iter_get(find_s_, find_p_, find_o_); iter_free(); timer_->set_timer<App, &App::start_find>(1000, this, 0); return; } ++choice; } debug_->debug("!fnd %d %d", (int)choice.elements, (int)choice.current); }
VEC *iter_splanczos2(SPMAT *A, int m, VEC *x0, VEC *evals, VEC *err_est) #endif { ITER *ip; VEC *a; ip = iter_get(0,0); ip->Ax = (Fun_Ax) sp_mv_mlt; ip->A_par = (void *) A; ip->x = x0; ip->k = m; a = iter_lanczos2(ip,evals,err_est); ip->shared_x = ip->shared_b = TRUE; iter_free(ip); /* release only ITER structure */ return a; }
bool graph_has_edge(graph_t *g, void *from, void *to, void **label) { iter_t *it; for (it = iter(g->edges); !iter_done(it); iter_next(it)) { edge_t *e = iter_get(it); if ((g->comp(from, e->from) && g->comp(to, e->to)) || (g->comp(from, e->to) && g->comp(to, e->from))) { *label = e->label; iter_free(it); return true; } } *label = NULL; iter_free(it); return false; }
void testITER_GET() { list_t *l = list_new(); for (int i = 0; i < 3; ++i) { int *x = malloc(sizeof(int)); *x = i; list_add(l, x); } iter_t *it = iter(l); int *first = iter_get(it); CU_ASSERT_EQUAL(*first, 0); iter_free(it); list_foreach(l, free); list_free(l); }
list_t *unvisited_neighbors(graph_t *g, void *current, list_t *visited) { list_t *neighs = graph_find_neighbors(g, current); list_t *unvisited_neighs = list_new(); iter_t *nit; for (nit = iter(neighs); !iter_done(nit); iter_next(nit)) { void *neigh = iter_get(nit); if (!list_has(visited, g->comp, neigh)) { list_add(unvisited_neighs, neigh); } } iter_free(nit); list_free(neighs); return unvisited_neighs; }
list_t *graph_find_neighbors(graph_t *g, void *node) { list_t *ret = list_new(); iter_t *eit; for (eit = iter(g->edges); !iter_done(eit); iter_next(eit)) { edge_t *e = iter_get(eit); if (g->comp(e->from, node)) { list_add(ret, e->to); } else if (g->comp(e->to, node)) { list_add(ret, e->from); } } iter_free(eit); return ret; }
distance_label_t *get_distance_label(list_t *distanceLabels, void *lbl, comparator_t comp) { assert(distanceLabels); assert(lbl); assert(comp); iter_t *dliter; for (dliter = iter(distanceLabels); !iter_done(dliter); iter_next(dliter)) { distance_label_t *dl = iter_get(dliter); if (comp(dl->label, lbl)) { //printf("Did find distance label for %s: path is: %p\n", lbl, dl->path); iter_free(dliter); return dl; } } // printf("did not find distance label for %s\n", lbl); iter_free(dliter); return NULL; }
void *graph_get_edge(graph_t *g,int line, void *node_el, list_t *visited_edges)// Egen Funktion { iter_t *it; for (it = iter(g->edges); !iter_done(it); iter_next(it)) { edge_t *e = iter_get(it); if( ( (g->comp(node_el,e->from) || g->comp(node_el,e->to) ) && (!graph_is_edge_visited(visited_edges, g->comp, e)) && (network_comp_line(e->label,line)) )) { void *temp_edge = e; iter_free(it); assert(temp_edge); return temp_edge; } } iter_free(it); return NULL; }
VEC *iter_spcg(SPMAT *A, SPMAT *LLT, VEC *b, double eps, VEC *x, int limit, int *steps) #endif { ITER *ip; ip = iter_get(0,0); ip->Ax = (Fun_Ax) sp_mv_mlt; ip->A_par = (void *)A; ip->Bx = (Fun_Ax) spCHsolve; ip->B_par = (void *)LLT; ip->info = (Fun_info) NULL; ip->b = b; ip->eps = eps; ip->limit = limit; ip->x = x; iter_cg(ip); x = ip->x; if (steps) *steps = ip->steps; ip->shared_x = ip->shared_b = TRUE; iter_free(ip); /* release only ITER structure */ return x; }
int main() { list* list1 = list_create(); list* list2 = list_create(); list* list3 = list_create(); int data[] = {1, 2, 3, 4, 5}; char* strings[] = {"a", "b", "c", "d", "e", "f"}; int data_size = 5; for(int i = 0; i < data_size; i++) { list_append(list1, &data[i]); } int string_size = 6; for(int i = 0; i < string_size; i++) { list_append(list2, strings[i]); } iterator* iter1 = iter_create(list1); printf("Data List Print Test....\n"); printf("Data List: "); while(!iter_at_end(iter1)) { node* n = iter_get(iter1); int* val = n->data; printf("%d ", *val); iter_advance(iter1); } printf("\n"); printf("Removing 2nd Element From Data\n"); iter1 = iter_create(list1); iter_advance(iter1); iter_remove(iter1); printf("Data List: "); while(!iter_at_end(iter1)) { node* n = iter_get(iter1); int* val = n->data; printf("%d ", *val); iter_advance(iter1); } printf("\n"); printf("\n"); printf("String List Print Test.......\n"); iterator* iter2 = iter_create(list2); printf("Strings List: "); while(!iter_at_end(iter2)) { node* n = iter_get(iter2); char* val = n->data; printf("%s ", val); iter_advance(iter2); } printf("\n"); list_remove_last(list2); iter2 = iter_create(list2); printf("Removing Last Element from String List\n"); printf("Strings List: "); while(!iter_at_end(iter2)) { node* n = iter_get(iter2); char* val = n->data; printf("%s ", val); iter_advance(iter2); } printf("\n"); printf("\n"); printf("Done Printing\n"); return 0; }