2009-03-22 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / src / sort.frag.h
blob2cf5a9ba1f3ea84c2b8e90ec014146f649323c9b
1 /*
2 * sort.frag.h: Common implementation of linked-list sorting
4 * Author:
5 * Raja R Harinath (rharinath@novell.com)
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * (C) 2006 Novell, Inc.
30 * This code requires a typedef named 'list_node' for the list node. It
31 * is assumed that the list type is the type of a pointer to a list
32 * node, and that the node has a field named 'next' that implements to
33 * the linked list. No additional invariant is maintained (e.g. the
34 * 'prev' pointer of a doubly-linked list node is _not_ updated). Any
35 * invariant would require a post-processing pass to fix matters if
36 * necessary.
38 typedef list_node *digit;
41 * The maximum possible depth of the merge tree
42 * = ceiling (log2 (maximum number of list nodes))
43 * = ceiling (log2 (maximum possible memory size/size of each list node))
44 * = number of bits in 'size_t' - floor (log2 (sizeof digit))
45 * Also, each list in sort_info is at least 2 nodes long: we can reduce the depth by 1
47 #define FLOOR_LOG2(x) (((x)>=2) + ((x)>=4) + ((x)>=8) + ((x)>=16) + ((x)>=32) + ((x)>=64) + ((x)>=128))
48 #define MAX_RANKS ((sizeof (size_t) * 8) - FLOOR_LOG2(sizeof (list_node)) - 1)
50 struct sort_info
52 int min_rank, n_ranks;
53 GCompareFunc func;
55 /* Invariant: ranks[i] == NULL || length(ranks[i]) >= 2**(i+1) */
56 list_node *ranks [MAX_RANKS]; /* ~ 128 bytes on 32bit, ~ 512 bytes on 64bit */
59 static inline void
60 init_sort_info (struct sort_info *si, GCompareFunc func)
62 si->min_rank = si->n_ranks = 0;
63 si->func = func;
64 /* we don't need to initialize si->ranks, since we never lookup past si->n_ranks. */
67 static inline list_node *
68 merge_lists (list_node *first, list_node *second, GCompareFunc func)
70 /* merge the two lists */
71 list_node *list = NULL;
72 list_node **pos = &list;
73 while (first && second) {
74 if (func (first->data, second->data) > 0) {
75 *pos = second;
76 second = second->next;
77 } else {
78 *pos = first;
79 first = first->next;
81 pos = &((*pos)->next);
83 *pos = first ? first : second;
84 return list;
87 /* Pre-condition: upto <= si->n_ranks, list == NULL || length(list) == 1 */
88 static inline list_node *
89 sweep_up (struct sort_info *si, list_node *list, int upto)
91 int i;
92 for (i = si->min_rank; i < upto; ++i) {
93 list = merge_lists (si->ranks [i], list, si->func);
94 si->ranks [i] = NULL;
96 return list;
100 * The 'ranks' array essentially captures the recursion stack of a mergesort.
101 * The merge tree is built in a bottom-up manner. The control loop for
102 * updating the 'ranks' array is analogous to incrementing a binary integer,
103 * and the O(n) time for counting upto n translates to O(n) merges when
104 * inserting rank-0 lists. When we plug in the sizes of the lists involved in
105 * those merges, we get the O(n log n) time for the sort.
107 * Inserting higher-ranked lists reduce the height of the merge tree, and also
108 * eliminate a lot of redundant comparisons when merging two lists that would've
109 * been part of the same run. Adding a rank-i list is analogous to incrementing
110 * a binary integer by 2**i in one operation, thus sharing a similar speedup.
112 * When inserting higher-ranked lists, we choose to clear out the lower ranks
113 * in the interests of keeping the sort stable, but this makes analysis harder.
114 * Note that clearing the lower-ranked lists is O(length(list))-- thus it
115 * shouldn't affect the O(n log n) behaviour. IOW, inserting one rank-i list
116 * is equivalent to inserting 2**i rank-0 lists, thus even if we do i additional
117 * merges in the clearing-out (taking at most 2**i time) we are still fine.
120 #define stringify2(x) #x
121 #define stringify(x) stringify2(x)
123 /* Pre-condition: 2**(rank+1) <= length(list) < 2**(rank+2) (therefore: length(list) >= 2) */
124 static inline void
125 insert_list (struct sort_info *si, list_node* list, int rank)
127 int i;
129 if (rank > si->n_ranks) {
130 if (rank > MAX_RANKS) {
131 g_warning ("Rank '%d' should not exceed " stringify (MAX_RANKS), rank);
132 rank = MAX_RANKS;
134 list = merge_lists (sweep_up (si, NULL, si->n_ranks), list, si->func);
135 for (i = si->n_ranks; i < rank; ++i)
136 si->ranks [i] = NULL;
137 } else {
138 if (rank)
139 list = merge_lists (sweep_up (si, NULL, rank), list, si->func);
140 for (i = rank; i < si->n_ranks && si->ranks [i]; ++i) {
141 list = merge_lists (si->ranks [i], list, si->func);
142 si->ranks [i] = NULL;
146 if (i == MAX_RANKS) /* Will _never_ happen: so we can just devolve into quadratic ;-) */
147 --i;
148 if (i >= si->n_ranks)
149 si->n_ranks = i + 1;
150 si->min_rank = i;
151 si->ranks [i] = list;
154 #undef stringify2
155 #undef stringify
156 #undef MAX_RANKS
157 #undef FLOOR_LOG2
159 /* A non-recursive mergesort */
160 static inline digit
161 do_sort (list_node* list, GCompareFunc func)
163 struct sort_info si;
165 init_sort_info (&si, func);
167 while (list && list->next) {
168 list_node* next = list->next;
169 list_node* tail = next->next;
171 if (func (list->data, next->data) > 0) {
172 next->next = list;
173 next = list;
174 list = list->next;
176 next->next = NULL;
178 insert_list (&si, list, 0);
180 list = tail;
183 return sweep_up (&si, list, si.n_ranks);