From: Junio C Hamano <junkio@cox.net>
To: Morten Welinder <mwelinder@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: type_size_sort
Date: Tue, 06 Dec 2005 18:28:22 -0800 [thread overview]
Message-ID: <7vmzjdy721.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <118833cc0512061651y57ddcdc7pd26996b40c08d225@mail.gmail.com> (Morten Welinder's message of "Tue, 6 Dec 2005 19:51:44 -0500")
Morten Welinder <mwelinder@gmail.com> writes:
>> It's perfectly correct. If the same list was to be passed to
>> create_sorted_list() twice it will come out exactly the same the second
>> time as it did the first. The only thing to remark on is that the return
>> above could be written as below instead:
>>
>> return a - b;
>
> That is not what the part of the standard I quoted says. It very
> clearly forbids the sorting function from depending on the pointers'
> values. I can even see an implementation actually using this
> requirement.
What you say is correct, and people should be careful when using
qsort(), but it does not apply to this case. The patch I posted
is not necessary. Andreas' rewrite quoted above, however, is
invalid when ptrdiff_t (a-b) overflows int range.
The code as written by Linus back on June 25 is correct, but
that sort callchain is written in an unusual way to confuse us
(you were, and I was initially after seeing your message).
(1) The caller look like this. It prepares an array of pointers
in list[], calls qsort with sort_comparator() as the
function to sort this list[]. Each element in this list[]
is a pointer to struct object_entry.
for (i = 0; i < nr_objects; i++)
list[i] = objects + i;
current_sort = sort;
qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
(2) sort_comparator() is called by qsort() with the standard
calling convention -- two pointers pointing into the array
being sorted. It calls current_sort() function set up by
(1), giving it the pointers *FETCHED* *FROM* the locations
these two incoming pointers are pointing at.
static entry_sort_t current_sort;
static int sort_comparator(const void *_a, const void *_b)
{
struct object_entry *a = *(struct object_entry **)_a;
struct object_entry *b = *(struct object_entry **)_b;
return current_sort(a,b);
}
(3) Then type_size_sort() uses compariosn of these two pointers
as the tiebreaker.
Notice? The comparison of two pointers you originally pointed
out is not about the location in list[]. They are values stored
there.
next prev parent reply other threads:[~2005-12-07 2:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-12-06 21:19 type_size_sort Morten Welinder
2005-12-06 21:38 ` type_size_sort Andreas Ericsson
2005-12-06 21:46 ` type_size_sort Junio C Hamano
2005-12-07 0:51 ` type_size_sort Morten Welinder
2005-12-07 2:28 ` Junio C Hamano [this message]
2005-12-07 3:01 ` type_size_sort Morten Welinder
2005-12-06 21:45 ` type_size_sort Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7vmzjdy721.fsf@assigned-by-dhcp.cox.net \
--to=junkio@cox.net \
--cc=git@vger.kernel.org \
--cc=mwelinder@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).