1 #ifndef CGPERF_KEYWORD_LIST_C
2 #define CGPERF_KEYWORD_LIST_C
6 #include "keyword_list.h"
7 /*------------------------------------------------------------------------------------------------*/
8 #include "namespace/keyword.h"
9 #include "namespace/keyword_list.h"
10 /*------------------------------------------------------------------------------------------------*/
12 static struct Keyword_List
*kwl_new(struct Keyword
*kw
)
14 struct Keyword_List
*t
;
16 t
= calloc(1, sizeof(*t
));
21 static void kwl_del(struct Keyword_List
*t
)
26 static void delete_list(struct Keyword_List
*list
)
29 struct Keyword_List
*next
;
38 /*{{{ copy_list_ext */
39 static struct Keyword_List
*copy_list(struct Keyword_List
*list
)
41 struct Keyword_List
*result
;
42 struct Keyword_List
**lastp
;
46 struct Keyword_List
*new_cons
;
50 new_cons
= kwl_new(list
->kw
);
52 lastp
= &new_cons
->next
;
58 /*{{{ mergesort_list */
60 * Sorts a linear list, given a comparison function.
61 * Note: This uses a variant of mergesort that is *not* a stable sorting algorithm.
63 static struct Keyword_List
*mergesort_list(struct Keyword_List
*list
, bool (*less
)(
64 struct Keyword
*kw1
, struct Keyword
*kw2
))
66 struct Keyword_List
*middle
;
67 struct Keyword_List
*tmp
;
68 struct Keyword_List
*right_half
;
70 if (list
== 0 || list
->next
== 0)
71 /* List of length 0 or 1. Nothing to do. */
80 middle
= middle
->next
;;
85 * Cut the list into two halves.
86 * If the list has n elements, the left half has ceiling(n/2) elements and the right half
87 * has floor(n/2) elements.
89 right_half
= middle
->next
;
91 return merge(mergesort_list(list
, less
), mergesort_list(right_half
, less
), less
);
94 static struct Keyword_List
*merge(struct Keyword_List
*list1
, struct Keyword_List
*list2
, bool
95 (*less
)(struct Keyword
*kw1
, struct Keyword
*kw2
))
97 struct Keyword_List
*result
;
98 struct Keyword_List
**resultp
;
110 if (less(list2
->kw
, list1
->kw
)) {
112 resultp
= &list2
->next
;
114 * We would have a stable sorting if the next line would read: list2 =
121 resultp
= &list1
->next
;
127 /*------------------------------------------------------------------------------------------------*/
129 #include "namespace/keyword.h"
130 #include "namespace/keyword_list.h"
132 /*------------------------------------------------------------------------------------------------*/