Changeset 1407 for trunk/psLib/src/collections/psArray.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psArray.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psArray.c
r1406 r1407 1 1 2 /** @file psArray.c 2 3 * … … 8 9 * @author Ross Harman, MHPCC 9 10 * 10 * @version $Revision: 1.1 1$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-08-0 6 22:34:05$11 * @version $Revision: 1.12 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-08-07 00:06:06 $ 12 13 * 13 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 16 16 17 /******************************************************************************/ 18 17 19 /* INCLUDE FILES */ 20 18 21 /******************************************************************************/ 19 #include<stdlib.h> // for qsort, etc.22 #include<stdlib.h> // for qsort, etc. 20 23 21 24 #include "psMemory.h" … … 24 27 #include "psLogMsg.h" 25 28 29 /*****************************************************************************/ 30 31 /* FUNCTION IMPLEMENTATION - LOCAL */ 26 32 27 33 /*****************************************************************************/ 28 /* FUNCTION IMPLEMENTATION - LOCAL */ 29 /*****************************************************************************/ 30 static void arrayFree(psArray *restrict psArr); 34 static void arrayFree(psArray * restrict psArr); 31 35 32 36 /*****************************************************************************/ 37 33 38 /* FUNCTION IMPLEMENTATION - PUBLIC */ 39 34 40 /*****************************************************************************/ 35 psArray *psArrayAlloc(unsigned int nalloc)41 psArray *psArrayAlloc(unsigned int nalloc) 36 42 { 37 43 psArray *psArr = NULL; 38 44 39 45 // Invalid nalloc 40 if (nalloc < 1) {46 if (nalloc < 1) { 41 47 psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc); 42 48 return NULL; 43 49 } 44 45 50 // Create vector struct 46 psArr = (psArray *) psAlloc(sizeof(psArray));47 p_psMemSetDeallocator(psArr, (psFreeFcn)arrayFree);51 psArr = (psArray *) psAlloc(sizeof(psArray)); 52 p_psMemSetDeallocator(psArr, (psFreeFcn) arrayFree); 48 53 49 54 psArr->nalloc = nalloc; … … 51 56 52 57 // Create vector data array 53 psArr->data = psAlloc(nalloc *sizeof(psPTR));58 psArr->data = psAlloc(nalloc * sizeof(psPTR)); 54 59 55 60 return psArr; 56 61 } 57 62 58 psArray *psArrayRealloc(unsigned int nalloc, psArray * restrict in)63 psArray *psArrayRealloc(unsigned int nalloc, psArray * restrict in) 59 64 { 60 65 // Invalid nalloc 61 if (nalloc < 1) {66 if (nalloc < 1) { 62 67 psError(__func__, "Invalid value for realloc (%d)\n", nalloc); 63 68 return NULL; 64 69 } 65 70 66 if (in == NULL) {71 if (in == NULL) { 67 72 psError(__func__, "Null input vector\n"); 68 73 return NULL; 69 } else 70 if(in->nalloc != nalloc) { // No need to realloc to same size 71 if(nalloc < in->n) { 72 for (int i = nalloc; i < in->n; i++) { // For reduction in vector size 73 psFree(in->data[i]); 74 } 75 in->n = nalloc; 74 } else if (in->nalloc != nalloc) { // No need to realloc to same size 75 if (nalloc < in->n) { 76 for (int i = nalloc; i < in->n; i++) { // For reduction in vector size 77 psFree(in->data[i]); 76 78 } 77 78 // Realloc after decrementation to avoid accessing freed array elements 79 in->data = psRealloc(in->data,nalloc*sizeof(psPTR)); 80 in->nalloc = nalloc; 79 in->n = nalloc; 81 80 } 81 // Realloc after decrementation to avoid accessing freed array elements 82 in->data = psRealloc(in->data, nalloc * sizeof(psPTR)); 83 in->nalloc = nalloc; 84 } 82 85 83 86 return in; 84 87 } 85 88 86 static void arrayFree(psArray * restrict psArr)89 static void arrayFree(psArray * restrict psArr) 87 90 { 88 91 if (psArr == NULL) { … … 95 98 } 96 99 97 void psArrayElementFree(psArray * restrict psArr)100 void psArrayElementFree(psArray * restrict psArr) 98 101 { 99 102 100 if (psArr == NULL) {103 if (psArr == NULL) { 101 104 return; 102 105 } 103 106 104 for (int i = 0; i < psArr->n; i++) {107 for (int i = 0; i < psArr->n; i++) { 105 108 psFree(psArr->data[i]); 106 109 psArr->data[i] = NULL; … … 108 111 } 109 112 110 psArray * psArraySort(psArray* in, psComparePtrFcn compare)113 psArray *psArraySort(psArray * in, psComparePtrFcn compare) 111 114 { 112 115 if (in == NULL) { … … 114 117 } 115 118 116 qsort(in->data, in->n, sizeof(psPTR), 117 (int(*)(const void *, const void *))compare); 118 119 qsort(in->data, in->n, sizeof(psPTR), (int (*)(const void *, const void *))compare); 119 120 120 121 return in;
Note:
See TracChangeset
for help on using the changeset viewer.
