Go to the source code of this file.
Defines | |
#define | DECL_SORT1 static |
#define | DECL_SORT static |
#define | COMPTYPE void * |
Functions | |
DECL_SORT TYPE * | SORT (TYPE *list_in, int(*compare)(COMPTYPE, COMPTYPE), int cnt) |
DECL_SORT TYPE * SORT | ( | TYPE * | list_in, | |
int(*)(COMPTYPE, COMPTYPE) | compare, | |||
int | cnt | |||
) |
Definition at line 96 of file lsort.h.
00097 { 00098 register TYPE *p, **plast, *list1, *list2; 00099 register int i; 00100 00101 if (cnt > 1) { 00102 /* break the list in half */ 00103 for(p = list_in, i = cnt/2-1; i > 0; p = p->NEXT, i--) 00104 ; 00105 list1 = list_in; 00106 list2 = p->NEXT; 00107 p->NEXT = 0; 00108 00109 /* Recursively sort the sub-lists (unless only 1 element) */ 00110 if ((i = cnt/2) > 1) { 00111 list1 = SORT(list1, compare, i); 00112 } 00113 if ((i = cnt - i) > 1) { 00114 list2 = SORT(list2, compare, i); 00115 } 00116 00117 /* Merge the two sorted sub-lists */ 00118 plast = &list_in; 00119 for(;;) { 00120 #ifdef FIELD 00121 #ifdef DIRECT_COMPARE 00122 if (list1->FIELD < list2->FIELD) { 00123 #else 00124 if ((*compare)(list1->FIELD, list2->FIELD) <= 0) { 00125 #endif 00126 #else 00127 if ((*compare)(list1, list2) <= 0) { 00128 #endif 00129 *plast = list1; 00130 plast = &(list1->NEXT); 00131 if ((list1 = list1->NEXT) == 0) { 00132 *plast = list2; 00133 break; 00134 } 00135 } else { 00136 *plast = list2; 00137 plast = &(list2->NEXT); 00138 if ((list2 = list2->NEXT) == 0) { 00139 *plast = list1; 00140 break; 00141 } 00142 } 00143 } 00144 } 00145 00146 return list_in; 00147 }