Giter Site home page Giter Site logo

sort_r's Introduction

sort_r

Isaac Turner 2013
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 mikepb avatar noporpoise avatar

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.