1
/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
things to define before including the file:
20
#define LS_LIST_ITEM ListItem
21
#define LS_COMPARE_FUNC_DECL compare_func var_name,
22
#define LS_COMPARE_FUNC_CALL(list_el1, list_el2) (*var_name)(list_el1, list_el2)
23
#define LS_NEXT(A) (A)->next
24
#define LS_SET_NEXT(A,val) (A)->next= val
25
#define LS_P_NEXT(A) &(A)->next
26
#define LS_NAME plistsort
27
#define LS_SCOPE static
28
#define LS_STRUCT_NAME ls_struct_name
31
typedef struct LS_STRUCT_NAME
38
LS_SCOPE LS_LIST_ITEM* LS_NAME(LS_COMPARE_FUNC_DECL LS_LIST_ITEM *list, int list_len)
40
LS_LIST_ITEM *list_end;
41
LS_LIST_ITEM *sorted_list;
43
struct LS_STRUCT_NAME stack[63], *sp= stack;
48
sp->list_len= list_len;
55
LS_LIST_ITEM *e1, *e2;
57
e1= LS_NEXT(sorted_list);
58
list_end= LS_NEXT(e1);
59
if (LS_COMPARE_FUNC_CALL(sorted_list, e1))
64
if (sp->list_len == 2)
66
LS_SET_NEXT(sorted_list, e1);
67
LS_SET_NEXT(e1, NULL);
71
list_end= LS_NEXT(e2);
72
if (LS_COMPARE_FUNC_CALL(e1, e2))
75
LS_LIST_ITEM *tmp_e= e1;
79
if (LS_COMPARE_FUNC_CALL(sorted_list, e1))
81
LS_LIST_ITEM *tmp_e= sorted_list;
87
LS_SET_NEXT(sorted_list, e1);
89
LS_SET_NEXT(e2, NULL);
94
register struct LS_STRUCT_NAME *sp0= sp++;
95
sp->list_len= sp0->list_len >> 1;
96
sp0->list_len-= sp->list_len;
101
sp->list1= sorted_list;
103
register struct LS_STRUCT_NAME *sp0= sp++;
105
sp->list_len= sp0->list_len;
108
goto recursion_point;
111
register LS_LIST_ITEM **hook= &sorted_list;
112
register LS_LIST_ITEM *list1= sp->list1;
113
register LS_LIST_ITEM *list2= sorted_list;
115
if (LS_COMPARE_FUNC_CALL(list1, list2))
117
LS_LIST_ITEM *tmp_e= list2;
126
if (!(list1= *(hook= LS_P_NEXT(list1))))
131
} while (LS_COMPARE_FUNC_CALL(list2, list1));
136
if (!(list2= *(hook= LS_P_NEXT(list2))))
141
} while (LS_COMPARE_FUNC_CALL(list1, list2));
146
switch ((sp--)->return_point)
148
case 0: goto return_point0;
149
case 1: goto return_point1;
162
#undef LS_STRUCT_NAME
164
#undef LS_COMPARE_FUNC_DECL
165
#undef LS_COMPARE_FUNC_CALL