search.h 5.3 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, see
  14. <http://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_SVID || 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. libc_hidden_proto(hsearch_r)
  85. extern int hcreate_r (size_t __nel, struct hsearch_data *__htab) __THROW;
  86. libc_hidden_proto(hcreate_r)
  87. extern void hdestroy_r (struct hsearch_data *__htab) __THROW;
  88. libc_hidden_proto(hdestroy_r)
  89. #endif
  90. /* The tsearch routines are very interesting. They make many
  91. assumptions about the compiler. It assumes that the first field
  92. in node must be the "key" field, which points to the datum.
  93. Everything depends on that. */
  94. /* For tsearch */
  95. typedef enum
  96. {
  97. preorder,
  98. postorder,
  99. endorder,
  100. leaf
  101. }
  102. VISIT;
  103. /* Search for an entry matching the given KEY in the tree pointed to
  104. by *ROOTP and insert a new element if not found. */
  105. extern void *tsearch (const void *__key, void **__rootp,
  106. __compar_fn_t __compar);
  107. libc_hidden_proto(tsearch)
  108. /* Search for an entry matching the given KEY in the tree pointed to
  109. by *ROOTP. If no matching entry is available return NULL. */
  110. extern void *tfind (const void *__key, void *const *__rootp,
  111. __compar_fn_t __compar);
  112. libc_hidden_proto(tfind)
  113. /* Remove the element matching KEY from the tree pointed to by *ROOTP. */
  114. extern void *tdelete (const void *__restrict __key,
  115. void **__restrict __rootp,
  116. __compar_fn_t __compar);
  117. #ifndef __ACTION_FN_T
  118. # define __ACTION_FN_T
  119. typedef void (*__action_fn_t) (const void *__nodep, VISIT __value,
  120. int __level);
  121. #endif
  122. /* Walk through the whole tree and call the ACTION callback for every node
  123. or leaf. */
  124. extern void twalk (const void *__root, __action_fn_t __action);
  125. #ifdef __USE_GNU
  126. /* Callback type for function to free a tree node. If the keys are atomic
  127. data this function should do nothing. */
  128. typedef void (*__free_fn_t) (void *__nodep);
  129. /* Destroy the whole tree, call FREEFCT for each node or leaf. */
  130. extern void tdestroy (void *__root, __free_fn_t __freefct);
  131. libc_hidden_proto(tdestroy)
  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. libc_hidden_proto(lfind)
  138. /* Perform linear search for KEY by comparing by COMPAR function in
  139. array [BASE,BASE+NMEMB*SIZE) and insert entry if not found. */
  140. extern void *lsearch (const void *__key, void *__base,
  141. size_t *__nmemb, size_t __size, __compar_fn_t __compar);
  142. __END_DECLS
  143. #endif /* search.h */