29
29
# endif
30
30
#endif
31
31
32
+ #define __STDC_WANT_LIB_EXT1__ 1 /* request C11 Annex K, which includes qsort_s() */
33
+
32
34
#include <stdio.h> /* fprintf */
33
35
#include <stdlib.h> /* malloc, free, qsort_r */
34
36
68
70
#define ZDICT_QSORT_C90 ZDICT_QSORT_MIN
69
71
#define ZDICT_QSORT_GNU 1
70
72
#define ZDICT_QSORT_APPLE 2
71
- #define ZDICT_QSORT_MSVC ZDICT_QSORT_MAX
72
- #define ZDICT_QSORT_MAX 3
73
+ #define ZDICT_QSORT_MSVC 3
74
+ #define ZDICT_QSORT_C11 ZDICT_QSORT_MAX
75
+ #define ZDICT_QSORT_MAX 4
73
76
74
77
#ifndef ZDICT_QSORT
75
78
# if defined(__APPLE__ )
76
79
# define ZDICT_QSORT ZDICT_QSORT_APPLE /* uses qsort_r() with a different order for parameters */
77
80
# elif defined(_GNU_SOURCE )
78
81
# define ZDICT_QSORT ZDICT_QSORT_GNU /* uses qsort_r() */
79
82
# elif defined(_WIN32 ) && defined(_MSC_VER )
80
- # define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() */
83
+ # define ZDICT_QSORT ZDICT_QSORT_MSVC /* uses qsort_s() with a different order for parameters */
84
+ # elif defined(STDC_LIB_EXT1 ) && (STDC_LIB_EXT1 > 0 ) /* C11 Annex K */
85
+ # define ZDICT_QSORT ZDICT_QSORT_C11 /* uses qsort_s() */
81
86
# else
82
87
# define ZDICT_QSORT ZDICT_QSORT_C90 /* uses standard qsort() which is not re-entrant (requires global variable) */
83
88
# endif
@@ -204,7 +209,7 @@ static U32 *COVER_map_at(COVER_map_t *map, U32 key) {
204
209
*/
205
210
static void COVER_map_remove (COVER_map_t * map , U32 key ) {
206
211
U32 i = COVER_map_index (map , key );
207
- COVER_map_pair_t * del = & map -> data [i ];
212
+ COVER_map_pair_t * del = & map -> data [i ];
208
213
U32 shift = 1 ;
209
214
if (del -> value == MAP_EMPTY_VALUE ) {
210
215
return ;
@@ -307,7 +312,7 @@ static int COVER_cmp8(COVER_ctx_t *ctx, const void *lp, const void *rp) {
307
312
*/
308
313
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC ) || (ZDICT_QSORT == ZDICT_QSORT_APPLE )
309
314
static int WIN_CDECL COVER_strict_cmp (void * g_coverCtx , const void * lp , const void * rp ) {
310
- #elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
315
+ #elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
311
316
static int COVER_strict_cmp (const void * lp , const void * rp , void * g_coverCtx ) {
312
317
#else /* C90 fallback.*/
313
318
static int COVER_strict_cmp (const void * lp , const void * rp ) {
@@ -323,7 +328,7 @@ static int COVER_strict_cmp(const void *lp, const void *rp) {
323
328
*/
324
329
#if (ZDICT_QSORT == ZDICT_QSORT_MSVC ) || (ZDICT_QSORT == ZDICT_QSORT_APPLE )
325
330
static int WIN_CDECL COVER_strict_cmp8 (void * g_coverCtx , const void * lp , const void * rp ) {
326
- #elif (ZDICT_QSORT == ZDICT_QSORT_GNU)
331
+ #elif (ZDICT_QSORT == ZDICT_QSORT_GNU) || (ZDICT_QSORT == ZDICT_QSORT_C11)
327
332
static int COVER_strict_cmp8 (const void * lp , const void * rp , void * g_coverCtx ) {
328
333
#else /* C90 fallback.*/
329
334
static int COVER_strict_cmp8 (const void * lp , const void * rp ) {
@@ -355,6 +360,10 @@ static void stableSort(COVER_ctx_t *ctx)
355
360
qsort_s (ctx -> suffix , ctx -> suffixSize , sizeof (U32 ),
356
361
(ctx -> d <= 8 ? & COVER_strict_cmp8 : & COVER_strict_cmp ),
357
362
ctx );
363
+ #elif (ZDICT_QSORT == ZDICT_QSORT_C11 )
364
+ qsort_s (ctx -> suffix , ctx -> suffixSize , sizeof (U32 ),
365
+ (ctx -> d <= 8 ? & COVER_strict_cmp8 : & COVER_strict_cmp ),
366
+ ctx );
358
367
#elif defined(__OpenBSD__ )
359
368
/* On OpenBSD, qsort() is not guaranteed to be stable, their mergesort() is.
360
369
* Note(@cyan): qsort() is never guaranteed to be stable,
0 commit comments