.\" Part of publib. .\" .\" Copyright (c) 2012 Antti-Juhani Kaijanaho. .\" Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" 2. Redistributions in binary form must reproduce the above .\" copyright notice, this list of conditions and the following .\" disclaimer in the documentation and/or other materials provided .\" with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS .\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING .\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" .TH HASH 3pub "C Programmer's Manual" "Publib" "C Programmer's Manual" .SH NAME hash_create, hash_destroy, hash_install, hash_lookup, hash_uninstall, hash_iter \- generic hash tables .SH SYNOPSIS .nf #include .sp 1 Hashtab *\fBhash_create\fR(unsigned long (*\fIfun\fR)(void *), int (*\fIcmp\fR)(const void *, const void *)); void \fBhash_destroy\fR(Hashtab *\fIht\fR); void *\fBhash_install\fR(Hashtab *\fIht\fR, void *\fIdata\fR, size_t \fIsize\fR); void *\fBhash_lookup\fR(Hashtab *\fIht\fR, void *\fIdata\fR); int \fBhash_uninstall\fR(Hashtab *\fIht\fR, void *\fIdata\fR); int \fBhash_iter\fR(Hashtab *\fIht\fR, int (*\fIdoit\fR)(void *, void *), void *\fIparam\fR); .SH "DESCRIPTION" These functions implement generic hash tables. The table is created by \fIhash_create\fR and destroyed by \fIhash_destroy\fR. The \fIfun\fR argument is a pointer to the hashing function, which must convert a datum to an unsigned long, which is then converted to an index into the hashing table. \fIcmp\fR is a \fIqsort\fR(3)-like comparison functions, used to compare to (wannabe) hash table elements. .PP \fIhash_install\fR installs a new datum into the table. A pointer to the data and the size of the data are given as the arguments. If the size is 0, only the pointer value is copied to the table. Otherwise a copy of the data is made into dynamically allocated memory. .PP \fIhash_lookup\fR attempts to find a datum in the hash table. A pointer to another datum is given as the argument. The comparison function should compare equal (return 0) the desired datum and this datum (but the argument needn't be a fully initialized datum, although that is up to the writer of the comparison function). There cannot be two elements in the hash table that are equal (the comparison function returns 0 for them). It is up to the user to handle collisions. .PP \fIhash_uninstall\fR removes an element from a table. The argument is a pointer to a datum that identifies the element. .PP \fIhash_iter\fR goes through every element in the hash table and calls the \fIdoit\fR function for each. The first argument it provides to \fIdoit\fR is the element in question, the second is whatever was given to \fIhash_iter\fR as \fIparam\fR. If \fIdoit\fR returns -1 or 0 for any element in the hash table, \fIhash_iter\fR immediately returns without going through the remaining elements in the hash table. Any other return value from \fIdoit\fR is ignored. .SH RETURNS \fIhash_create\fR returns a pointer to the new hash table, or NULL if it fails. .PP \fIhash_install\fR returns a pointer to an element in the table (either the installed one, or one that was already installed, if one tries to install the same datum twice). .PP \fIhash_uninstall\fR returns 0 if it found the element in the array, or -1 if it didn't. .PP \fIhash_lookup\fR return a pointer to the element it finds, or NULL if it doesn't find anything beautiful. .PP \fIhash_iter\fR returns -1, 0, or 1. If \fIhash_iter\fR receives a return value of -1 or 0 for some element from \fIdoit\fR, \fIhash_iter\fR immediately returns -1 or 0, respectively. In all other cases \fIhash_iter\fR returns 1. .SH "SEE ALSO" publib(3), qsort(3), bsearch(3) .SH AUTHOR Lars Wirzenius (lars.wirzenius@helsinki.fi)