/* *--------------------------------------------------------------------------- * * Blt_Chain_Sort -- * * Sorts the chain according to the given comparison routine. * * Results: * None. * * Side Effects: * The chain is reordered. * *--------------------------------------------------------------------------- */ void Blt_Chain_Sort(Chain *chainPtr, Blt_ChainCompareProc *proc) { ChainLink **linkArr; ChainLink *linkPtr; long i; if (chainPtr->nLinks < 2) { return; } linkArr = Blt_Malloc(sizeof(Blt_ChainLink) * (chainPtr->nLinks + 1)); if (linkArr == NULL) { return; /* Out of memory. */ } i = 0; for (linkPtr = chainPtr->head; linkPtr != NULL; linkPtr = linkPtr->next) { linkArr[i++] = linkPtr; } qsort((char *)linkArr, chainPtr->nLinks, sizeof(Blt_ChainLink), (QSortCompareProc *)proc); /* Rethread the chain. */ linkPtr = linkArr[0]; chainPtr->head = linkPtr; linkPtr->prev = NULL; for (i = 1; i < chainPtr->nLinks; i++) { linkPtr->next = linkArr[i]; linkPtr->next->prev = linkPtr; linkPtr = linkPtr->next; } chainPtr->tail = linkPtr; linkPtr->next = NULL; Blt_Free(linkArr); }
/* *---------------------------------------------------------------------- * * Blt_ChainCreate -- * * Creates a new linked list (chain) structure and initializes * its pointers; * * Results: * Returns a pointer to the newly created chain structure. * *---------------------------------------------------------------------- */ Blt_Chain * Blt_ChainCreate() { Blt_Chain *chainPtr; chainPtr = Blt_Malloc(sizeof(Blt_Chain)); if (chainPtr != NULL) { Blt_ChainInit(chainPtr); } return chainPtr; }
/* *--------------------------------------------------------------------------- * * Blt_Chain_Create -- * * Creates a new linked list (chain) structure and initializes its * pointers; * * Results: * Returns a pointer to the newly created chain structure. * *--------------------------------------------------------------------------- */ Blt_Chain Blt_Chain_Create(void) { Chain *chainPtr; chainPtr = Blt_Malloc(sizeof(Chain)); if (chainPtr != NULL) { Blt_Chain_Init(chainPtr); } return chainPtr; }