sys/binsearch: Add binary search module.#8974
sys/binsearch: Add binary search module.#8974jcarrano wants to merge 3 commits intoRIOT-OS:masterfrom
Conversation
|
Is there any benefit to using this over stdlib.h |
|
A more flexible approach is to use a comparison function pointer argument, which allows adapting it to comparing all kinds of data, not only strings. |
|
@gebart : It could be implemented in terms of bsearch, but in the end you'll end up with even more code. The idea of adding a comparison callback is quite easy to implement, but I don't know how useful it will be. |
|
In my opinion, a custom comparison I'd extremely useful. Even just reversing the sort order can be trivially implemented with a custom comparison function ( I don't like adding code which does the same thing as some standard library part unless there is any special circumstance that makes the standard library version unsuitable (e.g. the fmt library which uses a lot less memory than the libc printf family of functions). |
Description
There are places (for example, the shell and the Lua interpreter I'm working on) where one has to search an array of structs (a table), which is generally ordered.
This code allows to do this in a generic way. Also, it uses binary search so it should be faster than iteration for bigger arrays.
There are two functions and two macros. The macros are a convenience to avoid having to use
sizeofandoffsetofall the time.