summaryrefslogtreecommitdiff
path: root/scipy/base/src/_sortmodule.c.src
blob: ccf7a0f84074169cd299cb4d36a9a8a11fa6d2c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* The purpose of this module is to add faster sort functions
   that are type-specific.  This is done by altering the
   function table for the builtin descriptors. 

   These sorting functions are copied almost directly from numarray 
   with a few modifications (complex comparisons compare the imaginary 
   part if the real parts are equal, for example), and the names
   are changed. 
*/

/* Quick sort is usually the fastest, but the worst case scenario can
   be slower than the merge and heap sorts.  The merge sort requires
   extra memory and so for large arrays may not be useful. 
 
   The merge sort is *stable*, meaning that equal components
   are unmoved from their entry versions, so it can be used to
   implement lexigraphic sorting on multiple keys. 
*/


#include "Python.h"
#include "scipy/arrayobject.h"

#define STDC_LT(a,b) ((a) < (b))
#define STDC_LE(a,b) ((a) <= (b))
#define STDC_EQ(a,b) ((a) == (b))
#define SWAP(a,b) {SWAP_temp = (b); (b)=(a); (a) = SWAP_temp;}
#define NUMC_LT(p,q) ((((p).real==(q).real) ? ((p).imag < (q).imag): ((p).real < (q).real)))
#define NUMC_LE(p,q) ((((p).real==(q).real) ? ((p).imag <= (q).imag): ((p).real <= (q).real)))
#define NUMC_EQ(p,q) (((p).real==(q).real) && ((p).imag == (q).imag))

/**begin repeat
#TYPE=BOOL,BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG,FLOAT,DOUBLE,LONGDOUBLE,CFLOAT,CDOUBLE,CLONGDOUBLE#
#type=Bool,byte,ubyte,short,ushort,int,uint,long,ulong,longlong,ulonglong,float,double,longdouble,cfloat,cdouble,clongdouble#
#lessthan=STDC_LT*14,NUMC_LT*3#
#lessequal=STDC_LE*14,NUMC_LE*3#
 **/
static int
@TYPE@_quicksort(@type@ *start, intp num, void *unused)
{
	@type@ *pl = start;
	@type@ *pr = start + num - 1;
	@type@ vp, SWAP_temp;
	@type@ *stack[100], **sptr = stack, *pm, *pi, *pj, *pt;

        for(;;) {
                while ((pr - pl) > 15) {
                        /* quicksort partition */
                        pm = pl + ((pr - pl) >> 1);
                        if (@lessthan@(*pm,*pl)) SWAP(*pm,*pl);
                        if (@lessthan@(*pr,*pm)) SWAP(*pr,*pm);
                        if (@lessthan@(*pm,*pl)) SWAP(*pm,*pl);
                        vp = *pm;
                        pi = pl;
                        pj = pr - 1;
                        SWAP(*pm,*pj);
                        for(;;) {
                                do ++pi; while (@lessthan@(*pi,vp));
                                do --pj; while (@lessthan@(vp,*pj));
                                if (pi >= pj)  break;
                                SWAP(*pi,*pj);
                        }
                        SWAP(*pi,*(pr-1));
                        /* push largest partition on stack */
                        if (pi - pl < pr - pi) {
                                *sptr++ = pi + 1;
                                *sptr++ = pr;
                                pr = pi - 1;
                        }else{
                                *sptr++ = pl;
                                *sptr++ = pi - 1;
                                pl = pi + 1;
                        }
                }
                /* insertion sort */
                for(pi = pl + 1; pi <= pr; ++pi) {
                        vp = *pi;
                        for(pj = pi, pt = pi - 1; \
			    pj > pl && @lessthan@(vp, *pt);) {
                                *pj-- = *pt--;
                        }
                        *pj = vp;
                }
                if (sptr == stack) break;
                pr = *(--sptr);
                pl = *(--sptr);
        }
	return 0;
}

static int
@TYPE@_aquicksort(@type@ *v, intp* tosort, intp num, void *unused)
{
	@type@ vp;
	intp *pl, *pr, SWAP_temp;
	intp *stack[100], **sptr=stack, *pm, *pi, *pj, *pt, vi;

	pl = tosort;
	pr = tosort + num - 1;

        for(;;) {
                while ((pr - pl) > 15) {
                        /* quicksort partition */
                        pm = pl + ((pr - pl) >> 1);
                        if (@lessthan@(v[*pm],v[*pl])) SWAP(*pm,*pl);
                        if (@lessthan@(v[*pr],v[*pm])) SWAP(*pr,*pm);
                        if (@lessthan@(v[*pm],v[*pl])) SWAP(*pm,*pl);
                        vp = v[*pm];
                        pi = pl;
                        pj = pr - 1;
                        SWAP(*pm,*pj);
                        for(;;) {
                                do ++pi; while (@lessthan@(v[*pi],vp));
                                do --pj; while (@lessthan@(vp,v[*pj]));
                                if (pi >= pj)  break;
                                SWAP(*pi,*pj);
                        }
                        SWAP(*pi,*(pr-1));
                        /* push largest partition on stack */
                        if (pi - pl < pr - pi) {
                                *sptr++ = pi + 1;
                                *sptr++ = pr;
                                pr = pi - 1;
                        }else{
                                *sptr++ = pl;
                                *sptr++ = pi - 1;
                                pl = pi + 1;
                        }
                }
                /* insertion sort */
                for(pi = pl + 1; pi <= pr; ++pi) {
                        vi = *pi;
                        vp = v[vi];
                        for(pj = pi, pt = pi - 1; \
			    pj > pl && @lessthan@(vp, v[*pt]);)
				{
					*pj-- = *pt--;
				}
                        *pj = vi;
                }
                if (sptr == stack) break;
                pr = *(--sptr);
                pl = *(--sptr);
        }
	return 0;
}


static int
@TYPE@_heapsort(@type@ *start, intp n, void *unused)
{
	
        @type@ tmp, *a;
        intp i,j,l;

        /* The array needs to be offset by one for heapsort indexing */
	a = start - 1;

        for (l = n>>1; l > 0; --l) {
                tmp = a[l];
                for (i = l, j = l<<1; j <= n;) {
                        if (j < n && @lessthan@(a[j], a[j+1]))
                                j += 1;
                        if (@lessthan@(tmp, a[j])) {
                                a[i] = a[j];
                                i = j;
                                j += j;
                        }else
                                break;
                }
                a[i] = tmp;
        }

        for (; n > 1;) {
                tmp = a[n];
                a[n] = a[1];
                n -= 1;
                for (i = 1, j = 2; j <= n;) {
                        if (j < n && @lessthan@(a[j], a[j+1]))
                                j++;
                        if (@lessthan@(tmp, a[j])) {
                                a[i] = a[j];
                                i = j;
                                j += j;
                        }else
                                break;
                }
                a[i] = tmp;
        }
	return 0;
}

static int
@TYPE@_aheapsort(@type@ *v, intp *tosort, intp n, void *unused)
{
	intp *a, i,j,l, tmp;
	/* The arrays need to be offset by one for heapsort indexing */
	a = tosort - 1;

        for (l = n>>1; l > 0; --l) {
                tmp = a[l];
                for (i = l, j = l<<1; j <= n;) {
                        if (j < n && @lessthan@(v[a[j]], v[a[j+1]]))
                                j += 1;
                        if (@lessthan@(v[tmp], v[a[j]])) {
                                a[i] = a[j];
                                i = j;
                                j += j;
                        }else
                                break;
                }
                a[i] = tmp;
        }

        for (; n > 1;) {
                tmp = a[n];
                a[n] = a[1];
                n -= 1;
                for (i = 1, j = 2; j <= n;) {
                        if (j < n && @lessthan@(v[a[j]], v[a[j+1]]))
                                j++;
                        if (@lessthan@(v[tmp], v[a[j]])) {
                                a[i] = a[j];
                                i = j;
                                j += j;
                        }else
                                break;
                }
                a[i] = tmp;
        }

	return 0;
}

static void
@TYPE@_mergesort0(@type@ *pl, @type@ *pr, @type@ *pw)
{
	@type@ vp, *pi, *pj, *pk, *pm;

        if (pr - pl > 20) {
                /* merge sort */
                pm = pl + ((pr - pl + 1)>>1);
                @TYPE@_mergesort0(pl,pm-1,pw);
                @TYPE@_mergesort0(pm,pr,pw);
                for(pi = pw, pj = pl; pj < pm; ++pi, ++pj) {
                        *pi = *pj;
                }
                for(pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) {
                        if (@lessequal@(*pk,*pj)) {
                                *pm = *pk;
                                ++pk;
                        }else{
                                *pm = *pj;
                                ++pj;
                        }
                }
                for(; pk < pi; ++pm, ++pk) {
                        *pm = *pk;
                }
        }else{
                /* insertion sort */
                for(pi = pl + 1; pi <= pr; ++pi) {
                        vp = *pi;
                        for(pj = pi, pk = pi - 1;\
			    pj > pl && @lessthan@(vp, *pk); --pj, --pk) {
                                *pj = *pk;
                        }
                        *pj = vp;
                }
        }
}

static int
@TYPE@_mergesort(@type@ *start, intp num, void *unused)
{
	@type@ *pl, *pr, *pw;

	pl = start; pr = pl + num - 1;
	pw = (@type@ *) PyDataMem_NEW(((1+num/2))*sizeof(@type@));
	
	if (!pw) {
		PyErr_NoMemory();
		return -1;
	}

	@TYPE@_mergesort0(pl, pr, pw);
	PyDataMem_FREE(pw);
	return 0;
}

static void 
@TYPE@_amergesort0(intp *pl, intp *pr, @type@ *v, intp *pw)
{
        @type@ vp;
        intp vi, *pi, *pj, *pk, *pm;

        if (pr - pl > 20) {
                /* merge sort */
                pm = pl + ((pr - pl + 1)>>1);
                @TYPE@_amergesort0(pl,pm-1,v,pw);
                @TYPE@_amergesort0(pm,pr,v,pw);
                for(pi = pw, pj = pl; pj < pm; ++pi, ++pj) {
                        *pi = *pj;
                }
                for(pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) {
                        if (@lessequal@(v[*pk],v[*pj])) {
                                *pm = *pk;
                                ++pk;
                        }else{
                                *pm = *pj;
                                ++pj;
                        }
                }
                for(; pk < pi; ++pm, ++pk) {
                        *pm = *pk;
                }
        }else{
                /* insertion sort */
                for(pi = pl + 1; pi <= pr; ++pi) {
                        vi = *pi;
                        vp = v[vi];
                        for(pj = pi, pk = pi - 1;			\
			    pj > pl && @lessthan@(vp, v[*pk]); --pj, --pk) {
                                *pj = *pk;
                        }
                        *pj = vi;
                }
        }
}

static int
@TYPE@_amergesort(@type@ *v, intp *tosort, intp num, void *unused)
{
	intp *pl, *pr, *pw;

	pl = tosort; pr = pl + num - 1;
	pw = PyDimMem_NEW((1+num/2));
	
	if (!pw) {
		PyErr_NoMemory();
		return -1;
	}

	@TYPE@_amergesort0(pl, pr, v, pw);
	PyDimMem_FREE(pw);
	return 0;
}
/**end repeat**/

static int
unincmp(Py_UNICODE *s1, Py_UNICODE *s2, register int len)
{
	register Py_UNICODE c1, c2;
	while(len-- > 0) {
		c1 = *s1++;
		c2 = *s2++;
		if (c1 != c2)
			return (c1 < c2) ? -1 : 1;
	}
	return 0;
}

/**begin repeat
#TYPE=STRING,UNICODE#
#comp=strncmp,unincmp#
#type=char *, Py_UNICODE *#
*/
static void 
@TYPE@_amergesort0(intp *pl, intp *pr, @type@*v, intp *pw, int elsize)
{
	@type@ vp;
        intp vi, *pi, *pj, *pk, *pm;

        if (pr - pl > 20) {
                /* merge sort */
                pm = pl + ((pr - pl + 1)>>1);
                @TYPE@_amergesort0(pl,pm-1,v,pw,elsize);
                @TYPE@_amergesort0(pm,pr,v,pw,elsize);
                for(pi = pw, pj = pl; pj < pm; ++pi, ++pj) {
                        *pi = *pj;
                }
                for(pk = pw, pm = pl; pk < pi && pj <= pr; ++pm) {
                        if (@comp@(v[*pk],v[*pj],elsize)<=0) {
                                *pm = *pk;
                                ++pk;
                        }else{
                                *pm = *pj;
                                ++pj;
                        }
                }
                for(; pk < pi; ++pm, ++pk) {
                        *pm = *pk;
                }
        }else{
                /* insertion sort */
                for(pi = pl + 1; pi <= pr; ++pi) {
                        vi = *pi;
                        vp = v[vi];
                        for(pj = pi, pk = pi - 1; 		\
			    pj > pl && (@comp@(vp, v[*pk],elsize)<=0); \
			    --pj, --pk) {
                                *pj = *pk;
                        }
                        *pj = vi;
                }
        }
}

static int
@TYPE@_amergesort(@type@*v, intp *tosort, intp num, PyArrayObject *arr)
{
	intp *pl, *pr, *pw;
	int elsize;

	elsize = arr->descr->elsize;

	pl = tosort; pr = pl + num - 1;
	pw = PyDimMem_NEW((1+num/2));
	
	if (!pw) {
		PyErr_NoMemory();
		return -1;
	}

	@TYPE@_amergesort0(pl, pr, v, pw, elsize);
	PyDimMem_FREE(pw);
	return 0;
}
/**end repeat**/
	
static void 
add_sortfuncs(void)
{
	PyArray_Descr *descr;

/**begin repeat
#TYPE=BOOL,BYTE,UBYTE,SHORT,USHORT,INT,UINT,LONG,ULONG,LONGLONG,ULONGLONG,FLOAT,DOUBLE,LONGDOUBLE,CFLOAT,CDOUBLE,CLONGDOUBLE#
**/
	descr = PyArray_DescrFromType(PyArray_@TYPE@);
	descr->f->sort[PyArray_QUICKSORT] = \
		(PyArray_SortFunc *)@TYPE@_quicksort;
	descr->f->argsort[PyArray_QUICKSORT] = \
		(PyArray_ArgSortFunc *)@TYPE@_aquicksort;
	descr->f->sort[PyArray_HEAPSORT] = \
		(PyArray_SortFunc *)@TYPE@_heapsort;
	descr->f->argsort[PyArray_HEAPSORT] = \
		(PyArray_ArgSortFunc *)@TYPE@_aheapsort;
	descr->f->sort[PyArray_MERGESORT] = \
		(PyArray_SortFunc *)@TYPE@_mergesort;
	descr->f->argsort[PyArray_MERGESORT] = \
		(PyArray_ArgSortFunc *)@TYPE@_amergesort;
/**end repeat**/

	descr = PyArray_DescrFromType(PyArray_STRING);
	descr->f->argsort[PyArray_MERGESORT] = \
		(PyArray_ArgSortFunc *)STRING_amergesort;
	descr = PyArray_DescrFromType(PyArray_UNICODE);
	descr->f->argsort[PyArray_MERGESORT] = \
		(PyArray_ArgSortFunc *)UNICODE_amergesort;
}

static struct PyMethodDef methods[] = {
	{NULL, NULL, 0}
};

PyMODINIT_FUNC
init_sort(void) {
	PyObject *m;

	m = Py_InitModule("_sort", methods);
	
	if (import_array() < 0) return;
	add_sortfuncs();
}