Re: busting sp datatypes



Ron Ford wrote:
(snip)

qsort is in C's stdlib; can fortran make the necessary interface? I can
think of many ways that this question can be answered in the ngative. I
still don't know what you mean with the word 'interp'.

I think so. You need C_SIZEOF() to pass the appropriate element
size, and you need to be able to pass a comparison function.

The compare function could be written in C, which might be
the easiest way. Otherwise, it is passed (by value) pointers
to two elements to be compared. I believe that could be done
properly with the C interoperability features, including C_FUNPTR
to pass the appropriate function pointer.

Assuming that passing variables to C passes the address,
calling from C passes the pointer by value, which should be
exactly what a Fortran routine would need.

integer compare(i1,i2) bind(c)
compare=0
if(i1>i2) compare=1
if(i1<i2) compare=-1
return
end

external, bind(c) :: compare, qsort
integer x(10)
read(*,*) x
call qsort(x,10,c_sizeof(x(1)),c_funloc(compare))
write(*,*) x
end

-- glen

.



Relevant Pages

  • Re: busting sp datatypes
    ... glen herrmannsfeldt wrote: ... and you need to be able to pass a comparison function. ... to pass the appropriate function pointer. ...
    (comp.lang.fortran)
  • Re: qsort
    ... First, as someone else already pointed out, comparison function expects ... int blbp_CtnRecCompare (void * Rec_1, ... then 99 out of 100 cases arg#4 is not the correct type. ... You tried to pass a function pointer, but that's not what your syntax ...
    (comp.sys.hp.hpux)
  • Re: [D7] Bug in TList.Sort
    ... Perhaps we talk about different implementations. ... Delphi code doesn't change very much. ... If the comparison function doesn't correctly handle the case when the pointers are equal, then QuickSort will try to compare the midpoint value with the "second" value in the list's internal pointer array. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Building a Binary Heap
    ... put the comparison code in the heap code itself; ... pass in a comparison function pointer, ... point to the comparison function in the structure. ... and destructor) within the controllinng data structure. ...
    (comp.programming)
  • Re: Sorting a list of integers in C
    ... >to sort these set of integers using C, ... Do not use arrays ... unless every pointer points to a dynamically allocated area. ... For the comparison function, simply subtract one ...
    (comp.lang.c)