libc: minimal: Add bsearch function

This function implements generic binary-search.

Fixes #15159

Signed-off-by: Balaji Kulkarni <balaji.kulkarni92@gmail.com>
This commit is contained in:
Balaji Kulkarni 2019-04-20 12:13:35 +05:30 committed by Andrew Boie
commit a25dce964b
5 changed files with 81 additions and 1 deletions

View file

@ -237,6 +237,7 @@
/lib/gui/ @vanwinkeljan /lib/gui/ @vanwinkeljan
/lib/os/ @andrewboie @andyross /lib/os/ @andrewboie @andyross
/lib/posix/ @pfalcon /lib/posix/ @pfalcon
/lib/libc/minimal/source/stdlib/bsearch.c @balajikulkarni
/kernel/device.c @andrewboie @andyross @nashif /kernel/device.c @andrewboie @andyross @nashif
/kernel/idle.c @andrewboie @andyross @nashif /kernel/idle.c @andrewboie @andyross @nashif
/samples/basic/servo_motor/*microbit* @jhe /samples/basic/servo_motor/*microbit* @jhe

View file

@ -8,6 +8,7 @@ zephyr_library_sources(
source/stdlib/strtol.c source/stdlib/strtol.c
source/stdlib/strtoul.c source/stdlib/strtoul.c
source/stdlib/malloc.c source/stdlib/malloc.c
source/stdlib/bsearch.c
source/string/strncasecmp.c source/string/strncasecmp.c
source/string/strstr.c source/string/strstr.c
source/string/string.c source/string/string.c

View file

@ -24,6 +24,10 @@ void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size); void *reallocarray(void *ptr, size_t nmemb, size_t size);
void *bsearch(const void *key, const void *array,
size_t count, size_t size,
int (*cmp)(const void *key, const void *element));
int rand(void); int rand(void);
#define abs(x) ((x) < 0 ? -(x) : (x)) #define abs(x) ((x) < 0 ? -(x) : (x))

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2019 Balaji Kulkarni
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <stdio.h>
/**
* @brief Generic implementation of Binary Search
*
* @param key pointer to first element to search
* @param array pointer to item being searched for
* @param count number of elements
* @param size size of each element
* @param cmp pointer to comparison function
*
* @return pointer to key if present in the array, or NULL otherwise
*/
void *bsearch(const void *key, const void *array, size_t count, size_t size,
int (*cmp)(const void *key, const void *element))
{
size_t low = 0;
size_t high = count;
size_t index;
int result;
const char *pivot;
while (low < high) {
index = low + (high - low) / 2;
pivot = (const char *)array + index * size;
result = cmp(key, pivot);
if (result == 0) {
return (void *)pivot;
} else if (result > 0) {
low = index + 1;
} else {
high = index;
}
}
return NULL;
}

View file

@ -24,6 +24,7 @@
#include <stddef.h> #include <stddef.h>
#include <zephyr/types.h> #include <zephyr/types.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
/* Recent GCC's are issuing a warning for the truncated strncpy() /* Recent GCC's are issuing a warning for the truncated strncpy()
* below (the static source string is longer than the locally-defined * below (the static source string is longer than the locally-defined
@ -254,6 +255,32 @@ void test_memcmp(void)
zassert_true((ret != 0), "memcmp 5"); zassert_true((ret != 0), "memcmp 5");
} }
/**
*
* @brief Test binary search function
*
*/
int cmp_func(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}
void test_bsearch(void)
{
void *result = NULL;
int arr[5] = { 2, 5, 20, 50, 60 };
size_t size = ARRAY_SIZE(arr);
int key = 30;
result = (int *)bsearch(&key, arr, size, sizeof(int), cmp_func);
zassert_is_null(result, "bsearch -key not found");
key = 60;
result = (int *)bsearch(&key, arr, size, sizeof(int), cmp_func);
zassert_not_null(result, "bsearch -key found");
}
void test_main(void) void test_main(void)
{ {
ztest_test_suite(test_c_lib, ztest_test_suite(test_c_lib,
@ -267,7 +294,8 @@ void test_main(void)
ztest_unit_test(test_strncpy), ztest_unit_test(test_strncpy),
ztest_unit_test(test_memset), ztest_unit_test(test_memset),
ztest_unit_test(test_strlen), ztest_unit_test(test_strlen),
ztest_unit_test(test_strcmp) ztest_unit_test(test_strcmp),
ztest_unit_test(test_bsearch)
); );
ztest_run_test_suite(test_c_lib); ztest_run_test_suite(test_c_lib);
} }