P99
p99_swap.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the authors and copyright holders for this work are as follows: */
5 /* (C) copyright 2010-2012 Jens Gustedt, INRIA, France */
6 /* (C) copyright 2012 William Morris */
7 /* */
8 /* This file is free software; it is part of the P99 project. */
9 /* */
10 /* Licensed under the Apache License, Version 2.0 (the "License"); */
11 /* you may not use this file except in compliance with the License. */
12 /* You may obtain a copy of the License at */
13 /* */
14 /* http://www.apache.org/licenses/LICENSE-2.0 */
15 /* */
16 /* Unless required by applicable law or agreed to in writing, software */
17 /* distributed under the License is distributed on an "AS IS" BASIS, */
18 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
19 /* See the License for the specific language governing permissions and */
20 /* limitations under the License. */
21 /* */
22 /*
23 ** p99_swap.h
24 **
25 ** Made by Jens Gustedt
26 ** Login <gustedt@damogran.loria.fr>
27 **
28 ** Started on Thu Oct 21 11:40:09 2010 Jens Gustedt
29 ** Last update Thu Oct 21 11:40:09 2010 Jens Gustedt
30 */
31 
32 #ifndef P99_SWAP_H_
33 # define P99_SWAP_H_
34 
35 #include "p99_int.h"
36 
38 void p00_swap2(void* p00_p0, void* p00_p1, size_t p00_size, void*restrict p00_t0, void*restrict p00_t1) {
39  if (p00_p0 != p00_p1) {
40  memcpy(p00_t0, p00_p0, p00_size);
41  memcpy(p00_t1, p00_p1, p00_size);
42  memcpy(p00_p1, p00_t0, p00_size);
43  memcpy(p00_p0, p00_t1, p00_size);
44  }
45 }
46 
56 void p99_swap2(void* p00_p0, void* p00_p1, size_t p00_size) {
57  if (p00_p0 != p00_p1) {
58  char* p00_t0 = malloc(2 * p00_size);
59  char* p00_t1 = p00_t0 + p00_size;
60  p00_swap2(p00_p0, p00_p1, p00_size, p00_t0, p00_t1);
61  free(p00_t0);
62  }
63 }
64 
66 void p00_swap1(void* p00_p0, void* p00_p1, size_t p00_size, void*restrict p00_t0) {
67  if (p00_p0 != p00_p1) {
68  memcpy(p00_t0, p00_p0, p00_size);
69  memcpy(p00_p0, p00_p1, p00_size);
70  memcpy(p00_p1, p00_t0, p00_size);
71  }
72 }
73 
83 void p99_swap1(void* p00_p0, void* p00_p1, size_t p00_size) {
84  if (p00_p0 != p00_p1) {
85  void* p00_t0 = malloc(p00_size);
86  p00_swap1(p00_p0, p00_p1, p00_size, p00_t0);
87  free(p00_t0);
88  }
89 }
90 
91 #define P00_SWAP2(_0, _1) \
92 p00_swap2( \
93  /* check if the two are assignment compatible */ \
94  P99_SIGN_PROMOTE(&(_0), ((_0) = (_1), (void*)0)), \
95  P99_SIGN_PROMOTE(&(_1), ((_1) = (_0), (void*)0)), \
96  sizeof(_0), \
97  /* only works if sizeof(_0) >= sizeof(_1) */ \
98  (char[sizeof(_0)]){ \
99  [(intmax_t)sizeof(_0) - sizeof(_1)] = 0, \
100  }, \
101  /* only works if sizeof(_0) <= sizeof(_1) */ \
102  (char[sizeof(_0)]){ \
103  [(intmax_t)sizeof(_1) - sizeof(_0)] = 0, \
104  })
105 
106 #define P00_SWAP1(_0, _1) \
107 p00_swap1( \
108  /* check if the two are assignment compatible */ \
109  P99_SIGN_PROMOTE(&(_0), ((_0) = (_1), (void*)0)), \
110  P99_SIGN_PROMOTE(&(_1), ((_1) = (_0), (void*)0)), \
111  sizeof(_0), \
112  /* only works if sizeof(_0) <= sizeof(_1) */ \
113  (char[sizeof(_0)]){ \
114  [(intmax_t)sizeof(_1) - sizeof(_0)] = 0, \
115  })
116 
117 
170 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_SWAP, 0)
171 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_SWAP, 1)
172 #define P99_SWAP(_0, _1) ((sizeof(_0) > sizeof(uintmax_t)) ? P00_SWAP1(_0, _1) : P00_SWAP2(_0, _1))
173 
174 
175 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_QSORT, 0)
176 #define P99_QSORT(TAB, NB, ...) \
177 P99_IF_LT(P99_NARG(__VA_ARGS__), 2) \
178 (qsort((TAB), (NB), sizeof (TAB)[0], __VA_ARGS__)) \
179 (qsort_s((TAB), (NB), sizeof (TAB)[0], __VA_ARGS__))
180 
181 P00_DOCUMENT_PERMITTED_ARGUMENT(P99_ASORT, 0)
182 #define P99_ASORT(TAB, ...) \
183 P99_IF_LT(P99_NARG(__VA_ARGS__), 2) \
184 (qsort((TAB), P99_ALEN(TAB), sizeof (TAB)[0], __VA_ARGS__)) \
185 (qsort_s((TAB), P99_ALEN(TAB), sizeof (TAB)[0], __VA_ARGS__))
186 
187 #endif /* !P99_SWAP_H_ */
p99_swap2
void p99_swap2(void *p00_p0, void *p00_p1, size_t p00_size)
Swap the contents of the arguments.
Definition: p99_swap.h:56
p99_inline
#define p99_inline
Try to force a function always to be inlined.
Definition: p99_compiler.h:496
P99_ASORT
#define P99_ASORT(TAB,...)
Definition: p99_swap.h:182
P99_QSORT
#define P99_QSORT(TAB, NB,...)
Definition: p99_swap.h:176
p99_swap1
void p99_swap1(void *p00_p0, void *p00_p1, size_t p00_size)
Swap the contents of the arguments.
Definition: p99_swap.h:83
p99_int.h
Macros handling integer types and initialization.
P99_SWAP
#define P99_SWAP(_0, _1)
Swap the contents of the arguments.
Definition: p99_swap.h:172