Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

how do I set up sortq to sort a char string

kvrichardson
Beginner
940 Views
How do I use sortq? I can't get it to work and I don't understand the documentation in dfport or in help. Here's what I have so far:

subroutine loadcuts       (my subroutine) 
use dfport 
.... 
integer(2), external :: cmp_char 
CHARACTER cv5*5(10) 
...... 
!sort width strings 
CALL QSORT (cv5,10,5,cmp_char) 
 
      integer(2) function cmp_char(a1, a2) 
      character*5 a1, a2 
	if (a1.lt.a2) cmp_char=1 
	if (a1.eq.a2) cmp_char=0 
	if (a1.gt.a2) cmp_char=-1 
      end function
0 Kudos
11 Replies
Jugoslav_Dujic
Valued Contributor II
940 Views
It looks OK at first sight, except that I'm not sure whether .lt. and .gt. work with strings (my 5.0 docs do not mention that -- maybe an extension was added in the meantime) -- I'd expect a compile-time error for that.
Take a look on intrinsics LGT and LLT instead.

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
940 Views
.lt., etc., are fine with strings. That's standard. LLT and LGT are for where you want to always use the ASCII collating sequence, even on machines that don't use ASCII (such as IBM System390). In CVF, they're equivalent.

I haven't looked at the manual yet to see if I can tell what's going wrong (writing this from home.)

Steve
0 Kudos
kvrichardson
Beginner
940 Views
I hope you can find it, Steve. I searched and viewed every QSORT in the Samples directory and saw no example for characters.

I also searched on SORTQ in help and read every word.....probably something I'm missing, of course...but your advice would save me in this case.

Keith Richardson
0 Kudos
Steven_L_Intel1
Employee
940 Views
Your comparison function is backwards - it should return 1 if a1 is greater than a2, not the other way around. Other than that, I don't see anything wrong with this.

If correcting the comparison function doesn't fix the problem, please send a complete example to us at vf-support@compaq.com and we'll take a look. I tried the following test case and it seemed to work fine:

program sort
use dfport
character*5 :: carray(5) = &
  (/'BBBBB','CCCCC','AAAAA','EEEEE','DDDDD'/)
integer(2), external :: compare
call qsort (carray, 5, 5, compare)
write (*,*) carray
end program sort
integer(2) function compare (arg1,arg2)
character*(*) arg1, arg2
if (arg1 > arg2) then
  compare = 1
else if (arg1 == arg2) then
  compare = 0
else
  compare = -1
  end if
return
end function compare


Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
940 Views
Makes me think... since QSORT is basically a stupid thing (written in C?)
which knows nothing about prototype of cmp_char (it knows only its address), how does it handle hidden string length arguments? !DEC$ATTRIBUTES REFERENCE on a1 and a2 IMO should help.
If I'm right, Steve, how does your example work? I'd expect that you get
that len(arg1)=loc(arg2), while arg2 contains garbage. What's actually the difference between declaring character*5 and character*(*) arg1, arg2?

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
940 Views
Jugoslav,

Read the declaration of QSORT in DFPORT.F90 and your questions will be answered. Briefly, it's a generic, with a specific routine for CHARACTER arguments. You can even extend the generic for your own types. Rather clever, I think.

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
940 Views
...and, Keith, Steve, what's the setting in your Project/Settings/Fortran/External Procedures/String length arg passing?
IMO for your examples to work, it should be "After all args", which is not the default?

Jugoslav
0 Kudos
Jugoslav_Dujic
Valued Contributor II
940 Views
Uh, Steve, you were so fast in replying that I didn't make it to read your
answer before I posted another. OK, you're right, I retreat and I state the opposite. Still, could the setting I mentioned possibly affect the mechanism (though with the default there apparently should be no problem)?

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
940 Views
This is why we created the DEFAULT attribute in 6.1 and used it in all our modules.

Steve
0 Kudos
kvrichardson
Beginner
940 Views
Thank you, gentlemen, once again. My qsort works great now.

I just had to add an Attribute statement - since I'm calling the integer function from within a callback, and I need the following statement for all my callbacks (since the project is mixed C++ and DVF):

!DEC$ ATTRIBUTES DEFAULT :: [subroutine name]

Keith Richardson
0 Kudos
kvrichardson
Beginner
940 Views
What I mean to say is, I had to add the attributes statement to the integer function, since I called it from within a callback that has that attributes statement. I guess they couldn't talk to one another otherwise.
0 Kudos
Reply