-
Notifications
You must be signed in to change notification settings - Fork 16
/
qtypegeneric.c
374 lines (353 loc) · 13.2 KB
/
qtypegeneric.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*!
* @file qtypegeneric.c
* @author J. Camilo Gomez C.
* @note This file is part of the qLibs distribution.
**/
#include "qtypegeneric.h"
#include <string.h>
/*! @cond */
typedef struct {
uint8_t *lo, *hi;
} qTypeGeneric_SortStackNode_t;
/*! @endcond */
static void qTypeGeneric_SortStackPush( qTypeGeneric_SortStackNode_t **top,
uint8_t *low,
uint8_t *high );
/*============================================================================*/
void qTypeGeneric_Swap( void * const x,
void * const y,
size_t n )
{
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
uint8_t * const a = (uint8_t * const)x;
uint8_t *b = (uint8_t * const)y;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
size_t i = 0U, j = 0U;
do {
uint8_t tmp = a[ i ];
/*cstat -CERT-INT30-C_a*/
a[ i++ ] = b[ j ];
b[ j++ ] = tmp;
/*cstat +CERT-INT30-C_a*/
} while( --n > 0U );
}
/*============================================================================*/
static void qTypeGeneric_SortStackPush( qTypeGeneric_SortStackNode_t **top,
uint8_t *low,
uint8_t *high )
{
top[ 0 ]->lo = low;
top[ 0 ]->hi = high;
++top[ 0 ];
}
/*============================================================================*/
void qTypeGeneric_Sort( void * const pbase,
size_t n,
size_t size,
qTypeGeneric_CompareFcn_t cmp,
void *arg )
{
/*cstat -MISRAC2012-Rule-18.4*/
if ( ( NULL != pbase ) && ( size > 0U ) && ( n > 0U ) && ( NULL != cmp ) ) {
const size_t max_thresh = 4U*size;
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
uint8_t *base_ptr = (uint8_t *)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
uint8_t * const end_ptr = &base_ptr[ size*( n - 1U ) ];
uint8_t *tmp_ptr = base_ptr, *run_ptr;
uint8_t *thresh = ( end_ptr < ( base_ptr + max_thresh) ) ? end_ptr : ( base_ptr + max_thresh ) ;
if ( n > 4U ) {
uint8_t *lo = base_ptr, *hi = &lo[ size*( n - 1U ) ];
qTypeGeneric_SortStackNode_t stack[ 8U*sizeof(size_t) ], *top = stack;
qTypeGeneric_SortStackPush( &top, NULL, NULL );
while ( stack < top ) {
uint8_t *left_ptr, *right_ptr;
/*cstat -ATH-div-0-unchk-param -CERT-INT33-C_h*/
/*cppcheck-suppress misra-c2012-10.8 */
uint8_t *mid = &lo[ size*( ( (size_t)( hi - lo )/size ) >> 1U ) ];
/*cstat +ATH-div-0-unchk-param +CERT-INT33-C_h*/
if ( cmp( mid, lo, arg ) < 0 ) {
qTypeGeneric_Swap( mid, lo, size );
}
if ( cmp( hi, mid, arg ) < 0 ) {
qTypeGeneric_Swap( mid, hi, size );
}
else {
/*cppcheck-suppress misra-c2012-15.1 */
goto jump_over; /*MISRAC deviation allowed*/
}
if ( cmp( mid, lo, arg ) < 0 ) {
qTypeGeneric_Swap( mid, lo, size );
}
jump_over:
left_ptr = lo + size;
right_ptr = hi - size;
do {
while ( cmp( left_ptr, mid, arg ) < 0 ) {
/*cppcheck-suppress misra-c2012-10.3 */
left_ptr += size;
}
while ( cmp( mid, right_ptr, arg ) < 0 ) {
/*cppcheck-suppress misra-c2012-10.3 */
right_ptr -= size;
}
if ( left_ptr < right_ptr ) {
qTypeGeneric_Swap( left_ptr, right_ptr, size );
if ( mid == left_ptr ) {
mid = right_ptr;
}
else if ( mid == right_ptr ) {
mid = left_ptr;
}
else {
/*nothing to do here*/
}
/*cppcheck-suppress misra-c2012-10.3 */
left_ptr += size;
/*cppcheck-suppress misra-c2012-10.3 */
right_ptr -= size;
}
else if ( left_ptr == right_ptr ) {
/*cppcheck-suppress misra-c2012-10.3 */
left_ptr += size;
/*cppcheck-suppress misra-c2012-10.3 */
right_ptr -= size;
break;
}
else {
/*nothing to do here*/
}
} while (left_ptr <= right_ptr);
/*cppcheck-suppress misra-c2012-10.8 */
if ( (size_t)( right_ptr - lo ) <= max_thresh ) {
/*cppcheck-suppress misra-c2012-10.8 */
if ( (size_t)( hi - left_ptr ) <= max_thresh ) {
--top; /*POP form the stack*/
lo = top->lo;
hi = top->hi;
}
else {
lo = left_ptr;
}
}
/*cppcheck-suppress misra-c2012-10.8 */
else if ( (size_t)( hi - left_ptr ) <= max_thresh ) {
hi = right_ptr;
}
else if ( ( right_ptr - lo ) > ( hi - left_ptr ) ) {
qTypeGeneric_SortStackPush( &top, lo, right_ptr );
lo = left_ptr;
}
else {
qTypeGeneric_SortStackPush( &top, left_ptr, hi );
hi = right_ptr;
}
}
}
/*cppcheck-suppress misra-c2012-10.3 */
for ( run_ptr = tmp_ptr + size ; run_ptr <= thresh ; run_ptr += size ) {
if ( cmp( run_ptr, tmp_ptr, arg ) < 0 ) {
tmp_ptr = run_ptr;
}
}
if ( tmp_ptr != base_ptr ) {
qTypeGeneric_Swap( tmp_ptr, base_ptr, size );
}
run_ptr = base_ptr + size;
/*cppcheck-suppress misra-c2012-10.3 */
while ( (run_ptr += size ) <= end_ptr ) {
tmp_ptr = run_ptr - size;
while ( cmp( run_ptr, tmp_ptr, arg ) < 0 ) {
/*cppcheck-suppress misra-c2012-10.3 */
tmp_ptr -= size;
}
/*cppcheck-suppress misra-c2012-10.3 */
tmp_ptr += size;
if ( tmp_ptr != run_ptr ) {
uint8_t *tra = run_ptr + size;
while ( --tra >= run_ptr ) {
uint8_t c = *tra;
uint8_t *hi = tra;
uint8_t *lo = tra;
/*cppcheck-suppress misra-c2012-10.3 */
while ( (lo -= size) >= tmp_ptr ) {
*hi = *lo;
hi = lo;
}
*hi = c;
}
}
}
}
/*cstat +MISRAC2012-Rule-18.4*/
}
/*============================================================================*/
void qTypeGeneric_Reverse( void * const pbase,
const size_t size,
const size_t init,
const size_t end )
{
if ( ( NULL != pbase ) && ( size > 0U ) && ( end > init ) ) {
size_t s = size*init;
size_t e = size*end;
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
uint8_t *v = (uint8_t*)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
while( s < e ) {
qTypeGeneric_Swap( &v[ s ], &v[ e ], size );
s += size;
e -= size;
}
}
}
/*============================================================================*/
void qTypeGeneric_Rotate( void * const pbase,
const size_t size,
const size_t n,
const int k )
{
if ( ( NULL != pbase ) && ( 0 != k ) && ( n > 0U ) ) {
size_t r;
if ( k > 0 ) {
r = (size_t)k;
r %= n;
qTypeGeneric_Reverse( pbase, size, n - r, n - 1U );
qTypeGeneric_Reverse( pbase, size, 0U, n - r - 1U );
qTypeGeneric_Reverse( pbase, size, 0U, n - 1U );
}
else {
r = (size_t)(-k);
r %= n;
qTypeGeneric_Reverse( pbase, size, 0U, r - 1U );
qTypeGeneric_Reverse( pbase, size, r, n - 1U );
qTypeGeneric_Reverse( pbase, size, 0U, n - 1U );
}
}
}
/*============================================================================*/
void* qTypeGeneric_Set( void * const pbase,
const size_t size,
const size_t n,
const void * const ref )
{
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
uint8_t *p = (uint8_t*)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
void *retVal = NULL;
if ( ( NULL != pbase ) && ( size > 0U ) && ( n > 0U ) && ( NULL != ref ) ) {
size_t i;
for ( i = 0U ; i < n ; i++ ) {
retVal = memcpy( &p[ size*i ], ref, size );
}
}
return retVal;
}
/*============================================================================*/
void* qTypeGeneric_LSearch( const void *key,
const void *pbase,
const size_t n,
const size_t size,
qTypeGeneric_CompareFcn_t compar,
void *arg )
{
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
const uint8_t *pb = (const uint8_t *)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
void* retVal = NULL;
size_t i;
for ( i = 0U ; i < n ; ++i ) {
const uint8_t *element = &pb[ i*size ];
if ( 0 == compar( key, element, arg) ) {
/*cstat -MISRAC2012-Rule-11.8*/
/*cppcheck-suppress [ misra-c2012-11.8, cert-EXP05-C ]*/
retVal = (void*)element;
/*cstat +MISRAC2012-Rule-11.8*/
break;
}
}
return retVal;
}
/*============================================================================*/
void* qTypeGeneric_BSearch( const void *key,
const void *pbase,
const size_t n,
const size_t size,
qTypeGeneric_CompareFcn_t compar,
void *arg )
{
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
const uint8_t *base = (const uint8_t *)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
size_t lim = n;
const uint8_t *p;
void *retVal = NULL;
while ( 0U != lim ) {
int cmp;
p = &base[ ( lim >> 1U )*size ];
cmp = compar( key, p, arg );
if ( 0 == cmp ) {
/*cstat -MISRAC2012-Rule-11.8*/
/*cppcheck-suppress [ misra-c2012-11.8, cert-EXP05-C ]*/
retVal = (void *)p;
/*cstat +MISRAC2012-Rule-11.8*/
break;
}
else if ( cmp > 0 ) {
base = &p[ size ];
lim--;
}
else {
/*nothing to do here*/
}
lim >>= 1U;
}
return retVal;
}
/*============================================================================*/
int qTypeGeneric_ForEach( void *pbase,
const size_t size,
const size_t n,
qTypeGeneric_ForEachFcn_t f,
const bool dir,
void *arg )
{
int retVal = 0;
if ( ( NULL != pbase ) && ( NULL != f ) && ( n > 0U ) ) {
/*cstat -MISRAC2012-Rule-11.5 -CERT-EXP36-C_b*/
/*cppcheck-suppress misra-c2012-11.5 */
uint8_t *pb = (uint8_t *)pbase;
/*cstat +MISRAC2012-Rule-11.5 +CERT-EXP36-C_b*/
if ( 1 != f( -1, NULL, arg ) ) {
size_t i;
uint8_t *element;
if ( false == dir ) {
for ( i = 0U ; i < n ; ++i ) {
element = &pb[ i*size ];
retVal = f( (int)i , element, arg );
if ( 1 == retVal ) {
break;
}
}
}
else {
i = n;
while ( i-- > 0U ) {
element = &pb[ i*size ];
retVal = f( (int)i, element, arg );
if ( 1 == retVal ) {
break;
}
}
}
}
(void)f( (int)n, NULL, arg );
}
return retVal;
}
/*============================================================================*/