/* * listshuffle() */ LIST *listshuffle(LIST * head) { LIST **array, *newhead = NULL; int n, i = 0, val; struct timeval tv; gettimeofday(&tv, NULL); srandom(tv.tv_usec); n = listcnt(head); array = list2array(head); while (i < n) { val = random() % n; if (array[val]) { newhead = listprep(newhead, array[val]); array[val] = NULL; i++; } } G_free(array); return newhead; }
static List *sort(List *s) { size_t nel = listnel(s); if (nel > 1) { char **a; List *t; qsort(a = list2array(s, FALSE), nel, sizeof(char *), starstrcmp); for (t = s; t != NULL; t = t->n) t->w = *a++; } return s; }
int bucketsort(void **a, size_t n, keyaccessor_t key, indexer_t idx, comparator_t cmp) { _cons_t *lists = (_cons_t *) malloc(n * sizeof(_cons_t)); if (!lists) { perror("malloc failed!"); return -1; } #if BUCKETSORT_PROFILE when = dtime(); array2list(a, lists, n); ptime(); when = dtime(); list_t l = bucketsort_l(lists, key, idx, cmp); ptime(); when = dtime(); list2array(l, a, n); ptime(); #else array2list(a, lists, n); list_t l = bucketsort_l(lists, key, idx, cmp); list2array(l, a, n); #endif free(lists); return 0; }
int bucketsort(void **a, size_t n, keyaccessor_t key, indexer_t idx, comparator_t cmp) { _cons_t *lists = (_cons_t *) malloc(n * sizeof(_cons_t)); if (!lists) { perror("malloc failed!"); return -1; } array2list(a, lists, n); list_t l = bucketsort_l(lists, key, idx, cmp); list2array(l, a, n); free(lists); return 0; }
static void set_instance_fields(struct xcl_context *xc, struct ML *mlp) { const char *lastw = ""; int i; static char formbuf[128], normbuf[128]; List *parts = list_create(LIST_SINGLE); *formbuf = *normbuf = '\0'; for (i = 0; i < mlp->matches_used; ++i) { /* Should we be discriminating about which match of matches[i].matches[] we are using for this? */ struct f2 *lform = mlp->matches[i].matching_f2s[0]; struct f2 *clone = mb_new(xc->sigs->mb_f2s); /* This is a shallow clone; we only need it so we can set the flags locally */ *clone = *lform; list_add(parts, lform); if (strcmp(lastw,mlp->matches[i].lp->ref)) { if (*formbuf) strcat(formbuf, " "); strcat(formbuf,(char*)lform->form); } else BIT_SET(lform->flags,F2_FLAGS_SAME_REF); lastw = mlp->matches[i].lp->ref; if (lform->norm) { if (*normbuf) strcat(normbuf, " "); strcat(normbuf,(char*)lform->norm); } } mlp->matches[0].psu_form->form = (unsigned char*)formbuf; mlp->matches[0].psu_form->norm = (unsigned char*)normbuf; mlp->matches[0].psu_form->file = (unsigned char*)mlp->matches[0].lp->f->file; mlp->matches[0].psu_form->lnum = mlp->matches[0].lp->f->lnum; mlp->matches[0].psu_form->parts = (struct f2**)list2array(parts); list_free(parts, NULL); }
void getpars(char *procparfile,struct data *d) { /* Null the data structure */ nulldata(d); /* Copy procpar filename */ if ((d->procpar = (char *)malloc((strlen(procparfile)+1)*sizeof(char))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); strcpy(d->procpar,procparfile); /* Get the parameters, some attributes, and their values */ getprocparpars(procparfile,&d->p); /* Set and fill the 'pars' array structure. We convert array to an array of pars in the 'pars' array structure. We also figure how often each par cycles as the expt is run */ setarray(&d->p,&d->a); /* Convert sviblist to an array of pars in the 'pars' sviblist structure */ list2array("sviblist",&d->p,&d->s); }
void setarray(struct pars *p,struct pars *a) { int i,j,k,n; int joint; double *cycles; char str[MAXPATHLEN]; if (!list2array("array",p,a)) return; /* Take a copy of the array string */ strcpy(str,*sval("array",p)); /* Fill with parameter names from the array string */ /* 'array' is a string, so we will use member 'd' to hold a 'joint' index with which we can track parameters that are jointly arrayed */ /* Allocate memory for a->d */ if ((a->d = (double **)malloc(sizeof(double *))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); if ((a->d[0] = (double *)malloc(*a->nvals*sizeof(double))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); /* Initialise a->d */ for (i=0;i<*a->nvals;i++) a->d[0][i]=0; joint=0; j=0; k=0; for (i=0;i<strlen(str);i++) { if (str[i] == '(') { i++; a->d[0][j]=1; joint=1; } if (str[i] == ')') { i++; joint=0; } if (str[i] == ',') { if (joint) { a->d[0][j]++; a->d[0][j+1]=a->d[0][j]; /* for a ',' j+1 will always exist */ } a->s[0][j++][k]=0; /* NULL terminate */ k=0; } else a->s[0][j][k++]=str[i]; } a->s[0][j][k]=0; /* NULL terminate */ /* As such: The 'joint' index is 0 for parameters that are not jointly arrayed. Arrayed parameters generate indices as follows: array 'joint' index parameter a b c d e (a,b),c,d,e : 2 2 0 0 0 a,(b,c),d,e : 0 2 2 0 0 (a,b,c),d,e : 2 3 3 0 0 a,(b,c,d),e : 0 2 3 3 0 a,(b,c,d,e) : 0 2 3 4 4 a,(b,c),(d,e) : 0 2 2 2 2 (a,b,c,d,e) : 2 3 4 5 5 Starting from the last parameter (which cycles the quickest) we can now easily figure how many parameters are jointly arrayed */ /* Convert the 'joint' index to 'cycles' which describes the number of volumes over which the parameter cycles */ /* Allocate for a->nvals 'cycles' */ if ((cycles = (double *)malloc(*a->nvals*sizeof(double))) == NULL) nomem(__FILE__,__FUNCTION__,__LINE__); /* Initialise cycles and joint for the last parameter */ cycles[*a->nvals-1]=1; if (a->d[0][*a->nvals-1] > 0) joint=1; else joint=0; for (i=*a->nvals-2;i>=0;i--) { if (joint) { n=a->d[0][i+1]-1; for (j=0;j<n;j++) { /* was for (j=0;j<a->d[0][i+1]-1;j++) { */ cycles[i]=cycles[i+1]; i--; } i++; joint=0; } else { cycles[i]=cycles[i+1]*nvals(a->s[0][i+1],p); if (a->d[0][i] > 0) joint=1; } } /* Copy cycles to a->d */ for (i=0;i<*a->nvals;i++) a->d[0][i]=cycles[i]; #ifdef DEBUG fprintf(stdout,"\n%s: %s()\n",__FILE__,__FUNCTION__); printarray(a,*sval("array",p)); #endif }
extern void exec(List *s, bool parent) { char **av, **ev = NULL; int pid, stat; builtin_t *b; char *path = NULL; bool didfork, returning, saw_exec, saw_builtin; av = list2array(s, dashex); saw_builtin = saw_exec = FALSE; do { if (*av == NULL || isabsolute(*av)) b = NULL; else if (!saw_builtin && fnlookup(*av) != NULL) b = funcall; else b = isbuiltin(*av); /* a builtin applies only to the immmediately following command, e.g., builtin exec echo hi */ saw_builtin = FALSE; if (b == b_exec) { av++; saw_exec = TRUE; parent = FALSE; } else if (b == b_builtin) { av++; saw_builtin = TRUE; } } while (b == b_exec || b == b_builtin); if (*av == NULL && saw_exec) { /* do redirs and return on a null exec */ doredirs(); return; } /* force an exit on exec with any rc_error, but not for null commands as above */ if (saw_exec) rc_pid = -1; if (b == NULL) { path = which(*av, TRUE); if (path == NULL && *av != NULL) { /* perform null commands for redirections */ set(FALSE); redirq = NULL; if (parent) return; rc_exit(1); } ev = makeenv(); /* environment only needs to be built for execve() */ } /* If parent & the redirq is nonnull, builtin or not it has to fork. If the fifoq is nonnull, then it must be emptied at the end so we must fork no matter what. */ if ((parent && (b == NULL || redirq != NULL)) || outstanding_cmdarg()) { pid = rc_fork(); didfork = TRUE; } else { pid = 0; didfork = FALSE; } returning = (!didfork && parent); switch (pid) { case -1: uerror("fork"); rc_error(NULL); /* NOTREACHED */ case 0: if (!returning) setsigdefaults(FALSE); pop_cmdarg(FALSE); doredirs(); /* null commands performed for redirections */ if (*av == NULL || b != NULL) { if (b != NULL) (*b)(av); if (returning) return; rc_exit(getstatus()); } execve(path, (char * const *) av, (char * const *) ev); uerror(*av); rc_exit(1); /* NOTREACHED */ default: redirq = NULL; rc_wait4(pid, &stat, TRUE); setstatus(-1, stat); if ((stat & 0xff) == 0) nl_on_intr = FALSE; SIGCHK; nl_on_intr = TRUE; pop_cmdarg(TRUE); } }