Blender  V2.93
sort.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
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.
11  *
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 Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h>
25 
26 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8))
27 /* do nothing! */
28 #else
29 
30 # include "BLI_utildefines.h"
31 
32 # include "BLI_sort.h"
33 
34 # ifdef min /* for msvc */
35 # undef min
36 # endif
37 
38 /* Maintained by FreeBSD. */
39 /* clang-format off */
40 
48 BLI_INLINE char *med3(char *a, char *b, char *c, BLI_sort_cmp_t cmp, void *thunk);
49 BLI_INLINE void swapfunc(char *a, char *b, int n, int swaptype);
50 
51 #define min(a, b) (a) < (b) ? (a) : (b)
52 #define swapcode(TYPE, parmi, parmj, n) \
53 { \
54  long i = (n) / sizeof(TYPE); \
55  TYPE *pi = (TYPE *) (parmi); \
56  TYPE *pj = (TYPE *) (parmj); \
57  do { \
58  TYPE t = *pi; \
59  *pi++ = *pj; \
60  *pj++ = t; \
61  } while (--i > 0); \
62 }
63 #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
64  es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
65 
66 BLI_INLINE void swapfunc(char *a, char *b, int n, int swaptype)
67 {
68  if (swaptype <= 1)
69  swapcode(long, a, b, n)
70  else
71  swapcode(char, a, b, n)
72 }
73 
74 #define swap(a, b) \
75  if (swaptype == 0) { \
76  long t = *(long *)(a); \
77  *(long *)(a) = *(long *)(b);\
78  *(long *)(b) = t; \
79  } else \
80  swapfunc(a, b, es, swaptype)
81 
82 #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
83 #define CMP(t, x, y) (cmp((x), (y), (t)))
84 
85 BLI_INLINE char *med3(char *a, char *b, char *c, BLI_sort_cmp_t cmp, void *thunk)
86 {
87  return CMP(thunk, a, b) < 0 ?
88  (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a )) :
89  (CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
90 }
91 
95 void BLI_qsort_r(void *a, size_t n, size_t es, BLI_sort_cmp_t cmp, void *thunk)
96 {
97  char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
98  int d, r, swaptype, swap_cnt;
99 
100 loop:
101  SWAPINIT(a, es);
102  swap_cnt = 0;
103  if (n < 7) {
104  for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) {
105  for (pl = pm;
106  pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
107  pl -= es)
108  {
109  swap(pl, pl - es);
110  }
111  }
112  return;
113  }
114  pm = (char *)a + (n / 2) * es;
115  if (n > 7) {
116  pl = (char *)a;
117  pn = (char *)a + (n - 1) * es;
118  if (n > 40) {
119  d = (n / 8) * es;
120  pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
121  pm = med3(pm - d, pm, pm + d, cmp, thunk);
122  pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
123  }
124  pm = med3(pl, pm, pn, cmp, thunk);
125  }
126  swap((char *)a, pm);
127  pa = pb = (char *)a + es;
128 
129  pc = pd = (char *)a + (n - 1) * es;
130  for (;;) {
131  while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
132  if (r == 0) {
133  swap_cnt = 1;
134  swap(pa, pb);
135  pa += es;
136  }
137  pb += es;
138  }
139  while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
140  if (r == 0) {
141  swap_cnt = 1;
142  swap(pc, pd);
143  pd -= es;
144  }
145  pc -= es;
146  }
147  if (pb > pc) {
148  break;
149  }
150  swap(pb, pc);
151  swap_cnt = 1;
152  pb += es;
153  pc -= es;
154  }
155  if (swap_cnt == 0) { /* Switch to insertion sort */
156  for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) {
157  for (pl = pm;
158  pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
159  pl -= es)
160  {
161  swap(pl, pl - es);
162  }
163  }
164  return;
165  }
166 
167  pn = (char *)a + n * es;
168  r = min(pa - (char *)a, pb - pa);
169  vecswap((char *)a, pb - r, r);
170  r = min(pd - pc, pn - pd - es);
171  vecswap(pb, pn - r, r);
172  if ((r = pb - pa) > es) {
173  BLI_qsort_r(a, r / es, es, cmp, thunk);
174  }
175  if ((r = pd - pc) > es) {
176  /* Iterate rather than recurse to save stack space */
177  a = pn - r;
178  n = r / es;
179  goto loop;
180  }
181 }
182 
183 /* clang-format on */
184 
185 #endif /* __GLIBC__ */
#define BLI_INLINE
int(* BLI_sort_cmp_t)(const void *a, const void *b, void *ctx)
Definition: BLI_sort.h:34
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
BLI_INLINE void swapfunc(char *a, char *b, int n, int swaptype)
Definition: sort.c:66
#define swap(a, b)
Definition: sort.c:74
#define vecswap(a, b, n)
Definition: sort.c:82
#define CMP(t, x, y)
Definition: sort.c:83
#define swapcode(TYPE, parmi, parmj, n)
Definition: sort.c:52
#define min(a, b)
Definition: sort.c:51
BLI_INLINE char * med3(char *a, char *b, char *c, BLI_sort_cmp_t cmp, void *thunk)
Definition: sort.c:85
#define SWAPINIT(a, es)
Definition: sort.c:63
void BLI_qsort_r(void *a, size_t n, size_t es, BLI_sort_cmp_t cmp, void *thunk)
Definition: sort.c:95