-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rs/commit-list-sort-in-batch'
Setting up a revision traversal with many starting points was inefficient as these were placed in a date-order priority queue one-by-one. By René Scharfe (3) and Junio C Hamano (1) * rs/commit-list-sort-in-batch: mergesort: rename it to llist_mergesort() revision: insert unsorted, then sort in prepare_revision_walk() commit: use mergesort() in commit_list_sort_by_date() add mergesort() for linked lists
- Loading branch information
Showing
8 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "cache.h" | ||
#include "mergesort.h" | ||
|
||
struct mergesort_sublist { | ||
void *ptr; | ||
unsigned long len; | ||
}; | ||
|
||
static void *get_nth_next(void *list, unsigned long n, | ||
void *(*get_next_fn)(const void *)) | ||
{ | ||
while (n-- && list) | ||
list = get_next_fn(list); | ||
return list; | ||
} | ||
|
||
static void *pop_item(struct mergesort_sublist *l, | ||
void *(*get_next_fn)(const void *)) | ||
{ | ||
void *p = l->ptr; | ||
l->ptr = get_next_fn(l->ptr); | ||
l->len = l->ptr ? (l->len - 1) : 0; | ||
return p; | ||
} | ||
|
||
void *llist_mergesort(void *list, | ||
void *(*get_next_fn)(const void *), | ||
void (*set_next_fn)(void *, void *), | ||
int (*compare_fn)(const void *, const void *)) | ||
{ | ||
unsigned long l; | ||
|
||
if (!list) | ||
return NULL; | ||
for (l = 1; ; l *= 2) { | ||
void *curr; | ||
struct mergesort_sublist p, q; | ||
|
||
p.ptr = list; | ||
q.ptr = get_nth_next(p.ptr, l, get_next_fn); | ||
if (!q.ptr) | ||
break; | ||
p.len = q.len = l; | ||
|
||
if (compare_fn(p.ptr, q.ptr) > 0) | ||
list = curr = pop_item(&q, get_next_fn); | ||
else | ||
list = curr = pop_item(&p, get_next_fn); | ||
|
||
while (p.ptr) { | ||
while (p.len || q.len) { | ||
void *prev = curr; | ||
|
||
if (!p.len) | ||
curr = pop_item(&q, get_next_fn); | ||
else if (!q.len) | ||
curr = pop_item(&p, get_next_fn); | ||
else if (compare_fn(p.ptr, q.ptr) > 0) | ||
curr = pop_item(&q, get_next_fn); | ||
else | ||
curr = pop_item(&p, get_next_fn); | ||
set_next_fn(prev, curr); | ||
} | ||
p.ptr = q.ptr; | ||
p.len = l; | ||
q.ptr = get_nth_next(p.ptr, l, get_next_fn); | ||
q.len = q.ptr ? l : 0; | ||
|
||
} | ||
set_next_fn(curr, NULL); | ||
} | ||
return list; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef MERGESORT_H | ||
#define MERGESORT_H | ||
|
||
/* | ||
* Sort linked list in place. | ||
* - get_next_fn() returns the next element given an element of a linked list. | ||
* - set_next_fn() takes two elements A and B, and makes B the "next" element | ||
* of A on the list. | ||
* - compare_fn() takes two elements A and B, and returns negative, 0, positive | ||
* as the same sign as "subtracting" B from A. | ||
*/ | ||
void *llist_mergesort(void *list, | ||
void *(*get_next_fn)(const void *), | ||
void (*set_next_fn)(void *, void *), | ||
int (*compare_fn)(const void *, const void *)); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "cache.h" | ||
#include "mergesort.h" | ||
|
||
struct line { | ||
char *text; | ||
struct line *next; | ||
}; | ||
|
||
static void *get_next(const void *a) | ||
{ | ||
return ((const struct line *)a)->next; | ||
} | ||
|
||
static void set_next(void *a, void *b) | ||
{ | ||
((struct line *)a)->next = b; | ||
} | ||
|
||
static int compare_strings(const void *a, const void *b) | ||
{ | ||
const struct line *x = a, *y = b; | ||
return strcmp(x->text, y->text); | ||
} | ||
|
||
int main(int argc, const char **argv) | ||
{ | ||
struct line *line, *p = NULL, *lines = NULL; | ||
struct strbuf sb = STRBUF_INIT; | ||
|
||
for (;;) { | ||
if (strbuf_getwholeline(&sb, stdin, '\n')) | ||
break; | ||
line = xmalloc(sizeof(struct line)); | ||
line->text = strbuf_detach(&sb, NULL); | ||
if (p) { | ||
line->next = p->next; | ||
p->next = line; | ||
} else { | ||
line->next = NULL; | ||
lines = line; | ||
} | ||
p = line; | ||
} | ||
|
||
lines = llist_mergesort(lines, get_next, set_next, compare_strings); | ||
|
||
while (lines) { | ||
printf("%s", lines->text); | ||
lines = lines->next; | ||
} | ||
return 0; | ||
} |