Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #143572

Re: qsort() standard functions

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: qsort() standard functions
Date 2019-05-02 09:46 -0700
Organization A noiseless patient Spider
Message-ID <86h8acg5y5.fsf@mailhub.linuxsc.com> (permalink)
References (6 earlier) <20190426164329.8cd319730cce76ab734f5ac6@g{oogle}mail.com> <87ef5osoib.fsf@bsb.me.uk> <20190427145541.4f9f1d28cc434e9136029307@gmail.com> <878svvsg52.fsf@bsb.me.uk> <c919ce5osb9vloiag3ugnusnjf40191gr2@4ax.com>

Show all headers | View raw


Robert Wessel <robertwessel2@yahoo.com> writes:

> On Sat, 27 Apr 2019 14:59:37 +0100, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
>
>> Anton Shepelev <anton.txt@gmail.com> writes:
>>
>>> Ben Bacarisse to Anton Shepelev:
>>
>> <cut>
>>
>>>> I think it's better to consider what's logical rather than
>>>> what you (or I) can think of.  But there are certainly
>>>> other uses of non-const target extra data.
>>>
>>> If so, you might want to have `extra' not only in the
>>> comparison function but also in the swap function, in order
>>> for example to gather statistics about the number of swap
>>> operations.
>>
>> That's an interesting thought.  Yes, in a language without
>> first-class functions it is probably wise to pass some context
>> pointer to every call-back at least in general-purpose utilities
>> like sorting.
>>
>>>>> Not a big deal if I do, though, except if one is a
>>>>> devotee of const correctness...
>>>>
>>>> Again, I'm not [sure] what you mean here.  It seems as if
>>>> you are saying that if you did make 'extra' a 'const void
>>>> *', that would annoy devotees of const correctness.  Seems
>>>> like the other way round to me, but many I've missed your
>>>> meaning.
>>>
>>> I meant avoiding `const' before `extra' for the rare case of
>>> debugging and gathering statistics whereas in actual use
>>> `extra' is very unlikely to change.
>>
>> I can see you won't be persuaded, but it's not about 'avoiding'
>> const, it's about const being, in my opinion, wrong.  To take
>> another example, if the comparison is expensive, you might want to
>> store some derived key in the extra data.
>
> I don't really have a strong opinion either way, but since the
> "extra" area is provided and used (only) by the user of the sort
> function, I'm not so sure that requiring the user to cast away the
> const-ness, if the user wants that, is really all that wrong.  This
> is in keeping with the not uncommon practice of objects that have
> hidden state still being treated as "const" in many contexts.

My reaction is very much the opposite.  The point of providing the
'extra' parameter is for the convenience of the client, and so
should accommodate as wide a range of use cases as it reasonably
can.  We do not know that a client data area will be used only
with calls to the sort function - it might be used in conjunction
with several different callback functions, for all sorts (no pun
intended) of purposes.  Furthermore the combination of a function
pointer and a void * pointer is a common idiom in C to provide the
functionality of a closure.  Surely the idiom expects that data
in the "closure" might be modified, and so a 'void *' type rather
than a 'const void *' type is more usual, and more appropriate.
Since the 'extra' parameter (almost always) needs to be converted
by the callback function, that function can choose a 'const Foo *'
type if const-ness is important there.  Forcing client code to put
in an unnecessary cast says in effect the sort routine knows better
than its callers.  Service routines such as sort should accommodate
their callers, not the other way around.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

qsort() standard functions rick.c.hodgin@gmail.com - 2019-04-24 09:08 -0700
  Re: qsort() standard functions gazelle@shell.xmission.com (Kenny McCormack) - 2019-04-24 16:12 +0000
    Re: qsort() standard functions rick.c.hodgin@gmail.com - 2019-04-24 09:18 -0700
      Re: qsort() standard functions Chad <cdalten@gmail.com> - 2019-05-02 12:02 -0700
    Re: qsort() standard functions Real Troll <real.troll@trolls.com> - 2019-04-24 22:30 -0400
      Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 19:32 -0700
        Re: qsort() standard functions Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-04-25 10:19 +0000
          Re: qsort() standard functions rick.c.hodgin@gmail.com - 2019-04-25 04:09 -0700
            Re: qsort() standard functions Richard Damon <Richard@Damon-Family.org> - 2019-04-25 08:13 -0400
              Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-25 06:39 -0700
              Re: qsort() standard functions already5chosen@yahoo.com - 2019-04-25 07:23 -0700
      Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-25 00:39 -0700
        Re: qsort() standard functions Real Troll <Real.Troll@Trolls.com> - 2019-04-25 23:27 +0100
  Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 09:34 -0700
    Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 09:47 -0700
    Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 09:47 -0700
      Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 12:54 -0400
        Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 11:33 -0700
          Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 14:41 -0400
            Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 11:52 -0700
              Re: qsort() standard functions gazelle@shell.xmission.com (Kenny McCormack) - 2019-04-24 19:12 +0000
              Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 16:05 -0400
                Re: qsort() standard functions queequeg@trust.no1 (Queequeg) - 2019-04-24 20:39 +0000
            Re: qsort() standard functions James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-24 16:33 -0700
      Re: qsort() standard functions gazelle@shell.xmission.com (Kenny McCormack) - 2019-04-24 19:18 +0000
        Re: qsort() standard functions Bart <bc@freeuk.com> - 2019-04-24 20:28 +0100
          Re: qsort() standard functions gazelle@shell.xmission.com (Kenny McCormack) - 2019-04-24 21:27 +0000
        Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 12:31 -0700
        Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 13:02 -0700
          Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 13:41 -0700
            Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 14:00 -0700
              Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-25 00:22 +0300
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 15:04 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 15:20 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 15:40 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 15:57 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 16:06 -0700
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-25 23:49 +0300
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-25 22:45 +0100
                Re: qsort() standard functions Bart <bc@freeuk.com> - 2019-04-25 23:00 +0100
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-25 23:24 +0100
                Re: qsort() standard functions Real Troll <Real.Troll@Trolls.com> - 2019-04-25 23:20 +0100
                Re: qsort() standard functions Anton Shepelev <anton.txt@g{oogle}mail.com> - 2019-04-26 13:06 +0300
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-26 13:38 +0100
                Re: qsort() standard functions Anton Shepelev <anton.txt@g{oogle}mail.com> - 2019-04-26 16:43 +0300
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-26 17:46 +0100
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-27 14:55 +0300
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-27 14:59 +0100
                Re: qsort() standard functions Robert Wessel <robertwessel2@yahoo.com> - 2019-04-27 11:41 -0500
                Re: qsort() standard functions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-05-02 09:46 -0700
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-02 20:35 +0300
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-05-02 15:35 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-05-05 04:39 -0700
                Re: qsort() standard functions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-05-04 07:35 -0700
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-05 00:01 +0300
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-02 15:11 +0300
                Re: qsort() standard functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-05-02 16:36 +0100
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-27 14:47 +0300
                Re: qsort() standard functions Robert Wessel <robertwessel2@yahoo.com> - 2019-04-27 08:30 -0500
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-02 14:52 +0300
                Re: qsort() standard functions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-05-04 07:13 -0700
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-05 22:57 +0300
                Re: qsort() standard functions David Brown <david.brown@hesbynett.no> - 2019-05-06 10:09 +0200
                Re: qsort() standard functions Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-06-01 05:22 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-25 17:09 -0700
                Re: qsort() standard functions Bart <bc@freeuk.com> - 2019-04-26 10:58 +0100
                Re: qsort() standard functions mark.bluemel@gmail.com - 2019-04-26 03:59 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 06:34 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 07:16 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 07:45 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 08:29 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 12:06 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 13:09 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 13:37 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-26 14:05 -0700
                Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-27 14:59 +0300
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-27 06:19 -0700
                Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-27 06:32 -0700
  Re: qsort() standard functions Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-04-24 10:54 -0600
    Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 10:10 -0700
    Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-04-25 00:06 +0300
  Re: qsort() standard functions scott@slp53.sl.home (Scott Lurndal) - 2019-04-24 17:37 +0000
    Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 10:56 -0700
      Re: qsort() standard functions Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-04-24 12:12 -0600
        Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 14:27 -0400
          Re: qsort() standard functions John Bode <jfbode1029@gmail.com> - 2019-04-24 11:44 -0700
            Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 11:57 -0700
              Re: qsort() standard functions fir <profesor.fir@gmail.com> - 2019-04-24 12:04 -0700
            Re: qsort() standard functions nobody@example.org (Scott) - 2019-04-25 01:16 +0000
              Re: qsort() standard functions James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-24 21:38 -0400
  Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 14:42 -0400
    Re: qsort() standard functions David Brown <david.brown@hesbynett.no> - 2019-04-24 22:13 +0200
    Re: qsort() standard functions queequeg@trust.no1 (Queequeg) - 2019-04-24 20:46 +0000
  Re: qsort() standard functions jadill33@gmail.com - 2019-04-24 14:01 -0700
  Re: qsort() standard functions "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-04-24 15:41 -0700
  Re: qsort() standard functions "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-04-26 22:24 +0800
    Re: qsort() standard functions Anton Shepelev <anton.txt@gmail.com> - 2019-05-02 15:05 +0300
      Re: qsort() standard functions "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-05-03 17:02 +0800
        Re: qsort() standard functions Chad <cdalten@gmail.com> - 2019-05-03 10:10 -0700
          Re: qsort() standard functions "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-05-04 01:22 +0800
            Re: qsort() standard functions Chad <cdalten@gmail.com> - 2019-05-03 15:14 -0700
              [OT]Eternal.... programmer? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2019-05-04 06:20 +0800
                Re: [OT]Eternal.... programmer? Chad <cdalten@gmail.com> - 2019-05-04 05:53 -0700

csiph-web