diff options
Diffstat (limited to 'devdocs/c/algorithm%2Fbsearch.html')
| -rw-r--r-- | devdocs/c/algorithm%2Fbsearch.html | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/devdocs/c/algorithm%2Fbsearch.html b/devdocs/c/algorithm%2Fbsearch.html new file mode 100644 index 00000000..295e3093 --- /dev/null +++ b/devdocs/c/algorithm%2Fbsearch.html @@ -0,0 +1,104 @@ + <h1 id="firstHeading" class="firstHeading">bsearch, bsearch_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">void* bsearch( const void *key, const void *ptr, size_t count, size_t size, + int (*comp)(const void*, const void*) );</pre> +</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">void* bsearch_s( const void *key, const void *ptr, rsize_t count, rsize_t size, + int (*comp)(const void *, const void *, void *), + void *context );</pre> +</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">/*QVoid*/* bsearch( const void *key, /*QVoid*/ *ptr, size_t count, size_t size, + int (*comp)(const void*, const void*) );</pre> +</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">/*QVoid*/* bsearch_s( const void *key, /*QVoid*/ *ptr, rsize_t count, rsize_t size, + int (*comp)(const void *, const void *, void *), + void *context );</pre> +</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <div class="t-li1"> +<span class="t-li">1)</span> Finds an element equal to element pointed to by <code>key</code> in an array pointed to by <code>ptr</code>. The array contains <code>count</code> elements of <code>size</code> bytes and must be partitioned with respect to <code>key</code>, that is, all the elements that compare less than must appear before all the elements that compare equal to, and those must appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using function pointed to by <code>comp</code>. The behavior is undefined if the array is not already partitioned with respect to <code>*key</code> in ascending order according to the same criterion that <code>comp</code> uses.</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the additional state argument <code>context</code> is passed to <code>comp</code> and that the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl> +<dd> +<ul> +<li> <code>count</code> or <code>size</code> is greater than <code>RSIZE_MAX</code> </li> +<li> <code>key</code>, <code>ptr</code> or <code>comp</code> is a null pointer (unless <code>count</code> is zero) </li> +</ul> </dd> +<dd>As with all bounds-checked functions, <code>bsearch_s</code> <span class="t-rev-inl t-since-c23"><span> (and the corresponding type-generic macro)</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> is only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../program" title="c/program"><code><stdlib.h></code></a>.</dd> +</dl> +</div> <div class="t-li1"> +<span class="t-li">3,4)</span> Type-generic macros equivalent to <span class="t-v">(1)</span> and <span class="t-v">(2)</span> respectively. Let <code>T</code> be a unqualified object type (including <code>void</code>). <ul> +<li> If <code>ptr</code> is of type <code>const T*</code>, the return type is <code>const void*</code>. </li> +<li> Otherwise, if <code>ptr</code> is of type <code>T*</code>, the return type is <code>void*</code>. </li> +<li> Otherwise, the behavior is undefined. </li> +</ul> If a macro definition of each of these generic functions is suppressed to access an actual function (e.g. if <code>(bsearch)</code>, <code>(bsearch_s)</code>, or a function pointer is used), the actual function declaration <span class="t-v">(1)</span> or <span class="t-v">(2)</span> becomes visible.</div> <p>If the array contains several elements that <code>comp</code> would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <p>Direct usages of actual functions <span class="t-v">(1)</span> and <span class="t-v">(2)</span> are deprecated.</p> +</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> key </td> <td> - </td> <td> pointer to the element to search for </td> +</tr> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to the array to examine </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> number of element in the array </td> +</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of each element in the array in bytes </td> +</tr> <tr class="t-par"> <td> comp </td> <td> - </td> <td> comparison function which returns a negative integer value if the first argument is <i>less</i> than the second, a positive integer value if the first argument is <i>greater</i> than the second and zero if the arguments are equivalent. <code>key</code> is passed as the first argument, an element from the array as the second.<br> <p>The signature of the comparison function should be equivalent to the following:</p> +<p><span class="t-cc"> <span class="kw4">int</span> cmp<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">void</span> <span class="sy2">*</span>a, <span class="kw4">const</span> <span class="kw4">void</span> <span class="sy2">*</span>b<span class="br0">)</span><span class="sy4">;</span></span></p> +<p>The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.</p> +<p></p> +</td> +</tr> <tr class="t-par"> <td> context </td> <td> - </td> <td> state of the comparator (e.g., collating sequence), passed to <code>comp</code> as the third argument </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1)</span> Pointer to an element in the array that compares equal to <code>*key</code>, or null pointer if such element has not been found.</div> <div class="t-li1"> +<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the null pointer is also returned on runtime constraints violations.</div> <div class="t-li1"> +<span class="t-li">3,4)</span> Same as <span class="t-v">(1)</span> and <span class="t-v">(2)</span> respectively, except that cv-qualification is adjusted.</div> <h3 id="Notes"> Notes</h3> <p>Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees.</p> +<p>Unlike other bounds-checked functions, <code>bsearch_s</code> does not treat arrays of zero size as a runtime constraint violation and instead indicates element not found (the other function that accepts arrays of zero size is <code>qsort_s</code>).</p> +<p>Until <code>bsearch_s</code>, users of <code>bsearch</code> often used global variables to represent the state of the comparator.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdlib.h> +#include <stdio.h> + +struct data { + int nr; + char const *value; +} dat[] = { + {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"} +}; + +int data_cmp(void const *lhs, void const *rhs) +{ + struct data const *const l = lhs; + struct data const *const r = rhs; + + if (l->nr < r->nr) return -1; + else if (l->nr > r->nr) return 1; + else return 0; + + // return (l->nr > r->nr) - (l->nr < r->nr); // possible shortcut + // return l->nr - r->nr; // erroneous shortcut (fails if INT_MIN is present) +} + +int main(void) +{ + struct data key = { .nr = 3 }; + struct data const *res = bsearch(&key, dat, sizeof dat / sizeof dat[0], + sizeof dat[0], data_cmp); + if (res) { + printf("No %d: %s\n", res->nr, res->value); + } else { + printf("No %d not found\n", key.nr); + } +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">No 3: Hello</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.22.5.1 The bsearch function (p: 258) </li> +<li> K.3.6.3.1 The bsearch_s function (p: 441-442) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.22.5.1 The bsearch function (p: 355) </li> +<li> K.3.6.3.1 The bsearch_s function (p: 608-609) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.20.5.1 The bsearch function (p: 318-319) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.10.5.1 The bsearch function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="qsort" title="c/algorithm/qsort"> <span class="t-lines"><span>qsort</span><span>qsort_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> sorts a range of elements with unspecified type <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/algorithm/bsearch" title="cpp/algorithm/bsearch">C++ documentation</a></span> for <code>bsearch</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/algorithm/bsearch" class="_attribution-link">https://en.cppreference.com/w/c/algorithm/bsearch</a> + </p> +</div> |
