#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libant/ListSplit.c $ %D% SwRI" /* * LinkSplit.c * * this routine will split a link list into an equal number of link lists * and return a pointer to them. */ #include #include "LinkList.h" LinkList * ListSplit(LinkList List, int *n) { int i, k, r; int count = LinkDepth(List); LinkList *L, DupList; if (!List || *n < 1) return (LinkList *)0; /* * calculate the number of link nodes per concurrent process */ k = count / *n; r = count % *n; /* the remainder, if any */ *n = k > 0 ? *n : r; /* * allocate the number of link lists, one per concurrent process */ if ((L = (LinkList *)calloc(*n, sizeof(LinkList *))) == 0) return (LinkList *)0; DupList = LinkListDup(List); for (i=0; i < *n; i++) { int j; LinkList l; L[i] = DupList; for (j=0; j < k + (r > 0 ? 1 : 0); j++) { l = DupList; DupList = DupList->next; } l->next = 0; r--; } return L; }