//Dumps values from the tree to an array void array_dump(tnode_t *tree, int **arraypos) { //Return if bottom has been reached if(tree == NULL) { return; } //Left side of tree array_dump(tree->lchild, arraypos); //Write to array **arraypos = tree->data; (*arraypos)++; //Right side of tree array_dump(tree->rchild, arraypos); }
NBR_API int nbr_array_free(ARRAY ad, void *p) { ASSERT(ad); array_t *a = ad; element_t *e = array_get_top_address(p); //TRACE( "free: p=0x%08x, 0x%08x, %s, top=0x%08x, elm=0x%08x\n", p, e, // element_is_inuse(e) ? "use" : "empty", array_get_top(a), array_get_top_address(p)); //, array_get_elm_size(a->size), sizeof(element_t), sizeof(a->first->data) ); if (!element_is_inuse(e)) { ARRAY_ERROUT(ERROR,INVPTR,"not used:%p/%p", ad, p); //array_dump(a); ASSERT(FALSE); return LASTERR; } if (element_get_flag(e, ELEM_FROM_HEAP) || array_check_address(a, e)) { ARRAY_WRITE_LOCK(a,LASTERR); array_free_elm(a, e); a->use--; ASSERT((a->use >= 0 && a->max >= a->use) || array_count_usenum(a)); ARRAY_WRITE_UNLOCK(a); return NBR_OK; } ARRAY_ERROUT(ERROR,INVPTR,"align:%p/%p", ad, p); array_dump(a); ASSERT(FALSE); return LASTERR; }
int main(int argc, char *argv[]) { char option = '\0'; char *filepath = NULL; unsigned int length = 0; int *array = NULL; int *original_array = NULL; struct sorting_stats ops; /* parse the filepath given in command line arguments */ filepath = parse_filepath(argc, argv); /* parse the array given in the input file */ array = array_from_file(filepath, &length); /* save a copy of array used to make some checks later */ original_array = array_duplicate(array, length); /* print a simple menu and do the actual sorting */ do { option = print_menu(); switch (option) { case INSERTION_SORT: ops = insertion_sort(array, length); break; case SELECTION_SORT: ops = selection_sort(array, length); break; case QUICK_SORT: ops = quick_sort(array, length); break; case EXIT: printf("Exiting.\n"); return (EXIT_SUCCESS); default: printf("\n\"%c\" is invalid. Please choose a valid option." "\n\n", option); } } while (!is_valid_option(option)); /* show the ordered array in the screen */ array_dump(array, length); // show the comparation of the algorithm printf("Comparisons: %d \n",(int)ops.comps); printf("Swaps: %d \n",(int)ops.swaps); /* check if it is sorted */ assert(array_is_sorted(array, length)); /* check if it is a permutation of original */ assert(array_is_permutation_of(array, original_array, length)); /* destroy array */ array_destroy(array); array_destroy(original_array); return (EXIT_SUCCESS); }
int* to_array(tnode_t *tree) { //Allocate memory for the array int *arraypos, *retval; arraypos = retval = malloc(sizeof(int)*size(tree)); if(retval == NULL) { printf("malloc failed"); exit(EXIT_FAILURE); } //Write to the array array_dump(tree, &arraypos); return retval; }
/*{{{ export_point(transform_info_ptr tinfo) {*/ METHODDEF DATATYPE * export_point(transform_info_ptr tinfo) { struct export_point_info *exp_info=(struct export_point_info *)tinfo->methods->local_storage; transform_argument *args=tinfo->methods->arguments; array_dump_format format=(args[ARGS_MATLAB].is_set ? ARRAY_MATLAB : ARRAY_ASCII); int channel, itempart, i; double *channelpos; array myarray, newarray; if (exp_info->pointno<0 || exp_info->pointno>=tinfo->nr_of_points) { ERREXIT1(tinfo->emethods, "export_point: Point number %d is out of range.\n", MSGPARM(exp_info->pointno)); } TRACEMS2(tinfo->emethods, 1, "export_point: Exporting point number %d to file %s\n", MSGPARM(exp_info->pointno), MSGPARM(args[ARGS_OFILE].arg.s)); if (args[ARGS_CLOSE].is_set) export_file_open(tinfo); tinfo_array(tinfo, &myarray); myarray.current_element=exp_info->pointno; if (exp_info->channelcoords_to_display==0 && tinfo->itemsize==1) { /*{{{ Don't need to build new array, just access tinfo*/ newarray=myarray; array_transpose(&newarray); array_setto_vector(&newarray); /* Select only one element */ array_transpose(&newarray); /*}}} */ } else { /*{{{ Build new array containing the data*/ newarray.nr_of_vectors=myarray.nr_of_vectors; newarray.nr_of_elements=exp_info->channelcoords_to_display+tinfo->itemsize; newarray.element_skip=1; if (array_allocate(&newarray)==NULL) { ERREXIT(tinfo->emethods, "export_point: Can't allocate output array\n"); } for (channel=0; channel<tinfo->nr_of_channels; channel++) { channelpos=tinfo->probepos+3*channel; for (i=0; i<exp_info->channelcoords_to_display; i++) { array_write(&newarray, channelpos[i]); } myarray.current_vector=channel; for (itempart=0; itempart<tinfo->itemsize; itempart++) { array_use_item(&myarray, itempart); array_write(&newarray, READ_ELEMENT(&myarray)); } } /*}}} */ } array_dump(exp_info->file, &newarray, format); if (newarray.nr_of_elements>1) array_free(&newarray); if (args[ARGS_CLOSE].is_set) export_file_close(tinfo); return tinfo->tsdata; }
int main(int argc, char **argv) { FILE *fp; surfspline_desc sspline; int err, npoints, itempart=0; int binary_output=FALSE, matlab_output=FALSE, mtv_output=FALSE, interpolate_only=FALSE; float fbuf, external_value=0.0; DATATYPE f, x, y, xmin, xmax, ymin, ymax, xstep, ystep; array index; pid_t pid; char outname[L_tmpnam], inname[L_tmpnam], **inargs, *infile; /*{{{ Read command line args*/ for (inargs=argv+1; inargs-argv<argc && **inargs=='-'; inargs++) { switch(inargs[0][1]) { case 'B': binary_output=TRUE; break; case 'I': if (inargs[0][2]!='\0') { itempart=atoi(*inargs+2); } break; case 'i': interpolate_only=TRUE; if (inargs[0][2]!='\0') { external_value=atof(*inargs+2); } break; case 'M': matlab_output=TRUE; break; case 'm': mtv_output=TRUE; break; default: fprintf(stderr, "%s: Ignoring unknown option %s\n", argv[0], *inargs); continue; } } if (argc-(inargs-argv)!=3) { fprintf(stderr, "Usage: %s array_filename degree npoints\n" "Options:\n" " -Iitem: Use item number item as z-axis (2+item'th column); default: 0\n" " -i[value]: Interpolate only; set external values to value (default: 0.0)\n" " -B: Binary output (gnuplot floats)\n" " -M: Matlab output (x, y vectors and z matrix)\n" " -m: Plotmtv output\n" , argv[0]); return -1; } infile= *inargs++; if ((fp=fopen(infile, "r"))==NULL) { fprintf(stderr, "Can't open %s\n", infile); return -2; } array_undump(fp, &sspline.inpoints); fclose(fp); if (sspline.inpoints.message==ARRAY_ERROR) { fprintf(stderr, "Error in array_undump\n"); return -3; } if (sspline.inpoints.nr_of_elements<3+itempart) { fprintf(stderr, "Not enough columns in array %s\n", *(inargs-1)); return -3; } sspline.degree=atoi(*inargs++);; npoints=atoi(*inargs++); /*}}} */ /*{{{ Get the index of qhull point pairs by running qhull*/ tmpnam(outname); tmpnam(inname); if ((pid=fork())==0) { /* I'm the child */ #define LINEBUF_SIZE 128 char linebuf[LINEBUF_SIZE]; array tmp_array; /*{{{ Dump xy positions to tmp file inname*/ tmp_array.nr_of_elements=2; tmp_array.nr_of_vectors=sspline.inpoints.nr_of_vectors; tmp_array.element_skip=1; if (array_allocate(&tmp_array)==NULL) { fprintf(stderr, "Error allocating tmp_array\n"); return -4; } array_reset(&sspline.inpoints); do { array_write(&tmp_array, array_scan(&sspline.inpoints)); array_write(&tmp_array, array_scan(&sspline.inpoints)); array_nextvector(&sspline.inpoints); } while (tmp_array.message!=ARRAY_ENDOFSCAN); if ((fp=fopen(inname, "w"))==NULL) { fprintf(stderr, "Can't open %s\n", inname); return -5; } array_dump(fp, &tmp_array, ARRAY_ASCII); fclose(fp); array_free(&tmp_array); /*}}} */ snprintf(linebuf, LINEBUF_SIZE, "qhull C0 i b <%s >%s", inname, outname); execl("/bin/sh", "sh", "-c", linebuf, 0); #undef LINEBUF_SIZE } else { wait(NULL); } unlink(inname); if ((fp=fopen(outname, "r"))==NULL) { fprintf(stderr, "Can't open %s\n", outname); return -6; } array_undump(fp, &index); fclose(fp); unlink(outname); /*}}} */ /*{{{ Find min, max and mean coordinates*/ array_transpose(&sspline.inpoints); array_reset(&sspline.inpoints); xmin=array_min(&sspline.inpoints); ymin=array_min(&sspline.inpoints); array_reset(&sspline.inpoints); xmax=array_max(&sspline.inpoints); ymax=array_max(&sspline.inpoints); array_reset(&sspline.inpoints); xmean=array_mean(&sspline.inpoints); ymean=array_mean(&sspline.inpoints); xstep=(xmax-xmin)/npoints; ystep=(ymax-ymin)/npoints; array_transpose(&sspline.inpoints); array_reset(&sspline.inpoints); /*}}} */ /*{{{ Copy itempart to 3rd location if necessary*/ /* Note: For this behavior it is essential that array_surfspline * will accept vectors of any size >=3 and only process the first three * elements ! */ if (itempart>0) { DATATYPE hold; do { sspline.inpoints.current_element=2+itempart; hold=READ_ELEMENT(&sspline.inpoints); sspline.inpoints.current_element=2; WRITE_ELEMENT(&sspline.inpoints, hold); array_nextvector(&sspline.inpoints); } while (sspline.inpoints.message!=ARRAY_ENDOFSCAN); } /*}}} */ /*{{{ Do surface spline*/ if ((err=array_surfspline(&sspline))!=0) { fprintf(stderr, "Error %d in array_surfspline\n", err); return err; } xmin-=xstep; xmax+=2*xstep; ymin-=ystep; ymax+=2*ystep; if (binary_output) { /*{{{ Gnuplot binary output*/ int n_xval=(xmax-xmin)/xstep+1, n; /* Number of x values */ fbuf=n_xval; fwrite(&fbuf, sizeof(float), 1, stdout); for (fbuf=xmin, n=0; n<n_xval; fbuf+=xstep, n++) { /* x values */ fwrite(&fbuf, sizeof(float), 1, stdout); } for (y=ymin; y<=ymax; y+=ystep) { fbuf=y; fwrite(&fbuf, sizeof(float), 1, stdout); for (x=xmin, n=0; n<n_xval; x+=xstep, n++) { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } fbuf=f; fwrite(&fbuf, sizeof(float), 1, stdout); } } /*}}} */ } else if (matlab_output) { /*{{{ Matlab output*/ array xm, ym, zm; xm.nr_of_elements=ym.nr_of_elements=1; xm.nr_of_vectors=zm.nr_of_elements=(xmax-xmin)/xstep+1; ym.nr_of_vectors=zm.nr_of_vectors=(ymax-ymin)/ystep+1; xm.element_skip=ym.element_skip=zm.element_skip=1; array_allocate(&xm); array_allocate(&ym); array_allocate(&zm); if (xm.message==ARRAY_ERROR || ym.message==ARRAY_ERROR || zm.message==ARRAY_ERROR) { fprintf(stderr, "Error allocating output arrays\n"); return -7; } x=xmin; do { array_write(&xm, x); x+=xstep; } while (xm.message!=ARRAY_ENDOFSCAN); y=ymin; do { array_write(&ym, y); x=xmin; do { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } array_write(&zm, f); x+=xstep; } while (zm.message==ARRAY_CONTINUE); y+=ystep; } while (zm.message!=ARRAY_ENDOFSCAN); array_dump(stdout, &xm, ARRAY_MATLAB); array_dump(stdout, &ym, ARRAY_MATLAB); array_dump(stdout, &zm, ARRAY_MATLAB); array_free(&xm); array_free(&ym); array_free(&zm); /*}}} */ } else if (mtv_output) { /*{{{ Plotmtv output*/ array zm; DATATYPE zmin=FLT_MAX, zmax= -FLT_MAX; zm.nr_of_elements=(xmax-xmin)/xstep+1; zm.nr_of_vectors=(ymax-ymin)/ystep+1; zm.element_skip=1; array_allocate(&zm); if (zm.message==ARRAY_ERROR) { fprintf(stderr, "Error allocating output arrays\n"); return -7; } y=ymin; do { x=xmin; do { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } array_write(&zm, f); if (f<zmin) zmin=f; if (f>zmax) zmax=f; x+=xstep; } while (zm.message==ARRAY_CONTINUE); y+=ystep; } while (zm.message!=ARRAY_ENDOFSCAN); /*{{{ Print file header*/ printf( "# Output of Spline_Gridder (C) Bernd Feige 1995\n\n" "$ DATA=CONTOUR\n\n" "%% toplabel = \"Spline_Gridder output\"\n" "%% subtitle = \"File: %s\"\n\n" "%% interp = 0\n" "%% contfill = on\n" "%% meshplot = off\n\n" "%% xmin = %g xmax = %g\n" "%% ymin = %g ymax = %g\n" "%% zmin = %g zmax = %g\n" "%% nx = %d\n" "%% ny = %d\n" , infile, xmin, xmax, ymin, ymax, zmin, zmax, zm.nr_of_elements, zm.nr_of_vectors); /*}}} */ array_dump(stdout, &zm, ARRAY_MATLAB); array_free(&zm); /*}}} */ } else { /*{{{ Gnuplot output*/ for (x=xmin; x<=xmax; x+=xstep) { for (y=ymin; y<=ymax; y+=ystep) { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } printf("%g %g %g\n", x, y, f); } printf("\n"); } /*}}} */ } /*}}} */ return 0; }
/*{{{ project_init(transform_info_ptr tinfo)*/ METHODDEF void project_init(transform_info_ptr tinfo) { struct project_args_struct *project_args=(struct project_args_struct *)tinfo->methods->local_storage; transform_argument *args=tinfo->methods->arguments; transform_info_ptr side_tinfo= &project_args->side_tinfo; array *vectors= &project_args->vectors; growing_buf buf; #define BUFFER_SIZE 80 char buffer[BUFFER_SIZE]; side_tinfo->methods= &project_args->methods; side_tinfo->emethods=tinfo->emethods; select_readasc(side_tinfo); growing_buf_init(&buf); growing_buf_allocate(&buf, 0); if (args[ARGS_FROMEPOCH].is_set) { snprintf(buffer, BUFFER_SIZE, "-f %ld ", args[ARGS_FROMEPOCH].arg.i); growing_buf_appendstring(&buf, buffer); } if (args[ARGS_EPOCHS].is_set) { project_args->epochs=args[ARGS_EPOCHS].arg.i; if (project_args->epochs<=0) { ERREXIT(tinfo->emethods, "project_init: The number of epochs must be positive.\n"); } } else { project_args->epochs=1; } snprintf(buffer, BUFFER_SIZE, "-e %d ", project_args->epochs); growing_buf_appendstring(&buf, buffer); growing_buf_appendstring(&buf, args[ARGS_PROJECTFILE].arg.s); if (!buf.can_be_freed || !setup_method(side_tinfo, &buf)) { ERREXIT(tinfo->emethods, "project_init: Error setting readasc arguments.\n"); } project_args->points=(args[ARGS_POINTS].is_set ? args[ARGS_POINTS].arg.i : 1); project_args->nr_of_item=(args[ARGS_NROFITEM].is_set ? args[ARGS_NROFITEM].arg.i : 0); project_args->orthogonalize_vectors_first=args[ARGS_ORTHOGONALIZE].is_set; if (args[ARGS_SUBSPACE].is_set) { project_args->project_mode=PROJECT_MODE_SSP; } else if (args[ARGS_SUBTRACT_SUBSPACE].is_set) { project_args->project_mode=PROJECT_MODE_SSPS; } else if (args[ARGS_MULTIPLY].is_set) { project_args->project_mode=PROJECT_MODE_MULTIPLY; } else { project_args->project_mode=PROJECT_MODE_SCALAR; } if (args[ARGS_CHANNELNAMES].is_set) { project_args->channel_list=expand_channel_list(tinfo, args[ARGS_CHANNELNAMES].arg.s); if (project_args->channel_list==NULL) { ERREXIT(tinfo->emethods, "project_init: Zero channels were selected by -N!\n"); } } else { project_args->channel_list=NULL; } (*side_tinfo->methods->transform_init)(side_tinfo); /*{{{ Read first project_file epoch and allocate vectors array accordingly*/ if ((side_tinfo->tsdata=(*side_tinfo->methods->transform)(side_tinfo))==NULL) { ERREXIT(tinfo->emethods, "project_init: Can't get the first project epoch.\n"); } if (project_args->project_mode==PROJECT_MODE_MULTIPLY) { /* Save channel names and positions for later */ project_args->save_side_tinfo.nr_of_channels=side_tinfo->nr_of_channels; copy_channelinfo(&project_args->save_side_tinfo, side_tinfo->channelnames, side_tinfo->probepos); } if (project_args->points==0) project_args->points=side_tinfo->nr_of_points; if (project_args->points>side_tinfo->nr_of_points) { ERREXIT1(tinfo->emethods, "project_init: There are only %d points in the project_file epoch.\n", MSGPARM(side_tinfo->nr_of_points)); } if (args[ARGS_AT_XVALUE].is_set) { project_args->nr_of_point=find_pointnearx(side_tinfo, (DATATYPE)atof(args[ARGS_NROFPOINT].arg.s)); } else { project_args->nr_of_point=gettimeslice(side_tinfo, args[ARGS_NROFPOINT].arg.s); } vectors->nr_of_vectors=project_args->epochs*project_args->points; vectors->nr_of_elements=side_tinfo->nr_of_channels; vectors->element_skip=1; if (array_allocate(vectors)==NULL) { ERREXIT(tinfo->emethods, "project_init: Error allocating vectors memory\n"); } /*}}} */ do { /*{{{ Copy [points] vectors from project_file to vectors array*/ array indata; int point; if (vectors->nr_of_elements!=side_tinfo->nr_of_channels) { ERREXIT(side_tinfo->emethods, "project_init: Varying channel numbers in project file!\n"); } if (project_args->nr_of_point>=side_tinfo->nr_of_points) { ERREXIT2(side_tinfo->emethods, "project_init: nr_of_point=%d, nr_of_points=%d\n", MSGPARM(project_args->nr_of_point), MSGPARM(side_tinfo->nr_of_points)); } if (project_args->nr_of_item>=side_tinfo->itemsize) { ERREXIT2(side_tinfo->emethods, "project_init: nr_of_item=%d, itemsize=%d\n", MSGPARM(project_args->nr_of_item), MSGPARM(side_tinfo->itemsize)); } for (point=0; point<project_args->points; point++) { tinfo_array(side_tinfo, &indata); array_transpose(&indata); /* Vector = map */ if (vectors->nr_of_elements!=indata.nr_of_elements) { ERREXIT(side_tinfo->emethods, "project_init: vector size doesn't match\n"); } indata.current_vector=project_args->nr_of_point+point; array_setto_vector(&indata); array_use_item(&indata, project_args->nr_of_item); array_copy(&indata, vectors); } /*}}} */ free_tinfo(side_tinfo); } while ((side_tinfo->tsdata=(*side_tinfo->methods->transform)(side_tinfo))!=NULL); if (vectors->message!=ARRAY_ENDOFSCAN) { ERREXIT1(tinfo->emethods, "project_init: Less than %d epochs in project file\n", MSGPARM(vectors->nr_of_vectors)); } /*{{{ Set unused channels to zero if requested*/ if (args[ARGS_ZERO_UNUSEDCOEFFICIENTS].is_set) { if (project_args->channel_list!=NULL) { do { do { if (is_in_channellist(vectors->current_element+1, project_args->channel_list)) { array_advance(vectors); } else { array_write(vectors, 0.0); } } while (vectors->message==ARRAY_CONTINUE); } while (vectors->message!=ARRAY_ENDOFSCAN); } else { ERREXIT(tinfo->emethods, "project_init: Option -z only makes sense in combination with -N!\n"); } } /*}}} */ /*{{{ De-mean vectors if requested*/ if (args[ARGS_CORRELATION].is_set) { do { DATATYPE mean=0.0; int nrofaverages=0; do { if (project_args->channel_list!=NULL && !is_in_channellist(vectors->current_element+1, project_args->channel_list)) { array_advance(vectors); } else { mean+=array_scan(vectors); nrofaverages++; } } while (vectors->message==ARRAY_CONTINUE); mean/=nrofaverages; array_previousvector(vectors); do { array_write(vectors, READ_ELEMENT(vectors)-mean); } while (vectors->message==ARRAY_CONTINUE); } while (vectors->message!=ARRAY_ENDOFSCAN); } /*}}} */ /*{{{ Orthogonalize vectors if requested*/ if (project_args->orthogonalize_vectors_first) { array_make_orthogonal(vectors); if (vectors->message==ARRAY_ERROR) { ERREXIT(tinfo->emethods, "project_init: Error orthogonalizing projection vectors\n"); } } /*}}} */ if (!args[ARGS_NONORMALIZATION].is_set) { /*{{{ Normalize vectors*/ do { DATATYPE length=0.0; do { if (project_args->channel_list!=NULL && !is_in_channellist(vectors->current_element+1, project_args->channel_list)) { array_advance(vectors); } else { const DATATYPE hold=array_scan(vectors); length+=hold*hold; } } while (vectors->message==ARRAY_CONTINUE); length=sqrt(length); array_previousvector(vectors); if (length==0.0) { ERREXIT1(tinfo->emethods, "project_init: Vector %d has zero length !\n", MSGPARM(vectors->current_vector)); } array_scale(vectors, 1.0/length); } while (vectors->message!=ARRAY_ENDOFSCAN); /*}}} */ } /*{{{ Dump vectors if requested*/ if (args[ARGS_DUMPVECTORS].is_set) { FILE * const fp=fopen(args[ARGS_DUMPVECTORS].arg.s, "w"); if (fp==NULL) { ERREXIT1(tinfo->emethods, "project_init: Error opening output file >%s<.\n", MSGPARM(args[ARGS_DUMPVECTORS].arg.s)); } array_transpose(vectors); /* We want one vector to be one column */ array_dump(fp, vectors, ARRAY_MATLAB); array_transpose(vectors); fclose(fp); } /*}}} */ (*side_tinfo->methods->transform_exit)(side_tinfo); free_methodmem(side_tinfo); growing_buf_free(&buf); tinfo->methods->init_done=TRUE; }