search.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Declarations for System V style searching functions.
  2. Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _SEARCH_H
  17. #define _SEARCH_H 1
  18. #include <features.h>
  19. #define __need_size_t
  20. #include <stddef.h>
  21. __BEGIN_DECLS
  22. #if defined __USE_SVID || defined __USE_XOPEN_EXTENDED
  23. /* Prototype structure for a linked-list data structure.
  24. This is the type used by the `insque' and `remque' functions. */
  25. # ifdef __USE_GNU
  26. struct qelem
  27. {
  28. struct qelem *q_forw;
  29. struct qelem *q_back;
  30. char q_data[1];
  31. };
  32. # endif
  33. /* Insert ELEM into a doubly-linked list, after PREV. */
  34. extern void insque (void *__elem, void *__prev) __THROW;
  35. /* Unlink ELEM from the doubly-linked list that it is in. */
  36. extern void remque (void *__elem) __THROW;
  37. #endif
  38. /* For use with hsearch(3). */
  39. #ifndef __COMPAR_FN_T
  40. # define __COMPAR_FN_T
  41. typedef int (*__compar_fn_t) (__const void *, __const void *);
  42. # ifdef __USE_GNU
  43. typedef __compar_fn_t comparison_fn_t;
  44. # endif
  45. #endif
  46. /* Action which shall be performed in the call the hsearch. */
  47. typedef enum
  48. {
  49. FIND,
  50. ENTER
  51. }
  52. ACTION;
  53. typedef struct entry
  54. {
  55. char *key;
  56. void *data;
  57. }
  58. ENTRY;
  59. /* Opaque type for internal use. */
  60. struct _ENTRY;
  61. /* Family of hash table handling functions. The functions also
  62. have reentrant counterparts ending with _r. The non-reentrant
  63. functions all work on a signle internal hashing table. */
  64. /* Search for entry matching ITEM.key in internal hash table. If
  65. ACTION is `FIND' return found entry or signal error by returning
  66. NULL. If ACTION is `ENTER' replace existing data (if any) with
  67. ITEM.data. */
  68. extern ENTRY *hsearch (ENTRY __item, ACTION __action) __THROW;
  69. /* Create a new hashing table which will at most contain NEL elements. */
  70. extern int hcreate (size_t __nel) __THROW;
  71. /* Destroy current internal hashing table. */
  72. extern void hdestroy (void) __THROW;
  73. #ifdef __USE_GNU
  74. /* Data type for reentrant functions. */
  75. struct hsearch_data
  76. {
  77. struct _ENTRY *table;
  78. unsigned int size;
  79. unsigned int filled;
  80. };
  81. /* Reentrant versions which can handle multiple hashing tables at the
  82. same time. */
  83. extern int hsearch_r (ENTRY __item, ACTION __action, ENTRY **__retval,
  84. struct hsearch_data *__htab) __THROW;
  85. libc_hidden_proto(hsearch_r)
  86. extern int hcreate_r (size_t __nel, struct hsearch_data *__htab) __THROW;
  87. libc_hidden_proto(hcreate_r)
  88. extern void hdestroy_r (struct hsearch_data *__htab) __THROW;
  89. libc_hidden_proto(hdestroy_r)
  90. #endif
  91. /* The tsearch routines are very interesting. They make many
  92. assumptions about the compiler. It assumes that the first field
  93. in node must be the "key" field, which points to the datum.
  94. Everything depends on that. */
  95. /* For tsearch */
  96. typedef enum
  97. {
  98. preorder,
  99. postorder,
  100. endorder,
  101. leaf
  102. }
  103. VISIT;
  104. /* Search for an entry matching the given KEY in the tree pointed to
  105. by *ROOTP and insert a new element if not found. */
  106. extern void *tsearch (__const void *__key, void **__rootp,
  107. __compar_fn_t __compar);
  108. libc_hidden_proto(tsearch)
  109. /* Search for an entry matching the given KEY in the tree pointed to
  110. by *ROOTP. If no matching entry is available return NULL. */
  111. extern void *tfind (__const void *__key, void *__const *__rootp,
  112. __compar_fn_t __compar);
  113. libc_hidden_proto(tfind)
  114. /* Remove the element matching KEY from the tree pointed to by *ROOTP. */
  115. extern void *tdelete (__const void *__restrict __key,
  116. void **__restrict __rootp,
  117. __compar_fn_t __compar);
  118. #ifndef __ACTION_FN_T
  119. # define __ACTION_FN_T
  120. typedef void (*__action_fn_t) (__const void *__nodep, VISIT __value,
  121. int __level);
  122. #endif
  123. /* Walk through the whole tree and call the ACTION callback for every node
  124. or leaf. */
  125. extern void twalk (__const void *__root, __action_fn_t __action);
  126. #ifdef __USE_GNU
  127. /* Callback type for function to free a tree node. If the keys are atomic
  128. data this function should do nothing. */
  129. typedef void (*__free_fn_t) (void *__nodep);
  130. /* Destroy the whole tree, call FREEFCT for each node or leaf. */
  131. extern void tdestroy (void *__root, __free_fn_t __freefct);
  132. libc_hidden_proto(tdestroy)
  133. #endif
  134. /* Perform linear search for KEY by comparing by COMPAR in an array
  135. [BASE,BASE+NMEMB*SIZE). */
  136. extern void *lfind (__const void *__key, __const void *__base,
  137. size_t *__nmemb, size_t __size, __compar_fn_t __compar);
  138. libc_hidden_proto(lfind)
  139. /* Perform linear search for KEY by comparing by COMPAR function in
  140. array [BASE,BASE+NMEMB*SIZE) and insert entry if not found. */
  141. extern void *lsearch (__const void *__key, void *__base,
  142. size_t *__nmemb, size_t __size, __compar_fn_t __compar);
  143. __END_DECLS
  144. #endif /* search.h */