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


Groups > comp.lang.c > #149854

Re: template(ish) qsort() function possible in language standard?

From Anton Shepelev <anton.txt@gmail.com>
Newsgroups comp.lang.c
Subject Re: template(ish) qsort() function possible in language standard?
Date 2020-02-08 17:50 +0300
Organization A noiseless patient Spider
Message-ID <20200208175042.09677802619cd0c4789f03a6@gmail.com> (permalink)
References <r1f8cm$3mj$1@dont-email.me>

Show all headers | View raw


mathog:

> The C language's standard qsort() function requires a
> pointer to a comparison function.  Of course this works,
> but the overhead of all those function calls can be
> substantial for a large sort.

My opinion is that the general qsort() routine is fast
enough for the overwhelming majority of applications,
whereas in the rare cases when one needs as fast a sort as
possible, one is welcome to write a specific function of
one's own.  Swaps, by the way, may be long too if the
elements are large, so you may want to sort pointers or
array indices.

An alternative semi-generic implementation without a
comparison function is possible, i.e.:

    sort
    (  void*   data,    /* data to be sorted          */
       size_t  count    /* number of elements         */
       size_t  elemsz   /* element size               */
       size_t  cmp_ofs, /* offset of comparison field */
       size_t  cmp_len  /* size   of comparison field */
       char    invert   /* inverse order (descending) */
    )

Such a funciton would compare elements via something like:

    strncmp( elem_a + cmp_ofs, elem_b + cmp_ofs, cmp_len )

Is it not, also, possible to write a macro-based generic
sort that would accept the comparison code as another macro
and expand into a specialised sort routine?

-- 
()  ascii ribbon campaign -- against html e-mail
/\  http://preview.tinyurl.com/qcy6mjc [archived]

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


Thread

template(ish) qsort() function possible in language standard? mathog <dmathog@gmail.com> - 2020-02-05 12:25 -0800
  Re: template(ish) qsort() function possible in language standard? Robert Wessel <robertwessel2@yahoo.com> - 2020-02-05 18:25 -0600
    Re: template(ish) qsort() function possible in language standard? mathog <dmathog@gmail.com> - 2020-02-06 10:02 -0800
      Re: template(ish) qsort() function possible in language standard? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-06 13:20 -0800
        Re: template(ish) qsort() function possible in language standard? mathog <dmathog@gmail.com> - 2020-02-06 15:24 -0800
          Re: template(ish) qsort() function possible in language standard? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-06 15:38 -0800
  Re: template(ish) qsort() function possible in language standard? Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-06 20:56 +0100
  Re: template(ish) qsort() function possible in language standard? Anton Shepelev <anton.txt@gmail.com> - 2020-02-08 17:50 +0300

csiph-web