forked from shadow-maint/shadow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib/search/cmp/, lib/, tests/: CMP(), cmp_*(): Add macro and functions
These macros are for use with bsearch(3),lfind(3),qsort(3). Reviewed-by: Serge Hallyn <[email protected]> Signed-off-by: Alejandro Colomar <[email protected]>
- Loading branch information
1 parent
de941a7
commit 224848e
Showing
6 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <[email protected]> | ||
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]> | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
|
@@ -13,6 +13,7 @@ | |
#include <stddef.h> | ||
#include <stdlib.h> | ||
|
||
#include "search/cmp/cmp.h" | ||
#include "sizeof.h" | ||
|
||
|
||
|
@@ -27,8 +28,6 @@ | |
inline long addsl2(long a, long b); | ||
inline long addslN(size_t n, long addend[n]); | ||
|
||
inline int cmpl(const void *p1, const void *p2); | ||
|
||
|
||
inline long | ||
addsl2(long a, long b) | ||
|
@@ -57,7 +56,7 @@ addslN(size_t n, long addend[n]) | |
|
||
e = errno; | ||
while (n > 1) { | ||
qsort(addend, n, sizeof(addend[0]), cmpl); | ||
qsort(addend, n, sizeof(addend[0]), cmp_long); | ||
|
||
errno = 0; | ||
addend[0] = addsl2(addend[0], addend[--n]); | ||
|
@@ -69,18 +68,4 @@ addslN(size_t n, long addend[n]) | |
} | ||
|
||
|
||
inline int | ||
cmpl(const void *p1, const void *p2) | ||
{ | ||
const long *l1 = p1; | ||
const long *l2 = p2; | ||
|
||
if (*l1 < *l2) | ||
return -1; | ||
if (*l1 > *l2) | ||
return +1; | ||
return 0; | ||
} | ||
|
||
|
||
#endif // include guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]> | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
#include <config.h> | ||
|
||
#include "search/cmp/cmp.h" | ||
|
||
|
||
extern inline int cmp_int(const void *key, const void *elt); | ||
extern inline int cmp_long(const void *key, const void *elt); | ||
extern inline int cmp_uint(const void *key, const void *elt); | ||
extern inline int cmp_ulong(const void *key, const void *elt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]> | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
|
||
#ifndef SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_ | ||
#define SHADOW_INCLUDE_LIB_SEARCH_CMP_CMP_H_ | ||
|
||
|
||
#include <config.h> | ||
|
||
|
||
#define CMP(TYPE) \ | ||
( \ | ||
_Generic((TYPE) 0, \ | ||
int *: cmp_int, \ | ||
long *: cmp_long, \ | ||
unsigned int *: cmp_uint, \ | ||
unsigned long *: cmp_ulong \ | ||
) \ | ||
) | ||
|
||
|
||
/* Compatible with bsearch(3), lfind(3), and qsort(3). */ | ||
inline int cmp_int(const void *key, const void *elt); | ||
inline int cmp_long(const void *key, const void *elt); | ||
inline int cmp_uint(const void *key, const void *elt); | ||
inline int cmp_ulong(const void *key, const void *elt); | ||
|
||
|
||
inline int | ||
cmp_int(const void *key, const void *elt) | ||
{ | ||
const int *k = key; | ||
const int *e = elt; | ||
|
||
if (*k < *e) | ||
return -1; | ||
if (*k > *e) | ||
return +1; | ||
return 0; | ||
} | ||
|
||
|
||
inline int | ||
cmp_long(const void *key, const void *elt) | ||
{ | ||
const long *k = key; | ||
const long *e = elt; | ||
|
||
if (*k < *e) | ||
return -1; | ||
if (*k > *e) | ||
return +1; | ||
return 0; | ||
} | ||
|
||
|
||
inline int | ||
cmp_uint(const void *key, const void *elt) | ||
{ | ||
const unsigned int *k = key; | ||
const unsigned int *e = elt; | ||
|
||
if (*k < *e) | ||
return -1; | ||
if (*k > *e) | ||
return +1; | ||
return 0; | ||
} | ||
|
||
|
||
inline int | ||
cmp_ulong(const void *key, const void *elt) | ||
{ | ||
const unsigned long *k = key; | ||
const unsigned long *e = elt; | ||
|
||
if (*k < *e) | ||
return -1; | ||
if (*k > *e) | ||
return +1; | ||
return 0; | ||
} | ||
|
||
|
||
#endif // include guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters