search.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Declarations for System V style searching functions.
  2. Copyright (C) 1995-2020 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, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _SEARCH_H
  16. #define _SEARCH_H 1
  17. #include <features.h>
  18. #define __need_size_t
  19. #include <stddef.h>
  20. __BEGIN_DECLS
  21. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  22. /* Prototype structure for a linked-list data structure.
  23. This is the type used by the `insque' and `remque' functions. */
  24. # ifdef __USE_GNU
  25. struct qelem
  26. {
  27. struct qelem *q_forw;
  28. struct qelem *q_back;
  29. char q_data[1];
  30. };
  31. # endif
  32. /* Insert ELEM into a doubly-linked list, after PREV. */
  33. extern void insque (void *__elem, void *__prev) __THROW;
  34. /* Unlink ELEM from the doubly-linked list that it is in. */
  35. extern void remque (void *__elem) __THROW;
  36. #endif
  37. /* For use with hsearch(3). */
  38. #ifndef __COMPAR_FN_T
  39. # define __COMPAR_FN_T
  40. typedef int (*__compar_fn_t) (const void *, const void *);
  41. # ifdef __USE_GNU
  42. typedef __compar_fn_t comparison_fn_t;
  43. # endif
  44. #endif
  45. /* Action which shall be performed in the call the hsearch. */
  46. typedef enum
  47. {
  48. FIND,
  49. ENTER
  50. }
  51. ACTION;
  52. typedef struct entry
  53. {
  54. char *key;
  55. void *data;
  56. }
  57. ENTRY;
  58. /* Opaque type for internal use. */
  59. struct _ENTRY;
  60. /* Family of hash table handling functions. The functions also
  61. have reentrant counterparts ending with _r. The non-reentrant
  62. functions all work on a signle internal hashing table. */
  63. /* Search for entry matching ITEM.key in internal hash table. If
  64. ACTION is `FIND' return found entry or signal error by returning
  65. NULL. If ACTION is `ENTER' replace existing data (if any) with
  66. ITEM.data. */
  67. extern ENTRY *hsearch (ENTRY __item, ACTION __action) __THROW;
  68. /* Create a new hashing table which will at most contain NEL elements. */
  69. extern int hcreate (size_t __nel) __THROW;
  70. /* Destroy current internal hashing table. */
  71. extern void hdestroy (void) __THROW;
  72. #ifdef __USE_GNU
  73. /* Data type for reentrant functions. */
  74. struct hsearch_data
  75. {
  76. struct _ENTRY *table;
  77. unsigned int size;
  78. unsigned int filled;
  79. };
  80. /* Reentrant versions which can handle multiple hashing tables at the
  81. same time. */
  82. extern int hsearch_r (ENTRY __item, ACTION __action, ENTRY **__retval,
  83. struct hsearch_data *__htab) __THROW;
  84. extern int hcreate_r (size_t __nel, struct hsearch_data *__htab) __THROW;
  85. extern void hdestroy_r (struct hsearch_data *__htab) __THROW;
  86. #endif
  87. /* The tsearch routines are very interesting. They make many
  88. assumptions about the compiler. It assumes that the first field
  89. in node must be the "key" field, which points to the datum.
  90. Everything depends on that. */
  91. /* For tsearch */
  92. typedef enum
  93. {
  94. preorder,
  95. postorder,
  96. endorder,
  97. leaf
  98. }
  99. VISIT;
  100. /* Search for an entry matching the given KEY in the tree pointed to
  101. by *ROOTP and insert a new element if not found. */
  102. extern void *tsearch (const void *__key, void **__rootp,
  103. __compar_fn_t __compar);
  104. /* Search for an entry matching the given KEY in the tree pointed to
  105. by *ROOTP. If no matching entry is available return NULL. */
  106. extern void *tfind (const void *__key, void *const *__rootp,
  107. __compar_fn_t __compar);
  108. /* Remove the element matching KEY from the tree pointed to by *ROOTP. */
  109. extern void *tdelete (const void *__restrict __key,
  110. void **__restrict __rootp,
  111. __compar_fn_t __compar);
  112. #ifndef __ACTION_FN_T
  113. # define __ACTION_FN_T
  114. typedef void (*__action_fn_t) (const void *__nodep, VISIT __value,
  115. int __level);
  116. #endif
  117. /* Walk through the whole tree and call the ACTION callback for every node
  118. or leaf. */
  119. extern void twalk (const void *__root, __action_fn_t __action);
  120. #ifdef __USE_GNU
  121. /* Like twalk, but pass down a closure parameter instead of the
  122. level. */
  123. extern void twalk_r (const void *__root,
  124. void (*) (const void *__nodep, VISIT __value,
  125. void *__closure),
  126. void *__closure);
  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. #endif
  133. /* Perform linear search for KEY by comparing by COMPAR in an array
  134. [BASE,BASE+NMEMB*SIZE). */
  135. extern void *lfind (const void *__key, const void *__base,
  136. size_t *__nmemb, size_t __size, __compar_fn_t __compar);
  137. /* Perform linear search for KEY by comparing by COMPAR function in
  138. array [BASE,BASE+NMEMB*SIZE) and insert entry if not found. */
  139. extern void *lsearch (const void *__key, void *__base,
  140. size_t *__nmemb, size_t __size, __compar_fn_t __compar);
  141. __END_DECLS
  142. #endif /* search.h */