sort.c File Reference

#include "../headers/sort.h"

Go to the source code of this file.

Functions

static void merge_atom_vert (s_vsort *lsort, s_atm **atoms, int natms, s_vvertice **pvert, int nvert)
static void qsort_dim (s_vect_elem *lst, int len)
static void qsort_rec (s_vect_elem *lst, int start, int end)
static int partition_x (s_vect_elem *lst, int start, int end)
s_vsortget_sorted_list (s_atm **atoms, int natms, s_vvertice **pvert, int nvert)
void print_sorted_lst (s_vsort *lsort, FILE *buf)
void free_s_vsort (s_vsort *lsort)


Function Documentation

void free_s_vsort ( s_vsort lsort  ) 

## FUNCTION: free_s_vsort

## SPECIFICATION: Free memory for s_vsort structure

## PARAMETRES: @ s_vsort *lsort: Structure to free

## RETURN:

Definition at line 328 of file sort.c.

References my_free(), and s_vsort::xsort.

Referenced by count_atm_prop_vert_neigh(), count_pocket_lig_vert_ovlp(), count_vert_neigh_P(), get_mol_atm_neigh(), get_mol_ctd_atm_neigh(), and get_mol_vert_neigh().

00329 {
00330         if(lsort) {
00331                 if(lsort->xsort) my_free(lsort->xsort) ;
00332                 
00333                 my_free(lsort) ;
00334         }
00335 }

s_vsort* get_sorted_list ( s_atm **  atoms,
int  natms,
s_vvertice **  pvert,
int  nvert 
)

## FUNCTION: get_sorted_list

## SPECIFICATION: This function will return a lists which will contains all atoms and vertices sorted on x axis. First, we merge atom and vertice lists into those a single list. Then they will be sorted using a quickSort algorithm, using the x positions of vertices and atoms as criteria for sorting.

## PARAMETRES: @ s_pdb *pdb : PDB structure, basically containing atoms @ s_vvertice **pvert, int nvert : List of vertices (if NULL, only atoms will be sorted)

## RETURN: s_vsort*: pointer to the structure containing all sorted data (see .h)

Definition at line 86 of file sort.c.

References merge_atom_vert(), my_malloc(), s_vsort::nelem, qsort_dim(), and s_vsort::xsort.

Referenced by count_atm_prop_vert_neigh(), count_pocket_lig_vert_ovlp(), count_vert_neigh_P(), get_mol_atm_neigh(), get_mol_ctd_atm_neigh(), and get_mol_vert_neigh().

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         /* Allocate memory */
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 }

static void merge_atom_vert ( s_vsort lsort,
s_atm **  atoms,
int  natms,
s_vvertice **  pvert,
int  nvert 
) [static]

## FUNCTION: merge_atom_vert

## SPECIFICATION: Merge atom and vertice lists into three single list that will be sorted next.

## PARAMETRES: @ s_vsort *lsort : Structure that should contains the 3 lists @ s_pdb *pdb : pdb structure containing atoms @ s_vvertice **pvert, int nvert : List of v ertices

## RETURN:

Definition at line 123 of file sort.c.

References s_vect_elem::data, M_ATOM_TYPE, M_VERTICE_TYPE, s_vvertice::sort_x, s_atm::sort_x, s_vect_elem::type, and s_vsort::xsort.

Referenced by get_sorted_list().

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 }

static int partition_x ( s_vect_elem lst,
int  start,
int  end 
) [static]

## FUNCTION: static partition_x

## SPECIFICATION: Partition function for the qsort on atoms and vertices, using the X coordinate as criteria to sort the list.

## PARAMETRES: @ s_vect_elem *lst : List of vector to sort @ int start : Sort from start @ int end : to end

## RETURN: int: qsort index

Definition at line 213 of file sort.c.

References s_vect_elem::data, M_ATOM_TYPE, s_vvertice::sort_x, s_atm::sort_x, s_vect_elem::type, s_vvertice::x, and s_atm::x.

Referenced by qsort_rec().

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 ;      /* TODO: chose randomly the pivot. */
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                                 /* We have to swap, so change indices, and swap elements. */
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                                 /* We have to swap, so change indices, and swap elements. */
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 }

void print_sorted_lst ( s_vsort lsort,
FILE *  buf 
)

## FUNCTION: print_sorted_lst

## SPECIFICATION: Print one of the sorted tab of a s_vsort structure

## PARAMETRES: @ s_vsort *lsort : Structure containing tab @ FILE *buf : Buffer to print in.

## RETURN:

Definition at line 286 of file sort.c.

References M_ATOM_TYPE, s_vsort::nelem, s_vvertice::sort_x, s_atm::sort_x, s_vvertice::x, s_atm::x, and s_vsort::xsort.

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 }

static void qsort_dim ( s_vect_elem lst,
int  len 
) [static]

## FUNCTION: qsort_dim

## SPECIFICATION:

## PARAMETRES: @ s_vect_elem *lst : List of vector to sort @ int *len : Length of the list

## RETURN:

Definition at line 164 of file sort.c.

References qsort_rec().

Referenced by get_sorted_list().

00165 {
00166         qsort_rec(lst, 0,  len-1) ;
00167 }

static void qsort_rec ( s_vect_elem lst,
int  start,
int  end 
) [static]

## FUNCTION: static qsort_rec

## SPECIFICATION: qsort routine adapted to what we wanna do.

## PARAMETRES: @ s_vect_elem *lst : List of vector to sort @ int start : Sort from start @ int end : to end

## RETURN: void

Definition at line 185 of file sort.c.

References partition_x().

Referenced by qsort_dim().

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 }


Generated on Mon Jun 7 16:44:23 2010 for fpocket by  doxygen 1.5.6