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
00058
00059
00060
00061
00062 #ifndef __NUSMV_CORE_UTILS_LSORT_H__
00063 #define __NUSMV_CORE_UTILS_LSORT_H__
00064
00065 #ifndef NEXT
00066
00072 #define NEXT next
00073 #endif
00074
00075 #ifndef DECL_SORT1
00076
00082 #define DECL_SORT1 static
00083 #endif
00084
00085 #ifndef DECL_SORT
00086
00092 #define DECL_SORT static
00093 #endif
00094
00095 DECL_SORT TYPE *SORT(TYPE*, int (*compare)(char*, char*), int cnt);
00096
00097
00098 #ifdef SORT1
00099
00100 DECL_SORT TYPE *SORT1(TYPE*, int (*compare)(char*, char*));
00101
00102 DECL_SORT1 TYPE *
00103 SORT1(list_in, compare)
00104 TYPE *list_in;
00105 int (*compare)(char*, char*);
00106 {
00107 register int cnt;
00108 register TYPE *p;
00109
00110
00111 for(p = list_in, cnt = 0; p != 0; p = p->NEXT, cnt++)
00112 ;
00113 return SORT(list_in, compare, cnt);
00114 }
00115
00116 #endif
00117
00118 DECL_SORT TYPE *
00119 SORT(list_in, compare, cnt)
00120 TYPE *list_in;
00121 int (*compare)(char*, char*);
00122 int cnt;
00123 {
00124 register TYPE *p, **plast, *list1, *list2;
00125 register int i;
00126
00127 if (cnt > 1) {
00128
00129 for(p = list_in, i = cnt/2-1; i > 0; p = p->NEXT, i--)
00130 ;
00131 list1 = list_in;
00132 list2 = p->NEXT;
00133 p->NEXT = 0;
00134
00135
00136 if ((i = cnt/2) > 1) {
00137 list1 = SORT(list1, compare, i);
00138 }
00139 if ((i = cnt - i) > 1) {
00140 list2 = SORT(list2, compare, i);
00141 }
00142
00143
00144 plast = &list_in;
00145 for(;;) {
00146 #ifdef FIELD
00147 #ifdef DIRECT_COMPARE
00148 if (list1->FIELD < list2->FIELD) {
00149 #else
00150 if ((*compare)(list1->FIELD, list2->FIELD) <= 0) {
00151 #endif
00152 #else
00153 if ((*compare)(list1, list2) <= 0) {
00154 #endif
00155 *plast = list1;
00156 plast = &(list1->NEXT);
00157 if ((list1 = list1->NEXT) == 0) {
00158 *plast = list2;
00159 break;
00160 }
00161 } else {
00162 *plast = list2;
00163 plast = &(list2->NEXT);
00164 if ((list2 = list2->NEXT) == 0) {
00165 *plast = list1;
00166 break;
00167 }
00168 }
00169 }
00170 }
00171
00172 return list_in;
00173 }
00174
00175 #undef TYPE
00176 #undef SORT
00177 #undef SORT1
00178 #undef DECL_SORT
00179 #undef DECL_SORT1
00180 #undef FIELD
00181 #undef DIRECT_COMPARE
00182 #undef NEXT
00183
00184 #endif