00001
00002 #include "../headers/sort.h"
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 static void merge_atom_vert(s_vsort *lsort, s_atm **atoms, int natms, s_vvertice **pvert, int nvert) ;
00063 static void qsort_dim(s_vect_elem *lst, int len) ;
00064 static void qsort_rec(s_vect_elem *lst, int start, int end) ;
00065 static int partition_x(s_vect_elem *lst, int start, int end) ;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 s_vsort* get_sorted_list(s_atm **atoms, int natms, s_vvertice **pvert, int nvert)
00087 {
00088 s_vsort *lsort = (s_vsort *) my_malloc(sizeof(s_vsort)) ;
00089
00090 lsort->nelem = 0 ;
00091
00092 if(atoms) lsort->nelem += natms ;
00093 if(pvert) lsort->nelem += nvert ;
00094
00095 if(lsort->nelem == 0) return NULL ;
00096
00097
00098 lsort->xsort = (s_vect_elem*) my_malloc((lsort->nelem)*sizeof(s_vect_elem)) ;
00099
00100 merge_atom_vert(lsort, atoms, natms, pvert, nvert) ;
00101 qsort_dim(lsort->xsort, lsort->nelem) ;
00102
00103 return lsort ;
00104
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 static void merge_atom_vert(s_vsort *lsort, s_atm **atoms, int natms, s_vvertice **pvert, int nvert)
00124 {
00125 s_vect_elem *cur = NULL ;
00126
00127 int i = 0, j = 0,
00128 pos ;
00129
00130 if(atoms) {
00131 for(i = 0 ; i < natms ; i++) {
00132 atoms[i]->sort_x = i ;
00133 cur = &(lsort->xsort[i]) ;
00134 cur->data = atoms[i] ;
00135 cur->type = M_ATOM_TYPE ;
00136 }
00137 }
00138
00139 if(pvert) {
00140 for(j = 0 ; j < nvert ; j++) {
00141 pos = i + j ;
00142
00143 pvert[j]->sort_x = pos ;
00144 cur = &(lsort->xsort[pos]) ;
00145 cur->data = pvert[j] ;
00146 cur->type = M_VERTICE_TYPE ;
00147 }
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 static void qsort_dim(s_vect_elem *lst, int len)
00165 {
00166 qsort_rec(lst, 0, len-1) ;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 static void qsort_rec(s_vect_elem *lst, int start, int end)
00186 {
00187 if(start < end) {
00188 int piv = 0 ;
00189 piv = partition_x(lst, start, end) ;
00190
00191 qsort_rec(lst, start, piv-1) ;
00192 qsort_rec(lst, piv+1, end) ;
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 static int partition_x(s_vect_elem *lst, int start, int end)
00214 {
00215 s_vect_elem tmp ;
00216 s_atm *acur = NULL ;
00217 s_vvertice *vcur = NULL ;
00218
00219 int c = start,
00220 i ;
00221
00222 float piv ;
00223
00224 piv = (lst[start].type == M_ATOM_TYPE)? ((s_atm*)lst[start].data)->x :
00225 ((s_vvertice*)lst[start].data)->x ;
00226
00227 for(i = start+1 ; i <= end ; i++) {
00228 if(lst[i].type == M_ATOM_TYPE) {
00229 acur = ((s_atm*) lst[i].data) ;
00230 if(acur->x < piv) {
00231 c++ ;
00232
00233
00234 if(lst[c].type == M_ATOM_TYPE) ((s_atm*)lst[c].data)->sort_x = i ;
00235 else ((s_vvertice*)lst[c].data)->sort_x = i ;
00236
00237 acur->sort_x = c ;
00238
00239 tmp = lst[c] ;
00240 lst[c] = lst[i] ; lst[i] = tmp ;
00241 }
00242 }
00243 else {
00244 vcur = ((s_vvertice*)lst[i].data) ;
00245 if(vcur->x < piv) {
00246 c++ ;
00247
00248
00249 if(lst[c].type == M_ATOM_TYPE) ((s_atm*)lst[c].data)->sort_x = i ;
00250 else ((s_vvertice*)lst[c].data)->sort_x = i ;
00251
00252 vcur->sort_x = c ;
00253
00254 tmp = lst[c] ;
00255 lst[c] = lst[i] ; lst[i] = tmp ;
00256 }
00257 }
00258 }
00259
00260 if(lst[c].type == M_ATOM_TYPE) ((s_atm*)lst[c].data)->sort_x = start ;
00261 else ((s_vvertice*)lst[c].data)->sort_x = start ;
00262
00263 if(lst[start].type == M_ATOM_TYPE) ((s_atm*)lst[start].data)->sort_x = c ;
00264 else ((s_vvertice*)lst[start].data)->sort_x = c ;
00265
00266 tmp = lst[c] ;
00267 lst[c] = lst[start] ; lst[start] = tmp ;
00268
00269 return(c);
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void print_sorted_lst(s_vsort *lsort, FILE *buf)
00287 {
00288 s_vect_elem *lst = NULL ;
00289 lst = lsort->xsort ; fprintf(buf, "\n========== Printing list of vertices and atoms sorted on X axe:\n") ;
00290
00291 float cval, prev = -1.0 ;
00292 int i ;
00293
00294 for(i = 0 ; i < lsort->nelem ; i++) {
00295 fprintf(buf, "> Element at %d: ", i);
00296 if(lst[i].type == M_ATOM_TYPE) {
00297 s_atm *a = (s_atm*) lst[i].data ;
00298 cval = a->x ;
00299 fprintf(buf, " ATOM coord = %f, index stored: %d", cval, a->sort_x) ;
00300 }
00301 else {
00302 s_vvertice *v = (s_vvertice*) lst[i].data ;
00303 cval = v->x ;
00304
00305 fprintf(buf, " VERTICE coord = %f, index stored: %d", cval, v->sort_x) ;
00306 }
00307
00308 if(prev > cval) fprintf(buf, " !!!!!!! ") ;
00309 fprintf(buf, "\n") ;
00310
00311 prev = cval ;
00312 }
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 void free_s_vsort(s_vsort *lsort)
00329 {
00330 if(lsort) {
00331 if(lsort->xsort) my_free(lsort->xsort) ;
00332
00333 my_free(lsort) ;
00334 }
00335 }
00336