OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [search/] [bsearch.c] - Diff between revs 207 and 345

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*
/*
 * bsearch.c
 * bsearch.c
 * Original Author:     G. Haley
 * Original Author:     G. Haley
 * Rewritten by:        G. Noer
 * Rewritten by:        G. Noer
 *
 *
 * Searches an array of nmemb members, the initial member of which is pointed
 * Searches an array of nmemb members, the initial member of which is pointed
 * to by base, for a member that matches the object pointed to by key. The
 * to by base, for a member that matches the object pointed to by key. The
 * contents of the array shall be in ascending order according to a comparison
 * contents of the array shall be in ascending order according to a comparison
 * function pointed to by compar. The function shall return an integer less
 * function pointed to by compar. The function shall return an integer less
 * than, equal to or greater than zero if the first argument is considered to be
 * than, equal to or greater than zero if the first argument is considered to be
 * respectively less than, equal to or greater than the second. Returns a
 * respectively less than, equal to or greater than the second. Returns a
 * pointer to the matching member of the array, or a null pointer if no match
 * pointer to the matching member of the array, or a null pointer if no match
 * is found.
 * is found.
 */
 */
 
 
/*
/*
FUNCTION
FUNCTION
<<bsearch>>---binary search
<<bsearch>>---binary search
 
 
INDEX
INDEX
        bsearch
        bsearch
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdlib.h>
        #include <stdlib.h>
        void *bsearch(const void *<[key]>, const void *<[base]>,
        void *bsearch(const void *<[key]>, const void *<[base]>,
                size_t <[nmemb]>, size_t <[size]>,
                size_t <[nmemb]>, size_t <[size]>,
                int (*<[compar]>)(const void *, const void *));
                int (*<[compar]>)(const void *, const void *));
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdlib.h>
        #include <stdlib.h>
        char *bsearch(<[key]>, <[base]>, <[nmemb]>, <[size]>, <[compar]>)
        char *bsearch(<[key]>, <[base]>, <[nmemb]>, <[size]>, <[compar]>)
        char *<[key]>;
        char *<[key]>;
        char *<[base]>;
        char *<[base]>;
        size_t <[nmemb]>, <[size]>;
        size_t <[nmemb]>, <[size]>;
        int (*<[compar]>)();
        int (*<[compar]>)();
 
 
DESCRIPTION
DESCRIPTION
<<bsearch>> searches an array beginning at <[base]> for any element
<<bsearch>> searches an array beginning at <[base]> for any element
that matches <[key]>, using binary search.  <[nmemb]> is the element
that matches <[key]>, using binary search.  <[nmemb]> is the element
count of the array; <[size]> is the size of each element.
count of the array; <[size]> is the size of each element.
 
 
The array must be sorted in ascending order with respect to the
The array must be sorted in ascending order with respect to the
comparison function <[compar]> (which you supply as the last argument of
comparison function <[compar]> (which you supply as the last argument of
<<bsearch>>).
<<bsearch>>).
 
 
You must define the comparison function <<(*<[compar]>)>> to have two
You must define the comparison function <<(*<[compar]>)>> to have two
arguments; its result must be negative if the first argument is
arguments; its result must be negative if the first argument is
less than the second, zero if the two arguments match, and
less than the second, zero if the two arguments match, and
positive if the first argument is greater than the second (where
positive if the first argument is greater than the second (where
``less than'' and ``greater than'' refer to whatever arbitrary
``less than'' and ``greater than'' refer to whatever arbitrary
ordering is appropriate).
ordering is appropriate).
 
 
RETURNS
RETURNS
Returns a pointer to an element of <[array]> that matches <[key]>.  If
Returns a pointer to an element of <[array]> that matches <[key]>.  If
more than one matching element is available, the result may point to
more than one matching element is available, the result may point to
any of them.
any of them.
 
 
PORTABILITY
PORTABILITY
<<bsearch>> is ANSI.
<<bsearch>> is ANSI.
 
 
No supporting OS subroutines are required.
No supporting OS subroutines are required.
*/
*/
 
 
#include <stdlib.h>
#include <stdlib.h>
 
 
_PTR
_PTR
_DEFUN (bsearch, (key, base, nmemb, size, compar),
_DEFUN (bsearch, (key, base, nmemb, size, compar),
        _CONST _PTR key _AND
        _CONST _PTR key _AND
        _CONST _PTR base _AND
        _CONST _PTR base _AND
        size_t nmemb _AND
        size_t nmemb _AND
        size_t size _AND
        size_t size _AND
        int _EXFNPTR(compar, (const _PTR, const _PTR)))
        int _EXFNPTR(compar, (const _PTR, const _PTR)))
{
{
  _PTR current;
  _PTR current;
  size_t lower = 0;
  size_t lower = 0;
  size_t upper = nmemb;
  size_t upper = nmemb;
  size_t index;
  size_t index;
  int result;
  int result;
 
 
  if (nmemb == 0 || size == 0)
  if (nmemb == 0 || size == 0)
    return NULL;
    return NULL;
 
 
  while (lower < upper)
  while (lower < upper)
    {
    {
      index = (lower + upper) / 2;
      index = (lower + upper) / 2;
      current = (_PTR) (((char *) base) + (index * size));
      current = (_PTR) (((char *) base) + (index * size));
 
 
      result = compar (key, current);
      result = compar (key, current);
 
 
      if (result < 0)
      if (result < 0)
        upper = index;
        upper = index;
      else if (result > 0)
      else if (result > 0)
        lower = index + 1;
        lower = index + 1;
      else
      else
        return current;
        return current;
    }
    }
 
 
  return NULL;
  return NULL;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.