void ft_exit(char **av, t_env *env) { int i; if (!av[1]) { ft_putchar('\n'); free_double_array(env->environ); ft_memdel((void**)&env); exit(0); } else if (av[1] && !ft_isalpha(av[1][0]) && !av[2]) { i = ft_atoi(av[1]); free_double_array(av); ft_putchar('\n'); free_double_array(env->environ); ft_memdel((void**)&env); exit(i); } else if (av[2] || ft_isalpha(av[1][0])) { ft_putstr_fd(RED, 2); ft_putchar('\n'); ft_putstr_fd("exit: Expression Syntax.\n", 2); ft_putstr_fd(RESET, 2); } }
void loop_remove_env(char **av, t_env *env) { int i; int j; char **env_var; i = 0; j = 0; while (av[i]) { while (env->environ[j]) { env_var = ft_strsplit(env->environ[j], '='); if (!ft_strcmp(env_var[0], av[i])) { remove_env(env->environ, j); j--; } j++; free_double_array(env_var); } j = 0; i++; } }
void free_struct(t_norme *s) { ft_memdel((void**)&s->old); ft_memdel((void**)&s->pwd); ft_memdel((void**)&s->str); if (s->copy) free_double_array(s->copy); ft_memdel((void**)&s); }
char *append_path(char *environ, char *av) { char **path_env; char *ret; if (!environ) return (av); ret = NULL; path_env = ft_strsplit(environ, ':'); path_env = append_slash(path_env, &environ); ret = check_path(path_env, av); if (ret) av = ft_strdup(ret); free_double_array(path_env); ft_memdel((void**)&ret); return (av); }
int check_bultins(char **av, t_env *env) { if (!ft_strcmp(av[0], "exit")) ft_exit(av, env); else if (!ft_strcmp(av[0], "cd")) ft_cd(av, env); else if (!ft_strcmp(av[0], "setenv")) ft_setenv(av, env); else if (!ft_strcmp(av[0], "unsetenv")) ft_unsetenv(av, env); else if (!ft_strcmp(av[0], "env")) { ft_env(av, env->environ); resumed_terminal(); } else if (!ft_strcmp(av[0], "echo")) ft_echo(av, env->environ); else return (0); free_double_array(av); return (1); }
int main(int argc, char* argv[]) { char q[512]; MYSQL_ROW row; MYSQL_RES* res; int err = init_network(); if (err) { return err; } for (int year = 1990; year <= 2011; year++) { std::cout << "Querying " << year << "\n"; snprintf(q, 512, "select count(value) from entries where year=%d and inserted>(select updated from visualizations where name='GAP2' and year=%d)", year, year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); if ((row = mysql_fetch_row(res)) == 0 || atoi(row[0]) == 0) { mysql_free_result(res); continue; } mysql_free_result(res); err = read_network(year); if (err) { return err; } std::cout << "Calculating " << year << "\n"; Basetype* in_flow = create_array(0); Basetype* out_flow = create_array(0); int regions_size = regions.size(); Basetype* total_output = new Basetype[regions_size]; for (int r = 0; r < regions_size; r++) { total_output[r] = 0; } for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow[v] += flows[w][v]; out_flow[v] += flows[v][w]; } total_output[get_region(v)] += out_flow[v]; } int sectors_size = sectors.size(); Basetype** in_flow_by_sector = new Basetype*[sectors_size]; for (int i = 0; i < sectors_size; i++) { in_flow_by_sector[i] = create_array(0); } for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow_by_sector[get_sector(v)][w] += flows[v][w]; } } snprintf(q, 512, "delete from visualization_data where visualization in (select id from visualizations where (name='GAP1' or name='GAP2') and year=%d)", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "select id from visualizations where name='GAP1' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id1 = atoi(row[0]); mysql_free_result(res); snprintf(q, 512, "select id from visualizations where name='GAP2' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id2 = atoi(row[0]); mysql_free_result(res); for (int r = 0; r < regions_size; r++) { Basetype* damage1 = create_array(0); Basetype* damage2 = create_array(0); int js; #pragma omp parallel default(shared) private(js) { #pragma omp for schedule(guided) nowait for (js = 0; js < network_size; js++) { if (get_region(js) == r) { damage1[js] = 1; } else { for (int i = 0; i < sectors_size; i++) { Basetype damage = flows[get_index(i, r)][js] / in_flow_by_sector[i][js]; if (damage1[js] < damage) { damage1[js] = damage; } } } } } #pragma omp parallel default(shared) private(js) { #pragma omp for schedule(guided) nowait for (js = 0; js < network_size; js++) { if (get_region(js) == r) { damage2[js] = 1; } else { for (int i = 0; i < sectors_size; i++) { Basetype damage = 0; for (int r = 0; r < regions_size; r++) { int ir = get_index(i, r); damage += damage1[ir] * flows[ir][js] / in_flow_by_sector[i][js]; } if (damage2[js] < damage) { damage2[js] = damage; } } } } } Basetype* region_damage1 = new Basetype[regions_size]; Basetype* region_damage2 = new Basetype[regions_size]; for (int r = 0; r < regions_size; r++) { region_damage1[r] = 0; region_damage2[r] = 0; } for (int js = 0; js < network_size; js++) { int s = get_region(js); if (total_output[s] > 0) { region_damage1[s] += damage1[js] * out_flow[js] / total_output[s]; region_damage2[s] += damage2[js] * out_flow[js] / total_output[s]; } } delete[] damage1; delete[] damage2; std::stringstream query1("insert into visualization_data (visualization, region_from, region_to, value) values ", std::ios_base::app | std::ios_base::out); bool first1 = true; std::stringstream query2("insert into visualization_data (visualization, region_from, region_to, value) values ", std::ios_base::app | std::ios_base::out); bool first2 = true; for (int s = 0; s < regions_size; s++) { region_damage1[s] = round(region_damage1[s] * 1000) / 1000; if (region_damage1[s] > 0) { if (first1) { first1 = false; } else { query1 << ","; } query1 << "(" << id1 << ",'" << regions[r] << "','" << regions[s] << "'," << region_damage1[s] << ")"; } region_damage2[s] = round(region_damage2[s] * 1000) / 1000; if (region_damage2[s] > 0) { if (first2) { first2 = false; } else { query2 << ","; } query2 << "(" << id2 << ",'" << regions[r] << "','" << regions[s] << "'," << region_damage2[s] << ")"; } } if (!first1) { if (mysql_query(mysql, query1.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } if (!first2) { if (mysql_query(mysql, query2.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } delete[] region_damage1; delete[] region_damage2; } delete[] in_flow; delete[] out_flow; free_double_array(flows); for (int i = 0; i < sectors_size; i++) { delete[] in_flow_by_sector[i]; } delete[] in_flow_by_sector; delete[] total_output; snprintf(q, 512, "update visualizations set updated=now() where (name='GAP1' or name='GAP2') and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } } return disconnect(); }
int main(int argc, char* argv[]) { char q[512]; MYSQL_ROW row; MYSQL_RES* res; int err = init_network(); if (err) { return err; } for (int year = 1990; year <= 2011; year++) { std::cout << "Querying " << year << "\n"; snprintf(q, 512, "select count(value) from entries where year=%d and inserted>(select updated from visualizations where name='Flow Centrality' and year=%d)", year, year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); if((row = mysql_fetch_row(res)) == 0 || atoi(row[0]) == 0) { mysql_free_result(res); continue; } mysql_free_result(res); err = read_network(year); if (err) { return err; } std::cout << "Calculating " << year << "\n"; err = disconnect(); if (err) { return err; } Basetype* betweenness = create_array(0); Basetype* in_flow = create_array(0); Basetype* out_flow = create_array(0); for (int v = 0; v < network_size; v++) { for (int w = 0; w < network_size; w++) { in_flow[v] += flows[w][v]; out_flow[v] += flows[v][w]; } } // Algorithm according to "A space-efficient parallel algorithm for computing betweenness centrality in distributed memory", p. 2 int s; #pragma omp parallel default(none) shared(betweenness, flows, network_size, in_flow, std::cerr) { #pragma omp for schedule(guided) nowait for (s = 0; s < network_size; s++) { std::vector<int> S; std::queue<int> PQ; BasetypeInt* sigma; Basetype* delta; Basetype* dist; BasetypeInt** P; BasetypeInt* P_size; Basetype* pipe; sigma = create_array_int(0); sigma[s] = 1; delta = create_array(0); P = create_double_int_array(0); P_size = create_array_int(0); dist = create_array(-1); dist[s] = 0; pipe = create_array(0); pipe[s] = -1; PQ.push(s); while (!PQ.empty()) { int v = PQ.front(); PQ.pop(); for (std::vector<int>::iterator it = S.begin(); it != S.end(); it++) { if (*it == v) { S.erase(it); break; } } S.push_back(v); for (int w = 0; w < network_size; w++) { if (w != v && w != s && flows[v][w] > 0) { Basetype c_v_w = flows[v][w]; Basetype new_pipe; if (pipe[v] < 0) { new_pipe = c_v_w; } else { new_pipe = std::min(pipe[v], c_v_w); } if (pipe[w] < new_pipe || (pipe[w] == new_pipe && dist[w] > dist[v] + 1)) { // Better best path via v PQ.push(w); pipe[w] = new_pipe; dist[w] = dist[v] + 1; sigma[w] = 0; P_size[w] = 0; } if (pipe[w] == new_pipe && dist[w] == dist[v] + 1 && !in_array(v,P[w],P_size[w])) { // Some best path via v sigma[w] += sigma[v]; P[w][P_size[w]] = v; P_size[w]++; } } } } while (!S.empty()) { int w = S.back(); S.pop_back(); for (int v_index = 0; v_index < P_size[w]; v_index++) { int v = P[w][v_index]; delta[v] += ((1 + delta[w]) * (Basetype) sigma[v]) / (Basetype) sigma[w]; } if (w != s) { #pragma omp atomic betweenness[w] += delta[w]; } } delete[] delta; delete[] sigma; delete[] dist; delete[] P_size; free_double_int_array(P); delete[] pipe; } } err = connect(); if (err) { return err; } snprintf(q, 512, "delete from visualization_data where visualization in (select id from visualizations where name='Flow Centrality' and year=%d)", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "select id from visualizations where name='Flow Centrality' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } res = mysql_use_result(mysql); row = mysql_fetch_row(res); int id = atoi(row[0]); mysql_free_result(res); std::stringstream query("insert into visualization_data (visualization, sector_from, region_from, value) values ", std::ios_base::app | std::ios_base::out); for (int js = 0; js < network_size; js++) { if (js > 0) { query << ","; } query << "(" << id << ",'" << sectors[get_sector(js)] << "','" << regions[get_region(js)] << "'," << betweenness[js] << ")"; } if (mysql_query(mysql, query.str().c_str())) { std::cerr << mysql_error(mysql) << "\n"; return -2; } snprintf(q, 512, "update visualizations set updated=now() where name='Flow Centrality' and year=%d", year); if (mysql_query(mysql, q)) { std::cerr << mysql_error(mysql) << "\n"; return -2; } delete[] betweenness; free_double_array(flows); } return 0; }