P99
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
p99_swap.h
Go to the documentation of this file.
00001 /* This may look like nonsense, but it really is -*- mode: C -*-             */
00002 /*                                                                           */
00003 /* Except of parts copied from previous work and as explicitly stated below, */
00004 /* the author and copyright holder for this work is                          */
00005 /* (C) copyright  2010-2012 Jens Gustedt, INRIA, France                      */
00006 /*                                                                           */
00007 /* This file is free software; it is part of the P99 project.                */
00008 /* You can redistribute it and/or modify it under the terms of the QPL as    */
00009 /* given in the file LICENSE. It is distributed without any warranty;        */
00010 /* without even the implied warranty of merchantability or fitness for a     */
00011 /* particular purpose.                                                       */
00012 /*                                                                           */
00013 /*
00014 ** p99_swap.h
00015 **
00016 ** Made by Jens Gustedt
00017 ** Login   <gustedt@damogran.loria.fr>
00018 **
00019 ** Started on  Thu Oct 21 11:40:09 2010 Jens Gustedt
00020 ** Last update Thu Oct 21 11:40:09 2010 Jens Gustedt
00021 */
00022 
00023 #ifndef     P99_SWAP_H_
00024 # define    P99_SWAP_H_
00025 
00026 #include "p99_int.h"
00027 
00028 p99_inline
00029 void p00_swap2(void* p00_p0, void* p00_p1, size_t p00_size, void* p00_t0, void* p00_t1) {
00030   memcpy(p00_t0, p00_p0, p00_size);
00031   memcpy(p00_t1, p00_p1, p00_size);
00032   memcpy(p00_p1, p00_t0, p00_size);
00033   memcpy(p00_p0, p00_t1, p00_size);
00034 }
00035 
00044 p99_inline
00045 void p99_swap2(void* p00_p0, void* p00_p1, size_t p00_size) {
00046   char* p00_t0 = malloc(2 * p00_size);
00047   char* p00_t1 = p00_t0 + p00_size;
00048   p00_swap2(p00_p0, p00_p1, p00_size, p00_t0, p00_t1);
00049   free(p00_t0);
00050 }
00051 
00052 p99_inline
00053 void p00_swap1(void* p00_p0, void* p00_p1, size_t p00_size, void* p00_t0) {
00054   memcpy(p00_t0, p00_p0, p00_size);
00055   memcpy(p00_p0, p00_p1, p00_size);
00056   memcpy(p00_p1, p00_t0, p00_size);
00057 }
00058 
00067 p99_inline
00068 void p99_swap1(void* p00_p0, void* p00_p1, size_t p00_size) {
00069   void* p00_t0 = malloc(p00_size);
00070   p00_swap1(p00_p0, p00_p1, p00_size, p00_t0);
00071   free(p00_t0);
00072 }
00073 
00074 #define P00_SWAP2(_0, _1)                                      \
00075 p00_swap2(                                                     \
00076           /* check if the two are assignment compatible */     \
00077           P99_SIGN_PROMOTE(&(_0), ((_0) = (_1), (void*)0)),    \
00078           P99_SIGN_PROMOTE(&(_1), ((_1) = (_0), (void*)0)),    \
00079           sizeof(_0),                                          \
00080           /* only works if sizeof(_0) >= sizeof(_1) */         \
00081           (char[sizeof(_0)]){                                  \
00082             [(intmax_t)sizeof(_0) - sizeof(_1)] = 0,           \
00083               },                                               \
00084           /* only works if sizeof(_0) <= sizeof(_1) */         \
00085           (char[sizeof(_0)]){                                  \
00086             [(intmax_t)sizeof(_1) - sizeof(_0)] = 0,           \
00087               })
00088 
00089 #define P00_SWAP1(_0, _1)                                      \
00090 p00_swap1(                                                     \
00091           /* check if the two are assignment compatible */     \
00092           P99_SIGN_PROMOTE(&(_0), ((_0) = (_1), (void*)0)),    \
00093           P99_SIGN_PROMOTE(&(_1), ((_1) = (_0), (void*)0)),    \
00094           sizeof(_0),                                          \
00095           /* only works if sizeof(_0) <= sizeof(_1) */         \
00096           (char[sizeof(_0)]){                                  \
00097             [(intmax_t)sizeof(_1) - sizeof(_0)] = 0,           \
00098               })
00099 
00100 
00153 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_SWAP, 0)
00154 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_SWAP, 1)
00155 #define P99_SWAP(_0, _1) ((sizeof(_0) > sizeof(uintmax_t)) ? P00_SWAP1(_0, _1) : P00_SWAP2(_0, _1))
00156 
00157 
00158 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_QSORT, 0)
00159 #define P99_QSORT(TAB, NB, COMP) qsort((TAB), (NB), sizeof (TAB)[0], (COMP))
00160 
00161 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_ASORT, 0)
00162 #define P99_ASORT(TAB, COMP) qsort((TAB), P99_ALEN(TAB), sizeof (TAB)[0], (COMP))
00163 
00164 #endif      /* !P99_SWAP_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines