SYMBOL *mapfunc(SYMBOL * func, SYMBOL * arglist) { SYMBOL *sym; MAP *res = NULL; int argc = -1; if (!func || !func->v.p || func->type != st_mfunc) { parseerror = 1; G_warning(_("Can't call bad map-function")); } else argc = listcnt((LIST *) arglist); if (argc == 0 && (!func->proto || !*func->proto)) res = (*(m_func) func->v.p) (); else if (argc == 1 && !strcmp(func->proto, "m")) res = (*(m_func_m) func->v.p) (arglist->v.p); else if (argc == 2 && !strcmp(func->proto, "mm")) res = (*(m_func_mm) func->v.p) (arglist->v.p, arglist->next->v.p); else { G_warning(_("Bad arguments to mapfunc %s (argc = %d)"), func->name, argc); parseerror = 1; } listdelall((LIST *) func, freesym); listdelall((LIST *) arglist, freesym); sym = (SYMBOL *) listitem(sizeof(SYMBOL)); sym->type = st_map; sym->v.p = res; return sym; }
/* * 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; }
LIST *listbsearchdatum(LIST * head, const void *datum, cmpfunc cmp) { LIST *elt; int n, max, result; if (!head || !datum || !cmp) return NULL; max = listcnt(head); n = max / 2; elt = listnth(head, n); result = (*cmp) (datum, elt); if (result == 0) return elt; if (result < 0) elt = _listbsearchdatum(head, n, datum, cmp); else elt = _listbsearchdatum(elt->next, max - n - 1, datum, cmp); return elt; }
LIST *listbsearch(LIST * head, LIST * elt, cmpfunc cmp) { LIST *item; int n, max, result; if (!head || !elt || !cmp) return NULL; max = listcnt(head); n = max / 2; item = listnth(head, n); result = (*cmp) (item, elt); if (result == 0) return item; if (result < 0) item = _listbsearch(head, n, elt, cmp); else item = _listbsearch(item->next, max - n - 1, elt, cmp); return item; }