📄 toolbox.c
字号:
rootPtr = HeapItemPtr(array, root, elementSize); if (leftChild <= numElements) leftChildPtr = HeapItemPtr(array, leftChild, elementSize); else leftChildPtr = 0; if (rightChild <= numElements) rightChildPtr = HeapItemPtr(array, rightChild, elementSize); else rightChildPtr = 0; if (leftChildPtr == 0) break; /* no more children, we are done */ if (rightChildPtr == 0) largestChild = leftChild; else { cmp = compareFunction ? compareFunction(leftChildPtr, rightChildPtr) : ListMemBlockCmp(leftChildPtr, rightChildPtr, elementSize); if (cmp >= 0) largestChild = leftChild; else largestChild = rightChild; } largestChildPtr = HeapItemPtr(array, largestChild, elementSize); cmp = compareFunction ? compareFunction(rootPtr, largestChildPtr) : ListMemBlockCmp(rootPtr, largestChildPtr, elementSize); if (cmp >= 0) break; /* element is in correct spot */ SwapBlock(rootPtr, largestChildPtr, elementSize); root = largestChild; }}/**************************************************************************/static void CVIFUNC BuildHeap(void *array, int numElements, int elementSize, CompareFunction compareFunction){ int index; for (index = 2; index <= numElements; index++) FilterUpToCorrectHeapLocation(array, index, elementSize, compareFunction);}/**************************************************************************/void CVIFUNC HeapSort(void *array, int numElements, int elementSize, CompareFunction compareFunction){ int index; BuildHeap(array, numElements, elementSize, compareFunction); for (index = numElements; index > 1; index--) { /* move greatest item for the root to last position that is still part of the heap and move the item there to root of the heap. */ SwapBlock(array, HeapItemPtr(array, index, elementSize), elementSize); /* filter the item at the top of the heap into its correct position in the now one element smaller heap. */ FilterDownToCorrectHeapPosition(array, index - 1, elementSize, compareFunction); }}/********************************************************************/ /* This function provides a stable alternative to quicksort */void CVIFUNC InsertionSort(void *array, int numElements, int elementSize, CompareFunction compareFunction){ int start, item, cmp; char *topItem, *bottomItem; for (start = 1; start < numElements; start++) { topItem = (char *)array + start * elementSize; bottomItem = topItem - elementSize; for (item = start; item > 0; item--) { cmp = compareFunction ? compareFunction(bottomItem, topItem) : ListMemBlockCmp(bottomItem, topItem, elementSize); if (cmp > 0) { SwapBlock(bottomItem, topItem, elementSize); topItem -= elementSize; bottomItem -= elementSize; } else break; } }}/********************************************************************/ /* starts a tinny sounding whine on the built PC speaker */void CVIFUNC StartPCSound(unsigned int frequency){#if defined(_NI_mswin_) /* _NI_mswin_ is defined automatically when compiling with the Windows version of CVI. */ outp(0x43, (char)0xb6); /* prepare timer */ if (frequency > 0) /* divide number of cycles per second by the input frequency */ frequency = (unsigned) (1193180L/frequency); outp(0x42, (char)frequency); outp(0x42, (char)(frequency >> 8)); outp(0x61, (char)(inp(0x61) | 0x3)); /* turn on sound with bits 0 and 1 */#endif /* _NI_mswin_ */}/****************************************************************************/ /* A function you would pay money for after calling StartPCSound(). */void CVIFUNC StopPCSound(void){#if defined(_NI_mswin_) /* _NI_mswin_ is defined automatically when compiling with the Windows version of CVI. */ outp(0x61, (char)(inp(0x61) & ~0x3)); /* turn off sound with bits 0 and 1 */#endif /* _NI_mswin_ */}/********************************************************************/Handle CVIFUNC NewHandle(unsigned int numBytes){ void *memPtr; HandleRecord *hanPtr; memPtr = calloc(numBytes, 1); hanPtr = (HandleRecord*)calloc(sizeof(HandleRecord), 1); if (hanPtr && (memPtr || numBytes == 0)) { hanPtr->ptr = memPtr; hanPtr->size = numBytes; return (Handle)hanPtr; } else { free(memPtr); free(hanPtr); return NULL; }}/********************************************************************/void CVIFUNC DisposeHandle(Handle handle){ if (handle) { free(*handle); free((void *)handle); }}/********************************************************************/unsigned int CVIFUNC GetHandleSize(Handle handle){#ifndef TB_NO_SAFETY_CHECKS AssertMsg(handle, "Parameter 1 to GetHandleSize was NULL.");#endif return ((HandleRecord *)handle)->size;}/********************************************************************/int CVIFUNC SetHandleSize(Handle handle, unsigned int newSize){ HandleRecord *hanRecPtr = (HandleRecord *)handle; void *newPtr, *oldPtr; unsigned int oldSize;#ifndef TB_NO_SAFETY_CHECKS AssertMsg(hanRecPtr, "Parameter 1 to SetHandleSize was NULL.");#endif oldPtr = hanRecPtr->ptr; oldSize = hanRecPtr->size; if (oldSize == newSize) return TRUE; if (oldPtr == NULL) newPtr = malloc(newSize); else newPtr = realloc(oldPtr, newSize); if (newPtr || (newSize == 0)) { hanRecPtr->ptr = newPtr; hanRecPtr->size = newSize; if (newSize > oldSize) memset ((char *)newPtr+oldSize, 0, newSize-oldSize); return TRUE; } else return FALSE;}/*******************************************************************************/ /* BEGIN PATCH to 4.0.1 - jag 8-29-96 */ /* If numNewItems == 0 then expand the list by the number of items indicated by its allocation policy. If numNewItems > 0 then expand the list by exactly the number of items indicated. If numNewItems < 0 then expand the list by the absolute value of numNewItems plus the number of items indicated by its allocation policy. Returns TRUE for success, FALSE if out of memory */static int CVIFUNC ExpandListSpace (ListType list, int numNewItems){ if (numNewItems == 0) numNewItems = NUMITEMSPERALLOC(list); else if (numNewItems < 0) numNewItems = (-numNewItems) + NUMITEMSPERALLOC(list); /* END PATCH to 4.0.1 - jag 8-29-96 */ if (SetHandleSize ((Handle)list, sizeof (ListStruct) + ((*list)->listSize + numNewItems) * (*list)->itemSize)) { (*list)->listSize += numNewItems; return TRUE; } else return FALSE;}/*******************************/ /* This function reallocate the list, minus any currently unused portion of its allotted memory. */void CVIFUNC ListCompact (ListType list){#ifndef TB_NO_SAFETY_CHECKS if (!list) return;#endif if (!SetHandleSize ((Handle)list, sizeof (ListStruct) + (*list)->numItems * (*list)->itemSize)) return; (*list)->listSize = (*list)->numItems;}/*******************************/ListType CVIFUNC ListCreate (int elementSize){ ListType list; list = (ListType)(NewHandle (sizeof(ListStruct))); /* create empty list */ if (list) { (*list)->signature = LIST_SIGNATURE; (*list)->numItems = 0; (*list)->listSize = 0; (*list)->itemSize = elementSize; (*list)->percentIncrease = kDefaultAllocationPercentIncrease; (*list)->minNumItemsIncrease = kDefaultAllocationminNumItemsIncrease; } return list;}/*******************************/void CVIFUNC ListSetAllocationPolicy (ListType list, int minItemsPerAlloc, int percentIncreasePerAlloc){#ifndef TB_NO_SAFETY_CHECKS if (!list) return;#endif (*list)->percentIncrease = percentIncreasePerAlloc; (*list)->minNumItemsIncrease = minItemsPerAlloc;}/*******************************/void CVIFUNC ListDispose (ListType list){ DisposeHandle ((Handle)list);}/*******************************/void CVIFUNC ListDisposePtrList(ListType list){ int index; int numItems; if (list) { numItems = ListNumItems(list); for (index = 1; index <= numItems; index++) free(*(void **)ListGetPtrToItem(list, index)); ListDispose(list); }}/*******************************/void CVIFUNC ListClear (ListType list) /* keeps memory, resets the number of items to 0 */{ if (!list) return; (*list)->numItems = 0;}/*******************************/ListType CVIFUNC ListCopy (ListType originalList) /* copy is only as large as necessary */{ ListType tempList = NULL; int numItems; if (!originalList) return NULL; tempList = ListCreate ((*originalList)->itemSize); if (tempList) { numItems = ListNumItems (originalList); if (!SetHandleSize ((Handle)tempList, sizeof (ListStruct) + numItems * (*tempList)->itemSize)) { ListDispose (tempList); return NULL; } (*tempList)->numItems = (*originalList)->numItems; (*tempList)->listSize = (*originalList)->numItems; (*tempList)->itemSize = (*originalList)->itemSize; (*tempList)->percentIncrease = (*originalList)->percentIncrease; (*tempList)->minNumItemsIncrease = (*originalList)->minNumItemsIncrease; memcpy (ITEMPTR(tempList,0), ITEMPTR(originalList,0), numItems * (*tempList)->itemSize); } return tempList;}/********************************/int CVIFUNC ListAppend (ListType list1, ListType list2) /* list1 = list1 + list2 */{ int numItemsL1, numItemsL2; if (!list2) return TRUE; if (!list1) return FALSE; if ((*list1)->itemSize != (*list2)->itemSize) return FALSE; numItemsL1 = ListNumItems (list1); numItemsL2 = ListNumItems (list2); if (numItemsL2 == 0) return TRUE; if (!SetHandleSize ((Handle)list1, sizeof (ListStruct) + (numItemsL1 + numItemsL2) * (*list1)->itemSize)) return FALSE; (*list1)->numItems = numItemsL1 + numItemsL2; (*list1)->listSize = numItemsL1 + numItemsL2; memmove (ITEMPTR(list1,numItemsL1), ITEMPTR(list2,0), numItemsL2 * (*list2)->itemSize); return TRUE;}/*******************************/ /* returns TRUE if the item is inserted, returns FALSE if out of memory or bad arguments were passed. */int CVIFUNC ListInsertItem (ListType list, void *ptrToItem, int itemPosition){ return ListInsertItems (list, ptrToItem, itemPosition, 1);}/*******************************/int CVIFUNC ListInsertItems (ListType list, void *ptrToItems, int firstItemPosition, int numItemsToInsert){ int numItems = (*list)->numItems;#ifndef TB_NO_SAFETY_CHECKS if (!list) return FALSE; if (numItemsToInsert <= 0) return FALSE;#endif if (firstItemPosition == numItems + 1) firstItemPosition = END_OF_LIST; else if (firstItemPosition > numItems) return FALSE; if ((*list)->numItems >= (*list)->listSize) if (!ExpandListSpace (list, -numItemsToInsert)) /* PATCH to 4.0.1 - jag 8-29-96 */ return FALSE; if (firstItemPosition == FRONT_OF_LIST) if (numItems == 0) firstItemPosition = END_OF_LIST; /* special case for empty list */ else firstItemPosition = 1; if (firstItemPosition == END_OF_LIST) /* add at the end of the list */ { if (ptrToItems) memcpy (ITEMPTR(list, numItems), ptrToItems,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -