Blame manual/search.texi

Packit Service 82fcde
@node Searching and Sorting, Pattern Matching, Message Translation, Top
Packit Service 82fcde
@c %MENU% General searching and sorting functions
Packit Service 82fcde
@chapter Searching and Sorting
Packit Service 82fcde
Packit Service 82fcde
This chapter describes functions for searching and sorting arrays of
Packit Service 82fcde
arbitrary objects.  You pass the appropriate comparison function to be
Packit Service 82fcde
applied as an argument, along with the size of the objects in the array
Packit Service 82fcde
and the total number of elements.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Comparison Functions::        Defining how to compare two objects.
Packit Service 82fcde
				 Since the sort and search facilities
Packit Service 82fcde
                                 are general, you have to specify the
Packit Service 82fcde
                                 ordering.
Packit Service 82fcde
* Array Search Function::       The @code{bsearch} function.
Packit Service 82fcde
* Array Sort Function::         The @code{qsort} function.
Packit Service 82fcde
* Search/Sort Example::         An example program.
Packit Service 82fcde
* Hash Search Function::        The @code{hsearch} function.
Packit Service 82fcde
* Tree Search Function::        The @code{tsearch} function.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Comparison Functions
Packit Service 82fcde
@section Defining the Comparison Function
Packit Service 82fcde
@cindex Comparison Function
Packit Service 82fcde
Packit Service 82fcde
In order to use the sorted array library functions, you have to describe
Packit Service 82fcde
how to compare the elements of the array.
Packit Service 82fcde
Packit Service 82fcde
To do this, you supply a comparison function to compare two elements of
Packit Service 82fcde
the array.  The library will call this function, passing as arguments
Packit Service 82fcde
pointers to two array elements to be compared.  Your comparison function
Packit Service 82fcde
should return a value the way @code{strcmp} (@pxref{String/Array
Packit Service 82fcde
Comparison}) does: negative if the first argument is ``less'' than the
Packit Service 82fcde
second, zero if they are ``equal'', and positive if the first argument
Packit Service 82fcde
is ``greater''.
Packit Service 82fcde
Packit Service 82fcde
Here is an example of a comparison function which works with an array of
Packit Service 82fcde
numbers of type @code{double}:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int
Packit Service 82fcde
compare_doubles (const void *a, const void *b)
Packit Service 82fcde
@{
Packit Service 82fcde
  const double *da = (const double *) a;
Packit Service 82fcde
  const double *db = (const double *) b;
Packit Service 82fcde
Packit Service 82fcde
  return (*da > *db) - (*da < *db);
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The header file @file{stdlib.h} defines a name for the data type of
Packit Service 82fcde
comparison functions.  This type is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@comment stdlib.h
Packit Service 82fcde
@comment GNU
Packit Service 82fcde
@tindex comparison_fn_t
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int comparison_fn_t (const void *, const void *);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@node Array Search Function
Packit Service 82fcde
@section Array Search Function
Packit Service 82fcde
@cindex search function (for arrays)
Packit Service 82fcde
@cindex binary search function (for arrays)
Packit Service 82fcde
@cindex array search function
Packit Service 82fcde
Packit Service 82fcde
Generally searching for a specific element in an array means that
Packit Service 82fcde
potentially all elements must be checked.  @Theglibc{} contains
Packit Service 82fcde
functions to perform linear search.  The prototypes for the following
Packit Service 82fcde
two functions can be found in @file{search.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} lfind (const void *@var{key}, const void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{lfind} function searches in the array with @code{*@var{nmemb}}
Packit Service 82fcde
elements of @var{size} bytes pointed to by @var{base} for an element
Packit Service 82fcde
which matches the one pointed to by @var{key}.  The function pointed to
Packit Service 82fcde
by @var{compar} is used to decide whether two elements match.
Packit Service 82fcde
Packit Service 82fcde
The return value is a pointer to the matching element in the array
Packit Service 82fcde
starting at @var{base} if it is found.  If no matching element is
Packit Service 82fcde
available @code{NULL} is returned.
Packit Service 82fcde
Packit Service 82fcde
The mean runtime of this function is @code{*@var{nmemb}}/2.  This
Packit Service 82fcde
function should only be used if elements often get added to or deleted from
Packit Service 82fcde
the array in which case it might not be useful to sort the array before
Packit Service 82fcde
searching.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} lsearch (const void *@var{key}, void *@var{base}, size_t *@var{nmemb}, size_t @var{size}, comparison_fn_t @var{compar})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c A signal handler that interrupted an insertion and performed an
Packit Service 82fcde
@c insertion itself would leave the array in a corrupt state (e.g. one
Packit Service 82fcde
@c new element initialized twice, with parts of both initializations
Packit Service 82fcde
@c prevailing, and another uninitialized element), but this is just a
Packit Service 82fcde
@c special case of races on user-controlled objects, that have to be
Packit Service 82fcde
@c avoided by users.
Packit Service 82fcde
Packit Service 82fcde
@c In case of cancellation, we know the array won't be left in a corrupt
Packit Service 82fcde
@c state; the new element is initialized before the element count is
Packit Service 82fcde
@c incremented, and the compiler can't reorder these operations because
Packit Service 82fcde
@c it can't know that they don't alias.  So, we'll either cancel after
Packit Service 82fcde
@c the increment and the initialization are both complete, or the
Packit Service 82fcde
@c increment won't have taken place, and so how far the initialization
Packit Service 82fcde
@c got doesn't matter.
Packit Service 82fcde
The @code{lsearch} function is similar to the @code{lfind} function.  It
Packit Service 82fcde
searches the given array for an element and returns it if found.  The
Packit Service 82fcde
difference is that if no matching element is found the @code{lsearch}
Packit Service 82fcde
function adds the object pointed to by @var{key} (with a size of
Packit Service 82fcde
@var{size} bytes) at the end of the array and it increments the value of
Packit Service 82fcde
@code{*@var{nmemb}} to reflect this addition.
Packit Service 82fcde
Packit Service 82fcde
This means for the caller that if it is not sure that the array contains
Packit Service 82fcde
the element one is searching for the memory allocated for the array
Packit Service 82fcde
starting at @var{base} must have room for at least @var{size} more
Packit Service 82fcde
bytes.  If one is sure the element is in the array it is better to use
Packit Service 82fcde
@code{lfind} so having more room in the array is always necessary when
Packit Service 82fcde
calling @code{lsearch}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
To search a sorted array for an element matching the key, use the
Packit Service 82fcde
@code{bsearch} function.  The prototype for this function is in
Packit Service 82fcde
the header file @file{stdlib.h}.
Packit Service 82fcde
@pindex stdlib.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} bsearch (const void *@var{key}, const void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{bsearch} function searches the sorted array @var{array} for an object
Packit Service 82fcde
that is equivalent to @var{key}.  The array contains @var{count} elements,
Packit Service 82fcde
each of which is of size @var{size} bytes.
Packit Service 82fcde
Packit Service 82fcde
The @var{compare} function is used to perform the comparison.  This
Packit Service 82fcde
function is called with two pointer arguments and should return an
Packit Service 82fcde
integer less than, equal to, or greater than zero corresponding to
Packit Service 82fcde
whether its first argument is considered less than, equal to, or greater
Packit Service 82fcde
than its second argument.  The elements of the @var{array} must already
Packit Service 82fcde
be sorted in ascending order according to this comparison function.
Packit Service 82fcde
Packit Service 82fcde
The return value is a pointer to the matching array element, or a null
Packit Service 82fcde
pointer if no match is found.  If the array contains more than one element
Packit Service 82fcde
that matches, the one that is returned is unspecified.
Packit Service 82fcde
Packit Service 82fcde
This function derives its name from the fact that it is implemented
Packit Service 82fcde
using the binary search algorithm.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Array Sort Function
Packit Service 82fcde
@section Array Sort Function
Packit Service 82fcde
@cindex sort function (for arrays)
Packit Service 82fcde
@cindex quick sort function (for arrays)
Packit Service 82fcde
@cindex array sort function
Packit Service 82fcde
Packit Service 82fcde
To sort an array using an arbitrary comparison function, use the
Packit Service 82fcde
@code{qsort} function.  The prototype for this function is in
Packit Service 82fcde
@file{stdlib.h}.
Packit Service 82fcde
@pindex stdlib.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void qsort (void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare})
Packit Service 82fcde
@standards{ISO, stdlib.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}}
Packit Service 82fcde
The @code{qsort} function sorts the array @var{array}.  The array
Packit Service 82fcde
contains @var{count} elements, each of which is of size @var{size}.
Packit Service 82fcde
Packit Service 82fcde
The @var{compare} function is used to perform the comparison on the
Packit Service 82fcde
array elements.  This function is called with two pointer arguments and
Packit Service 82fcde
should return an integer less than, equal to, or greater than zero
Packit Service 82fcde
corresponding to whether its first argument is considered less than,
Packit Service 82fcde
equal to, or greater than its second argument.
Packit Service 82fcde
Packit Service 82fcde
@cindex stable sorting
Packit Service 82fcde
@strong{Warning:} If two objects compare as equal, their order after
Packit Service 82fcde
sorting is unpredictable.  That is to say, the sorting is not stable.
Packit Service 82fcde
This can make a difference when the comparison considers only part of
Packit Service 82fcde
the elements.  Two elements with the same sort key may differ in other
Packit Service 82fcde
respects.
Packit Service 82fcde
Packit Service 82fcde
Although the object addresses passed to the comparison function lie
Packit Service 82fcde
within the array, they need not correspond with the original locations
Packit Service 82fcde
of those objects because the sorting algorithm may swap around objects
Packit Service 82fcde
in the array before making some comparisons.  The only way to perform
Packit Service 82fcde
a stable sort with @code{qsort} is to first augment the objects with a
Packit Service 82fcde
monotonic counter of some kind.
Packit Service 82fcde
Packit Service 82fcde
Here is a simple example of sorting an array of doubles in numerical
Packit Service 82fcde
order, using the comparison function defined above (@pxref{Comparison
Packit Service 82fcde
Functions}):
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@{
Packit Service 82fcde
  double *array;
Packit Service 82fcde
  int size;
Packit Service 82fcde
  @dots{}
Packit Service 82fcde
  qsort (array, size, sizeof (double), compare_doubles);
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The @code{qsort} function derives its name from the fact that it was
Packit Service 82fcde
originally implemented using the ``quick sort'' algorithm.
Packit Service 82fcde
Packit Service 82fcde
The implementation of @code{qsort} in this library might not be an
Packit Service 82fcde
in-place sort and might thereby use an extra amount of memory to store
Packit Service 82fcde
the array.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Search/Sort Example
Packit Service 82fcde
@section Searching and Sorting Example
Packit Service 82fcde
Packit Service 82fcde
Here is an example showing the use of @code{qsort} and @code{bsearch}
Packit Service 82fcde
with an array of structures.  The objects in the array are sorted
Packit Service 82fcde
by comparing their @code{name} fields with the @code{strcmp} function.
Packit Service 82fcde
Then, we can look up individual objects based on their names.
Packit Service 82fcde
Packit Service 82fcde
@comment This example is dedicated to the memory of Jim Henson.  RIP.
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include search.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@cindex Kermit the frog
Packit Service 82fcde
The output from this program looks like:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
Kermit, the frog
Packit Service 82fcde
Piggy, the pig
Packit Service 82fcde
Gonzo, the whatever
Packit Service 82fcde
Fozzie, the bear
Packit Service 82fcde
Sam, the eagle
Packit Service 82fcde
Robin, the frog
Packit Service 82fcde
Animal, the animal
Packit Service 82fcde
Camilla, the chicken
Packit Service 82fcde
Sweetums, the monster
Packit Service 82fcde
Dr. Strangepork, the pig
Packit Service 82fcde
Link Hogthrob, the pig
Packit Service 82fcde
Zoot, the human
Packit Service 82fcde
Dr. Bunsen Honeydew, the human
Packit Service 82fcde
Beaker, the human
Packit Service 82fcde
Swedish Chef, the human
Packit Service 82fcde
Packit Service 82fcde
Animal, the animal
Packit Service 82fcde
Beaker, the human
Packit Service 82fcde
Camilla, the chicken
Packit Service 82fcde
Dr. Bunsen Honeydew, the human
Packit Service 82fcde
Dr. Strangepork, the pig
Packit Service 82fcde
Fozzie, the bear
Packit Service 82fcde
Gonzo, the whatever
Packit Service 82fcde
Kermit, the frog
Packit Service 82fcde
Link Hogthrob, the pig
Packit Service 82fcde
Piggy, the pig
Packit Service 82fcde
Robin, the frog
Packit Service 82fcde
Sam, the eagle
Packit Service 82fcde
Swedish Chef, the human
Packit Service 82fcde
Sweetums, the monster
Packit Service 82fcde
Zoot, the human
Packit Service 82fcde
Packit Service 82fcde
Kermit, the frog
Packit Service 82fcde
Gonzo, the whatever
Packit Service 82fcde
Couldn't find Janice.
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Hash Search Function
Packit Service 82fcde
@section The @code{hsearch} function.
Packit Service 82fcde
Packit Service 82fcde
The functions mentioned so far in this chapter are for searching in a sorted
Packit Service 82fcde
or unsorted array.  There are other methods to organize information
Packit Service 82fcde
which later should be searched.  The costs of insert, delete and search
Packit Service 82fcde
differ.  One possible implementation is using hashing tables.
Packit Service 82fcde
The following functions are declared in the header file @file{search.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int hcreate (size_t @var{nel})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
@c hcreate @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c  hcreate_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
The @code{hcreate} function creates a hashing table which can contain at
Packit Service 82fcde
least @var{nel} elements.  There is no possibility to grow this table so
Packit Service 82fcde
it is necessary to choose the value for @var{nel} wisely.  The method
Packit Service 82fcde
used to implement this function might make it necessary to make the
Packit Service 82fcde
number of elements in the hashing table larger than the expected maximal
Packit Service 82fcde
number of elements.  Hashing tables usually work inefficiently if they are
Packit Service 82fcde
filled 80% or more.  The constant access time guaranteed by hashing can
Packit Service 82fcde
only be achieved if few collisions exist.  See Knuth's ``The Art of
Packit Service 82fcde
Computer Programming, Part 3: Searching and Sorting'' for more
Packit Service 82fcde
information.
Packit Service 82fcde
Packit Service 82fcde
The weakest aspect of this function is that there can be at most one
Packit Service 82fcde
hashing table used through the whole program.  The table is allocated
Packit Service 82fcde
in local memory out of control of the programmer.  As an extension @theglibc{}
Packit Service 82fcde
provides an additional set of functions with a reentrant
Packit Service 82fcde
interface which provides a similar interface but which allows keeping
Packit Service 82fcde
arbitrarily many hashing tables.
Packit Service 82fcde
Packit Service 82fcde
It is possible to use more than one hashing table in the program run if
Packit Service 82fcde
the former table is first destroyed by a call to @code{hdestroy}.
Packit Service 82fcde
Packit Service 82fcde
The function returns a non-zero value if successful.  If it returns zero,
Packit Service 82fcde
something went wrong.  This could either mean there is already a hashing
Packit Service 82fcde
table in use or the program ran out of memory.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void hdestroy (void)
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
@c hdestroy @mtasurace:hsearch @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c  hdestroy_r dup @mtsrace:htab @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
The @code{hdestroy} function can be used to free all the resources
Packit Service 82fcde
allocated in a previous call of @code{hcreate}.  After a call to this
Packit Service 82fcde
function it is again possible to call @code{hcreate} and allocate a new
Packit Service 82fcde
table with possibly different size.
Packit Service 82fcde
Packit Service 82fcde
It is important to remember that the elements contained in the hashing
Packit Service 82fcde
table at the time @code{hdestroy} is called are @emph{not} freed by this
Packit Service 82fcde
function.  It is the responsibility of the program code to free those
Packit Service 82fcde
strings (if necessary at all).  Freeing all the element memory is not
Packit Service 82fcde
possible without extra, separately kept information since there is no
Packit Service 82fcde
function to iterate through all available elements in the hashing table.
Packit Service 82fcde
If it is really necessary to free a table and all elements the
Packit Service 82fcde
programmer has to keep a list of all table elements and before calling
Packit Service 82fcde
@code{hdestroy} s/he has to free all element's data using this list.
Packit Service 82fcde
This is a very unpleasant mechanism and it also shows that this kind of
Packit Service 82fcde
hashing table is mainly meant for tables which are created once and
Packit Service 82fcde
used until the end of the program run.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Entries of the hashing table and keys for the search are defined using
Packit Service 82fcde
this type:
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data type} {struct ENTRY}
Packit Service 82fcde
Both elements of this structure are pointers to zero-terminated strings.
Packit Service 82fcde
This is a limiting restriction of the functionality of the
Packit Service 82fcde
@code{hsearch} functions.  They can only be used for data sets which use
Packit Service 82fcde
the NUL character always and solely to terminate the records.  It is not
Packit Service 82fcde
possible to handle general binary data.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item char *key
Packit Service 82fcde
Pointer to a zero-terminated string of characters describing the key for
Packit Service 82fcde
the search or the element in the hashing table.
Packit Service 82fcde
@item char *data
Packit Service 82fcde
Pointer to a zero-terminated string of characters describing the data.
Packit Service 82fcde
If the functions will be called only for searching an existing entry
Packit Service 82fcde
this element might stay undefined since it is not used.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {ENTRY *} hsearch (ENTRY @var{item}, ACTION @var{action})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:hsearch}}@asunsafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
Packit Service 82fcde
@c hsearch @mtasurace:hsearch @acucorrupt/action==ENTER
Packit Service 82fcde
@c  hsearch_r dup @mtsrace:htab @acucorrupt/action==ENTER
Packit Service 82fcde
To search in a hashing table created using @code{hcreate} the
Packit Service 82fcde
@code{hsearch} function must be used.  This function can perform a simple
Packit Service 82fcde
search for an element (if @var{action} has the value @code{FIND}) or it can
Packit Service 82fcde
alternatively insert the key element into the hashing table.  Entries
Packit Service 82fcde
are never replaced.
Packit Service 82fcde
Packit Service 82fcde
The key is denoted by a pointer to an object of type @code{ENTRY}.  For
Packit Service 82fcde
locating the corresponding position in the hashing table only the
Packit Service 82fcde
@code{key} element of the structure is used.
Packit Service 82fcde
Packit Service 82fcde
If an entry with a matching key is found the @var{action} parameter is
Packit Service 82fcde
irrelevant.  The found entry is returned.  If no matching entry is found
Packit Service 82fcde
and the @var{action} parameter has the value @code{FIND} the function
Packit Service 82fcde
returns a @code{NULL} pointer.  If no entry is found and the
Packit Service 82fcde
@var{action} parameter has the value @code{ENTER} a new entry is added
Packit Service 82fcde
to the hashing table which is initialized with the parameter @var{item}.
Packit Service 82fcde
A pointer to the newly added entry is returned.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
As mentioned before, the hashing table used by the functions described so
Packit Service 82fcde
far is global and there can be at any time at most one hashing table in
Packit Service 82fcde
the program.  A solution is to use the following functions which are a
Packit Service 82fcde
GNU extension.  All have in common that they operate on a hashing table
Packit Service 82fcde
which is described by the content of an object of the type @code{struct
Packit Service 82fcde
hsearch_data}.  This type should be treated as opaque, none of its
Packit Service 82fcde
members should be changed directly.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int hcreate_r (size_t @var{nel}, struct hsearch_data *@var{htab})
Packit Service 82fcde
@standards{GNU, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
@c Unlike the lsearch array, the htab is (at least in part) opaque, so
Packit Service 82fcde
@c let's make it absolutely clear that ensuring exclusive access is a
Packit Service 82fcde
@c caller responsibility.
Packit Service 82fcde
Packit Service 82fcde
@c Cancellation is unlikely to leave the htab in a corrupt state: the
Packit Service 82fcde
@c last field to be initialized is the one that tells whether the entire
Packit Service 82fcde
@c data structure was initialized, and there's a function call (calloc)
Packit Service 82fcde
@c in between that will often ensure all other fields are written before
Packit Service 82fcde
@c the table.  However, should this call be inlined (say with LTO), this
Packit Service 82fcde
@c assumption may not hold.  The calloc call doesn't cross our library
Packit Service 82fcde
@c interface barrier, so let's consider this could happen and mark this
Packit Service 82fcde
@c with @acucorrupt.  It's no safety loss, since we already have
Packit Service 82fcde
@c @ascuheap anyway...
Packit Service 82fcde
Packit Service 82fcde
@c hcreate_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c  isprime ok
Packit Service 82fcde
@c  calloc dup @ascuheap @acsmem
Packit Service 82fcde
The @code{hcreate_r} function initializes the object pointed to by
Packit Service 82fcde
@var{htab} to contain a hashing table with at least @var{nel} elements.
Packit Service 82fcde
So this function is equivalent to the @code{hcreate} function except
Packit Service 82fcde
that the initialized data structure is controlled by the user.
Packit Service 82fcde
Packit Service 82fcde
This allows having more than one hashing table at one time.  The memory
Packit Service 82fcde
necessary for the @code{struct hsearch_data} object can be allocated
Packit Service 82fcde
dynamically.  It must be initialized with zero before calling this
Packit Service 82fcde
function.
Packit Service 82fcde
Packit Service 82fcde
The return value is non-zero if the operation was successful.  If the
Packit Service 82fcde
return value is zero, something went wrong, which probably means the
Packit Service 82fcde
program ran out of memory.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void hdestroy_r (struct hsearch_data *@var{htab})
Packit Service 82fcde
@standards{GNU, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:htab}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
@c The table is released while the table pointer still points to it.
Packit Service 82fcde
@c Async cancellation is thus unsafe, but it already was because we call
Packit Service 82fcde
@c free().  Using the table in a handler while it's being released would
Packit Service 82fcde
@c also be dangerous, but calling free() already makes it unsafe, and
Packit Service 82fcde
@c the requirement on the caller to ensure exclusive access already
Packit Service 82fcde
@c guarantees this doesn't happen, so we don't get @asucorrupt.
Packit Service 82fcde
Packit Service 82fcde
@c hdestroy_r @mtsrace:htab @ascuheap @acucorrupt @acsmem
Packit Service 82fcde
@c  free dup @ascuheap @acsmem
Packit Service 82fcde
The @code{hdestroy_r} function frees all resources allocated by the
Packit Service 82fcde
@code{hcreate_r} function for this very same object @var{htab}.  As for
Packit Service 82fcde
@code{hdestroy} it is the program's responsibility to free the strings
Packit Service 82fcde
for the elements of the table.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int hsearch_r (ENTRY @var{item}, ACTION @var{action}, ENTRY **@var{retval}, struct hsearch_data *@var{htab})
Packit Service 82fcde
@standards{GNU, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:htab}}@assafe{}@acunsafe{@acucorrupt{/action==ENTER}}}
Packit Service 82fcde
@c Callers have to ensure mutual exclusion; insertion, if cancelled,
Packit Service 82fcde
@c leaves the table in a corrupt state.
Packit Service 82fcde
Packit Service 82fcde
@c hsearch_r @mtsrace:htab @acucorrupt/action==ENTER
Packit Service 82fcde
@c  strlen dup ok
Packit Service 82fcde
@c  strcmp dup ok
Packit Service 82fcde
The @code{hsearch_r} function is equivalent to @code{hsearch}.  The
Packit Service 82fcde
meaning of the first two arguments is identical.  But instead of
Packit Service 82fcde
operating on a single global hashing table the function works on the
Packit Service 82fcde
table described by the object pointed to by @var{htab} (which is
Packit Service 82fcde
initialized by a call to @code{hcreate_r}).
Packit Service 82fcde
Packit Service 82fcde
Another difference to @code{hcreate} is that the pointer to the found
Packit Service 82fcde
entry in the table is not the return value of the function.  It is
Packit Service 82fcde
returned by storing it in a pointer variable pointed to by the
Packit Service 82fcde
@var{retval} parameter.  The return value of the function is an integer
Packit Service 82fcde
value indicating success if it is non-zero and failure if it is zero.
Packit Service 82fcde
In the latter case the global variable @var{errno} signals the reason for
Packit Service 82fcde
the failure.
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item ENOMEM
Packit Service 82fcde
The table is filled and @code{hsearch_r} was called with a so far
Packit Service 82fcde
unknown key and @var{action} set to @code{ENTER}.
Packit Service 82fcde
@item ESRCH
Packit Service 82fcde
The @var{action} parameter is @code{FIND} and no corresponding element
Packit Service 82fcde
is found in the table.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Tree Search Function
Packit Service 82fcde
@section The @code{tsearch} function.
Packit Service 82fcde
Packit Service 82fcde
Another common form to organize data for efficient search is to use
Packit Service 82fcde
trees.  The @code{tsearch} function family provides a nice interface to
Packit Service 82fcde
functions to organize possibly large amounts of data by providing a mean
Packit Service 82fcde
access time proportional to the logarithm of the number of elements.
Packit Service 82fcde
@Theglibc{} implementation even guarantees that this bound is
Packit Service 82fcde
never exceeded even for input data which cause problems for simple
Packit Service 82fcde
binary tree implementations.
Packit Service 82fcde
Packit Service 82fcde
The functions described in the chapter are all described in the @w{System
Packit Service 82fcde
V} and X/Open specifications and are therefore quite portable.
Packit Service 82fcde
Packit Service 82fcde
In contrast to the @code{hsearch} functions the @code{tsearch} functions
Packit Service 82fcde
can be used with arbitrary data and not only zero-terminated strings.
Packit Service 82fcde
Packit Service 82fcde
The @code{tsearch} functions have the advantage that no function to
Packit Service 82fcde
initialize data structures is necessary.  A simple pointer of type
Packit Service 82fcde
@code{void *} initialized to @code{NULL} is a valid tree and can be
Packit Service 82fcde
extended or searched.  The prototypes for these functions can be found
Packit Service 82fcde
in the header file @file{search.h}.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} tsearch (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
@c The tree is not modified in a thread-safe manner, and rotations may
Packit Service 82fcde
@c leave the tree in an inconsistent state that could be observed in an
Packit Service 82fcde
@c asynchronous signal handler (except for the caller-synchronization
Packit Service 82fcde
@c requirement) or after asynchronous cancellation of the thread
Packit Service 82fcde
@c performing the rotation or the insertion.
Packit Service 82fcde
The @code{tsearch} function searches in the tree pointed to by
Packit Service 82fcde
@code{*@var{rootp}} for an element matching @var{key}.  The function
Packit Service 82fcde
pointed to by @var{compar} is used to determine whether two elements
Packit Service 82fcde
match.  @xref{Comparison Functions}, for a specification of the functions
Packit Service 82fcde
which can be used for the @var{compar} parameter.
Packit Service 82fcde
Packit Service 82fcde
If the tree does not contain a matching entry the @var{key} value will
Packit Service 82fcde
be added to the tree.  @code{tsearch} does not make a copy of the object
Packit Service 82fcde
pointed to by @var{key} (how could it since the size is unknown).
Packit Service 82fcde
Instead it adds a reference to this object which means the object must
Packit Service 82fcde
be available as long as the tree data structure is used.
Packit Service 82fcde
Packit Service 82fcde
The tree is represented by a pointer to a pointer since it is sometimes
Packit Service 82fcde
necessary to change the root node of the tree.  So it must not be
Packit Service 82fcde
assumed that the variable pointed to by @var{rootp} has the same value
Packit Service 82fcde
after the call.  This also shows that it is not safe to call the
Packit Service 82fcde
@code{tsearch} function more than once at the same time using the same
Packit Service 82fcde
tree.  It is no problem to run it more than once at a time on different
Packit Service 82fcde
trees.
Packit Service 82fcde
Packit Service 82fcde
The return value is a pointer to the matching element in the tree.  If a
Packit Service 82fcde
new element was created the pointer points to the new data (which is in
Packit Service 82fcde
fact @var{key}).  If an entry had to be created and the program ran out
Packit Service 82fcde
of space @code{NULL} is returned.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} tfind (const void *@var{key}, void *const *@var{rootp}, comparison_fn_t @var{compar})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@assafe{}@acsafe{}}
Packit Service 82fcde
The @code{tfind} function is similar to the @code{tsearch} function.  It
Packit Service 82fcde
locates an element matching the one pointed to by @var{key} and returns
Packit Service 82fcde
a pointer to this element.  But if no matching element is available no
Packit Service 82fcde
new element is entered (note that the @var{rootp} parameter points to a
Packit Service 82fcde
constant pointer).  Instead the function returns @code{NULL}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Another advantage of the @code{tsearch} functions in contrast to the
Packit Service 82fcde
@code{hsearch} functions is that there is an easy way to remove
Packit Service 82fcde
elements.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun {void *} tdelete (const void *@var{key}, void **@var{rootp}, comparison_fn_t @var{compar})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:rootp}}@asunsafe{@ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
Packit Service 82fcde
To remove a specific element matching @var{key} from the tree
Packit Service 82fcde
@code{tdelete} can be used.  It locates the matching element using the
Packit Service 82fcde
same method as @code{tfind}.  The corresponding element is then removed
Packit Service 82fcde
and a pointer to the parent of the deleted node is returned by the
Packit Service 82fcde
function.  If there is no matching entry in the tree nothing can be
Packit Service 82fcde
deleted and the function returns @code{NULL}.  If the root of the tree
Packit Service 82fcde
is deleted @code{tdelete} returns some unspecified value not equal to
Packit Service 82fcde
@code{NULL}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void tdestroy (void *@var{vroot}, __free_fn_t @var{freefct})
Packit Service 82fcde
@standards{GNU, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
Packit Service 82fcde
If the complete search tree has to be removed one can use
Packit Service 82fcde
@code{tdestroy}.  It frees all resources allocated by the @code{tsearch}
Packit Service 82fcde
functions to generate the tree pointed to by @var{vroot}.
Packit Service 82fcde
Packit Service 82fcde
For the data in each tree node the function @var{freefct} is called.
Packit Service 82fcde
The pointer to the data is passed as the argument to the function.  If
Packit Service 82fcde
no such work is necessary @var{freefct} must point to a function doing
Packit Service 82fcde
nothing.  It is called in any case.
Packit Service 82fcde
Packit Service 82fcde
This function is a GNU extension and not covered by the @w{System V} or
Packit Service 82fcde
X/Open specifications.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
In addition to the functions to create and destroy the tree data
Packit Service 82fcde
structure, there is another function which allows you to apply a
Packit Service 82fcde
function to all elements of the tree.  The function must have this type:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
void __action_fn_t (const void *nodep, VISIT value, int level);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The @var{nodep} is the data value of the current node (once given as the
Packit Service 82fcde
@var{key} argument to @code{tsearch}).  @var{level} is a numeric value
Packit Service 82fcde
which corresponds to the depth of the current node in the tree.  The
Packit Service 82fcde
root node has the depth @math{0} and its children have a depth of
Packit Service 82fcde
@math{1} and so on.  The @code{VISIT} type is an enumeration type.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} VISIT
Packit Service 82fcde
The @code{VISIT} value indicates the status of the current node in the
Packit Service 82fcde
tree and how the function is called.  The status of a node is either
Packit Service 82fcde
`leaf' or `internal node'.  For each leaf node the function is called
Packit Service 82fcde
exactly once, for each internal node it is called three times: before
Packit Service 82fcde
the first child is processed, after the first child is processed and
Packit Service 82fcde
after both children are processed.  This makes it possible to handle all
Packit Service 82fcde
three methods of tree traversal (or even a combination of them).
Packit Service 82fcde
Packit Service 82fcde
@vtable @code
Packit Service 82fcde
@item preorder
Packit Service 82fcde
The current node is an internal node and the function is called before
Packit Service 82fcde
the first child was processed.
Packit Service 82fcde
@item postorder
Packit Service 82fcde
The current node is an internal node and the function is called after
Packit Service 82fcde
the first child was processed.
Packit Service 82fcde
@item endorder
Packit Service 82fcde
The current node is an internal node and the function is called after
Packit Service 82fcde
the second child was processed.
Packit Service 82fcde
@item leaf
Packit Service 82fcde
The current node is a leaf.
Packit Service 82fcde
@end vtable
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun void twalk (const void *@var{root}, __action_fn_t @var{action})
Packit Service 82fcde
@standards{SVID, search.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtsrace{:root}}@assafe{}@acsafe{}}
Packit Service 82fcde
For each node in the tree with a node pointed to by @var{root}, the
Packit Service 82fcde
@code{twalk} function calls the function provided by the parameter
Packit Service 82fcde
@var{action}.  For leaf nodes the function is called exactly once with
Packit Service 82fcde
@var{value} set to @code{leaf}.  For internal nodes the function is
Packit Service 82fcde
called three times, setting the @var{value} parameter or @var{action} to
Packit Service 82fcde
the appropriate value.  The @var{level} argument for the @var{action}
Packit Service 82fcde
function is computed while descending the tree by increasing the value
Packit Service 82fcde
by one for each descent to a child, starting with the value @math{0} for
Packit Service 82fcde
the root node.
Packit Service 82fcde
Packit Service 82fcde
Since the functions used for the @var{action} parameter to @code{twalk}
Packit Service 82fcde
must not modify the tree data, it is safe to run @code{twalk} in more
Packit Service 82fcde
than one thread at the same time, working on the same tree.  It is also
Packit Service 82fcde
safe to call @code{tfind} in parallel.  Functions which modify the tree
Packit Service 82fcde
must not be used, otherwise the behavior is undefined.
Packit Service 82fcde
@end deftypefun