search.texi 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. @node Searching and Sorting, Pattern Matching, Message Translation, Top
  2. @c %MENU% General searching and sorting functions
  3. @chapter Searching and Sorting
  4. This chapter describes functions for searching and sorting arrays of
  5. arbitrary objects. You pass the appropriate comparison function to be
  6. applied as an argument, along with the size of the objects in the array
  7. and the total number of elements.
  8. @menu
  9. * Comparison Functions:: Defining how to compare two objects.
  10. Since the sort and search facilities
  11. are general, you have to specify the
  12. ordering.
  13. * Array Search Function:: The @code{bsearch} function.
  14. * Array Sort Function:: The @code{qsort} function.
  15. * Search/Sort Example:: An example program.
  16. * Hash Search Function:: The @code{hsearch} function.
  17. * Tree Search Function:: The @code{tsearch} function.
  18. @end menu
  19. @node Comparison Functions
  20. @section Defining the Comparison Function
  21. @cindex Comparison Function
  22. In order to use the sorted array library functions, you have to describe
  23. how to compare the elements of the array.
  24. To do this, you supply a comparison function to compare two elements of
  25. the array. The library will call this function, passing as arguments
  26. pointers to two array elements to be compared. Your comparison function
  27. should return a value the way @code{strcmp} (@pxref{String/Array
  28. Comparison}) does: negative if the first argument is ``less'' than the
  29. second, zero if they are ``equal'', and positive if the first argument
  30. is ``greater''.
  31. Here is an example of a comparison function which works with an array of
  32. numbers of type @code{long int}:
  33. @smallexample
  34. int
  35. compare_long_ints (const void *a, const void *b)
  36. @{
  37. const long int *la = a;
  38. const long int *lb = b;
  39. return (*la > *lb) - (*la < *lb);
  40. @}
  41. @end smallexample
  42. (The code would have to be more complicated for an array of @code{double},
  43. to handle NaNs correctly.)
  44. The header file @file{stdlib.h} defines a name for the data type of
  45. comparison functions. This type is a GNU extension.
  46. @comment stdlib.h
  47. @comment GNU
  48. @tindex comparison_fn_t
  49. @smallexample
  50. int comparison_fn_t (const void *, const void *);
  51. @end smallexample
  52. @node Array Search Function
  53. @section Array Search Function
  54. @cindex search function (for arrays)
  55. @cindex binary search function (for arrays)
  56. @cindex array search function
  57. Generally searching for a specific element in an array means that
  58. potentially all elements must be checked. @Theglibc{} contains
  59. functions to perform linear search. The prototypes for the following
  60. two functions can be found in @file{search.h}.
  61. @deftypefun {void *} lfind (const void *@var{key}, const void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
  62. @standards{SVID, search.h}
  63. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  64. The @code{lfind} function searches in the array with @code{*@var{nmemb}}
  65. elements of @var{size} bytes pointed to by @var{base} for an element
  66. which matches the one pointed to by @var{key}. The function pointed to
  67. by @var{compar} is used to decide whether two elements match.
  68. The return value is a pointer to the matching element in the array
  69. starting at @var{base} if it is found. If no matching element is
  70. available @code{NULL} is returned.
  71. The mean runtime of this function is proportional to @code{*@var{nmemb}/2},
  72. assuming random elements of the array are searched for. This
  73. function should be used only if elements often get added to or deleted from
  74. the array in which case it might not be useful to sort the array before
  75. searching.
  76. @end deftypefun
  77. @deftypefun {void *} lsearch (const void *@var{key}, void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
  78. @standards{SVID, search.h}
  79. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  80. @c A signal handler that interrupted an insertion and performed an
  81. @c insertion itself would leave the array in a corrupt state (e.g. one
  82. @c new element initialized twice, with parts of both initializations
  83. @c prevailing, and another uninitialized element), but this is just a
  84. @c special case of races on user-controlled objects, that have to be
  85. @c avoided by users.
  86. @c In case of cancellation, we know the array won't be left in a corrupt
  87. @c state; the new element is initialized before the element count is
  88. @c incremented, and the compiler can't reorder these operations because
  89. @c it can't know that they don't alias. So, we'll either cancel after
  90. @c the increment and the initialization are both complete, or the
  91. @c increment won't have taken place, and so how far the initialization
  92. @c got doesn't matter.
  93. The @code{lsearch} function is similar to the @code{lfind} function. It
  94. searches the given array for an element and returns it if found. The
  95. difference is that if no matching element is found the @code{lsearch}
  96. function adds the object pointed to by @var{key} (with a size of
  97. @var{size} bytes) at the end of the array and it increments the value of
  98. @code{*@var{nmemb}} to reflect this addition.
  99. This means for the caller that if it is not sure that the array contains
  100. the element one is searching for the memory allocated for the array
  101. starting at @var{base} must have room for at least @var{size} more
  102. bytes. If one is sure the element is in the array it is better to use
  103. @code{lfind} so having more room in the array is always necessary when
  104. calling @code{lsearch}.
  105. @end deftypefun
  106. To search a sorted or partially sorted array for an element matching the key,
  107. use the @code{bsearch} function. The prototype for this function is in
  108. the header file @file{stdlib.h}.
  109. @pindex stdlib.h
  110. @deftypefun {void *} bsearch (const void *@var{key}, const void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
  111. @standards{ISO, stdlib.h}
  112. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  113. The @code{bsearch} function searches @var{array} for an element
  114. that is equivalent to @var{key}. The array contains @var{count} elements,
  115. each of which is of size @var{size} bytes.
  116. The @var{compare} function is used to perform the comparison. This
  117. function is called with arguments that point to the key and to an
  118. array element, in that order, and should return an
  119. integer less than, equal to, or greater than zero corresponding to
  120. whether the key is considered less than, equal to, or greater than
  121. the array element. The function should not alter the array's contents,
  122. and the same array element should always compare the same way with the key.
  123. Although the array need not be completely sorted, it should be
  124. partially sorted with respect to @var{key}. That is, the array should
  125. begin with elements that compare less than @var{key}, followed by
  126. elements that compare equal to @var{key}, and ending with elements
  127. that compare greater than @var{key}. Any or all of these element
  128. sequences can be empty.
  129. The return value is a pointer to a matching array element, or a null
  130. pointer if no match is found. If the array contains more than one element
  131. that matches, the one that is returned is unspecified.
  132. This function derives its name from the fact that it is implemented
  133. using the binary search algorithm.
  134. In ISO C23 and later, this function is qualifier-generic:
  135. that is, it is also implemented as a function-like macro,
  136. and when the macro is used and @var{array} has a type
  137. that is a pointer to a @code{const}-qualified object type,
  138. @code{bsearch} returns @code{const void *}.
  139. As an obsolescent feature, if the macro is suppressed
  140. the external function returns @code{void *} regardless.
  141. @end deftypefun
  142. @node Array Sort Function
  143. @section Array Sort Function
  144. @cindex sort function (for arrays)
  145. @cindex quick sort function (for arrays)
  146. @cindex array sort function
  147. To sort an array using an arbitrary comparison function, use the
  148. @code{qsort} function. The prototype for this function is in
  149. @file{stdlib.h}.
  150. @pindex stdlib.h
  151. @deftypefun void qsort (void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
  152. @standards{ISO, stdlib.h}
  153. @safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
  154. The @code{qsort} function sorts the array @var{array}. The array
  155. contains @var{count} elements, each of which is of size @var{size}.
  156. The @var{compare} function is used to perform the comparison on the
  157. array elements. This function is called with two pointer arguments and
  158. should return an integer less than, equal to, or greater than zero
  159. corresponding to whether its first argument is considered less than,
  160. equal to, or greater than its second argument.
  161. The function must not alter the array's contents, and must define a
  162. total ordering on the array elements, including any unusual values
  163. such as floating-point NaN (@pxref{Infinity and NaN}).
  164. Because the sorting process can move elements,
  165. the function's return value must not depend on the element addresses
  166. or the relative positions of elements within the array,
  167. as these are meaningless while @code{qsort} is running.
  168. @cindex stable sorting
  169. @strong{Warning:} If two elements compare equal, their order after
  170. sorting is unpredictable. That is to say, the sorting is not stable.
  171. This can make a difference when the comparison considers only part of
  172. the elements and two elements that compare equal may differ in other
  173. respects. To ensure a stable sort in this situation, you can augment
  174. each element with an appropriate tie-breaking value, such as its
  175. original array index.
  176. Here is a simple example of sorting an array of @code{long int} in numerical
  177. order, using the comparison function defined above (@pxref{Comparison
  178. Functions}):
  179. @smallexample
  180. @{
  181. long int *array;
  182. size_t nmemb;
  183. @dots{}
  184. qsort (array, nmemb, sizeof *array, compare_long_ints);
  185. @}
  186. @end smallexample
  187. The @code{qsort} function derives its name from the fact that it was
  188. originally implemented using the ``quick sort'' algorithm.
  189. The implementation of @code{qsort} attempts to allocate auxiliary memory
  190. and use the merge sort algorithm, without violating C standard requirement
  191. that arguments passed to the comparison function point within the array.
  192. If the memory allocation fails, @code{qsort} resorts to a slower algorithm.
  193. @end deftypefun
  194. @node Search/Sort Example
  195. @section Searching and Sorting Example
  196. Here is an example showing the use of @code{qsort} and @code{bsearch}
  197. with an array of structures. The elements of the array are sorted
  198. by comparing their @code{name} fields with the @code{strcmp} function.
  199. Then, we can look up individual elements based on their names.
  200. @comment This example is dedicated to the memory of Jim Henson. RIP.
  201. @smallexample
  202. @include search.c.texi
  203. @end smallexample
  204. @cindex Kermit the frog
  205. The output from this program looks like:
  206. @smallexample
  207. Kermit, the frog
  208. Piggy, the pig
  209. Gonzo, the whatever
  210. Fozzie, the bear
  211. Sam, the eagle
  212. Robin, the frog
  213. Animal, the animal
  214. Camilla, the chicken
  215. Sweetums, the monster
  216. Dr. Strangepork, the pig
  217. Link Hogthrob, the pig
  218. Zoot, the human
  219. Dr. Bunsen Honeydew, the human
  220. Beaker, the human
  221. Swedish Chef, the human
  222. Animal, the animal
  223. Beaker, the human
  224. Camilla, the chicken
  225. Dr. Bunsen Honeydew, the human
  226. Dr. Strangepork, the pig
  227. Fozzie, the bear
  228. Gonzo, the whatever
  229. Kermit, the frog
  230. Link Hogthrob, the pig
  231. Piggy, the pig
  232. Robin, the frog
  233. Sam, the eagle
  234. Swedish Chef, the human
  235. Sweetums, the monster
  236. Zoot, the human
  237. Kermit, the frog
  238. Gonzo, the whatever
  239. Couldn't find Janice.
  240. @end smallexample
  241. @node Hash Search Function
  242. @section The @code{hsearch} function.
  243. The functions mentioned so far in this chapter are for searching in a sorted
  244. or unsorted array. There are other methods to organize information
  245. which later should be searched. The costs of insert, delete and search
  246. differ. One possible implementation is using hashing tables.
  247. The following functions are declared in the header file @file{search.h}.
  248. @deftypefun int hcreate (size_t @var{nel})
  249. @standards{SVID, search.h}
  250. @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  251. @c hcreate @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
  252. @c hcreate_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
  253. The @code{hcreate} function creates a hashing table which can contain at
  254. least @var{nel} elements. There is no possibility to grow this table so
  255. it is necessary to choose the value for @var{nel} wisely. The method
  256. used to implement this function might make it necessary to make the
  257. number of elements in the hashing table larger than the expected maximal
  258. number of elements. Hashing tables usually work inefficiently if they are
  259. filled 80% or more. The constant access time guaranteed by hashing can
  260. only be achieved if few collisions exist. See Knuth's ``The Art of
  261. Computer Programming, Part 3: Searching and Sorting'' for more
  262. information.
  263. The weakest aspect of this function is that there can be at most one
  264. hashing table used through the whole program. The table is allocated
  265. in local memory out of control of the programmer. As an extension @theglibc{}
  266. provides an additional set of functions with a reentrant
  267. interface which provides a similar interface but which allows keeping
  268. arbitrarily many hashing tables.
  269. It is possible to use more than one hashing table in the program run if
  270. the former table is first destroyed by a call to @code{hdestroy}.
  271. The function returns a non-zero value if successful. If it returns zero,
  272. something went wrong. This could either mean there is already a hashing
  273. table in use or the program ran out of memory.
  274. @end deftypefun
  275. @deftypefun void hdestroy (void)
  276. @standards{SVID, search.h}
  277. @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  278. @c hdestroy @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
  279. @c hdestroy_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
  280. The @code{hdestroy} function can be used to free all the resources
  281. allocated in a previous call of @code{hcreate}. After a call to this
  282. function it is again possible to call @code{hcreate} and allocate a new
  283. table with possibly different size.
  284. It is important to remember that the elements contained in the hashing
  285. table at the time @code{hdestroy} is called are @emph{not} freed by this
  286. function. It is the responsibility of the program code to free those
  287. strings (if necessary at all). Freeing all the element memory is not
  288. possible without extra, separately kept information since there is no
  289. function to iterate through all available elements in the hashing table.
  290. If it is really necessary to free a table and all elements the
  291. programmer has to keep a list of all table elements and before calling
  292. @code{hdestroy} s/he has to free all element's data using this list.
  293. This is a very unpleasant mechanism and it also shows that this kind of
  294. hashing table is mainly meant for tables which are created once and
  295. used until the end of the program run.
  296. @end deftypefun
  297. Entries of the hashing table and keys for the search are defined using
  298. this type:
  299. @deftp {Data type} ENTRY
  300. @table @code
  301. @item char *key
  302. Pointer to a zero-terminated string of characters describing the key for
  303. the search or the element in the hashing table.
  304. This is a limiting restriction of the functionality of the
  305. @code{hsearch} functions: They can only be used for data sets which
  306. use the NUL character always and solely to terminate keys. It is not
  307. possible to handle general binary data for keys.
  308. @item void *data
  309. Generic pointer for use by the application. The hashing table
  310. implementation preserves this pointer in entries, but does not use it
  311. in any way otherwise.
  312. @end table
  313. @end deftp
  314. @deftp {Data type} {struct entry}
  315. The underlying type of @code{ENTRY}.
  316. @end deftp
  317. @deftypefun {ENTRY *} hsearch (ENTRY @var{item}, ACTION @var{action})
  318. @standards{SVID, search.h}
  319. @safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
  320. @c hsearch @mtasurace:hsearch @acucorrupt/action==ENTER
  321. @c hsearch_r dup @mtsrace:htab @acucorrupt/action==ENTER
  322. To search in a hashing table created using @code{hcreate} the
  323. @code{hsearch} function must be used. This function can perform a simple
  324. search for an element (if @var{action} has the value @code{FIND}) or it can
  325. alternatively insert the key element into the hashing table. Entries
  326. are never replaced.
  327. The key is denoted by a pointer to an object of type @code{ENTRY}. For
  328. locating the corresponding position in the hashing table only the
  329. @code{key} element of the structure is used.
  330. If an entry with a matching key is found the @var{action} parameter is
  331. irrelevant. The found entry is returned. If no matching entry is found
  332. and the @var{action} parameter has the value @code{FIND} the function
  333. returns a @code{NULL} pointer. If no entry is found and the
  334. @var{action} parameter has the value @code{ENTER} a new entry is added
  335. to the hashing table which is initialized with the parameter @var{item}.
  336. A pointer to the newly added entry is returned.
  337. @end deftypefun
  338. As mentioned before, the hashing table used by the functions described so
  339. far is global and there can be at any time at most one hashing table in
  340. the program. A solution is to use the following functions which are a
  341. GNU extension. All have in common that they operate on a hashing table
  342. which is described by the content of an object of the type @code{struct
  343. hsearch_data}. This type should be treated as opaque, none of its
  344. members should be changed directly.
  345. @deftypefun int hcreate_r (size_t @var{nel}, struct hsearch_data *@var{htab})
  346. @standards{GNU, search.h}
  347. @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  348. @c Unlike the lsearch array, the htab is (at least in part) opaque, so
  349. @c let's make it absolutely clear that ensuring exclusive access is a
  350. @c caller responsibility.
  351. @c Cancellation is unlikely to leave the htab in a corrupt state: the
  352. @c last field to be initialized is the one that tells whether the entire
  353. @c data structure was initialized, and there's a function call (calloc)
  354. @c in between that will often ensure all other fields are written before
  355. @c the table. However, should this call be inlined (say with LTO), this
  356. @c assumption may not hold. The calloc call doesn't cross our library
  357. @c interface barrier, so let's consider this could happen and mark this
  358. @c with @acucorrupt. It's no safety loss, since we already have
  359. @c @ascuheap anyway...
  360. @c hcreate_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
  361. @c isprime ok
  362. @c calloc dup @ascuheap @acsmem
  363. The @code{hcreate_r} function initializes the object pointed to by
  364. @var{htab} to contain a hashing table with at least @var{nel} elements.
  365. So this function is equivalent to the @code{hcreate} function except
  366. that the initialized data structure is controlled by the user.
  367. This allows having more than one hashing table at one time. The memory
  368. necessary for the @code{struct hsearch_data} object can be allocated
  369. dynamically. It must be initialized with zero before calling this
  370. function.
  371. The return value is non-zero if the operation was successful. If the
  372. return value is zero, something went wrong, which probably means the
  373. program ran out of memory.
  374. @end deftypefun
  375. @deftypefun void hdestroy_r (struct hsearch_data *@var{htab})
  376. @standards{GNU, search.h}
  377. @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  378. @c The table is released while the table pointer still points to it.
  379. @c Async cancellation is thus unsafe, but it already was because we call
  380. @c free(). Using the table in a handler while it's being released would
  381. @c also be dangerous, but calling free() already makes it unsafe, and
  382. @c the requirement on the caller to ensure exclusive access already
  383. @c guarantees this doesn't happen, so we don't get @asucorrupt.
  384. @c hdestroy_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
  385. @c free dup @ascuheap @acsmem
  386. The @code{hdestroy_r} function frees all resources allocated by the
  387. @code{hcreate_r} function for this very same object @var{htab}. As for
  388. @code{hdestroy} it is the program's responsibility to free the strings
  389. for the elements of the table.
  390. @end deftypefun
  391. @deftypefun int hsearch_r (ENTRY @var{item}, ACTION @var{action}, ENTRY **@var{retval}, struct hsearch_data *@var{htab})
  392. @standards{GNU, search.h}
  393. @safety{@prelim{}@mtsafe{@mtsrace{:htab}}@assafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
  394. @c Callers have to ensure mutual exclusion; insertion, if cancelled,
  395. @c leaves the table in a corrupt state.
  396. @c hsearch_r @mtsrace:htab @acucorrupt/action==ENTER
  397. @c strlen dup ok
  398. @c strcmp dup ok
  399. The @code{hsearch_r} function is equivalent to @code{hsearch}. The
  400. meaning of the first two arguments is identical. But instead of
  401. operating on a single global hashing table the function works on the
  402. table described by the object pointed to by @var{htab} (which is
  403. initialized by a call to @code{hcreate_r}).
  404. Another difference to @code{hcreate} is that the pointer to the found
  405. entry in the table is not the return value of the function. It is
  406. returned by storing it in a pointer variable pointed to by the
  407. @var{retval} parameter. The return value of the function is an integer
  408. value indicating success if it is non-zero and failure if it is zero.
  409. In the latter case the global variable @code{errno} signals the reason for
  410. the failure.
  411. @table @code
  412. @item ENOMEM
  413. The table is filled and @code{hsearch_r} was called with a so far
  414. unknown key and @var{action} set to @code{ENTER}.
  415. @item ESRCH
  416. The @var{action} parameter is @code{FIND} and no corresponding element
  417. is found in the table.
  418. @end table
  419. @end deftypefun
  420. @node Tree Search Function
  421. @section The @code{tsearch} function.
  422. Another common form to organize data for efficient search is to use
  423. trees. The @code{tsearch} function family provides a nice interface to
  424. functions to organize possibly large amounts of data by providing a mean
  425. access time proportional to the logarithm of the number of elements.
  426. @Theglibc{} implementation even guarantees that this bound is
  427. never exceeded even for input data which cause problems for simple
  428. binary tree implementations.
  429. The functions described in the chapter are all described in the @w{System
  430. V} and X/Open specifications and are therefore quite portable.
  431. In contrast to the @code{hsearch} functions the @code{tsearch} functions
  432. can be used with arbitrary data and not only zero-terminated strings.
  433. The @code{tsearch} functions have the advantage that no function to
  434. initialize data structures is necessary. A simple pointer of type
  435. @code{void *} initialized to @code{NULL} is a valid tree and can be
  436. extended or searched. The prototypes for these functions can be found
  437. in the header file @file{search.h}.
  438. @deftypefun {void *} tsearch (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
  439. @standards{SVID, search.h}
  440. @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  441. @c The tree is not modified in a thread-safe manner, and rotations may
  442. @c leave the tree in an inconsistent state that could be observed in an
  443. @c asynchronous signal handler (except for the caller-synchronization
  444. @c requirement) or after asynchronous cancellation of the thread
  445. @c performing the rotation or the insertion.
  446. The @code{tsearch} function searches in the tree pointed to by
  447. @code{*@var{rootp}} for an element matching @var{key}. The function
  448. pointed to by @var{compar} is used to determine whether two elements
  449. match. @xref{Comparison Functions}, for a specification of the functions
  450. which can be used for the @var{compar} parameter.
  451. If the tree does not contain a matching entry the @var{key} value will
  452. be added to the tree. @code{tsearch} does not make a copy of the object
  453. pointed to by @var{key} (how could it since the size is unknown).
  454. Instead it adds a reference to this object which means the object must
  455. be available as long as the tree data structure is used.
  456. The tree is represented by a pointer to a pointer since it is sometimes
  457. necessary to change the root node of the tree. So it must not be
  458. assumed that the variable pointed to by @var{rootp} has the same value
  459. after the call. This also shows that it is not safe to call the
  460. @code{tsearch} function more than once at the same time using the same
  461. tree. It is no problem to run it more than once at a time on different
  462. trees.
  463. The return value is a pointer to the matching element in the tree. If a
  464. new element was created the pointer points to the new data (which is in
  465. fact @var{key}). If an entry had to be created and the program ran out
  466. of space @code{NULL} is returned.
  467. @end deftypefun
  468. @deftypefun {void *} tfind (const void *@var{key}, void *const *@var{rootp}, comparison_fn_t @var{compar})
  469. @standards{SVID, search.h}
  470. @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@assafe{}@acsafe{}}
  471. The @code{tfind} function is similar to the @code{tsearch} function. It
  472. locates an element matching the one pointed to by @var{key} and returns
  473. a pointer to this element. But if no matching element is available no
  474. new element is entered (note that the @var{rootp} parameter points to a
  475. constant pointer). Instead the function returns @code{NULL}.
  476. @end deftypefun
  477. Another advantage of the @code{tsearch} functions in contrast to the
  478. @code{hsearch} functions is that there is an easy way to remove
  479. elements.
  480. @deftypefun {void *} tdelete (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
  481. @standards{SVID, search.h}
  482. @safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  483. To remove a specific element matching @var{key} from the tree
  484. @code{tdelete} can be used. It locates the matching element using the
  485. same method as @code{tfind}. The corresponding element is then removed
  486. and a pointer to the parent of the deleted node is returned by the
  487. function. If there is no matching entry in the tree nothing can be
  488. deleted and the function returns @code{NULL}. If the root of the tree
  489. is deleted @code{tdelete} returns some unspecified value not equal to
  490. @code{NULL}.
  491. @end deftypefun
  492. @deftypefun void tdestroy (void *@var{vroot}, __free_fn_t @var{freefct})
  493. @standards{GNU, search.h}
  494. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  495. If the complete search tree has to be removed one can use
  496. @code{tdestroy}. It frees all resources allocated by the @code{tsearch}
  497. functions to generate the tree pointed to by @var{vroot}.
  498. For the data in each tree node the function @var{freefct} is called.
  499. The pointer to the data is passed as the argument to the function. If
  500. no such work is necessary @var{freefct} must point to a function doing
  501. nothing. It is called in any case.
  502. This function is a GNU extension and not covered by the @w{System V} or
  503. X/Open specifications.
  504. @end deftypefun
  505. In addition to the functions to create and destroy the tree data
  506. structure, there is another function which allows you to apply a
  507. function to all elements of the tree. The function must have this type:
  508. @smallexample
  509. void __action_fn_t (const void *nodep, VISIT value, int level);
  510. @end smallexample
  511. The @var{nodep} is the data value of the current node (once given as the
  512. @var{key} argument to @code{tsearch}). @var{level} is a numeric value
  513. which corresponds to the depth of the current node in the tree. The
  514. root node has the depth @math{0} and its children have a depth of
  515. @math{1} and so on. The @code{VISIT} type is an enumeration type.
  516. @deftp {Data Type} VISIT
  517. The @code{VISIT} value indicates the status of the current node in the
  518. tree and how the function is called. The status of a node is either
  519. `leaf' or `internal node'. For each leaf node the function is called
  520. exactly once, for each internal node it is called three times: before
  521. the first child is processed, after the first child is processed and
  522. after both children are processed. This makes it possible to handle all
  523. three methods of tree traversal (or even a combination of them).
  524. @vtable @code
  525. @item preorder
  526. The current node is an internal node and the function is called before
  527. the first child was processed.
  528. @item postorder
  529. The current node is an internal node and the function is called after
  530. the first child was processed.
  531. @item endorder
  532. The current node is an internal node and the function is called after
  533. the second child was processed.
  534. @item leaf
  535. The current node is a leaf.
  536. @end vtable
  537. @end deftp
  538. @deftypefun void twalk (const void *@var{root}, __action_fn_t @var{action})
  539. @standards{SVID, search.h}
  540. @safety{@prelim{}@mtsafe{@mtsrace{:root}}@assafe{}@acsafe{}}
  541. For each node in the tree with a node pointed to by @var{root}, the
  542. @code{twalk} function calls the function provided by the parameter
  543. @var{action}. For leaf nodes the function is called exactly once with
  544. @var{value} set to @code{leaf}. For internal nodes the function is
  545. called three times, setting the @var{value} parameter or @var{action} to
  546. the appropriate value. The @var{level} argument for the @var{action}
  547. function is computed while descending the tree by increasing the value
  548. by one for each descent to a child, starting with the value @math{0} for
  549. the root node.
  550. Since the functions used for the @var{action} parameter to @code{twalk}
  551. must not modify the tree data, it is safe to run @code{twalk} in more
  552. than one thread at the same time, working on the same tree. It is also
  553. safe to call @code{tfind} in parallel. Functions which modify the tree
  554. must not be used, otherwise the behavior is undefined. However, it is
  555. difficult to pass data external to the tree to the callback function
  556. without resorting to global variables (and thread safety issues), so
  557. see the @code{twalk_r} function below.
  558. @end deftypefun
  559. @deftypefun void twalk_r (const void *@var{root}, void (*@var{action}) (const void *@var{key}, VISIT @var{which}, void *@var{closure}), void *@var{closure})
  560. @standards{GNU, search.h}
  561. @safety{@prelim{}@mtsafe{@mtsrace{:root}}@assafe{}@acsafe{}}
  562. For each node in the tree with a node pointed to by @var{root}, the
  563. @code{twalk_r} function calls the function provided by the parameter
  564. @var{action}. For leaf nodes the function is called exactly once with
  565. @var{which} set to @code{leaf}. For internal nodes the function is
  566. called three times, setting the @var{which} parameter of @var{action} to
  567. the appropriate value. The @var{closure} parameter is passed down to
  568. each call of the @var{action} function, unmodified.
  569. It is possible to implement the @code{twalk} function on top of the
  570. @code{twalk_r} function, which is why there is no separate level
  571. parameter.
  572. @smallexample
  573. @include twalk.c.texi
  574. @end smallexample
  575. @end deftypefun