1 | *** ./pan/base/msort.c 2001-06-18 11:33:38.000000000 -0700
|
---|
2 | --- /home/peirthies/cvs/pan/pan/base/msort.c 2004-03-17 21:08:08.000000000 -0800
|
---|
3 | ***************
|
---|
4 | *** 5,37 ****
|
---|
5 | * then a second, provides an quick & easy way to get a two-key sort.
|
---|
6 | * This isn't possible without a stable sorter.
|
---|
7 | *
|
---|
8 | */
|
---|
9 |
|
---|
10 | /* An alternative to qsort, with an identical interface.
|
---|
11 | This file is part of the GNU C Library.
|
---|
12 | ! Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
|
---|
13 | Written by Mike Haertel, September 1988.
|
---|
14 |
|
---|
15 | The GNU C Library is free software; you can redistribute it and/or
|
---|
16 | ! modify it under the terms of the GNU Library General Public License as
|
---|
17 | ! published by the Free Software Foundation; either version 2 of the
|
---|
18 | ! License, or (at your option) any later version.
|
---|
19 |
|
---|
20 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
21 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
23 | ! Library General Public License for more details.
|
---|
24 |
|
---|
25 | ! You should have received a copy of the GNU Library General Public
|
---|
26 | ! License along with the GNU C Library; see the file COPYING.LIB. If not,
|
---|
27 | ! write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
28 | ! Boston, MA 02111-1307, USA. */
|
---|
29 |
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <errno.h>
|
---|
33 | #include <pan/base/pan-glib-extensions.h>
|
---|
34 |
|
---|
35 | static void
|
---|
36 | msort_with_tmp (
|
---|
37 | void *b,
|
---|
38 | --- 5,46 ----
|
---|
39 | * then a second, provides an quick & easy way to get a two-key sort.
|
---|
40 | * This isn't possible without a stable sorter.
|
---|
41 | *
|
---|
42 | + * Loosely adapted from glibc's libc/stdlib/msort.c revision 1.21.
|
---|
43 | + * op_t and OPSIZ comment & macro from memcmp.c revision 1.3
|
---|
44 | + *
|
---|
45 | */
|
---|
46 |
|
---|
47 | /* An alternative to qsort, with an identical interface.
|
---|
48 | This file is part of the GNU C Library.
|
---|
49 | ! Copyright (C) 1992,95-97,99,2000,01,02,04 Free Software Foundation, Inc.
|
---|
50 | Written by Mike Haertel, September 1988.
|
---|
51 |
|
---|
52 | The GNU C Library is free software; you can redistribute it and/or
|
---|
53 | ! modify it under the terms of the GNU Lesser General Public
|
---|
54 | ! License as published by the Free Software Foundation; either
|
---|
55 | ! version 2.1 of the License, or (at your option) any later version.
|
---|
56 |
|
---|
57 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
58 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
59 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
60 | ! Lesser General Public License for more details.
|
---|
61 |
|
---|
62 | ! You should have received a copy of the GNU Lesser General Public
|
---|
63 | ! License along with the GNU C Library; if not, write to the Free
|
---|
64 | ! Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
65 | ! 02111-1307 USA. */
|
---|
66 |
|
---|
67 | #include <stdlib.h>
|
---|
68 | #include <string.h>
|
---|
69 | #include <errno.h>
|
---|
70 | #include <pan/base/pan-glib-extensions.h>
|
---|
71 |
|
---|
72 | + /* Type to use for aligned memory operations.
|
---|
73 | + This should normally be the biggest type supported by a single load
|
---|
74 | + and store. Must be an unsigned type. */
|
---|
75 | + #define op_t unsigned long int
|
---|
76 | + #define OPSIZ (sizeof(op_t))
|
---|
77 | +
|
---|
78 | static void
|
---|
79 | msort_with_tmp (
|
---|
80 | void *b,
|
---|
81 | *************** msort_with_tmp (
|
---|
82 | *** 43,49 ****
|
---|
83 | char *tmp;
|
---|
84 | char *b1, *b2;
|
---|
85 | size_t n1, n2;
|
---|
86 | - const int opsiz = sizeof(unsigned long int);
|
---|
87 |
|
---|
88 | if (n <= 1)
|
---|
89 | return;
|
---|
90 | --- 52,57 ----
|
---|
91 | *************** msort_with_tmp (
|
---|
92 | *** 58,78 ****
|
---|
93 |
|
---|
94 | tmp = t;
|
---|
95 |
|
---|
96 | ! if (s == opsiz && (b1 - (char *) 0) % opsiz == 0)
|
---|
97 | /* operating on aligned words. Use direct word stores. */
|
---|
98 | while (n1 > 0 && n2 > 0)
|
---|
99 | {
|
---|
100 | if ((*cmp) (b1, b2) <= 0)
|
---|
101 | {
|
---|
102 | --n1;
|
---|
103 | ! *((unsigned long int *) tmp)++ =
|
---|
104 | ! *((unsigned long int *) b1)++;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | --n2;
|
---|
109 | ! *((unsigned long int *) tmp)++ =
|
---|
110 | ! *((unsigned long int *) b2)++;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else
|
---|
114 | --- 66,88 ----
|
---|
115 |
|
---|
116 | tmp = t;
|
---|
117 |
|
---|
118 | ! if (s == OPSIZ && (b1 - (char *) 0) % OPSIZ == 0)
|
---|
119 | /* operating on aligned words. Use direct word stores. */
|
---|
120 | while (n1 > 0 && n2 > 0)
|
---|
121 | {
|
---|
122 | if ((*cmp) (b1, b2) <= 0)
|
---|
123 | {
|
---|
124 | --n1;
|
---|
125 | ! *((op_t *) tmp) = *((op_t *) b1);
|
---|
126 | ! tmp += sizeof (op_t);
|
---|
127 | ! b1 += sizeof (op_t);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | --n2;
|
---|
132 | ! *((op_t *) tmp) = *((op_t *) b2);
|
---|
133 | ! tmp += sizeof (op_t);
|
---|
134 | ! b2 += sizeof (op_t);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | else
|
---|
138 | *************** msort_with_tmp (
|
---|
139 | *** 99,119 ****
|
---|
140 | }
|
---|
141 |
|
---|
142 | void
|
---|
143 | ! msort (void *b, size_t n, size_t s, int(*cmp)(const void *, const void*))
|
---|
144 | {
|
---|
145 | const size_t size = n * s;
|
---|
146 |
|
---|
147 | ! int save = errno;
|
---|
148 | ! char *tmp = malloc (size);
|
---|
149 | ! if (tmp == NULL)
|
---|
150 | {
|
---|
151 | ! /* Couldn't get space, so fall back to the system sorter */
|
---|
152 | ! qsort (b, n, s, cmp);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | ! msort_with_tmp (b, n, s, cmp, tmp);
|
---|
157 | ! free (tmp);
|
---|
158 | }
|
---|
159 | - errno = save;
|
---|
160 | }
|
---|
161 | --- 109,140 ----
|
---|
162 | }
|
---|
163 |
|
---|
164 | void
|
---|
165 | ! msort (void *b, size_t n, size_t s, int(*cmp)(const void*, const void*))
|
---|
166 | {
|
---|
167 | const size_t size = n * s;
|
---|
168 |
|
---|
169 | ! if (size < 1024)
|
---|
170 | {
|
---|
171 | ! void *buf = g_alloca (size);
|
---|
172 | !
|
---|
173 | ! /* The temporary array is small, so put it on the stack. */
|
---|
174 | ! msort_with_tmp (b, n, s, cmp, buf);
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | ! int save = errno;
|
---|
179 | ! char *tmp = malloc (size);
|
---|
180 | ! if (tmp == NULL)
|
---|
181 | ! {
|
---|
182 | ! /* Couldn't get space, so use the slower algorithm
|
---|
183 | ! that doesn't need a temporary array. */
|
---|
184 | ! qsort (b, n, s, cmp);
|
---|
185 | ! }
|
---|
186 | ! else
|
---|
187 | ! {
|
---|
188 | ! msort_with_tmp (b, n, s, cmp, tmp);
|
---|
189 | ! free (tmp);
|
---|
190 | ! }
|
---|
191 | ! errno = save;
|
---|
192 | }
|
---|
193 | }
|
---|