Skip to content

Commit 7365c95

Browse files
committed
mergesort: rename it to llist_mergesort()
Even though the function is generic enough, <anything>sort() inherits connotations from the standard function qsort() that sorts an array. Rename it to llist_mergesort() and describe the external interface in its header file. This incidentally avoids name clashes with mergesort() some platforms declare in, and contaminate user namespace with, their <stdlib.h>. Reported-by: Brian Gernhardt Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbc08ea commit 7365c95

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

commit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ static void commit_list_set_next(void *a, void *next)
429429

430430
void commit_list_sort_by_date(struct commit_list **list)
431431
{
432-
*list = mergesort(*list, commit_list_get_next, commit_list_set_next,
433-
commit_list_compare_by_date);
432+
*list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
433+
commit_list_compare_by_date);
434434
}
435435

436436
struct commit *pop_most_recent_commit(struct commit_list **list,

mergesort.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ static void *pop_item(struct mergesort_sublist *l,
2323
return p;
2424
}
2525

26-
void *mergesort(void *list,
27-
void *(*get_next_fn)(const void *),
28-
void (*set_next_fn)(void *, void *),
29-
int (*compare_fn)(const void *, const void *))
26+
void *llist_mergesort(void *list,
27+
void *(*get_next_fn)(const void *),
28+
void (*set_next_fn)(void *, void *),
29+
int (*compare_fn)(const void *, const void *))
3030
{
3131
unsigned long l;
3232

mergesort.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#ifndef MERGESORT_H
22
#define MERGESORT_H
33

4-
void *mergesort(void *list,
5-
void *(*get_next_fn)(const void *),
6-
void (*set_next_fn)(void *, void *),
7-
int (*compare_fn)(const void *, const void *));
4+
/*
5+
* Sort linked list in place.
6+
* - get_next_fn() returns the next element given an element of a linked list.
7+
* - set_next_fn() takes two elements A and B, and makes B the "next" element
8+
* of A on the list.
9+
* - compare_fn() takes two elements A and B, and returns negative, 0, positive
10+
* as the same sign as "subtracting" B from A.
11+
*/
12+
void *llist_mergesort(void *list,
13+
void *(*get_next_fn)(const void *),
14+
void (*set_next_fn)(void *, void *),
15+
int (*compare_fn)(const void *, const void *));
816

917
#endif

test-mergesort.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int main(int argc, const char **argv)
4242
p = line;
4343
}
4444

45-
lines = mergesort(lines, get_next, set_next, compare_strings);
45+
lines = llist_mergesort(lines, get_next, set_next, compare_strings);
4646

4747
while (lines) {
4848
printf("%s", lines->text);

0 commit comments

Comments
 (0)