| 1 |
/* genmergesort.h - generate code to do a mergesort */ |
| 2 |
|
| 3 |
/* Copyright 1991 Mark Russell, University of Kent at Canterbury. |
| 4 |
* |
| 5 |
* You can do what you like with this source code as long as |
| 6 |
* you don't try to make money out of it and you include an |
| 7 |
* unaltered copy of this message (including the copyright). |
| 8 |
*/ |
| 9 |
|
| 10 |
/* @(#)genmergesort.h 1.6 26/4/92 (UKC) */ |
| 11 |
|
| 12 |
/* GENERIC_MERGE_SORT generates the code necessary to mergesort |
| 13 |
* an arbitrary linked list. The arguments are: |
| 14 |
* |
| 15 |
* storageclass: |
| 16 |
* should be static or extern. Detrmines the storage class |
| 17 |
* of the function generated. |
| 18 |
* |
| 19 |
* name: the name of the function to be generated |
| 20 |
* |
| 21 |
* type: |
| 22 |
* the type of the elements of the list |
| 23 |
* for a structure xxst, the type will be: struct xxst |
| 24 |
* |
| 25 |
* next: |
| 26 |
* the name of the field linking the list |
| 27 |
* |
| 28 |
* As an example, consider a structure struct xxst, with a link field |
| 29 |
* "struct xxst *xx_next". Then the following macro: |
| 30 |
* |
| 31 |
* GENERIC_MERGE_SORT(static, sortxx, struct xxst, xx_next) |
| 32 |
* |
| 33 |
* would generate a static function sortxx: |
| 34 |
* |
| 35 |
* static struct xxst *sortxx(list, len, cmp) |
| 36 |
* struct xxst *list; |
| 37 |
* int len, (*cmp)(); |
| 38 |
* |
| 39 |
* where list is the head of the list to be sorted, len is the length |
| 40 |
* of this list, and cmp is a comparison function, called in the style |
| 41 |
* |
| 42 |
* (*cmp)(xx1, xx2) |
| 43 |
* |
| 44 |
* This should return -1, 0 or 1 according as the first argument should |
| 45 |
* be considered less than, equal to, or greater than the second. |
| 46 |
* |
| 47 |
* sortxx() returns a pointer to the head of the sorted list. |
| 48 |
* |
| 49 |
* The macro also generates the static functions split_name() and |
| 50 |
* merge_name, where "name" is what you supplied to the macro. |
| 51 |
* These functions are for the use of the sort routine only. |
| 52 |
*/ |
| 53 |
|
| 54 |
#define GENERIC_MERGE_SORT(storageclass,name,type,next) \ |
| 55 |
\ |
| 56 |
/* Mergesort a linked list of structures of type type. |
| 57 |
* List is a pointer to the head of the list, len is the number of |
| 58 |
* elements in the list, and cmp is the comparison function |
| 59 |
* as described above. Return a pointer to the head of the sorted list. |
| 60 |
* |
| 61 |
* Uses the standard recursive mergesort algorithm (see e.g. Kruse) |
| 62 |
*/ \ |
| 63 |
\ |
| 64 |
static type *CAT(name,_split)(type *list, int len);\ |
| 65 |
\ |
| 66 |
/* split list, which has len elements at element len/2. Return |
| 67 |
* a pointer to the second half of the list. |
| 68 |
*/ \ |
| 69 |
static type *CAT(name,_split)(type *list,int len) \ |
| 70 |
{ \ |
| 71 |
type *prev; \ |
| 72 |
\ |
| 73 |
prev = NULL; /* to satisfy gcc */ \ |
| 74 |
for (; len > 0; --len) { \ |
| 75 |
prev = list; \ |
| 76 |
list = list->next; \ |
| 77 |
} \ |
| 78 |
prev->next = NULL; \ |
| 79 |
return(list); \ |
| 80 |
} \ |
| 81 |
\ |
| 82 |
static type *CAT(name,_merge)(type *list1, type *list2, int (*cmp)(type *, type *));\ |
| 83 |
\ |
| 84 |
/* Merge the two lists list1 and list2, keeping the resulting |
| 85 |
* list in order with respect to comparison function cmp (see |
| 86 |
* above for specification of cmp. |
| 87 |
* Return a pointer to the merged list. |
| 88 |
*/ \ |
| 89 |
static type *CAT(name,_merge)(register type *list1,register type *list2, \ |
| 90 |
int (*cmp)(type *, type *)) \ |
| 91 |
{ \ |
| 92 |
register type *list, *head; \ |
| 93 |
\ |
| 94 |
if ((*cmp)(list1, list2) < 0) { \ |
| 95 |
list = list1; \ |
| 96 |
list1 = list1->next; \ |
| 97 |
} \ |
| 98 |
else { \ |
| 99 |
list = list2; \ |
| 100 |
list2 = list2->next; \ |
| 101 |
} \ |
| 102 |
head = list; \ |
| 103 |
while (list1 != NULL && list2 != NULL) { \ |
| 104 |
if ((*cmp)(list1, list2) < 0) { \ |
| 105 |
list->next = list1; \ |
| 106 |
list1 = list1->next; \ |
| 107 |
} \ |
| 108 |
else { \ |
| 109 |
list->next = list2; \ |
| 110 |
list2 = list2->next; \ |
| 111 |
} \ |
| 112 |
list = list->next; \ |
| 113 |
} \ |
| 114 |
list->next = (list1 != NULL) ? list1 : list2; \ |
| 115 |
return(head); \ |
| 116 |
} \ |
| 117 |
\ |
| 118 |
storageclass type *name(type *list, int len, int (*cmp)(type *, type *));\ |
| 119 |
\ |
| 120 |
storageclass type *name(type *list,int len, int (*cmp)(type *, type *)) \ |
| 121 |
{ \ |
| 122 |
type *list2; \ |
| 123 |
\ |
| 124 |
if (len > 1) { \ |
| 125 |
list2 = CAT(name,_split)(list,len/2); \ |
| 126 |
list = name(list,len/2,cmp); \ |
| 127 |
list2 = name(list2,(len+1)/2,cmp); \ |
| 128 |
return(CAT(name,_merge)(list,list2,cmp)); \ |
| 129 |
} \ |
| 130 |
else \ |
| 131 |
return(list); \ |
| 132 |
} |