Coder Social home page Coder Social logo

qsort-inline's Introduction

Quick Sort with inlined type-safe comparison

This Quick Sort is an extension/reimplementation of C qsort function (based on Apple's code, which is APSL licensed). It allows specialisation of the quicksort function with different types and comparison, such that the comprison function is inlined. This allows greater performance and type safety for qsort in C, though makes a comprimise regarding syntactic safety by using macros and requires an extra definition step.

Example

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "qsort-inline.h"

struct tagPerson {
  int age;
  char* name;
};

//Needs typedef's for complex types as type is a part of generated function name
typedef struct tagPerson* Person;

//Construct a person
Person make_person(int age, const char *name) {
  Person p = malloc(sizeof(struct tagPerson));
  if (!p) goto ret;
  size_t n = strlen(name);
  p->age = age;
  p->name = malloc(sizeof(char)*n);
  if (!(p->name))
    goto cleanup;
  strcpy(p->name, name);
  goto ret;
cleanup:
  free(p);
  p = NULL;
ret:
  return p;
}

//Delete a person
void delete_person(Person p) {
  if (!p) return;
  if (p->name) free(p->name);
  free(p);
}

//Define comparator as usual (Though notice that it is typesafe. This will sort by age, then by name.)
inline int Personcmp(Person * pa, Person * pb) {
  int agecmp = (*pa)->age - (*pb)->age;
  if (!agecmp) {
    return strcmp((*pa)->name, (*pb)->name);
  }
  return agecmp;
}

//Define quicksort overload for Person
DEF_QSORT(Person, Personcmp)

int main()
{
#define N 5
  Person arr[N];
  //Construct persons with random names and ages
  arr[0] = make_person(18, "Peter Peterson");
  arr[1] = make_person(20, "Jack Peterson");
  arr[2] = make_person(53, "Hansel Georgeson");
  arr[3] = make_person(18, "Alexander McWright");
  arr[4] = make_person(64, "Elizabeth Reader");

  QSORT(Person, Personcmp) (arr, N);
  int i = 0;

  //Print persons
  for (i = 0; i < N; i++) {
    if (arr[i])
      printf("Person { age: %d, name: %s }\n", arr[i]->age, arr[i]->name);
  }

  //Delete persons
  for (i = 0; i < N; i++) {
    delete_person(arr[i]);
    arr[i] = NULL;
  }
  return 0;
}

qsort-inline's People

Stargazers

 avatar Miëtek Bak avatar Ahmad Salim Al-Sibahi avatar Andréa Machizaud avatar

Watchers

James Cloos 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.