home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / LSEARCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.5 KB  |  150 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - lsearch.c
  3.  *
  4.  * function(s)
  5.  *        _lsearch - searches a table
  6.  *        lsearch  - searches and updates a table
  7.  *        lfind    - perform a linear search
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <mem.h>
  21. #include <stddef.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            _lsearch - searches a table
  26.  
  27. Usage           static void *near
  28.                 pascal _lsearch(const void      *key,
  29.                                 void            *base,
  30.                                 size_t          *nelem,
  31.                                 size_t          width,
  32.                                 int cdecl     (*fcmp)(const void *, const void *),
  33.                                 int             flag)
  34.  
  35. Description     performs lfind or lsearch depending on the value of flag.
  36.                 If flag is 1 it updates the table if no match, if flag
  37.                 is 0 it only searches.
  38.  
  39. Return value    Nothing
  40.  
  41. *---------------------------------------------------------------------*/
  42. static void *near pascal _lsearch(const void    *key,
  43.                                   register void *base,
  44.                                   size_t        *nelem,
  45.                                   size_t        width,
  46.                                   int cdecl (*fcmp)(const void *, const void *),
  47.                                   int            flag)
  48. {
  49.         register int    Wrk;
  50.  
  51.         for (Wrk = *nelem; Wrk > 0; Wrk--)
  52.         {
  53.                 if (((*fcmp)(key, base)) == 0)
  54.                         return(base);
  55.                 ((char *)base) += width;
  56.         }
  57.         if (flag)
  58.         {
  59.                 (*nelem)++;
  60.                 movmem((void *)key, base, width);
  61.         }
  62.         else
  63.                 base = NULL;
  64.         return(base);
  65. }
  66.  
  67.  
  68. /*--------------------------------------------------------------------------*
  69.  
  70. Name            lsearch - searches and updates a table
  71.                 lfind   - perform a linear search
  72.  
  73. Usage           void *lsearch(const void *key,
  74.                               void *base,
  75.                               size_t *nelem,
  76.                               size_t width,
  77.                               int cdecl (*fcmp)(const void *, const void *));
  78.  
  79.                 void *lfind(const void *key,
  80.                             const void *base,
  81.                             size_t *nelem,
  82.                             size_t width,
  83.                             int cdecl (*fcmp)(const void *, const void *));
  84.  
  85. Prototype in    stdlib.h
  86.  
  87. Description     lfind and lsearch search  a table for information. Because
  88.                 these are linear searches, the table entries do not need to
  89.                 be sorted before a call to lfind or lsearch. If the item that
  90.                 key  points  to  is  not  in the table, lsearch appends that
  91.                 item to the table, but lfind does not.
  92.  
  93.                 . key points to  the item to be searched  for. ("the search
  94.                   key")
  95.  
  96.                 . base points to the base (0th element) of the search table
  97.  
  98.                 . nelem points  to an integer containing  number of entries
  99.                   in the table.
  100.  
  101.                 . width contains the number of bytes in each entry
  102.  
  103.                 . fcmp  points to  a user-written  comparison routine. That
  104.                 routine compares 2  items and returns a value  based on the
  105.                 comparison.
  106.                 On each call to the comparison routine, the search function
  107.                 passes 2 arguments: key, a pointer to the item being searched
  108.                 for;  and elem,  a pointer   to the  element of  base being
  109.                 compared.
  110.                 fcmp  is free  to interpret  the search  key and  the table
  111.                 entry any way it likes.
  112.  
  113. Return value    lsearch and lfind return the  address of the first entry in
  114.                 the  table that  matches the   search key.  If no  match is
  115.                 found, lfind returns 0.
  116.  
  117.                 In lsearch and lfind:
  118.  
  119.                         If the key is                   fcmp returns
  120.                         -------------                   ------------
  121.                         Not identical to *elem          An integer != 0
  122.                         Identical to *elem              0
  123.  
  124. *---------------------------------------------------------------------------*/
  125. void * _FARFUNC lsearch(const void *key, void *base, size_t *nelem, size_t width,
  126.               int cdecl (*_FARFUNC fcmp)(const void *, const void *))
  127. {
  128.         return(_lsearch(key,base,nelem,width,fcmp,1));
  129. }
  130.  
  131.  
  132. /*---------------------------------------------------------------------*
  133.  
  134. Name            lfind - performs a linear search
  135.  
  136. Usage           void *lfind(void *key, void *base, size_t *nelem,
  137.                             size_t width,
  138.                             int (*fcmp)(const void *, const void *) );
  139.  
  140. Prototype in    stdlib.h
  141.  
  142. Description     see lsearch above
  143.  
  144. *---------------------------------------------------------------------*/
  145. void * _FARFUNC lfind(const void *key, const void *base, size_t *nelem, size_t width,
  146.             int cdecl (* _FARFUNC fcmp)(const void *, const void *))
  147. {
  148.         return(_lsearch(key,(void *)base,nelem,width,fcmp,0));
  149. }
  150.