Code Monkey home page Code Monkey logo

sort_r's Introduction

sort_r

Isaac Turner 2022
Portable qsort_r / qsort_s
Discussion here: http://stackoverflow.com/questions/4300896/how-portable-is-the-re-entrant-qsort-r-function-compared-to-qsort
License: Public Domain - use as you wish, no warranty

Build Status

About

If you want to qsort() an array with a comparison operator that takes parameters you need to use global variables to pass those parameters (not possible when writing multithreaded code), or use qsort_r/qsort_s which are not portable (there are separate GNU/BSD/Windows versions and they all take different arguments).

So I wrote a portable qsort_r/qsort_s called sort_r():

void sort_r(void *base, size_t nel, size_t width,
            int (*compar)(const void *a1, const void *a2, void *aarg),
            void *arg);

base is the array to be sorted nel is the number of elements in the array width is the size in bytes of each element of the array compar is the comparison function arg is a pointer to be passed to the comparison function

Using sort_r

Add #include "sort_r.h" to the top of your code. Then copy sort_r.h into your working directory, or add -I path/to/sort_r to your compile arguments.

Build Example

Compile example code (example.c) with:

make

To build using nested functions and qsort instead of qsort_r use

make NESTED_QSORT=1

Nested functions are not permitted under ISO C, they are a GCC extension.

License

Public Domain. Use as you wish. No warranty. There may be bugs.

sort_r's People

Contributors

cap avatar dajobe avatar delphij avatar mikepb avatar noporpoise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sort_r's Issues

Dereferencing void *base on 83 line

Hi again, at the 83 line:
qsort_s(*base, nel, width, &sort_r_arg_swap, &tmp);
perhaps it supposed to be:
qsort_s(base, nel, width, &sort_r_arg_swap, &tmp);
I have not tested it in this configuration yet. I hope I'm right.
...and I thanks - your code is better then mine I used to use before…

O(N^2) if all elements are equal

Hi,

I'm using this code in https://github.com/behdad/harfbuzz/, and our fuzzers have found a timeout for us, which I tracked down to this code performing slow. Upon inspection, it's clear that if all array elements are the same, the code performs at its worst case. The median-pivot method helps with the case of array being mostly sorted or reverse-sorted, but doesn't help all-the-same.

Given that elements equal to the pivot can be partitioned on either side, I'm going to modify the code to use that property to choose a more central breaking point. I'll report on my progress. Thanks.

Nallow _SORT_R_BSD condition

In this context, _SORT_R_BSD means qsort_r from FreeBSD,
so it must be defined only for FreeBSD, its forks (DragonFly) and having userland from it (MacOS X).

musl support

So I'm using this repo and it's great! But I tried compiling my project on alpine linux recently, which uses musl, which apparently doesn't provide an implementation of qsort_r. They also don't define any macros AFAIK that let you detect musl.

I'll see if I can think of a nice way to fix it, but it seems like it might just require a flag. I would just #undef some macros to hack it in but none of these look terribly safe to #undef:

#if (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
     defined __FreeBSD__ || defined __DragonFly__)
#  define _SORT_R_BSD
#elif (defined _GNU_SOURCE || defined __gnu_hurd__ || defined __GNU__ || \
       defined __linux__ || defined __MINGW32__ || defined __GLIBC__)
#  define _SORT_R_LINUX
#elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
#  define _SORT_R_WINDOWS
#  undef _SORT_R_INLINE
#  define _SORT_R_INLINE __inline
#else
  /* Using our own recursive quicksort sort_r_simple() */
#endif

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.