00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef NEXT
00058 #define NEXT next
00059 #endif
00060
00061 #ifndef DECL_SORT1
00062 #define DECL_SORT1 static
00063 #endif
00064
00065 #ifndef DECL_SORT
00066 #define DECL_SORT static
00067 #endif
00068
00069 #ifdef FIELD
00070 #define COMPTYPE void *
00071 #else
00072 #define COMPTYPE TYPE
00073 #endif
00074
00075 DECL_SORT TYPE *SORT(TYPE *list_in,
00076 int (*compare)(COMPTYPE, COMPTYPE), int cnt);
00077
00078
00079 #ifdef SORT1
00080
00081 DECL_SORT1 TYPE *
00082 SORT1(TYPE *list_in, int (*compare)(COMPTYPE, COMPTYPE))
00083 {
00084 register int cnt;
00085 register TYPE *p;
00086
00087
00088 for(p = list_in, cnt = 0; p != 0; p = p->NEXT, cnt++)
00089 ;
00090 return SORT(list_in, compare, cnt);
00091 }
00092
00093 #endif
00094
00095 DECL_SORT TYPE *
00096 SORT(TYPE *list_in, int (*compare)(COMPTYPE, COMPTYPE), int cnt)
00097 {
00098 register TYPE *p, **plast, *list1, *list2;
00099 register int i;
00100
00101 if (cnt > 1) {
00102
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
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
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 }
00148
00149 #undef TYPE
00150 #undef SORT
00151 #undef SORT1
00152 #undef DECL_SORT
00153 #undef DECL_SORT1
00154 #undef FIELD
00155 #undef DIRECT_COMPARE
00156 #undef NEXT