xref: /petsc/src/sys/utils/ftn-custom/zsortsof.c (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
1 #include <petsc/private/ftnimpl.h>
2 #include <petscsys.h>
3 
4 #if defined(PETSC_HAVE_FORTRAN_CAPS)
5   #define petsctimsort_          PETSCTIMSORT
6   #define petsctimsortwitharray_ PETSCTIMSORTWITHARRAY
7 #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
8   #define petsctimsort_          petsctimsort
9   #define petsctimsortwitharray_ petsctimsortwitharray
10 #endif
11 
12 struct fc_c {
13   void (*fcmp)(const void *a, const void *b, void *c, int *res);
14   void *fctx;
15 } fc_c;
16 
cmp_via_fortran(const void * a,const void * b,PetscCtx ctx)17 int cmp_via_fortran(const void *a, const void *b, PetscCtx ctx)
18 {
19   int          result;
20   struct fc_c *fc = (struct fc_c *)ctx;
21   fc->fcmp(a, b, fc->fctx, &result);
22   return result;
23 }
24 
petsctimsort_(PetscInt * n,void * arr,size_t * size,void (* cmp)(const void *,const void *,void *,int *),PetscCtx ctx,PetscErrorCode * ierr)25 PETSC_EXTERN void petsctimsort_(PetscInt *n, void *arr, size_t *size, void (*cmp)(const void *, const void *, void *, int *), PetscCtx ctx, PetscErrorCode *ierr)
26 {
27   struct fc_c fc = {cmp, ctx};
28   *ierr          = PetscTimSort(*n, arr, *size, cmp_via_fortran, &fc);
29 }
30 
petsctimsortwitharray_(PetscInt * n,void * arr,size_t * asize,void * barr,size_t * bsize,void (* cmp)(const void *,const void *,void *,int *),PetscCtx ctx,PetscErrorCode * ierr)31 PETSC_EXTERN void petsctimsortwitharray_(PetscInt *n, void *arr, size_t *asize, void *barr, size_t *bsize, void (*cmp)(const void *, const void *, void *, int *), PetscCtx ctx, PetscErrorCode *ierr)
32 {
33   struct fc_c fc = {cmp, ctx};
34   *ierr          = PetscTimSortWithArray(*n, arr, *asize, barr, *bsize, cmp_via_fortran, &fc);
35 }
36