/* Unset vector. */ void vector_unset(struct vector *v, void *val) { vindex_t i; size_t size; for (i = 0; i < vector_max(v); i++) { if (vector_slot(v, i) == val) { size = (size_t)(vector_max(v) - (i + 1)); memmove(v->index + i, v->index + (i + 1), size * sizeof(void *)); v->max--; return; } } }
int cnode_is_leaf(struct cnode *cnode) { if (CHECK_FLAG(cnode->flags, CNODE_FLAG_LEAF) || vector_max(cnode->v) == 0) { return 1; } else { return 0; } }
void GriString::draw(double xcm, double ycm, gr_textStyle s, double angle) const { if (strlen(this->c_str()) == 0) return; gr_show_at(this->c_str(), xcm, ycm, s, angle); // Figure bounding box double width, ascent, descent; gr_stringwidth(this->c_str(), &width, &ascent, &descent); //printf("GriString::draw on `%s' xcm=%.1f ycm=%.1f width=%.1f ascent=%.1f descent=%.1f angle=%.1f\n", this->c_str(), xcm,ycm,width,ascent, descent, angle); double tmpx[4], tmpy[4]; // 0123 from lower-left anticlockwise switch (s) { case TEXT_LJUST: gr_rotate_xy( 0.0, -descent, angle, tmpx + 0, tmpy + 0); gr_rotate_xy( width, -descent, angle, tmpx + 1, tmpy + 1); gr_rotate_xy( width, ascent, angle, tmpx + 2, tmpy + 2); gr_rotate_xy( 0.0, ascent, angle, tmpx + 3, tmpy + 3); break; case TEXT_RJUST: gr_rotate_xy( -width, -descent, angle, tmpx + 0, tmpy + 0); gr_rotate_xy( 0.0, -descent, angle, tmpx + 1, tmpy + 1); gr_rotate_xy( 0.0, ascent, angle, tmpx + 2, tmpy + 2); gr_rotate_xy( -width, ascent, angle, tmpx + 3, tmpy + 3); break; case TEXT_CENTERED: gr_rotate_xy(-width/2, -descent, angle, tmpx + 0, tmpy + 0); gr_rotate_xy( width/2, -descent, angle, tmpx + 1, tmpy + 1); gr_rotate_xy( width/2, ascent, angle, tmpx + 2, tmpy + 2); gr_rotate_xy(-width/2, ascent, angle, tmpx + 3, tmpy + 3); break; } tmpx[0] += xcm, tmpy[0] += ycm; tmpx[1] += xcm, tmpy[1] += ycm; tmpx[2] += xcm, tmpy[2] += ycm; tmpx[3] += xcm, tmpy[3] += ycm; rectangle box(vector_min(tmpx, 4), vector_min(tmpy, 4), vector_max(tmpx, 4), vector_max(tmpy, 4)); bounding_box_update(box); }
void cnode_child_free(struct cnode *cnode) { vindex_t i; struct cnode *child; for (i = 0; i < vector_max(cnode->v); i++) { if ((child = vector_slot(cnode->v, i)) != NULL) { cnode_child_free(child); } } cnode_free(cnode); }
/* Utility function for looking up passive interface settings. */ int ripng_passive_interface_lookup (const char *ifname) { unsigned int i; char *str; for (i = 0; i < vector_max (Vripng_passive_interface); i++) if ((str = vector_slot (Vripng_passive_interface, i)) != NULL) if (strcmp (str, ifname) == 0) return i; return -1; }
/* Lookup function. */ int ripng_enable_if_lookup (const char *ifname) { unsigned int i; char *str; for (i = 0; i < vector_max (ripng_enable_if); i++) if ((str = vector_slot (ripng_enable_if, i)) != NULL) if (strcmp (str, ifname) == 0) return i; return -1; }
void cnode_sort(struct cnode *cnode) { struct vector *v; vindex_t i; v = cnode->v; qsort(v->index, v->max, sizeof(void *), cnode_cmp_func); for (i = 0; i < vector_max(v); i++) { struct cnode *cn = (struct cnode *)vector_slot(v, i); cnode_sort(cn); } }
/* Free all configured RIP passive-interface settings. */ void ripng_passive_interface_clean (void) { unsigned int i; char *str; for (i = 0; i < vector_max (Vripng_passive_interface); i++) if ((str = vector_slot (Vripng_passive_interface, i)) != NULL) { free (str); vector_slot (Vripng_passive_interface, i) = NULL; } ripng_passive_interface_apply_all (); }
/* Write RIPng enable network and interface to the vty. */ int ripng_network_write (struct vty *vty, int config_mode) { unsigned int i; const char *ifname; struct route_node *node; char buf[BUFSIZ]; /* Write enable network. */ for (node = route_top (ripng_enable_network); node; node = route_next (node)) if (node->info) { struct prefix *p = &node->p; vty_out (vty, "%s%s/%d%s", config_mode ? " network " : " ", inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen, VTY_NEWLINE); } /* Write enable interface. */ for (i = 0; i < vector_max (ripng_enable_if); i++) if ((ifname = vector_slot (ripng_enable_if, i)) != NULL) vty_out (vty, "%s%s%s", config_mode ? " network " : " ", ifname, VTY_NEWLINE); /* Write passive interface. */ if (config_mode) for (i = 0; i < vector_max (Vripng_passive_interface); i++) if ((ifname = vector_slot (Vripng_passive_interface, i)) != NULL) vty_out (vty, " passive-interface %s%s", ifname, VTY_NEWLINE); return 0; }
int toScalarR(int code, KDVEC(x), DVEC(r)) { REQUIRES(rn==1,BAD_SIZE); DEBUGMSG("toScalarR"); double res; integer one = 1; integer n = xn; switch(code) { case 0: { res = dnrm2_(&n,xp,&one); break; } case 1: { res = dasum_(&n,xp,&one); break; } case 2: { res = vector_max_index(V(x)); break; } case 3: { res = vector_max(V(x)); break; } case 4: { res = vector_min_index(V(x)); break; } case 5: { res = vector_min(V(x)); break; } default: ERROR(BAD_CODE); } rp[0] = res; OK }
struct cnode * cnode_lookup(struct cnode *cnode, const char *name) { uint32_t i; struct cnode *cn; if (cnode == NULL) { return NULL; } for (i = 0; i < vector_max(cnode->v); i++) { if ((cn = vector_slot(cnode->v, i)) != NULL) { if (strcmp(cn->name, name) == 0) { return cn; } } } return NULL; }
/* Clear all network and neighbor configuration */ void ripng_clean_network () { unsigned int i; char *str; struct route_node *rn; /* ripng_enable_network */ for (rn = route_top (ripng_enable_network); rn; rn = route_next (rn)) if (rn->info) { rn->info = NULL; route_unlock_node(rn); } /* ripng_enable_if */ for (i = 0; i < vector_max (ripng_enable_if); i++) if ((str = vector_slot (ripng_enable_if, i)) != NULL) { free (str); vector_slot (ripng_enable_if, i) = NULL; } }
int32_t FALM(struct Struct_FALM *s) { _SDEBUG("I'm in FALM()\n"); // Make z0 feasible if(!s->info->ub_is_inf) vector_min(s->prob->ub,s->prob->z0,s->prob->z0,N); if(!s->info->lb_is_inf) vector_max(s->prob->lb,s->prob->z0,s->prob->z0,N); // Initialize the "inner" problem struct Struct_FGM p_in; init_inner_problem(s, &p_in); if (solve_FALM(s,&p_in) == -1){ ERROR("Error in solving solveFALM()\n"); clean_up_inner_problem(&p_in); return -1; }; clean_up_inner_problem(&p_in); _SDEBUG("End of FALM()\n"); return 0; }
void cparam_free(struct cparam *cparam) { if (cparam->args) { vindex_t i; for (i = 0; i < vector_max(cparam->args); i++) { free(vector_slot(cparam->args, i)); } vector_free(cparam->args); } if (cparam->matched) { vector_free(cparam->matched); } if (cparam->candidate) { vector_free(cparam->candidate); } if (cparam->argv) { vector_free(cparam->argv); } free(cparam); }
struct cnode * cnode_match(struct cnode *cnode, char *name) { uint32_t i; struct cnode *child; enum match_type type; if (cnode == NULL) { return NULL; } for (i = 0; i < vector_max(cnode->v); i++) { if ((child = vector_slot(cnode->v, i)) != NULL) { switch (child->type) { case CNODE_TYPE_KEYWORD: if (strcmp(child->name, name) == 0) { return child; } break; case CNODE_TYPE_WORD: return child; break; case CNODE_TYPE_IPV4: ipv4_match(name, &type); if (type == IPV4_MATCH) { return child; } break; case CNODE_TYPE_IPV4_PREFIX: ipv4_prefix_match(name, &type); if (type == IPV4_PREFIX_MATCH) { return child; } break; case CNODE_TYPE_IPV6: ipv6_match(name, &type); if (type == IPV6_MATCH) { return child; } break; case CNODE_TYPE_IPV6_PREFIX: ipv6_prefix_match(name, &type); if (type == IPV6_PREFIX_MATCH) { return child; } break; case CNODE_TYPE_MAC: mac_address_match(name, &type); if (type == MAC_ADDRESS_MATCH) { return child; } break; case CNODE_TYPE_RANGE: range_match(name, child, &type); if (type == RANGE_MATCH) { return child; } break; default: break; } } } return NULL; }
int otsu(double* data_out, double** thr, double* sep, double* data, int xsize, int ysize, int N) { int _verbose=1; int LEVELS = 256; //double data_out[xsize * ysize]; // Unique int unI_size; double* unI; unique(xsize*ysize, data, &unI_size, &unI); if(_verbose>=1) { std::cout << std::endl << "unI_c = "; double_vector_print(unI_size, unI); } // Histogram int* counts; double* bins; int nbins; if (unI_size < LEVELS) nbins = unI_size; else nbins = LEVELS; if (nbins < LEVELS) { counts = new int[unI_size]; for (int i = 0; i < unI_size; i++) counts[i] = 0; bins = new double[unI_size]; for (int i = 0; i < unI_size; i++) bins[i] = unI[i]; } else if (nbins == LEVELS) { counts = new int[LEVELS]; for (int i = 0; i < LEVELS; i++) counts[i] = 0; bins = new double[LEVELS]; double step = (LEVELS - 1) / LEVELS / 2; bins[0] = step; for (int i = 1; i < LEVELS; i++) bins[i] = bins[i - 1] + step; } free(unI); histogram(counts, xsize*ysize, nbins, data, bins); // Display vector for matlab comparison if(_verbose) { std::cout << std::endl << "cBins = "; double_vector_print(nbins, bins); double_vector_save_to_file("cBins.txt", nbins, bins); std::cout << std::endl << "cHist = "; int_vector_print(nbins, counts); int_vector_save_to_file("cHist.txt", nbins, counts); } double* P=(double*)malloc(nbins*sizeof(*P)); for (int i = 0; i < nbins; i++) P[i] = (double) counts[i] / (xsize * ysize); // cumsum double* w=(double*)malloc(nbins*sizeof(*P)); double* mu=(double*)malloc(nbins*sizeof(*P)); double* Pi=(double*)malloc(nbins*sizeof(*P)); Pi[0] = P[0]; for (int i = 1; i < nbins; i++) Pi[i] = (i + 1) * P[i]; cumsum(w, P, nbins); cumsum(mu, Pi, nbins); if(_verbose) { double_vector_save_to_file("P.txt", nbins, P); double_vector_save_to_file("w.txt", nbins, w); double_vector_save_to_file("mu.txt", nbins, mu); } double* w_aux = NULL; double* mu_aux = NULL; double* aux1; double* aux2; double* sigma2B; double sigma2Bmax; int ind_sigma2Bmax; if (N == 2) { w_aux = new double[nbins - 2]; mu_aux = new double[nbins - 2]; aux1 = new double[nbins - 2]; aux2 = new double[nbins - 2]; sigma2B = new double[nbins - 2]; for (int i = 0; i < nbins - 2; i++) { w_aux[i] = w[i + 1]; mu_aux[i] = mu[i + 1]; aux1[i] = mu[nbins - 1] * w_aux[i] - mu_aux[i]; } if(_verbose) { std::cout << std::endl << "saving aux"<<std::endl; double_vector_save_to_file("aux1.txt", nbins - 2, aux1); //double_vector_save_to_file("aux2.txt", nbins - 2, aux2); } vector_pow(aux2, aux1, 2, nbins - 2); divide_vectors(aux1, aux2, w_aux, nbins - 2); for (int i = 0; i < nbins - 2; i++) aux2[i] = 1 - w_aux[i]; divide_vectors(sigma2B, aux1, aux2, nbins - 2); vector_max(&sigma2Bmax, &ind_sigma2Bmax, sigma2B, nbins - 2); if(_verbose) { double_vector_save_to_file("aux1b.txt", nbins - 2, aux1); double_vector_save_to_file("aux2b.txt", nbins - 2, aux2); double_vector_save_to_file("sigma2B.txt", nbins - 2, sigma2B); std::cout << "Maximo: " << sigma2Bmax << std::endl << "Posicion del maximo: " << ind_sigma2Bmax << std::endl; std::cout << "pixval: " << bins[ind_sigma2Bmax + 1] << std::endl; } for (int i = 0; i < xsize * ysize; i++) if (data[i] <= bins[ind_sigma2Bmax + 1]) data_out[i] = 0.0; else data_out[i] = 1.0; double* th_aux; th_aux = new double[1]; th_aux[0] = bins[ind_sigma2Bmax + 1]; *thr = th_aux; } else if (N == 3) { // w_aux = new double[nbins ]; // mu_aux = new double[nbins ]; aux1 = new double[nbins]; aux2 = new double[nbins]; sigma2B = new double[nbins * nbins]; double* w0= new double[nbins*nbins]; double* w1= new double[nbins*nbins]; double* w2= new double[nbins*nbins]; double* mu0_aux= new double[nbins]; double* mu2_aux= new double[nbins]; double* mu0= new double[nbins*nbins]; double* mu2= new double[nbins*nbins]; vector_flip(aux1, P, nbins); cumsum(aux2, aux1, nbins); vector_flip(aux1, aux2, nbins); for (int i = 0; i < nbins; i++) for (int j = 0; j < nbins; j++) { w0[i * nbins + j] = w[i]; w2[i * nbins + j] = aux1[j]; } double_vector_save_to_file("w0.txt", nbins*nbins, w0); double_vector_save_to_file("w2.txt", nbins*nbins, w2); divide_vectors(mu0_aux, mu, w, nbins); vector_flip(aux1, Pi, nbins); cumsum(aux2, aux1, nbins); double* aux3=new double[nbins]; vector_flip(aux3, P, nbins); cumsum(aux1, aux3, nbins); divide_vectors(aux3, aux2, aux1, nbins); vector_flip(mu2_aux, aux3, nbins); for (int i = 0; i < nbins; i++) for (int j = 0; j < nbins; j++) { mu0[i * nbins + j] = mu0_aux[i]; mu2[i * nbins + j] = mu2_aux[j]; } double_vector_save_to_file("mu0.txt", nbins*nbins, mu0); double_vector_save_to_file("mu2.txt", nbins*nbins, mu2); for (int i = 0; i < nbins * nbins; i++) { w1[i] = 1 - w0[i] - w2[i]; } double* aux4=new double[nbins * nbins]; double* aux5=new double[nbins * nbins]; double* t1=new double[nbins * nbins]; double* t2=new double[nbins * nbins]; double* t3=new double[nbins * nbins]; double* t4=new double[nbins * nbins]; double* t34=new double[nbins * nbins]; double* t34_aux=new double[nbins * nbins]; for (int i = 0; i < nbins * nbins; i++) aux4[i] = mu0[i] - mu[nbins - 1]; vector_pow(aux5, aux4, 2, nbins * nbins); multiplicate_vectors(t1, w0, aux5, nbins * nbins); multiplicate_vectors(t3, w0, aux4, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) aux4[i] = mu2[i] - mu[nbins - 1]; vector_pow(aux5, aux4, 2, nbins * nbins); multiplicate_vectors(t2, w2, aux5, nbins * nbins); multiplicate_vectors(t4, w2, aux4, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) t34[i] = t3[i] + t4[i]; vector_pow(t34_aux, t34, 2, nbins * nbins); divide_vectors(t34, t34_aux, w1, nbins * nbins); for (int i = 0; i < nbins * nbins; i++) { sigma2B[i] = t1[i] + t2[i] + t34[i]; if (w1[i] <= 0) sigma2B[i] = 0; } if(_verbose) { double_vector_save_to_file("sigma2B.txt", nbins*nbins, sigma2B); double_vector_save_to_file("sigma2B.txt", nbins*nbins, sigma2B); } vector_max(&sigma2Bmax, &ind_sigma2Bmax, sigma2B, nbins * nbins); std::cout << "Maximo: " << sigma2Bmax << std::endl << "Posicion del maximo: " << ind_sigma2Bmax << std::endl; int k1, k2; k1 = (int) ind_sigma2Bmax / nbins; k2 = ind_sigma2Bmax % nbins; std::cout << "k1 = " << k1 << ", k2 = " << k2 << std::endl; for (int i = 0; i < xsize * ysize; i++) if (data[i] <= bins[k1]) data_out[i] = 0.0; else if ((data[i] > bins[k1]) && (data[i] <= bins[k2])) data_out[i] = 0.5; else data_out[i] = 1.0; double* thr_aux; thr_aux = new double[2]; thr_aux[0] = bins[k1]; thr_aux[1] = bins[k2]; *thr = thr_aux; delete[] w0; delete[] w1; delete[] w2; delete[] mu0_aux; delete[] mu2_aux; delete[] mu0; delete[] mu2; delete[] aux3; delete[] aux4; delete[] aux5; delete[] t1; delete[] t2; delete[] t3; delete[] t4; delete[] t34; delete[] t34_aux; } double* sep_aux=new double[nbins]; double* sep_aux_2=new double[nbins]; for(int i=0;i<nbins;i++) sep_aux[i]=i+1-mu[nbins-1]; vector_pow(sep_aux_2, sep_aux, 2, nbins); multiplicate_vectors(sep_aux, sep_aux_2, P, nbins); double cumu=0; for (int i=0;i<nbins;i++) cumu+=sep_aux[i]; *sep=sigma2Bmax/cumu; //*Iseg = data_out; delete[] counts; delete[] aux1; delete[] aux2; delete[] bins; if (w_aux) delete[] w_aux; if (mu_aux) delete[] mu_aux; delete[] sigma2B; delete[] sep_aux; delete[] sep_aux_2; free(w); free(mu); free(Pi); return 0; }
/* Parse the line. */ enum cparse_result cparse(char *line, struct cnode *top, struct cparam *param, int exec) { uint32_t i; uint32_t j; char *arg; struct cnode *cnode; enum match_type current; struct vector *args; struct vector *matched; struct vector *candidate; struct vector *argv; /* Arguments, matched and candidate. */ args = param->args; matched = param->matched; candidate = param->candidate; argv = param->argv; /* Lexical analysis. */ clex(line, args); /* Set top candidate. */ vector_reset(candidate); vector_append(candidate, top->v); /* Empty line. */ if (vector_max(args) == 0) { return CPARSE_EMPTY_LINE; } /* Parse user input arguments. */ for (i = 0; i < vector_max(args); i++) { /* Set current word to arg. */ arg = vector_slot(args, i); /* Empty tail space. */ if (strcmp(arg, "") == 0) { if (param->index > 0) { param->index--; } if (args->max > 0) { args->max--; } param->tail = 1; break; } /* Remember index. */ param->index = i; /* Starting from no match. */ current = NONE_MATCH; /* Rest matched vector. */ vector_reset(matched); /* Match with schema. */ for (j = 0; j < vector_max(candidate); j++) { enum match_type match = NONE_MATCH; cnode = vector_slot(candidate, j); if (exec == CPARSE_EXEC_MODE || exec == CPARSE_CONFIG_EXEC_MODE) { cnode_schema_match(cnode, arg, &match); } if (exec == CPARSE_CONFIG_MODE || (exec == CPARSE_CONFIG_EXEC_MODE && match == NONE_MATCH)) { if (strcmp(arg, cnode->name) == 0) { match = KEYWORD_MATCH; } } if (match == NONE_MATCH) { continue; } if (match > current) { vector_reset(matched); current = match; vector_set(matched, cnode); } else if (match == current) { vector_set(matched, cnode); } if (CHECK_FLAG(cnode->flags, CNODE_FLAG_SET_NODE)) { SET32_FLAG(param->flags, CNODE_FLAG_SET_NODE); } if (CHECK_FLAG(cnode->flags, CNODE_FLAG_DELETE_NODE)) { SET32_FLAG(param->flags, CNODE_FLAG_DELETE_NODE); } } /* There is no match. */ if (vector_max(matched) == 0) { break; } /* Update next level schema. */ vector_reset(candidate); for (j = 0; j < vector_max(matched); j++) { cnode = vector_slot(matched, j); vector_append(candidate, cnode->v); } } /* Match result check. */ if (vector_max(matched) == 0) { return CPARSE_NO_MATCH; } else if (vector_max(matched) > 1) { return CPARSE_AMBIGUOUS; } /* Parse success, set matched node. */ param->exec = vector_first(matched); /* Return incomplete if the node is not leaf. */ if (!cnode_is_leaf(param->exec)) { return CPARSE_INCOMPLETE; } /* Here we can build argc/argv. This is only possible at this stage * since during parsing, we can't determine which node is actually * matched. We allow user to abbreviate input so there could be * multiple candidate node during parsing. */ if (exec) { build_argv(param->exec, args, vector_max(args) - 1, argv); } /* Success. */ return CPARSE_SUCCESS; }