PRIVATE double adaptive_walk(char *start, const char *target) { #ifdef DUMMY printf("%s\n%s %c\n", start, target, backtrack_type ); return 0.; #endif int i,j,p,tt,w1,w2, n_pos, len, flag; long walk_len; char *string, *string2, *cstring, *structure, *struct2; int *mut_pos_list, mut_sym_list[MAXALPHA+1], mut_pair_list[2*MAXALPHA+1]; int *w1_list, *w2_list, mut_position, symbol, bp; int *target_table, *test_table; char cont; double cost, current_cost, ccost2; double (*cost_function)(const char *, char *, const char *); len = strlen(start); if (strlen(target)!=len) { fprintf(stderr, "%s\n%s\n", start, target); nrerror("adaptive_walk: start and target have unequal length"); } string = (char *) space(sizeof(char)*(len+1)); cstring = (char *) space(sizeof(char)*(len+1)); string2 = (char *) space(sizeof(char)*(len+1)); structure = (char *) space(sizeof(char)*(len+1)); struct2 = (char *) space(sizeof(char)*(len+1)); mut_pos_list = (int *) space(sizeof(int)*len); w1_list = (int *) space(sizeof(int)*len); w2_list = (int *) space(sizeof(int)*len); target_table = (int *) space(sizeof(int)*len); test_table = (int *) space(sizeof(int)*len); make_ptable(target, target_table); for (i=0; i<base; i++) mut_sym_list[i] = i; for (i=0; i<npairs; i++) mut_pair_list[i] = i; for (i=0; i<len; i++) string[i] = (islower(start[i]))?toupper(start[i]):start[i]; walk_len = 0; if (fold_type==0) cost_function = mfe_cost; else cost_function = pf_cost; cost = cost_function(string, structure, target); if (fold_type==0) ccost2=cost2; else { ccost2 = -1.; cost2=0; } strcpy(cstring, string); current_cost = cost; if (cost>0) do { cont=0; if (fold_type==0) { /* min free energy fold */ make_ptable(structure, test_table); for (j=w1=w2=flag=0; j<len; j++) if ((tt=target_table[j])!=test_table[j]) { if ((tt<j)&&(isupper(start[j]))) w1_list[w1++] = j; /* incorrectly paired */ if ((flag==0)&&(j>0)) if ((target_table[j-1]<j-1)&&isupper(start[j-1])) w2_list[w2++] = j-1; /* adjacent to incorrect position */ if (w2>1) if (w2_list[w2-2]==w2_list[w2-1]) w2--; flag = 1; } else { if (flag==1) if ((tt<j)&&isupper(start[j])) w2_list[w2++] = j; /* adjacent to incorrect position */ flag = 0; } shuffle(w1_list, w1); shuffle(w2_list, w2); for (j=n_pos=0; j<w1; j++) mut_pos_list[n_pos++] = w1_list[j]; for (j=0; j<w2; j++) mut_pos_list[n_pos++] = w2_list[j]; } else { /* partition_function */ for (j=n_pos=0; j<len; j++) if (isupper(start[j])) if (target_table[j]<=j) mut_pos_list[n_pos++] = j; shuffle(mut_pos_list, n_pos); } string2[0]='\0'; for (mut_position=0; mut_position<n_pos; mut_position++){ strcpy(string, cstring); shuffle(mut_sym_list, base); shuffle(mut_pair_list, npairs); i = mut_pos_list[mut_position]; if (target_table[i]<0) /* unpaired base */ for (symbol=0;symbol<base;symbol++) { if(cstring[i]== symbolset[mut_sym_list[symbol]]) continue; string[i] = symbolset[mut_sym_list[symbol]]; cost = cost_function(string, structure, target); if ( cost + DBL_EPSILON < current_cost ) break; if (( cost == current_cost)&&(cost2<ccost2)){ strcpy(string2, string); strcpy(struct2, structure); ccost2 = cost2; } } else /* paired base */ for (bp=0; bp<npairs; bp++) { j = target_table[i]; p = mut_pair_list[bp]*2; if ((cstring[i] == pairset[p]) && (cstring[j] == pairset[p+1])) continue; string[i] = pairset[p]; string[j] = pairset[p+1]; cost = cost_function(string, structure, target); if ( cost < current_cost ) break; if (( cost == current_cost)&&(cost2<ccost2)){ strcpy(string2, string); strcpy(struct2, structure); ccost2 = cost2; } } if ( cost < current_cost ) { strcpy(cstring, string); current_cost = cost; ccost2 = cost2; walk_len++; if (cost>0) cont=1; break; } } if ((current_cost>0)&&(cont==0)&&(string2[0])) { /* no mutation that decreased cost was found, but the the sequence in string2 decreases cost2 while keeping cost constant */ strcpy(cstring, string2); strcpy(structure, struct2); nc2++; cont=1; } } while (cont); for (i=0; i<len; i++) if (isupper(start[i])) start[i]=cstring[i]; #if TDIST if (fold_type==0) { free_tree(T0); T0=NULL; } #endif free(test_table); free(target_table); free(mut_pos_list); free(w2_list); free(w1_list); free(struct2); free(structure); free(string2); free(cstring); free(string); return current_cost; }
int get_user( UnwrapParams *uwp ) { static char *text[] = { "Draws polygons in the 2D coordinate system of the selected texture layer.", NULL }; static char *fgopt[] = { "Use Color", "Invert Background", "Brighten", "Darken", NULL }; static char *bgopt[] = { "Use Color", "Copy Image Map", NULL }; LWPanControlDesc desc; LWValue ival = { LWT_INTEGER }, sval = { LWT_STRING }, ivecval = { LWT_VINT }; LWPanelID panel; LWControl *ctl[ 10 ], *bdr[ 2 ]; Node *root; int i, x, y, w, ph, ok; root = make_list( panf->globalFun ); if ( !root ) return 0; if( !( panel = PAN_CREATE( panf, "Unwrap" ))) { free_tree( root, 1 ); return 0; } if ( !uwp->filename[ 0 ] ) { strcpy( uwp->filename, "unwrapped" ); filename_ext( uwp->saver, uwp->filename ); uwp->bgcolor[ 0 ] = uwp->bgcolor[ 1 ] = uwp->bgcolor[ 2 ] = 255; } TEXT_CTL( panf, panel, "", text ); ctl[ 0 ] = TREE_CTL( panf, panel, "Texture Layer", 200, 200, tree_info, tree_count, tree_child ); ph = PAN_GETH( panf, panel ); ph -= CON_X( ctl[ 0 ] ); ph -= CON_H( ctl[ 0 ] ); ctl[ 1 ] = SAVE_CTL( panf, panel, "Save As", 40 ); ctl[ 2 ] = CUSTPOPUP_CTL( panf, panel, "", 150, sname, scount ); ctl[ 3 ] = INT_CTL( panf, panel, "Width" ); ctl[ 4 ] = INT_CTL( panf, panel, "Height" ); ctl[ 5 ] = BUTTON_CTL( panf, panel, "From Image Map" ); ctl[ 6 ] = WPOPUP_CTL( panf, panel, "Edges", fgopt, 150 ); ctl[ 7 ] = MINIRGB_CTL( panf, panel, "" ); ctl[ 8 ] = WPOPUP_CTL( panf, panel, "Background", bgopt, 150 ); ctl[ 9 ] = MINIRGB_CTL( panf, panel, "" ); w = CON_W( ctl[ 1 ] ); w -= CON_LW( ctl[ 1 ] ); bdr[ 0 ] = BORDER_CTL( panf, panel, "", w, 2 ); bdr[ 1 ] = BORDER_CTL( panf, panel, "", w, 2 ); x = CON_X( ctl[ 0 ] ); x += CON_W( ctl[ 0 ] ); x += CON_LW( ctl[ 8 ] ) + 8; y = CON_Y( ctl[ 0 ] ); w = CON_LW( ctl[ 1 ] ); MOVE_CON( ctl[ 1 ], x - w, y ); w = CON_LW( ctl[ 2 ] ); y += CON_HOTH( ctl[ 1 ] ) + 4; MOVE_CON( ctl[ 2 ], x - w, y ); y += CON_HOTH( ctl[ 2 ] ) + 6; MOVE_CON( bdr[ 0 ], x, y ); w = CON_LW( ctl[ 3 ] ); y += 6; MOVE_CON( ctl[ 3 ], x - w, y ); w = CON_X( ctl[ 3 ] ); w += CON_W( ctl[ 3 ] ); MOVE_CON( ctl[ 5 ], w + 16, y ); w = CON_LW( ctl[ 4 ] ); y += CON_HOTH( ctl[ 3 ] ) + 4; MOVE_CON( ctl[ 4 ], x - w, y ); y += CON_HOTH( ctl[ 2 ] ) + 6; MOVE_CON( bdr[ 1 ], x, y ); y += 6; for ( i = 6; i <= 9; i++ ) { w = CON_LW( ctl[ i ] ); MOVE_CON( ctl[ i ], x - w, y ); y += CON_HOTH( ctl[ i ] ) + 4; } y = CON_Y( ctl[ 9 ] ); y += CON_H( ctl[ 9 ] ); PAN_SETH( panf, panel, y + ph ); SET_STR( ctl[ 1 ], uwp->filename, sizeof( uwp->filename )); SET_INT( ctl[ 2 ], uwp->saver ); SET_INT( ctl[ 3 ], uwp->width ); SET_INT( ctl[ 4 ], uwp->height ); SET_INT( ctl[ 6 ], uwp->fgoptions ); SET_INT( ctl[ 8 ], uwp->bgoptions ); SETV_IVEC( ctl[ 7 ], uwp->fgcolor ); SETV_IVEC( ctl[ 9 ], uwp->bgcolor ); CON_SETEVENT( ctl[ 0 ], tree_event, root ); CON_SETEVENT( ctl[ 2 ], saver_event, ctl[ 1 ] ); CON_SETEVENT( ctl[ 5 ], sizebtn_event, ctl ); ok = panf->open( panel, PANF_BLOCKING | PANF_CANCEL ); if ( ok ) { GET_STR( ctl[ 1 ], uwp->filename, sizeof( uwp->filename )); GET_INT( ctl[ 2 ], uwp->saver ); GET_INT( ctl[ 3 ], uwp->width ); GET_INT( ctl[ 4 ], uwp->height ); GET_INT( ctl[ 6 ], uwp->fgoptions ); GET_INT( ctl[ 8 ], uwp->bgoptions ); GETV_IVEC( ctl[ 7 ], uwp->fgcolor ); GETV_IVEC( ctl[ 9 ], uwp->bgcolor ); ctl[ 0 ]->get( ctl[ 0 ], CTL_VALUE, &ival ); if ( !getsel_tree( uwp, ( Node * ) ival.ptr.ptr )) { msgf->error( "No texture layer selected", NULL ); ok = 0; } } PAN_KILL( panf, panel ); free_tree( root, 1 ); return ok; }
void start_split(Widget w, XtPointer client_data, XmSelectionBoxCallbackStruct *call_data) { short i,j,condition; char *cond; /* Get threshold value from user's selection */ XmStringGetLtoR(call_data->value, XmSTRING_DEFAULT_CHARSET, &cond); condition = atoi(cond); if (!(condition>=0 && condition<=20512) ) { XBell(XtDisplay(w),100); return; } /* Fill in the image with background color */ for(i=0; i<IMAGE_WIDTH; i++) for(j=0; j<IMAGE_HEIGHT; j++) XPutPixel(theXImage_2,i,j,bg); /* Clear the drawing window so the image is displayed again */ XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True); /* Associate the watch cursor with the main window */ XDefineCursor(XtDisplay(draw_1), XtWindow(main_window), theCursor); /* Flush the request buffer and wait for all events */ /* and errors to be processed by the server. */ XSync(XtDisplay(draw_1), False); /* Free the memory used for giving room to the structure */ /* that represents the merged regions if any */ if (merged_region_list!=NULL) free_merge(&merged_region_list); /* Free the memory used for giving room to the tree structure if any */ if (root!=NULL) free_tree(root); root=NULL; num_reg=0; num_split=0; /* Generate the quadtree structure */ root = split(condition,0,0,0); /* Show usefull information on the screen */ fprintf (stderr,"\n\n\n************************ SPLIT ***********************\n"); fprintf (stderr," Standard deviation: %i\n", condition); fprintf (stderr," Number of split operations: %i\n", num_split); fprintf (stderr," Number of regions after split: %i\n", num_reg); display_tree(root); /* Clear the drawing window so the image is displayed again */ XClearArea(XtDisplay(draw_1),XtWindow(draw_1),0,0,0,0,True); /* Disassociate the watch cursor from the main window */ XUndefineCursor(XtDisplay(draw_1), XtWindow(main_window)); /* Copy image into pixmap */ XPutImage(XtDisplay(draw_2), thePixmap_2, image_gc_2, theXImage_2, 0, 0, 0, 0, theXImage_2->width, theXImage_2->height); /* Clear the drawing window so the image is displayed again */ XClearArea(XtDisplay(draw_1),XtWindow(draw_2),0,0,0,0,True); }
comp_quest_t * mk_comp_quest(float64 *wt_ent_dec, float32 ****mixw, float32 ****means, float32 ****vars, uint32 *veclen, uint32 n_model, uint32 n_state, uint32 n_stream, uint32 n_density, float32 *stwt, uint32 *id, uint32 n_id, quest_t *all_q, uint32 n_all_q, pset_t *pset, uint32 n_base_phone, uint32 **dfeat, uint32 n_dfeat, uint32 split_min, uint32 split_max, float32 split_thr, float32 mwfloor) { dtree_t *tr; uint32 n_a, n_b; comp_quest_t *q; tr = mk_tree(mixw, means, vars, veclen, n_model, n_state, n_stream, n_density, stwt, id, n_id, all_q, n_all_q, pset, dfeat, n_dfeat, split_min, split_max, split_thr, mwfloor); if (tr == NULL) { *wt_ent_dec = 0; return NULL; } print_tree(stderr, "s>", &tr->node[0], pset, 1); /* Note that the tree now contains both mixw and mean and var info so they no longer need to be passed */ cluster_leaves(tr, veclen, wt_ent_dec, &n_a, &n_b, pset, n_state, n_stream, n_density, stwt, mwfloor); /* Build the disjunction w/ the fewest terms */ if (n_b > n_a) { q = tree2quest(tr, pset, split_max, 1, n_a); } else { q = tree2quest(tr, pset, split_max, 0, n_b); } free_tree(tr); if (simplify_comp_quest(q, n_base_phone)) { } return q; }
void free_tree(commandlist_t *cl) { commandlist_t *cl2; while (cl != NULL) { cl2 = cl->next; switch (cl->type) { case IF: free_test(cl->u.i.t); free_tree(cl->u.i.do_then); free_tree(cl->u.i.do_else); break; case INCLUDE: if (cl->u.inc.script) free(cl->u.inc.script); break; case FILEINTO: if (cl->u.f.folder) free(cl->u.f.folder); if (cl->u.f.flags) strarray_free(cl->u.f.flags); break; case REDIRECT: if (cl->u.r.address) free(cl->u.r.address); break; case REJCT: if (cl->u.str) free(cl->u.str); break; case VACATION: if (cl->u.v.subject) free(cl->u.v.subject); if (cl->u.v.addresses) strarray_free(cl->u.v.addresses); if (cl->u.v.message) free(cl->u.v.message); break; case SETFLAG: case ADDFLAG: case REMOVEFLAG: strarray_free(cl->u.sl); break; case KEEP: if (cl->u.k.flags) strarray_free(cl->u.k.flags); break; case STOP: case DISCARD: case RETURN: break; case NOTIFY: if (cl->u.n.method) free(cl->u.n.method); if (cl->u.n.id) free(cl->u.n.id); if (cl->u.n.options) strarray_free(cl->u.n.options); if (cl->u.n.message) free(cl->u.n.message); break; case DENOTIFY: if (cl->u.d.pattern) { #ifdef ENABLE_REGEX if (cl->u.d.comptag == REGEX) { regfree((regex_t *) cl->u.d.pattern); } #endif free(cl->u.d.pattern); } break; } free(cl); cl = cl2; } }
void free_tree(struct node **root) { if((*root)->left!=NULL)free_tree(&(*root)->left); if((*root)->right!=NULL)free_tree(&(*root)->right); free(*root); }
int main(int argc, char *argv[]) { float **points = NULL; if (argc > 1) { points = read_float_points(argv[1]); if (points == NULL) { perror("malloc"); return 0; } } else { printf("Usage: ./kdtree <path_to_point_data>\n"); return 0; } struct Tree *tree = init_tree(); if (tree == NULL) { perror("malloc"); return 0; } #if 0 /* Debug points */ for (int i=0; i < number_of_points; i++) printf("Point %d: (%d, %d)\n", i, points[i][0], points[i][1]); #endif float *curr_point = malloc(sizeof(float) * 2); if (curr_point == NULL) { perror("malloc"); free(tree); return 0; } // always start @ (0.5, 0.5) /r/DailyProgrammer #214-hard curr_point[0] = 0.5; curr_point[1] = 0.5; /* No actually need for function pointers as part of struct Tree. The function pointers in struct Tree are left from experimenting with allowing multiple types to be used as though this were an api for kd-tree's. */ tree->insert = insert_float_point; tree->closest = closest_float_neighbor; tree->calculate_dist_point = calculate_distance_float_point; tree->calculate_dist_rect = calculate_distance_float_rect; tree->set_dimension = set_float_dimension; float travelled = 0.0; insert_float_point_list(tree, points, treats); for (int i=0; i < treats; i++) { closest_neighbor(tree, tree->root, curr_point); float *close = (float *) tree->close_point; tree->close_node->visited = 1; memcpy(curr_point, close, sizeof(float) * 2); travelled += tree->close_dist; tree->close_dist = LONG_MAX; } printf("%f\n", travelled); free_tree(tree); free(curr_point); free(points); return 0; }
void free_tree(tree* t) { if (!t) return; free_tree(t->l); free_tree(t->r); wrap_free(t); }
//4/3/2012: added penalty void s_xpred(Sint *sn, Sint *nvarx, Sint *ncat, Sint *method, Sint *penalty, double *opt, double *parms, Sint *xvals, Sint *x_grp, double *ymat, FLOAT *xmat, Sint *missmat, double *predict, Sint *ncp, double *cp, char **error, double *wt, Sint *ny, double *cost) { int i,j,k; int maxcat; double temp; int n, nvar; int maxpri; struct node *xtree; double old_wt, total_wt; int temp2[rp.max_depth]; //alg 4/9/2012 /* ** initialize the splitting functions from the function table ** ALG 6/14/2013. adjusted this to accept penalties, etc. */ if (*method <= NUM_METHODS) { i = *method -1; rp_init = func_table_objective[i].init_split; rp_choose = func_table_objective[i].choose_split; rp_eval = func_table_objective[i].eval; rp_error = func_table_objective[i].error; rp_parent_objective = func_table_objective[i].parent_objective; rp.num_y = *ny; rp.method_number = i; } else { *error = _("invalid value for 'method'"); *sn = -1; return; } /* ** Set other parameters ** ** The opt string is in the order of control.rpart() ** minsplit, minbucket, cp, maxcomptete, maxsurrogate, usesurrogate, ** and xval */ n = *sn; nvar = *nvarx; rp.nvar = nvar; rp.numcat = ncat; rp.n = n; rp.num_unique_cp = *ncp; rp.wt = wt; rp.min_node = opt[1]; rp.min_split = opt[0]; rp.complexity= opt[2]; maxpri = opt[3] +1; rp.maxpri = maxpri; if (maxpri <1) rp.maxpri =1; rp.maxsur = opt[4]; rp.usesurrogate = opt[5]; rp.sur_agree = opt[6]; rp.maxnode = pow((double)2.0, opt[7]) -1; rp.vcost = cost; //ALG, set up split penalties. rp.splitparams = (double *)ALLOC(2, sizeof(double *)); rp.splitparams[0] = opt[9]; //alpha rp.splitparams[1] = opt[10]; //beta /* ** create the "ragged array" pointers to the matrix ** x and missmat are in column major order ** y is in row major order */ rp.xdata = (FLOAT **) ALLOC(nvar, sizeof(FLOAT *)); for (i=0; i<nvar; i++) { rp.xdata[i] = &(xmat[i*n]); } rp.ydata = (double **) ALLOC(n, sizeof(double *)); for (i=0; i<n; i++) rp.ydata[i] = &(ymat[i*rp.num_y]); /* ** allocate some scratch */ rp.tempvec = (int *)ALLOC(2*n, sizeof(int)); rp.which = rp.tempvec +n; rp.xtemp = (FLOAT *)ALLOC(n, sizeof(FLOAT)); rp.ytemp = (double **)ALLOC(n, sizeof(double *)); rp.wtemp = (double *)ALLOC(n, sizeof(double)); /* ** create a matrix of sort indices, one for each continuous variable ** This sort is "once and for all". The result is stored on top ** of the 'missmat' array. ** I don't have to sort the categoricals. */ rp.sorts = (Sint**) ALLOC(nvar, sizeof(Sint *)); maxcat=0; for (i=0; i<nvar; i++) { rp.sorts[i] = &(missmat[i*n]); for (k=0; k<n; k++) { if (rp.sorts[i][k]==1) { rp.tempvec[k] = -(k+1); rp.xdata[i][k] =0; /* avoid weird numerics in S's NA */ } else rp.tempvec[k] = k; } if (ncat[i]==0) mysort(0, n-1, rp.xdata[i], rp.tempvec); else if (ncat[i] > maxcat) maxcat = ncat[i]; for (k=0; k<n; k++) rp.sorts[i][k] = rp.tempvec[k]; } /* ** And now the last of my scratch space */ if (maxcat >0) { rp.csplit = (int *) ALLOC(3*maxcat, sizeof(int)); rp.left = rp.csplit + maxcat; rp.right= rp.left + maxcat; rp.lwt = (double *) ALLOC(2*maxcat, sizeof(double)); rp.rwt = rp.lwt + maxcat; } else rp.csplit = (int *)ALLOC(1, sizeof(int)); (*rp_init)(n, rp.ydata, maxcat, error, parms, &rp.num_resp, 1, rp.wt); nodesize = sizeof(struct node) + (rp.num_resp-2)*sizeof(double); /* ** I need the risk of the full tree, to scale alpha */ xtree = (struct node *) ALLOC(1, nodesize); (*rp_eval)(n, rp.ydata, xtree->response_est, &(xtree->risk), rp.wt); rp.alpha = rp.complexity * (xtree)->risk; //ALG: the vector of variables used in each branch... for(i=0;i<rp.nvar;i++){ temp2[i]=0; } /* ** do the validations */ total_wt =0; for (i=0; i<rp.n; i++) total_wt += rp.wt[i]; old_wt = total_wt; for (i=0; i< *xvals; i++) { /* ** mark the "leave out" data as ficticious node 0 */ k=0; temp =0; for (j=0; j<rp.n; j++) { //loop through each observation. if (x_grp[j]==(i+1)) { rp.which[j] =0; } else { rp.which[j] =1; rp.ytemp[k] = rp.ydata[j]; rp.wtemp[k] = rp.wt[j]; k++; temp += rp.wt[j]; } } /* rescale the cp */ for (j=0; j<rp.num_unique_cp; j++) cp[j] *= temp/old_wt; rp.alpha *= temp/old_wt; old_wt = temp; /* ** partition the new tree */ xtree = (struct node *) CALLOC(1, nodesize); xtree->num_obs = k; //ALG added next two lines to conform to new penalty code rp.dummy = (double *) ALLOC(1,sizeof(double)); //7/18/2012: allocate (*rp_parent_objective)(k, rp.ytemp, rp.dummy, &rp.root_objective_scaling, rp.wtemp); (*rp_init)(k,rp.ytemp, maxcat, error, parms, &temp, 2, rp.wtemp); (*rp_eval)(k, rp.ytemp, xtree->response_est, &(xtree->risk), rp.wtemp); xtree->complexity = xtree->risk; partition(1, xtree, &temp, temp2); fix_cp(xtree, xtree->complexity); /* ** run the extra data down the new tree */ for (j=0; j<rp.n; j++) { if (rp.which[j]==0) { rundown2(xtree, j, cp, (predict+ j* *ncp)); } } free_tree(xtree, 1); }//end loop through xvals }
tree_node* alex_parsing(token_list* t_lt) { token* tk_p = NULL; tree_node* r_tn = NULL; if(t_lt == NULL) return NULL; while(tk_p=at_token(t_lt)) { switch(tk_p->token_type) { case token_type_var: // 定义变量 { r_tn = syn_var_def(t_lt); if(r_tn) add_main(r_tn); else return NULL; } break; case token_type_ide: { r_tn = syn_exp_stmt(t_lt); if(r_tn) add_main(r_tn); else return NULL; } break; case token_type_using: // 引入dll { r_tn = syn_using(t_lt); if(r_tn) add_main(r_tn); else return NULL; } break; case token_type_func: // 定义函数 { r_tn = syn_func_def(t_lt); if(r_tn) { if(look_table(global_table, r_tn->b_v.name.s_ptr)==NULL) { add_g_table(new_func_st(r_tn->b_v.name.s_ptr)); add_func(r_tn); } else { print("parsing[erro line %d]: the function %s is redef!\n", tk_p->token_line, r_tn->b_v.name.s_ptr); free_tree(r_tn); return NULL; } } else return NULL; } break; default: { print("parsing[error line %d]: not allow token \"%s\" at globle code seg!\n", tk_p->token_line, tk_p->token_name); next_token(t_lt); } free_tree(r_tn); return NULL; } } return r_tn; }
// ( const value var value tree_node* syn_factor_exp(token_list* t_lt) { tree_node* rt_n = NULL; token* n_t = NULL; switch(type_token(t_lt)) { case token_type_lbra: // ( { syn_watch(t_lt, token_type_lbra); // jump ( rt_n = syn_exp_def(t_lt); syn_watch(t_lt, token_type_rbra); // jump ) } break; case token_type_lal: { syn_watch(t_lt, token_type_lal); // jump [ rt_n = syn_al_def(t_lt); syn_watch(t_lt, token_type_ral); // jump ] } break; case token_type_ide: // var ide { switch(look_token(t_lt)) // if token=='(' func call { case token_type_lbra: // func call case token_type_lal: // arraylist { rt_n = syn_funcal_exp(t_lt); } break; default: // var { rt_n = syn_getvar_exp(t_lt); } break; } } break; case token_type_num: // const number { token* n_t = at_token(t_lt); rt_n = new_tree_node(get_line(t_lt), bnf_type_const); rt_n->b_v.number = n_t->token_value.number; next_token(t_lt); } break; case token_type_string: // const string { token* n_t = at_token(t_lt); rt_n = new_tree_node(get_line(t_lt), bnf_type_string); rt_n->b_v.str = alex_string(n_t->token_value.str.s_ptr); next_token(t_lt); } break; case token_type_sadd: // ++number case token_type_ssub: // --number case token_type_sub: // - { rt_n = new_tree_node(get_line(t_lt), bnf_type_value); rt_n->b_v.op_t = type_token(t_lt); next_token(t_lt); rt_n->childs_p[1] = syn_factor_exp(t_lt); } break; default: { token* n_t = at_token(t_lt); next_token(t_lt); print("factor[error line %d]: token \"%s\" is not allow!\n", n_t->token_line, n_t->token_name.s_ptr); } return NULL; } if(type_token(t_lt) == token_type_sadd || type_token(t_lt) == token_type_ssub) // if number-- number++ { tree_node* n_rt_n = new_tree_node(get_line(t_lt), bnf_type_value); n_rt_n->b_v.op_t = type_token(t_lt); n_rt_n->childs_p[0] = rt_n; rt_n = n_rt_n; next_token(t_lt); } if(error_flag) { free_tree(rt_n); return NULL; } return rt_n; }
// segment def tree_node* syn_seg_def(token_list* t_lt) { tree_node* rt_n = new_tree_node(get_line(t_lt), bnf_type_seg); tree_node** nrt_n = &rt_n->childs_p[0]; byte seg_flag = 0; if(type_token(t_lt) == token_type_lseg) { seg_flag = 1; syn_watch(t_lt, token_type_lseg); } do{ switch(type_token(t_lt)) { case token_type_lseg: // if { { syn_watch(t_lt, token_type_lseg); // watch { *nrt_n = syn_seg_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); syn_watch(t_lt, token_type_rseg); // watch } } break; case token_type_var: { *nrt_n = syn_var_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_if: { *nrt_n = syn_if_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_while: { *nrt_n = syn_while_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_continue: { *nrt_n = syn_continue_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_break: { *nrt_n = syn_break_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_return: { *nrt_n = syn_return_def(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; case token_type_rseg: break; default: { *nrt_n = syn_exp_stmt(t_lt); nrt_n = (*nrt_n)?(&((*nrt_n)->next)):(NULL); } break; } }while( nrt_n && seg_flag && (type_token(t_lt) != token_type_rseg) ); // if not find { is loop if(seg_flag) syn_watch(t_lt, token_type_rseg); if(error_flag) { free_tree(rt_n); return NULL; } return rt_n; }
bool setup_job(JCR *jcr) { int errstat; jcr->lock(); Dsm_check(100); init_msg(jcr, jcr->res.messages, job_code_callback_director); /* * Initialize termination condition variable */ if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) { berrno be; Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat)); jcr->unlock(); goto bail_out; } jcr->term_wait_inited = true; /* * Initialize nextrun ready condition variable */ if ((errstat = pthread_cond_init(&jcr->nextrun_ready, NULL)) != 0) { berrno be; Jmsg1(jcr, M_FATAL, 0, _("Unable to init job nextrun cond variable: ERR=%s\n"), be.bstrerror(errstat)); jcr->unlock(); goto bail_out; } jcr->nextrun_ready_inited = true; create_unique_job_name(jcr, jcr->res.job->name()); jcr->setJobStatus(JS_Created); jcr->unlock(); /* * Open database */ Dmsg0(100, "Open database\n"); jcr->db = db_sql_get_pooled_connection(jcr, jcr->res.catalog->db_driver, jcr->res.catalog->db_name, jcr->res.catalog->db_user, jcr->res.catalog->db_password, jcr->res.catalog->db_address, jcr->res.catalog->db_port, jcr->res.catalog->db_socket, jcr->res.catalog->mult_db_connections, jcr->res.catalog->disable_batch_insert); if (jcr->db == NULL) { Jmsg(jcr, M_FATAL, 0, _("Could not open database \"%s\".\n"), jcr->res.catalog->db_name); goto bail_out; } Dmsg0(150, "DB opened\n"); if (!jcr->fname) { jcr->fname = get_pool_memory(PM_FNAME); } if (!jcr->res.pool_source) { jcr->res.pool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.pool_source, _("unknown source")); } if (!jcr->res.npool_source) { jcr->res.npool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.npool_source, _("unknown source")); } if (jcr->JobReads()) { if (!jcr->res.rpool_source) { jcr->res.rpool_source = get_pool_memory(PM_MESSAGE); pm_strcpy(jcr->res.rpool_source, _("unknown source")); } } /* * Create Job record */ init_jcr_job_record(jcr); if (!get_or_create_client_record(jcr)) { goto bail_out; } if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) { Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db)); goto bail_out; } jcr->JobId = jcr->jr.JobId; Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel); new_plugins(jcr); /* instantiate plugins for this jcr */ generate_plugin_event(jcr, bDirEventJobStart); if (job_canceled(jcr)) { goto bail_out; } if (jcr->JobReads() && !jcr->rstorage) { if (jcr->res.job->storage) { copy_rwstorage(jcr, jcr->res.job->storage, _("Job resource")); } else { copy_rwstorage(jcr, jcr->res.job->pool->storage, _("Pool resource")); } } if (!jcr->JobReads()) { free_rstorage(jcr); } /* * Now, do pre-run stuff, like setting job level (Inc/diff, ...) * this allows us to setup a proper job start record for restarting * in case of later errors. */ switch (jcr->getJobType()) { case JT_BACKUP: switch (jcr->getJobProtocol()) { case PT_NDMP: if (!do_ndmp_backup_init(jcr)) { ndmp_backup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: if (jcr->is_JobLevel(L_VIRTUAL_FULL)) { if (!do_native_vbackup_init(jcr)) { native_vbackup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } } else { if (!do_native_backup_init(jcr)) { native_backup_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } } break; } break; case JT_VERIFY: if (!do_verify_init(jcr)) { verify_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; case JT_RESTORE: switch (jcr->getJobProtocol()) { case PT_NDMP: if (!do_ndmp_restore_init(jcr)) { ndmp_restore_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: /* * Any non NDMP restore is not interested at the items * that were selected for restore so drop them now. */ if (jcr->restore_tree_root) { free_tree(jcr->restore_tree_root); jcr->restore_tree_root = NULL; } if (!do_native_restore_init(jcr)) { native_restore_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; } break; case JT_ADMIN: if (!do_admin_init(jcr)) { admin_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; case JT_COPY: case JT_MIGRATE: if (!do_migration_init(jcr)) { migration_cleanup(jcr, JS_ErrorTerminated); goto bail_out; } break; default: Pmsg1(0, _("Unimplemented job type: %d\n"), jcr->getJobType()); jcr->setJobStatus(JS_ErrorTerminated); goto bail_out; } generate_plugin_event(jcr, bDirEventJobInit); Dsm_check(100); return true; bail_out: return false; }
int get_mode( char *cp, char **endp, ADDR_MODE *mode) { EX_TREE *value; mode->offset = NULL; mode->rel = 0; mode->type = 0; cp = skipwhite(cp); /* @ means "indirect," sets bit 3 */ if (*cp == '@') { cp++; mode->type |= 010; } /* Immediate modes #imm and @#imm */ if (*cp == '#') { cp++; mode->type |= 027; mode->offset = parse_expr(cp, 0); if (endp) *endp = mode->offset->cp; return TRUE; } /* Check for -(Rn) */ if (*cp == '-') { char *tcp = skipwhite(cp + 1); if (*tcp++ == '(') { unsigned reg; /* It's -(Rn) */ value = parse_expr(tcp, 0); reg = get_register(value); if (reg == NO_REG || (tcp = skipwhite(value->cp), *tcp++ != ')')) { free_tree(value); return FALSE; } mode->type |= 040 | reg; if (endp) *endp = tcp; free_tree(value); return TRUE; } } /* Check for (Rn) */ if (*cp == '(') { char *tcp; unsigned reg; value = parse_expr(cp + 1, 0); reg = get_register(value); if (reg == NO_REG || (tcp = skipwhite(value->cp), *tcp++ != ')')) { free_tree(value); return FALSE; } tcp = skipwhite(tcp); if (*tcp == '+') { tcp++; /* It's (Rn)+ */ if (endp) *endp = tcp; mode->type |= 020 | reg; free_tree(value); return TRUE; } if (mode->type == 010) { /* For @(Rn) there's an implied 0 offset */ mode->offset = new_ex_lit(0); mode->type |= 060 | reg; free_tree(value); if (endp) *endp = tcp; return TRUE; } mode->type |= 010 | reg; /* Mode 10 is register indirect as in (Rn) */ free_tree(value); if (endp) *endp = tcp; return TRUE; } /* Modes with an offset */ mode->offset = parse_expr(cp, 0); cp = skipwhite(mode->offset->cp); if (*cp == '(') { unsigned reg; /* indirect register plus offset */ value = parse_expr(cp + 1, 0); reg = get_register(value); if (reg == NO_REG || (cp = skipwhite(value->cp), *cp++ != ')')) { free_tree(value); return FALSE; /* Syntax error in addressing mode */ } mode->type |= 060 | reg; free_tree(value); if (endp) *endp = cp; return TRUE; } /* Plain old expression. */ if (endp) *endp = cp; /* It might be a register, though. */ if (mode->offset->type == EX_SYM) { SYMBOL *sym = mode->offset->data.symbol; if (sym->section->type == SECTION_REGISTER) { free_tree(mode->offset); mode->offset = NULL; mode->type |= sym->value; return TRUE; } } /* It's either 067 (PC-relative) or 037 (absolute) mode, depending */ /* on user option. */ if (mode->type & 010) { /* Have already noted indirection? */ mode->type |= 067; /* If so, then PC-relative is the only option */ mode->rel = 1; /* Note PC-relative */ } else if (enabl_ama) { /* User asked for absolute adressing? */ mode->type |= 037; /* Give it to him. */ } else { mode->type |= 067; /* PC-relative */ mode->rel = 1; /* Note PC-relative */ } return TRUE; }
void delete_tree(TREE* tree) { free_tree(tree, MYF(0)); /* my_free() mem_root if applicable */ }
/* 读入编码表,返回构建的 Huffman 树 */ static node *read_code_table(FILE *in, unsigned int *pdb) { node *root = new_node(0, NULL, NULL); uint32_t count; /* 读入符号数 */ if (fread(&count, sizeof(count), 1, in) != 1) { free_tree(root); return NULL; } count = ntohl(count); /* 读入当前分支编码数 */ if (fread(pdb, sizeof(*pdb), 1, in) != 1) { free_tree(root); return NULL; } *pdb = ntohl(*pdb); /* 读入符号表 */ while (count-- > 0) { int c; unsigned int c_bit; unsigned char symbol; unsigned char len; unsigned char lbytes; unsigned char *bytes; node *p = root; if ((c = fgetc(in)) == EOF) { free_tree(root); return NULL; } symbol = (unsigned char) c; if ((c = fgetc(in)) == EOF) { free_tree(root); return NULL; } len = (unsigned char) c; lbytes = (unsigned char) bit_len_byte(len); bytes = (unsigned char *) malloc(lbytes); if (fread(bytes, 1, lbytes, in) != lbytes) { free(bytes); free_tree(root); return NULL; } /* 依据当前位加入 Huffman 树 */ for (c_bit = 0; c_bit < len; ++c_bit) { if (get_bit(bytes, c_bit)) { if (p->one == NULL) { p->one = c_bit == (unsigned char) (len - 1) ? new_leaf_node(symbol) : new_node(0, NULL, NULL); p->one->parent = p; } p = p->one; } else { if (p->zero == NULL) { p->zero = c_bit == (unsigned char) (len - 1) ? new_leaf_node(symbol) : new_node(0, NULL, NULL); p->zero->parent = p; } p = p->zero; } } free(bytes); } return root; }
void reset_tree(TREE* tree) { /* do not free mem_root, just mark blocks as free */ free_tree(tree, MYF(MY_MARK_BLOCKS_FREE)); }
static sint malign(ALNPTR mult_aln,MULT_OPT mult_opt,sint output_order,Boolean verbose,ALNCOUNT *alncount) { Boolean use_maxid; sint i,j,n,tx,set; sint ix,t1,t2; sint status,entries; sint nsets; sint **sets; sint naligned_groups; sint *aligned_group; sint *aligned; lint score = 0; sint *seq_weight; IN_TREEPTR itree; float min_dist,mean_dist; float **group_dist; double **tmat; Boolean *delayed,ok; char *dsc_ss; char *jnet_ss; if (verbose) info("Start of Multiple Alignment"); /* get the guide tree (tree should be in phylip format) */ if (mult_aln->nseqs >= 2) { itree=(IN_TREEPTR)ckalloc(sizeof(IN_TREE)); status = read_tree(mult_aln->treename, mult_aln->seqs, (sint)0, mult_aln->nseqs,itree); if (status < 0) return((sint)0); } if(status<0) use_maxid=TRUE; else use_maxid=FALSE; /* group the sequences according to their relative divergence */ sets = (sint **) ckalloc( (mult_aln->nseqs+1) * sizeof (sint *) ); for(i=0;i<mult_aln->nseqs;i++) sets[i] = (sint *)ckalloc( (mult_aln->nseqs+1) * sizeof (sint) ); nsets=create_sets(mult_aln->nseqs,sets,itree); if(verbose) info("There are %d groups",(pint)nsets); if(debug>1) for(set=0;set<nsets;set++) { for (i=0;i<mult_aln->nseqs;i++) { printf("%d ",sets[set][i]); } printf("\n"); } /* calculate sequence weights according to branch lengths of the tree - weights are normalised to sum to 100 */ seq_weight = calc_seq_weights((sint)0, mult_aln->nseqs, itree,mult_opt.no_weights); for(i=0;i<mult_aln->nseqs;i++) mult_aln->seqs[i].weight=seq_weight[i]; /* calculate tmat matrix containing percent distances for all pairs of sequences */ tmat = pw_distances_from_tree(mult_aln->seqs,mult_aln->nseqs,itree); if (tmat == NULL) return((sint)0); /* clear the memory used for the phylogenetic tree */ if (mult_aln->nseqs >= 2) free_tree(itree); /* start the multiple alignments......... */ if(verbose) info("Aligning..."); if (output_order==INPUT) { for (i=0;i<mult_aln->nseqs;i++) mult_aln->seqs[i].output_index = i; } else { tx=0; for (i=0;i<mult_aln->nseqs;i++) mult_aln->seqs[i].output_index = -1; for(set=0;set<nsets;set++) { for (i=0;i<mult_aln->nseqs;i++) { if (sets[set][i] != 0) { if(mult_aln->seqs[i].output_index==(-1)) { mult_aln->seqs[i].output_index = tx++; } } } } } /* align the closely related groups first */ aligned_group = (sint *) ckalloc( (mult_aln->nseqs+1) * sizeof (sint) ); delayed = (Boolean *) ckalloc( (mult_aln->nseqs+1) * sizeof (Boolean) ); for(set=0;set<nsets;set++) { /* decide whether to do the alignment now - if the mean distance between sequences in the two groups to be aligned is greater than the cutoff, then don't align now */ mean_dist=0.0; entries=0; ok=TRUE; for (i=0,n=0;i<mult_aln->nseqs;i++) { if (sets[set][i] != 0 && mult_aln->seqs[i].len>0) { if(delayed[i]==TRUE) { ok=FALSE; break; } entries++; for (j=i+1;j<mult_aln->nseqs;j++) { if (sets[set][j] != 0 && mult_aln->seqs[j].len>0 && sets[set][i] != sets[set][j]) { if(delayed[j]==TRUE) { ok=FALSE; break; } mean_dist+=tmat[i][j]; n++; /* if(mean_dist>tmat[i][j]) mean_dist=tmat[i][j];*/ } } } } if(ok==TRUE && n>0) mean_dist=mean_dist/(float)(n); if ((ok==TRUE) && mean_dist < 1.0-(float)mult_opt.divergence_cutoff/100.0) { score = prfalign(0,mult_aln,sets[set],tmat,use_maxid,mult_opt,alncount,TRUE); /*if (score < 0) return(-1);*/ if(verbose) info("Group %d: Sequences:%4d Score:%d", (pint)set+1,(pint)entries,(pint)score); for (i=0;i<mult_aln->nseqs;i++) { if (sets[set][i] != 0) { aligned_group[i]=set+1; } } } else { if(verbose) info("Group %d: Sequences:%4d Delayed",(pint)set+1,(pint)entries); for (i=0;i<mult_aln->nseqs;i++) if (sets[set][i] != 0) { delayed[i]=TRUE; } } } /* renumber the groups and assign a group number to any orphan sequences, that haven't been aligned yet */ naligned_groups=renumber_groups(mult_aln->nseqs,aligned_group,0); if (verbose) fprintf(stdout,"done groups %d\n",naligned_groups); for(i=0;i<mult_aln->nseqs;i++) mult_aln->seqs[i].simgroup=aligned_group[i]; for(i=0;i<mult_aln->nseqs;i++) if(aligned_group[i]==0) { aligned_group[i]=naligned_groups+1; naligned_groups++; } if (verbose) fprintf(stdout,"done groups and added orphans %d\n",naligned_groups); if(naligned_groups==1) return naligned_groups; /* now align the two closest groups of sequences together */ /* the array aligned[] contains the group number of each sequence */ ix=0; if(naligned_groups>1) { for(i=0;i<mult_aln->nseqs;i++) mult_aln->seqs[i].output_index=(-1); group_dist = (float **) ckalloc( (naligned_groups+1) * sizeof (float *) ); for(i=0;i<naligned_groups+1;i++) group_dist[i] = (float *)ckalloc( (naligned_groups+1) * sizeof (float) ); aligned = (sint *) ckalloc( (mult_aln->nseqs+1) * sizeof (sint) ); /* calculate the distance between each pair of groups */ calc_group_dist(mult_aln->nseqs,naligned_groups,aligned_group,tmat,group_dist); for(i=0;i<mult_aln->nseqs;i++) sets[0][i] = 0; /* align the two closest groups together */ min_dist=1000.0; t1=t2=0; for (i=0;i<naligned_groups;i++) for (j=i+1;j<naligned_groups;j++) if(min_dist>=group_dist[i][j]) { min_dist=group_dist[i][j]; t1=i+1; t2=j+1; } for(i=0;i<mult_aln->nseqs;i++) if(aligned_group[i]==t1) { sets[0][i]=1; if (output_order==ALIGNED) mult_aln->seqs[i].output_index = ix++; } for(i=0;i<mult_aln->nseqs;i++) if(aligned_group[i]==t2) { sets[0][i]=2; aligned_group[i]=t1; if (output_order==ALIGNED) mult_aln->seqs[i].output_index = ix++; } for (i=0,tx=0;i<mult_aln->nseqs;i++) if (sets[0][i] != 0) { aligned[i]=1; tx++; } score = prfalign(0,mult_aln,sets[0],tmat,use_maxid,mult_opt,alncount,TRUE); /*if (score < 0) return(-1);*/ if(verbose) info("Group Align %d: Sequences:%4d Score:%d", (pint)tx,(pint)entries,(pint)score); } /* now align the remaining groups to the first two */ naligned_groups=renumber_groups(mult_aln->nseqs,aligned_group,t1); t1=1; while(naligned_groups>1) { /* calculate the distance of each remaining group to the first group */ calc_group_dist1(mult_aln->nseqs,naligned_groups,aligned_group,tmat,group_dist); /* align the next closest group with the first group */ for(i=0;i<mult_aln->nseqs;i++) sets[0][i] = 0; min_dist=100.0; t2=0; for (j=1;j<naligned_groups;j++) if(min_dist>=group_dist[0][j]) { min_dist=group_dist[0][j]; t2=j+1; } if (verbose) fprintf(stdout,"Aligning Group %d %d\n",t1,t2); for(i=0;i<mult_aln->nseqs;i++) if(aligned_group[i]==1) sets[0][i]=1; else if(aligned_group[i]==t2) { sets[0][i]=2; aligned_group[i]=1; if (output_order==ALIGNED) mult_aln->seqs[i].output_index = ix++; } if(debug>0) for(i=0;i<mult_aln->nseqs;i++) fprintf(stdout,"%s %d %d\n",mult_aln->seqs[i].name,mult_aln->seqs[i].output_index,aligned_group[i]); for (i=0,tx=0;i<mult_aln->nseqs;i++) if (sets[0][i] != 0) { aligned[i]=1; tx++; } score = prfalign(0,mult_aln,sets[0],tmat,use_maxid,mult_opt,alncount,TRUE); /*if (score < 0) return(-1);*/ if(verbose) info("Group Align %d: Sequences:%4d Score:%d", (pint)tx,(pint)entries,(pint)score); naligned_groups=renumber_groups(mult_aln->nseqs,aligned_group,t1); } for(i=0;i<mult_aln->nseqs;i++) tmat[i] = ckfree(tmat[i]); tmat = ckfree(tmat); for (i=0;i<naligned_groups;i++) group_dist[i]=ckfree((void *)group_dist[i]); group_dist=ckfree(group_dist); aligned=ckfree(aligned); for (i=0;i<naligned_groups;i++) sets[i]=ckfree((void *)sets[i]); sets=ckfree(sets); return((sint)1); }
/* * floorplanning using simulated annealing. * precondition: flp is a pre-allocated placeholder. * returns the number of compacted blocks in the selected * floorplan */ int floorplan(flp_t *flp, flp_desc_t *flp_desc, RC_model_t *model, double *power) { NPE_t *expr, *next, *best; /* Normalized Polish Expressions */ tree_node_stack_t *stack; /* for NPE evaluation */ tree_node_t *root; /* shape curve tree */ double cost, new_cost, best_cost, sum_cost, T, Tcold; int i, steps, downs, n, rejects, compacted, rim_blocks = 0; int original_n = flp->n_units; int wrap_l2; /* to maintain the order of power values during * the compaction/shifting around of blocks */ double *tpower = hotspot_vector(model); /* shortcut */ flp_config_t cfg = flp_desc->config; /* * make the rim strips disappear for slicing tree * purposes. can be restored at the end */ if (cfg.model_rim) flp->n_units = (flp->n_units - 2) / 3; /* wrap L2 around? */ wrap_l2 = FALSE; if (cfg.wrap_l2 && !strcasecmp(flp_desc->units[flp_desc->n_units-1].name, cfg.l2_label)) { wrap_l2 = TRUE; /* make L2 disappear too */ flp_desc->n_units--; flp->n_units -= (L2_ARMS+1); } /* initialization */ expr = NPE_get_initial(flp_desc); stack = new_tree_node_stack(); init_rand(); /* convert NPE to flp */ root = tree_from_NPE(flp_desc, stack, expr); /* compacts too small dead blocks */ compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio); /* update the tpower vector according to the compaction */ trim_hotspot_vector(model, tpower, power, flp->n_units, compacted); free_tree(root); if(wrap_l2) flp_wrap_l2(flp, flp_desc); if(cfg.model_rim) rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness); resize_thermal_model(model, flp->n_units); #if VERBOSE > 2 print_flp(flp); #endif cost = flp_evaluate_metric(flp, model, tpower, cfg.lambdaA, cfg.lambdaT, cfg.lambdaW); /* restore the compacted blocks */ restore_dead_blocks(flp, flp_desc, compacted, wrap_l2, cfg.model_rim, rim_blocks); best = NPE_duplicate(expr); /* best till now */ best_cost = cost; /* simulated annealing */ steps = 0; /* initial annealing temperature */ T = -cfg.Davg / log(cfg.P0); /* * final annealing temperature - we stop when there * are fewer than (1-cfg.Rreject) accepts. * of those accepts, assuming half are uphill moves, * we want the temperature so that the probability * of accepting uphill moves is as low as * (1-cfg.Rreject)/2. */ Tcold = -cfg.Davg / log ((1.0 - cfg.Rreject) / 2.0); #if VERBOSE > 0 fprintf(stdout, "initial cost: %g\tinitial T: %g\tfinal T: %g\n", cost, T, Tcold); #endif /* * stop annealing if temperature has cooled down enough or * max no. of iterations have been tried */ while (T >= Tcold && steps < cfg.Nmax) { /* shortcut */ n = cfg.Kmoves * flp->n_units; i = downs = rejects = 0; sum_cost = 0; /* try enough total or downhill moves per T */ while ((i < 2 * n) && (downs < n)) { next = make_random_move(expr); /* convert NPE to flp */ root = tree_from_NPE(flp_desc, stack, next); compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio); /* update the tpower vector according to the compaction */ trim_hotspot_vector(model, tpower, power, flp->n_units, compacted); free_tree(root); if(wrap_l2) flp_wrap_l2(flp, flp_desc); if(cfg.model_rim) rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness); resize_thermal_model(model, flp->n_units); #if VERBOSE > 2 print_flp(flp); #endif new_cost = flp_evaluate_metric(flp, model, tpower, cfg.lambdaA, cfg.lambdaT, cfg.lambdaW); restore_dead_blocks(flp, flp_desc, compacted, wrap_l2, cfg.model_rim, rim_blocks); #if VERBOSE > 1 fprintf(stdout, "count: %d\tdowns: %d\tcost: %g\t", i, downs, new_cost); #endif /* move accepted? */ if (new_cost < cost || /* downhill always accepted */ /* boltzmann probability function */ rand_fraction() < exp(-(new_cost-cost)/T)) { free_NPE(expr); expr = next; /* downhill move */ if (new_cost < cost) { downs++; /* found new best */ if (new_cost < best_cost) { free_NPE(best); best = NPE_duplicate(expr); best_cost = new_cost; } } #if VERBOSE > 1 fprintf(stdout, "accepted\n"); #endif cost = new_cost; sum_cost += cost; } else { /* rejected move */ rejects++; free_NPE(next); #if VERBOSE > 1 fprintf(stdout, "rejected\n"); #endif } i++; } #if VERBOSE > 0 fprintf(stdout, "step: %d\tT: %g\ttries: %d\taccepts: %d\trejects: %d\t", steps, T, i, (i-rejects), rejects); fprintf(stdout, "avg. cost: %g\tbest cost: %g\n", (i-rejects)?(sum_cost / (i-rejects)):sum_cost, best_cost); #endif /* stop annealing if there are too little accepts */ if(((double)rejects/i) > cfg.Rreject) break; /* annealing schedule */ T *= cfg.Rcool; steps++; } /* best floorplan found */ root = tree_from_NPE(flp_desc, stack, best); #if VERBOSE > 0 { int pos = min_area_pos(root->curve); print_tree_relevant(root, pos, flp_desc); } #endif compacted = tree_to_flp(root, flp, TRUE, cfg.compact_ratio); /* update the power vector according to the compaction */ trim_hotspot_vector(model, power, power, flp->n_units, compacted); free_tree(root); /* restore L2 and rim */ if(wrap_l2) { flp_wrap_l2(flp, flp_desc); flp_desc->n_units++; } if(cfg.model_rim) rim_blocks = flp_wrap_rim(flp, cfg.rim_thickness); resize_thermal_model(model, flp->n_units); #if VERBOSE > 2 print_flp(flp); #endif free_NPE(expr); free_NPE(best); free_tree_node_stack(stack); free_dvector(tpower); /* * return the number of blocks compacted finally * so that any deallocator can take care of memory * accordingly. */ return (original_n - flp->n_units); }
/** * Main function of the helper process to extract meta data. * * @param argc should be 3 * @param argv [0] our binary name * [1] name of the file or directory to process * [2] "-" to disable extraction, NULL for defaults, * otherwise custom plugins to load from LE * @return 0 on success */ int main (int argc, char *const *argv) { const char *filename_expanded; const char *ex; struct ScanTreeNode *root; #if WINDOWS /* We're using stdout to communicate binary data back to the parent; use * binary mode. */ _setmode (1, _O_BINARY); /* Get utf-8-encoded arguments */ if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) return 5; output_stream = 1; /* stdout */ #else ignore_sigpipe (); /* move stdout to some other FD for IPC, bind stdout/stderr to /dev/null */ output_stream = dup (1); make_dev_zero (1, O_WRONLY); make_dev_zero (2, O_WRONLY); #endif /* parse command line */ if ( (3 != argc) && (2 != argc) ) { FPRINTF (stderr, "%s", "gnunet-helper-fs-publish needs exactly one or two arguments\n"); #if WINDOWS GNUNET_free ((void*) argv); #endif return 1; } filename_expanded = argv[1]; ex = argv[2]; if ( (NULL == ex) || (0 != strcmp (ex, "-")) ) { #if HAVE_LIBEXTRACTOR plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY); if (NULL != ex) plugins = EXTRACTOR_plugin_add_config (plugins, ex, EXTRACTOR_OPTION_DEFAULT_POLICY); #endif } /* scan tree to find out how much work there is to be done */ if (GNUNET_OK != preprocess_file (filename_expanded, &root)) { (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0); #if HAVE_LIBEXTRACTOR EXTRACTOR_plugin_remove_all (plugins); #endif #if WINDOWS GNUNET_free ((void*) argv); #endif return 2; } /* signal that we're done counting files, so that a percentage of progress can now be calculated */ if (GNUNET_OK != write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE, NULL, 0)) { #if HAVE_LIBEXTRACTOR EXTRACTOR_plugin_remove_all (plugins); #endif #if WINDOWS GNUNET_free ((void*) argv); #endif return 3; } if (NULL != root) { if (GNUNET_OK != extract_files (root)) { (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0); free_tree (root); #if HAVE_LIBEXTRACTOR EXTRACTOR_plugin_remove_all (plugins); #endif #if WINDOWS GNUNET_free ((void*) argv); #endif return 4; } free_tree (root); } /* enable "clean" shutdown by telling parent that we are done */ (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED, NULL, 0); #if HAVE_LIBEXTRACTOR EXTRACTOR_plugin_remove_all (plugins); #endif #if WINDOWS GNUNET_free ((void*) argv); #endif return 0; }
dtree_t * mk_tree(float32 ****mixw, float32 ****means, float32 ****vars, uint32 *veclen, uint32 n_model, uint32 n_state, uint32 n_stream, uint32 n_density, float32 *stwt, uint32 *id, uint32 n_id, quest_t *all_q, uint32 n_all_q, pset_t *pset, uint32 **dfeat, uint32 n_dfeat, uint32 split_min, uint32 split_max, float32 split_thr, float32 mwfloor) { dtree_t *s_tree; uint32 i; dtree_node_t *b_n, *root; s_tree = ckd_calloc(1, sizeof(dtree_t)); s_tree->node = ckd_calloc(2*split_max + 1, sizeof(dtree_node_t)); s_tree->n_node = 0; s_tree->node[0].node_id = 0; s_tree->n_node = 1; root = &s_tree->node[0]; mk_node(root, 0, id, n_id, mixw, means, vars, veclen, n_model, n_state, n_stream, n_density, stwt, mwfloor); set_best_quest(root, mixw, means, vars, veclen, n_model, n_state, n_stream, n_density, stwt, all_q, n_all_q, pset, dfeat, n_dfeat, mwfloor); if (root->q == NULL) { /* No question found that is able to split node; can't go any further */ free_tree(s_tree); return NULL; } for (i = 0; i < split_max; i++) { b_n = best_leaf_node(root); if (b_n == NULL) { E_INFO("stop. leaf nodes are specific\n"); break; } /* DDDDDBUG The following criteria will fail if we use only likelihood and no likelihood increase */ if (b_n->wt_ent_dec <= 0) { E_INFO("stop. b_n->wt_ent_dec (%.3e) <= 0\n", b_n->wt_ent_dec); break; } if ((i > split_min) && (b_n->wt_ent_dec < split_thr * b_n->wt_ent)) { E_INFO("stop. b_n->wt_ent_dec (%.3e) < split_thr * b_n->wt_ent (%.3e)\n", b_n->wt_ent_dec, b_n->wt_ent * split_thr); break; } split_node(s_tree, b_n->node_id, mixw, means, vars, veclen, n_model, n_state, n_stream, n_density, stwt, all_q, n_all_q, pset, dfeat, n_dfeat, mwfloor); } #if 1 E_INFO("Final simple tree\n"); print_tree(stderr, "|", root, pset, 0); fprintf(stderr, "\n"); #endif return s_tree; }
void OcTreePCL::insertScanUniform(const ScanNode& scan, double maxrange) { octomap::pose6d scan_pose (scan.pose); octomap::point3d origin (scan_pose.x(), scan_pose.y(), scan_pose.z()); // preprocess data -------------------------- octomap::point3d p; CountingOcTree free_tree (this->getResolution()); CountingOcTree occupied_tree(this->getResolution()); octomap::KeyRay ray; for (octomap::Pointcloud::iterator point_it = scan.scan->begin(); point_it != scan.scan->end(); point_it++) { p = scan_pose.transform(*point_it); bool is_maxrange = false; if ( (maxrange > 0.0) && ((p - origin).norm2() > maxrange) ) is_maxrange = true; if (!is_maxrange) { // free cells if (this->computeRayKeys(origin, p, ray)){ for(octomap::KeyRay::iterator it=ray.begin(); it != ray.end(); it++) { free_tree.updateNode(*it); } } // occupied cells occupied_tree.updateNode(p); } // end if NOT maxrange else { point3d direction = (p - origin).unit(); point3d new_end = origin + direction * maxrange; if (this->computeRayKeys(origin, new_end, ray)){ for(octomap::KeyRay::iterator it=ray.begin(); it != ray.end(); it++) { free_tree.updateNode(*it); } } } // end if maxrange } // end for all points std::list<OcTreeVolume> free_cells; free_tree.getLeafNodes(free_cells); std::list<OcTreeVolume> occupied_cells; occupied_tree.getLeafNodes(occupied_cells); // delete free cells if cell is also measured occupied for (std::list<OcTreeVolume>::iterator cellit = free_cells.begin(); cellit != free_cells.end();){ if ( occupied_tree.search(cellit->first) ) { cellit = free_cells.erase(cellit); } else { cellit++; } } // end for // insert data into tree ----------------------- for (std::list<OcTreeVolume>::iterator it = free_cells.begin(); it != free_cells.end(); it++) { updateNode(it->first, false); } for (std::list<OcTreeVolume>::iterator it = occupied_cells.begin(); it != occupied_cells.end(); it++) { updateNode(it->first, true); } // unsigned int num_thres = 0; // unsigned int num_other = 0; // calcNumThresholdedNodes(num_thres, num_other); // std::cout << "Inserted scan, total num of thresholded nodes: "<< num_thres << ", num of other nodes: "<< num_other << std::endl; }
dtree_t * read_final_tree(FILE *fp, pset_t *pset, uint32 n_pset) { dtree_t *out; dtree_node_t *node; uint32 n_node; char *s, str[128]; lineiter_t *ln = NULL; uint32 n_scan; uint32 i, node_id, node_id_y, node_id_n; comp_quest_t *q; float64 ent; float32 occ; int err; out = ckd_calloc(1, sizeof(dtree_t)); ln = lineiter_start_clean(fp); s = ln->buf; sscanf(s, "%s%n", str, &n_scan); if (strcmp(str, "n_node") == 0) { s += n_scan; sscanf(s, "%u", &n_node); } else { E_FATAL("Format error; expecting n_node\n"); } out->n_node = n_node; out->node = node = ckd_calloc(n_node, sizeof(dtree_node_t)); for (i = 0; i < n_node; i++) node[i].node_id = i; err = FALSE; while ((ln = lineiter_next(ln))) { s = ln->buf; sscanf(s, "%u%n", &node_id, &n_scan); s += n_scan; sscanf(s, "%s%n", str, &n_scan); s += n_scan; if (strcmp(str, "-") == 0) { node_id_y = NO_ID; } else { node_id_y = atoi(str); } sscanf(s, "%s%n", str, &n_scan); s += n_scan; if (strcmp(str, "-") == 0) { node_id_n = NO_ID; } else { node_id_n = atoi(str); } sscanf(s, "%le%n", &ent, &n_scan); s += n_scan; sscanf(s, "%e%n", &occ, &n_scan); s += n_scan; if ((node_id_y != NO_ID) && (node_id_y != NO_ID)) { q = (comp_quest_t *)ckd_calloc(1, sizeof(comp_quest_t)); if (s3parse_comp_quest(pset, n_pset, q, s) != S3_SUCCESS) { err = TRUE; } node[node_id].q = q; } else node[node_id].q = NULL; /* ck if internal node */ if ((node_id_y != NO_ID) && (node_id_y != NO_ID)) node[node_id].wt_ent_dec = ent; else node[node_id].wt_ent = ent; node[node_id].occ = occ; if ((node_id_y != NO_ID) && (node_id_y != NO_ID)) { node[node_id].y = &node[node_id_y]; node[node_id].n = &node[node_id_n]; node[node_id_y].p = node[node_id_n].p = &node[node_id]; } else { node[node_id].y = NULL; node[node_id].n = NULL; } } if (err == TRUE) { free_tree(out); out = NULL; } lineiter_free(ln); return out; }
static void dec_usage(INPparseNode *p) { if(p && --p->usecnt <= 0) free_tree(p); }
int partition(int nodenum, pNode splitnode, double *sumrisk, int n1, int n2, double *parm) { pNode me; double tempcp; int i, j, k; double tempcp2; double left_risk, right_risk; int left_split, right_split; double twt; double ttreat; int nleft, nright; int n; me = splitnode; n = n2 - n1; /* total number of observations */ if (nodenum > 1) { twt = 0; ttreat = 0; k = 0; for (i = n1; i < n2; i++) { j = rp.sorts[0][i]; /* any variable would do, use first */ if (j < 0) j = -(1 + j); /* if missing, value = -(1+ true index) */ rp.wtemp[k] = rp.wt[j]; rp.treatemp[k] = rp.treat[j]; rp.ytemp[k] = rp.ydata[j]; twt += rp.wt[j]; ttreat += rp.treat[j]; k++; } (*rp_eval) (n, rp.ytemp, rp.treatemp, me->response_est, &(me->risk), rp.wtemp, rp.max_y); me->num_obs = n; me->sum_wt = twt; me->sum_treat = ttreat; tempcp = me->risk; if (tempcp > me->complexity) tempcp = me->complexity; } else tempcp = me->risk; /* * Can I quit now ? */ if (me->num_obs < rp.min_split || tempcp <= rp.alpha || nodenum > rp.maxnode) { me->complexity = rp.alpha; *sumrisk = me->risk; /* * make sure the split doesn't have random pointers to somewhere * i.e., don't trust that whoever allocated memory set it to zero */ me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; return 0; } /* * Guess I have to do the split */ bsplit(me, n1, n2, parm); if (!me->primary) { /* * This is rather rare -- but I couldn't find a split worth doing */ me->complexity = rp.alpha; me->leftson = (pNode) NULL; me->rightson = (pNode) NULL; me->primary = (pSplit) NULL; me->surrogate = (pSplit) NULL; *sumrisk = me->risk; return 0; } #ifdef DEBUG print_tree(me, 2); #endif if (rp.maxsur > 0) surrogate(me, n1, n2); else me->surrogate = (pSplit) NULL; nodesplit(me, nodenum, n1, n2, &nleft, &nright); /* * split the leftson */ me->leftson = (pNode) CALLOC(1, nodesize); (me->leftson)->complexity = tempcp - rp.alpha; left_split = partition(2 * nodenum, me->leftson, &left_risk, n1, n1 + nleft, parm); /* * Update my estimate of cp, and split the right son. */ tempcp = (me->risk - left_risk) / (left_split + 1); tempcp2 = (me->risk - (me->leftson)->risk); if (tempcp < tempcp2) tempcp = tempcp2; if (tempcp > me->complexity) tempcp = me->complexity; me->rightson = (pNode) CALLOC(1, nodesize); (me->rightson)->complexity = tempcp - rp.alpha; right_split = partition(1 + 2 * nodenum, me->rightson, &right_risk, n1 + nleft, n1 + nleft + nright, parm); /* * Now calculate my actual C.P., which depends on children nodes, and * on grandchildren who do not collapse before the children. * The calculation is done assuming that I am the top node of the * whole tree, an assumption to be fixed up later. */ tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); /* Who goes first -- minimum of tempcp, leftson, and rightson */ if ((me->rightson)->complexity > (me->leftson)->complexity) { if (tempcp > (me->leftson)->complexity) { /* leftson collapses first */ left_risk = (me->leftson)->risk; left_split = 0; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->rightson)->complexity) { /* right one goes too */ right_risk = (me->rightson)->risk; right_split = 0; } } } else if (tempcp > (me->rightson)->complexity) { /* right hand child goes first */ right_split = 0; right_risk = (me->rightson)->risk; tempcp = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (tempcp > (me->leftson)->complexity) { /* left one goes too */ left_risk = (me->leftson)->risk; left_split = 0; } } me->complexity = (me->risk - (left_risk + right_risk)) / (left_split + right_split + 1); if (me->complexity <= rp.alpha) { /* * All was in vain! This node doesn't split after all. */ free_tree(me, 0); *sumrisk = me->risk; for (i = n1; i < n2; i++) { j = rp.sorts[0][i]; if (j < 0) j = -(1 + j); rp.which[j] = nodenum; /* revert to the old nodenumber */ } return 0; /* return # of splits */ } else { *sumrisk = left_risk + right_risk; return left_split + right_split + 1; } }
static void release_tree(INPparseNode *p) { if(p && p->usecnt <= 0) free_tree(p); }
int main(int argc, char *argv[]){ struct timeval start_time, end_time; /* structs for timer */ struct timezone zone; long int sec = 0, usec = 0; /* sec & microsec for the timer */ long int time_total_sec = 0; double time_total_msec = 0; long int time_total_usec = 0; int numTrans, pid, numItems, item, count, i , j; FILE *f_input, *f_utility, *f_output; int sizeDB; double minshare; int MV = 0; int ML = 0; int tempML = 0; if(argc != 7){ fprintf(stderr, "Usage: %s transactionDB utilityTable outputFile min_util MAXITEMS MAXCANDIDATES\n", argv[0]); exit(0); } f_input = fopen(argv[1], "r"); f_utility = fopen(argv[2], "r"); f_output = fopen(argv[3], "w"); minshare = atof(argv[4]); MAXITEMS = atoi(argv[5]); MAXITEMSETS = atoi(argv[6]); if((f_input == NULL) || (f_utility == NULL) || (f_output == NULL)){ fprintf( stdout, "ERROR[%s]: Can't open %s or %s or %s\n", argv[0], argv[1], argv[2], argv[3] ); fprintf( stderr, "ERROR[%s]: Can't open %s or %s or %s\n", argv[0], argv[1], argv[2], argv[3] ); fprintf( stderr, "ERROR[%s]: Can't open %s or %s or %s\n", argv[0], argv[1], argv[2], argv[3] ); exit( 0 ); } TreeNode *rootCk; rootCk = create_new_node(ROOT); TreeNode *rootRc; rootRc = create_new_node(ROOT); TreeNode *rootF; //the tree for frequent itemsets rootF = create_new_node(ROOT); TreeNode *setRc[MAXITEMSETS + 1]; TreeNode *setCk[MAXITEMSETS + 1]; TreeNode *setF[MAXITEMSETS + 1]; int sizeRc = 0; int sizeCk = 0; int sizeF = 0; double utility[MAXITEMS + 1]; double TutilItem[MAXITEMS + 1]; int items[MAXITEMS + 1]; double lmv[MAXITEMS + 1]; double TutilDB = 0; float cost; for(i = 1; i <= MAXITEMS; i ++){ utility[i] = 0; TutilItem[i] = 0; items[i] = 0; lmv[i] = 0; } printf("===== %s %s %s %f =====\n\n", argv[0], argv[1], argv[2], minshare); //record the time for the first db scan if(gettimeofday(&start_time, &zone) == -1){ fprintf(stderr, "gettimeofday error\n"); } fscanf(f_utility, "%d ", &numItems); for(i = 1; i <= numItems; i ++){ fscanf(f_utility, "%d %f ", &item, &cost); utility[item] = cost; } fscanf(f_input, "%d ", &numTrans); double transUtil = 0; //read the whole db once to get candidate 1-itemsets for(i = 1; i <= numTrans; i ++){ fscanf(f_input, "%d ", &pid); fscanf(f_input, "%d ", &numItems); //printf("\n%d %d ", pid, numItems); for(j = 1; j <= numItems; j ++){ fscanf(f_input, "%d %d ", &item, &count); //printf("item %d count %d ", item, count); transUtil += count * utility[item]; items[item] = 1; if(count > MV){ MV = count; } tempML ++; //printf("\nitem: %d count: %d", item, count); lmv[item] += count; } for(j = 1; j <= MAXITEMS; j ++){ if(items[j] == 1){ TutilItem[j] += transUtil; items[j] = 0; } } if(tempML > ML){ ML = tempML; } TutilDB += transUtil; transUtil = 0; tempML = 0; } sizeDB = numTrans; if(gettimeofday(&end_time, &zone) == 0){ if(end_time.tv_usec >= start_time.tv_usec){ sec = end_time.tv_sec - start_time.tv_sec; usec = end_time.tv_usec - start_time.tv_usec; }else{ sec = end_time.tv_sec - start_time.tv_sec - 1; usec = end_time.tv_usec - start_time.tv_usec + 1000000; } time_total_sec += sec; time_total_usec += usec; fprintf(stdout, "\n[FUM] Total runtime for first db scan is %ld sec. %.3f msec\n", sec, usec/1000.0); f_output = fopen( argv[3], "a" ); fprintf(f_output, "\n[FUM] Total runtime for first db scan is %ld sec. %.3f msec\n", sec, usec/1000.0); fclose( f_output ); } if(DEBUG){ for(i = 1; i <= MAXITEMS; i ++){ printf("\nutil item (%d): %f", i, utility[i]); } printf("\n"); for(i = 1; i <= MAXITEMS; i ++){ printf("\nitem (%d) has Tutil: %f", i, TutilItem[i]); } printf("\n\nTutilDB: %f Min_Lutil: %f\n", TutilDB, MIN_LUTIL(TutilDB, minshare)); printf("\nMV: %d ML: %d", MV, ML); } int isolated_itemsets[MAXITEMS + 1]; int size = 0; //get candidate 1-itemsets for(i = 1; i <= MAXITEMS; i ++){ if(utility[i] > 0){ if(TutilItem[i] >= MIN_LUTIL(TutilDB, minshare)){ Itemset I; init_itemset(&I); I.itemset[i] = 1; double lutil = (lmv[i] * utility[i]); insert(I.itemset, rootRc, 1, &sizeRc, i, setRc, lutil); free_itemset(&I); } else{ isolated_itemsets[size] = i; size ++; } } } //int k_prime = 1; //needed for other version of critical function int c; for(c = 0; c < sizeRc; c ++){ TreeNode *X = setRc[c]; if(X->lutil >= MIN_LUTIL(TutilDB, minshare)){ Itemset I; to_itemset(&I, X); insert(I.itemset, rootF, 1, &sizeF, 1, setF, X->lutil); free_itemset(&I); } //printf("\nCF: %f", CF(X->lutil, 1, MV, ML, k_prime)); //if(CF(X->lutil, 1, MV, ML, k_prime) < MIN_LUTIL(TutilDB, minshare)){ if(TutilItem[X->item] < MIN_LUTIL(TutilDB, minshare)){ remove_itemset(X, setRc, c); } } adjust_set(setRc, &sizeRc); if(PRT_CNT){ printf("\n\nIteration (%d)", 0); printf("\n|Ck| = %d", sizeCk); printf("\n|Rc| = %d", sizeRc); printf("\n|Fk| = %d", sizeF); } int k; int counterF = 0; boolean stop = false; int num_false_positives[MAXITEMSETS + 1]; int sizeFP = 0; //record the time for mining if(gettimeofday(&start_time, &zone) == -1){ fprintf(stderr, "gettimeofday error\n"); } for(k = 1; sizeRc > 0 && stop == false; k ++){ //apriori join + apriori prune generate(rootRc, setRc, sizeRc, setCk, &sizeCk, rootCk, k); if(PRT_MEM){ printf("\n\nIteration (%d)", k); int mem_counterCk = 0; count_nodes(rootCk, &mem_counterCk); printf("\nNode Count = [%d]", mem_counterCk); printf("\nNode Size = [%ld bytes]", sizeof(TreeNode)); printf("\nMemory space required for candidate tree = %ld bytes", mem_counterCk * sizeof(TreeNode)); } if(sizeCk > 0){ double TransUtil; rewind(f_input); fscanf(f_input, "%d ", &numTrans); for(i = 1; i <= numTrans; i ++){ fscanf(f_input, "%d ", &pid); fscanf(f_input, "%d ", &numItems); Transaction T; init_transaction(&T); for(j = 1; j <= numItems; j ++){ fscanf(f_input, "%d %d ", &item, &count); if(is_isolated_item(item, isolated_itemsets, size) == false){ T.itemset[item] += count; T.total_count += (count * utility[item]); } } TransUtil = T.total_count; for(j = 0; j < sizeCk; j ++){ TreeNode *X = setCk[j]; boolean in_transaction = true; double Lutil = 0; while(is_root(X) == false && in_transaction == true){ //first check if itemset in transaction if(T.itemset[X->item] == 0){ in_transaction = false; } Lutil += (T.itemset[X->item] * utility[X->item]); X = X->parent; } if(in_transaction){ TreeNode *last_node = setCk[j]; last_node->lutil += Lutil; last_node->CF += TransUtil; } } free_transaction(&T); } if(PRT_CNT){ printf("\n\nIteration (%d)", k); printf("\n|Ck| = %d", sizeCk); } if(PRT_FALSE_POS){ num_false_positives[k] = sizeCk; sizeFP ++; } for(c = 0; c < sizeCk; c ++){ TreeNode *X = setCk[c]; //print_itemset(X); //printf(" lutil: %f minlutil: %f\n", X->lutil, MIN_LUTIL(TutilDB, minshare)); if(X->lutil >= MIN_LUTIL(TutilDB, minshare)){ counterF ++; Itemset I; to_itemset(&I, X); insert(I.itemset, rootF, 1, &sizeF, 1, setF, X->lutil); free_itemset(&I); } if(X->CF < MIN_LUTIL(TutilDB, minshare)){ //if(CF(X->lutil, 1, MV, ML, k_prime) < MIN_LUTIL(TutilDB, minshare)){ remove_itemset(X, setCk, c); } } adjust_set(setCk, &sizeCk); copy_tree(rootCk, setCk, &sizeCk, rootRc, setRc, &sizeRc); if(PRT_CNT){ printf("\n|Rc| = %d", sizeRc); printf("\n|Fk| = %d", counterF); } if(PRT_FALSE_POS){ num_false_positives[k] -= counterF; printf("\nFalse Positives Iteration (%d): %d", k, num_false_positives[k]); } } else{ stop = true; } counterF = 0; } if(gettimeofday(&end_time, &zone) == 0){ if(end_time.tv_usec >= start_time.tv_usec){ sec = end_time.tv_sec - start_time.tv_sec; usec = end_time.tv_usec - start_time.tv_usec; }else{ sec = end_time.tv_sec - start_time.tv_sec - 1; usec = end_time.tv_usec - start_time.tv_usec + 1000000; } time_total_sec += sec; time_total_usec += usec; fprintf(stdout, "\n[FUM] Total runtime for mining is %ld sec. %.3f msec\n", sec, usec/1000.0); f_output = fopen( argv[3], "a" ); fprintf(f_output, "\n[FUM] Total runtime for mining is %ld sec. %.3f msec\n", sec, usec/1000.0); fclose( f_output ); } f_output = fopen(argv[3], "a"); if(PRT_FALSE_POS){ int total_FP = 0; for(k = 1; k <= sizeFP; k ++){ total_FP += num_false_positives[k]; } printf("\nTotal False Positives: %d", total_FP); } if(PRT_FP){ printf("\n\nFound (%d) ShFrequent Itemsets:", sizeF); fprintf(f_output, "\n\nFound (%d) ShFrequent Itemsets:", sizeF); print_frequent_itemset(setF, sizeF, f_output); } if(PRT_MEM){ int mem_counterF = 0; count_nodes(rootF, &mem_counterF); printf("\n\nNode Count = [%d]", mem_counterF); printf("\nNode Size = [%ld bytes]", sizeof(TreeNode)); printf("\nMemory space required for frequent itemset tree = %ld bytes", mem_counterF * sizeof(TreeNode)); } time_total_msec = time_total_usec / 1000.0; if(time_total_msec >= 1000){ time_total_sec += floor(time_total_msec/1000); time_total_msec = time_total_usec % 1000; } //printf("\ntime sec: %ld time msec: %.3lf time usec: %ld", time_total_sec, time_total_msec, time_total_usec); fprintf(stdout, "\n\n[FUM] Total (aggregate) runtime is %ld sec. %.3lf msec\n", time_total_sec, time_total_msec); fprintf(f_output, "\n\n[FUM] Total (aggregate) runtime is %ld sec. %.3lf msec\n", time_total_sec, time_total_msec); free_tree(setRc, &sizeRc); free_tree(setCk, &sizeCk); free_tree(setF, &sizeF); fclose(f_input); fclose(f_output); fclose(f_utility); printf("\n\nProcessing Complete\n"); return 0; }
int main(int argc, char **argv) { si_t si = make_si(1024); FILE *grammarfp = stdin, *yieldfp; FILE *tracefp = NULL; /* trace output */ FILE *summaryfp = stderr; /* end of parse stats output */ FILE *parsefp = stdout; /* parse trees */ FILE *probfp = NULL; /* max_neglog_prob */ chart_cell root_cell; grammar g; chart c; vindex terms; int maxsentlen = 0; int sentenceno = 0, parsed_sentences = 0, failed_sentences = 0; double sum_neglog_prob = 0; int sentfrom = 0; int sentto = 0; srand(RAND_SEED); /* seed random number generator */ if (argc<2 || argc>6) { fprintf(stderr, "%s yieldfile [maxsentlen [grammarfile [sentfrom sentto]]]\n", argv[0]); exit(EXIT_FAILURE); } if ((yieldfp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: Couldn't open yieldfile %s\n", argv[0], argv[1]); exit(EXIT_FAILURE); } if (argc >= 3) if (!sscanf(argv[2], "%d", &maxsentlen)) { fprintf(stderr, "%s: Couldn't parse maxsentlen %s\n", argv[0], argv[2]); exit(EXIT_FAILURE); } if (argc >= 4) if ((grammarfp = fopen(argv[3], "r")) == NULL) { fprintf(stderr, "%s: Couldn't open grammarfile %s\n", argv[0], argv[3]); exit(EXIT_FAILURE); } if (argc >= 6) { if (!sscanf(argv[4], "%d", &sentfrom)) { fprintf(stderr, "%s: Couldn't parse sentfrom %s\n", argv[0], argv[4]); exit(EXIT_FAILURE); } if (!sscanf(argv[5], "%d", &sentto)) { fprintf(stderr, "%s: Couldn't parse sentto %s\n", argv[0], argv[5]); exit(EXIT_FAILURE); } } g = read_grammar(grammarfp, si); /* write_grammar(tracefp, g, si); */ while ((terms = read_terms(yieldfp, si))) { sentenceno++; if (sentfrom && sentenceno < sentfrom) { vindex_free(terms); continue; } if (sentto && sentenceno > sentto) { vindex_free(terms); break; } /* skip if sentence is too long */ if (!maxsentlen || (int) terms->n <= maxsentlen) { size_t i; if (tracefp) { fprintf(tracefp, "\nSentence %d:\n", sentenceno); for (i=0; i<terms->n; i++) fprintf(tracefp, " %s", si_index_string(si, terms->e[i])); fprintf(tracefp, "\n"); } c = cky(*terms, g, si); /* fetch best root node */ root_cell = sihashcc_ref(CHART_ENTRY(c, 0, terms->n), g.root_label); if (root_cell) { tree parse_tree = bintree_tree(&root_cell->tree, si); double prob = (double) root_cell->prob; parsed_sentences++; assert(prob > 0.0); sum_neglog_prob -= log(prob); if (probfp) fprintf(probfp, "max_neglog_prob(%d, %g).\n", sentenceno, -log(prob)); if (tracefp) fprintf(tracefp, " Prob = %g\n", prob); if (parsefp) { write_tree(parsefp, parse_tree, si); fprintf(parsefp, "\n"); /* write_prolog_tree(parsefp, parse_tree, si); */ } free_tree(parse_tree); } else { failed_sentences++; if (tracefp) fprintf(tracefp, "Failed to parse\n"); if (parsefp) fprintf(parsefp, "parse_failure.\n"); } chart_free(c, terms->n); /* free the chart */ } else { /* sentence too long */ if (parsefp) fprintf(parsefp, "too_long.\n"); } vindex_free(terms); /* free the terms */ assert(trees_allocated == 0); assert(bintrees_allocated == 0); } free_grammar(g); si_free(si); if (summaryfp) { fprintf(summaryfp, "\n%d/%d = %g%% test sentences met the length criteron," " of which %d/%d = %g%% were parsed\n", parsed_sentences+failed_sentences, sentenceno, (double) (100.0 * (parsed_sentences+failed_sentences)) / sentenceno, parsed_sentences, parsed_sentences+failed_sentences, (double) (100.0 * parsed_sentences) / (parsed_sentences + failed_sentences)); fprintf(summaryfp, "Sum(-log prob) = %g\n", sum_neglog_prob); } /* check that everything has been deallocated */ /* printf("mmm_blocks_allocated = %ld\n", (long) mmm_blocks_allocated); */ assert(mmm_blocks_allocated == 0); exit(EXIT_SUCCESS); }
/* parses the FreeBSD ports INDEX file and creates a list of categories with dedicated ports, it also creates a meta-category "all", all lists and categories are sorted ascending. */ int parse_index() { FILE *fd; int c, i, readyToken; int pipes = 0; char tok[MAX_TOKEN]; extern Config config; extern List *lprts; extern List *lcats; extern void *exists; extern TNode *tcat; TNode *tdirs; TNode *tprt = NULL; List *lpdir = (List *)malloc(sizeof(List)); Port *p, *dprt; int num_of_inst_ports = 0; /* init */ p = NULL; tcat = NULL; lpdir->num_of_items = 0; lpdir->head = NULL; lpdir->tail = NULL; if ((fd = fopen(config.index_file, "r")) == NULL) return ERROR_OPEN_INDEX; /* error */ /* parse installed pkgs */ tdirs = parse_dir(config.inst_pkg_dir); /* parse ports dir and create list */ create_inorder_list(lpdir, parse_dir(config.ports_dir)); i = 0; readyToken = 0; /* token not ready */ while (feof(fd) == 0) { c = fgetc(fd); /* get next char */ switch (c) { case '|': /* next token */ readyToken = 1; /* ready token */ break; case '\n': /* end of port */ #if defined(__FreeBSD__) if (pipes != PORT_URL) /* seems to be some strange INDEX */ return ERROR_CORRUPT_INDEX; #endif readyToken = 1; /* tail token of port ready */ break; default: if ((pipes != PORT_CATEGORY) && (pipes != PORT_BUILD_DEPENDENCY) && (pipes != PORT_RUN_DEPENDENCY)) /* default, no port category, build dep or run dep modus */ tok[i++] = (char)c; break; } /* to speed up the parsing, we also parse categories here, I guessed if it'll be senseful to also parse BDEP and RDEP, but this makes no sense until there aren't all entries scanned */ if (pipes == PORT_CATEGORY) { /* port category mode */ if ((c == ' ') || (c == '|')) { if (i > 0) { /* maybe there're ports without a category */ tok[i] = '\0'; /* terminate current cat token */ add_list_item(p->lcats, add_category(tok, lpdir)); i = 0; /* reset i */ } } else { /* inside a token */ tok[i++] = (char)c; } } else if ((pipes == PORT_BUILD_DEPENDENCY) || (pipes == PORT_RUN_DEPENDENCY)) { /* port build/run dep mode */ if ((c == ' ') || (c == '|')) { if (i > 0) { /* maybe there're ports without a build dep */ tok[i] = '\0'; dprt = create_port(tok, tdirs); exists = NULL; tprt = add_tree_item(tprt, dprt, cmp_name); if (exists != NULL) { free_port(dprt); dprt = (Port *)((TNode *)exists)->item; } else { if (dprt->state >= STATE_INSTALLED) num_of_inst_ports++; } if (pipes == PORT_BUILD_DEPENDENCY) { add_list_item(p->lbdep, dprt); } else if (pipes == PORT_RUN_DEPENDENCY) { add_list_item(p->lrdep, dprt); } /* add also p to dprt->ldep, so that dprt knows the port for which dprt is a dependency, this helps seeking for unused ports */ add_list_item(dprt->ldep, p); i = 0; /* reset i */ } } else { /* inside a token */ tok[i++] = (char)c; } } if (readyToken == 1) { /* a new token is ready */ tok[i] = '\0'; /* terminate current token */ switch (pipes) { case PORT_NAME_VERSION: p = create_port(tok, tdirs); /* add the port */ exists = NULL; tprt = add_tree_item(tprt, p, cmp_name); if (exists != NULL) { free_port(p); p = (Port *)((TNode *)exists)->item; } else { if (p->state >= STATE_INSTALLED) num_of_inst_ports++; } break; case PORT_PATH: p->path = strdup(tok); break; #if defined(__FreeBSD__) case PORT_INSTALL_PREFIX: p->instpfx = strdup(tok); break; #endif case PORT_DESCR: p->descr = strdup(tok); break; case PORT_PKGDESCR: p->pathpkgdesc = strdup(tok); break; case PORT_MAINTAINER: p->maintainer = strdup(tok); break; #if defined(__FreeBSD__) case PORT_URL: p->url = strdup(tok); pipes = -1; break; #endif } readyToken = i = 0; /* token processed, not ready token anymore */ pipes++; } } fclose(fd); /* close INDEX file */ create_inorder_list(lcats, tcat); create_inorder_list(lprts, tprt); create_categories(num_of_inst_ports); free_tree(tdirs); /* finished */ return (0); }
int main(int argc,char **argv) { sint i,j,k,n,s; sint status; char c; char infile[FILENAMELEN+1]; char treefile[FILENAMELEN+1]; FILE *tree; sint maxres,*gapptr=NULL; IN_TREEPTR itree; double dscore; sint window; OPT opt; if(argc!=2) { fprintf(stdout,"Usage: %s input_aln\n",argv[0]); exit(1); } strcpy(infile,argv[1]); window=8; init_options(&opt); /* read in the sequences */ seq_input(infile,opt.explicit_type,FALSE,&mult_aln); if(mult_aln.nseqs<=0) { error("No sequences in %s\n",infile); exit(1); } /* count pairwise residue percent identities */ tmat = (double **) ckalloc( (mult_aln.nseqs+1) * sizeof (double *) ); for(i=0;i<mult_aln.nseqs;i++) tmat[i] = (double *)ckalloc( (mult_aln.nseqs+1) * sizeof (double) ); for (i=0;i<mult_aln.nseqs;i++) { for (j=i+1;j<mult_aln.nseqs;j++) { dscore = countid1(mult_aln.seqs[i],mult_aln.seqs[j]); tmat[j][i] = tmat[i][j] = (100.0 - dscore)/100.0; } } /* make a tree from the percent identities (used for sequence weighting) */ /*strcpy(treefile,infile); strcat(treefile,".ph");*/ strcpy(treefile,".ph"); if((tree = open_explicit_file(treefile))==NULL) exit(1); guide_tree(tree,mult_aln.seqs,mult_aln.nseqs, tmat, QUICKNJ); itree=(IN_TREEPTR)ckalloc(sizeof(IN_TREE)); status = read_tree(treefile, mult_aln.seqs, 0, mult_aln.nseqs,itree); if (status < 0) exit(1); seq_weight = calc_seq_weights(0,mult_aln.nseqs,itree,FALSE); free_tree(itree); remove(treefile); /* find the start and end positions of each sequence */ is = (sint *)ckalloc((mult_aln.nseqs+1) * sizeof(sint)); ie = (sint *)ckalloc((mult_aln.nseqs+1) * sizeof(sint)); for(s=0;s<mult_aln.nseqs;s++) { is[s]=0; ie[s] = mult_aln.seqs[s].len; for (i=0; i<mult_aln.seqs[s].len; i++) { c = mult_aln.seqs[s].data[i]; if (!isalpha(c)) is[s]++; else break; } for (i=mult_aln.seqs[s].len-1; i>=0; i--) { c = mult_aln.seqs[s].data[i]; if (!isalpha(c)) ie[s]--; else break; } } matrix.format=0; maxres = get_cl_matrix(FALSE, gon250mt, gapptr, TRUE, 100, &matrix); /* get the global core blocks */ all_blocks(window); for(i=0;i<mult_aln.nseqs;i++) ckfree(tmat[i]); ckfree(tmat); }