Changeset 438 for trunk/psLib/src/collections/psSort.c
- Timestamp:
- Apr 16, 2004, 4:43:59 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psSort.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psSort.c
r437 r438 1 #include "psArray.h" 2 #include "psSort.h" 1 /** @file psSort.h 2 * 3 * @brief Sorts an array of floats 4 * 5 * The psSort functions use the qsort() stdlib function with a user-defined callback to sort an array of 6 * floats. The qsort function requires the starting point of the array, number of elements in the array, size 7 * of each element, and a pointer to a callback comparison function. Once called, qsort() will sort the array 8 * contents in ascending order according to the comparison function. The comparison function is called with 9 * two arguments that point to the objects being compared - in this case two floats. The comparison function 10 * must return an integer less than, equal to, or greater than zero if the first argument is considered to be 11 * respectively less than, equal to, or greater than the second. If two members compare as equal, their order 12 * in the sorted array is undefined. 13 * 14 * @author Ross Harman, MHPCC 15 * 16 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-17 02:43:59 $ 18 * 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 20 */ 21 22 /******************************************************************************/ 23 /* INCLUDE FILES */ 24 /******************************************************************************/ 3 25 4 26 #include <stdio.h> … … 7 29 #include <math.h> 8 30 #include <float.h> 9 /* qsort - sorts an array */ 10 /* The qsort() function sorts an array with nmemb elements of size size. 11 The base argument points to the start of the array. 12 13 The contents of the array are sorted in ascending order according to a 14 comparison function pointed to by compar, which is called with two 15 arguments that point to the objects being compared. 16 17 The comparison function must return an integer less than, equal to, or 18 greater than zero if the first argument is considered to be respec- 19 tively less than, equal to, or greater than the second. If two members 20 compare as equal, their order in the sorted array is undefined. 21 */ 22 static int psCompare(const void *x, const void *y); 23 24 25 psFloatArray *psSort(psFloatArray *restrict outArray, const psFloatArray *restrict inArray) 31 32 #include "psArray.h" 33 #include "psSort.h" 34 35 /******************************************************************************/ 36 /* DEFINE STATEMENTS */ 37 /******************************************************************************/ 38 39 // None 40 41 /******************************************************************************/ 42 /* TYPE DEFINITIONS */ 43 /******************************************************************************/ 44 45 // None 46 47 /*****************************************************************************/ 48 /* GLOBAL VARIABLES */ 49 /*****************************************************************************/ 50 51 // None 52 53 /*****************************************************************************/ 54 /* FILE STATIC VARIABLES */ 55 /*****************************************************************************/ 56 57 // None 58 59 /*****************************************************************************/ 60 /* FUNCTION IMPLEMENTATION - LOCAL */ 61 /*****************************************************************************/ 62 63 /** Private callback comparison function to compare two floats. 64 * 65 * The comparison function is called by qsort() with two arguments that point to the objects being 66 * compared. The comparison function must return an integer less than, equal to, or greater than zero if 67 * the first argument is considered to be respectively less than, equal to, or greater than the second. If 68 * two members compare as equal, their order in the sorted array is undefined. 69 * 70 * @return int: Result of comparsion (-1, 0, or 1). 71 */ 72 73 static int psCompare( 74 const void *x, /**< First float to compare. */ 75 const void *y /**< Second float to compare. */ 76 ) 77 { 78 float *item1 = NULL; 79 float *item2 = NULL; 80 81 if(x == NULL || y == NULL) { 82 printf("Null input argument\n"); 83 } 84 85 item1 = (float*)x; 86 item2 = (float*)y; 87 88 if(item1 == NULL || item2 == NULL) { 89 printf("Improper cast to float\n"); 90 } 91 92 if(*item1 < *item2) { 93 return -1; 94 } 95 else if(*item1 > *item2) { 96 return 1; 97 } 98 else { 99 return 0; 100 } 101 } 102 103 /** Sort an array of floats. 104 * 105 * Sorts an array of floats in ascendin order with the qsort() stdlib function. 106 * 107 * @return psFloatArray*: Pointer to sorted psFloatArray. 108 */ 109 110 psFloatArray *psSort( 111 psFloatArray *restrict outArray, /**< Sorted output array. */ 112 const psFloatArray *restrict inArray /**< Input array to sort. */ 113 ) 26 114 { 27 115 int inN = 0; … … 65 153 } 66 154 67 psIntArray *psSortIndex(psIntArray *restrict outArray, const psFloatArray *restrict inArray) 155 /** Creates an array of indices based on sort odred of float array. 156 * 157 * Sorts an array of floats and creates an integer array holding indices of sorted float values based on 158 * pre-sort index positions. 159 * 160 * @return psIntArray*: Pointer to psIntArray of sorted indices. 161 */ 162 163 psIntArray *psSortIndex( 164 psIntArray *restrict outArray, /**< Output array of sorted indices. */ 165 const psFloatArray *restrict inArray /**< Input array to be sorted. */ 166 ) 68 167 { 69 168 int inN = 0; … … 117 216 return outArray; 118 217 } 119 120 static int psCompare(const void *x, const void *y)121 {122 float *item1 = NULL;123 float *item2 = NULL;124 125 if(x == NULL || y == NULL) {126 printf("Null input argument\n");127 }128 129 item1 = (float*)x;130 item2 = (float*)y;131 132 if(item1 == NULL || item2 == NULL) {133 printf("Improper cast to float\n");134 }135 136 if(*item1 < *item2) {137 return -1;138 }139 else if(*item1 > *item2) {140 return 1;141 }142 else {143 return 0;144 }145 }
Note:
See TracChangeset
for help on using the changeset viewer.
