135c4bbdfSmrg#ifndef HASHTABLE_H
235c4bbdfSmrg#define HASHTABLE_H 1
335c4bbdfSmrg
435c4bbdfSmrg#include <dix-config.h>
535c4bbdfSmrg#include <X11/Xfuncproto.h>
635c4bbdfSmrg#include <X11/Xdefs.h>
735c4bbdfSmrg#include "list.h"
835c4bbdfSmrg
935c4bbdfSmrg/** @brief A hashing function.
1035c4bbdfSmrg
1135c4bbdfSmrg  @param[in/out] cdata  Opaque data that can be passed to HtInit that will
1235c4bbdfSmrg                        eventually end up here
1335c4bbdfSmrg  @param[in] ptr        The data to be hashed. The size of the data, if
1435c4bbdfSmrg                        needed, can be configured via a record that can be
1535c4bbdfSmrg                        passed via cdata.
1635c4bbdfSmrg  @param[in] numBits    The number of bits this hash needs to have in the
1735c4bbdfSmrg                        resulting hash
1835c4bbdfSmrg
1935c4bbdfSmrg  @return  A numBits-bit hash of the data
2035c4bbdfSmrg*/
2135c4bbdfSmrgtypedef unsigned (*HashFunc)(void * cdata, const void * ptr, int numBits);
2235c4bbdfSmrg
2335c4bbdfSmrg/** @brief A comparison function for hashed keys.
2435c4bbdfSmrg
2535c4bbdfSmrg  @param[in/out] cdata  Opaque data that ca be passed to Htinit that will
2635c4bbdfSmrg                        eventually end up here
2735c4bbdfSmrg  @param[in] l          The left side data to be compared
2835c4bbdfSmrg  @param[in] r          The right side data to be compared
2935c4bbdfSmrg
3035c4bbdfSmrg  @return -1 if l < r, 0 if l == r, 1 if l > r
3135c4bbdfSmrg*/
3235c4bbdfSmrgtypedef int (*HashCompareFunc)(void * cdata, const void * l, const void * r);
3335c4bbdfSmrg
3435c4bbdfSmrgstruct HashTableRec;
3535c4bbdfSmrg
3635c4bbdfSmrgtypedef struct HashTableRec *HashTable;
3735c4bbdfSmrg
3835c4bbdfSmrg/** @brief  A configuration for HtGenericHash */
3935c4bbdfSmrgtypedef struct {
4035c4bbdfSmrg    int             keySize;
4135c4bbdfSmrg} HtGenericHashSetupRec, *HtGenericHashSetupPtr;
4235c4bbdfSmrg
43ed6184dfSmrg/** @brief  ht_create initializes a hash table for a certain hash table
4435c4bbdfSmrg            configuration
4535c4bbdfSmrg
4635c4bbdfSmrg    @param[out] ht       The hash table structure to initialize
4735c4bbdfSmrg    @param[in] keySize   The key size in bytes
4835c4bbdfSmrg    @param[in] dataSize  The data size in bytes
4935c4bbdfSmrg    @param[in] hash      The hash function to use for hashing keys
5035c4bbdfSmrg    @param[in] compare   The comparison function for hashing keys
5135c4bbdfSmrg    @param[in] cdata     Opaque data that will be passed to hash and
5235c4bbdfSmrg                         comparison functions
5335c4bbdfSmrg*/
5435c4bbdfSmrgextern _X_EXPORT HashTable ht_create(int             keySize,
5535c4bbdfSmrg                                     int             dataSize,
5635c4bbdfSmrg                                     HashFunc        hash,
5735c4bbdfSmrg                                     HashCompareFunc compare,
5835c4bbdfSmrg                                     void            *cdata);
5935c4bbdfSmrg/** @brief  HtDestruct deinitializes the structure. It does not free the
6035c4bbdfSmrg            memory allocated to HashTableRec
6135c4bbdfSmrg*/
6235c4bbdfSmrgextern _X_EXPORT void ht_destroy(HashTable ht);
6335c4bbdfSmrg
6435c4bbdfSmrg/** @brief  Adds a new key to the hash table. The key will be copied
6535c4bbdfSmrg            and a pointer to the value will be returned. The data will
6635c4bbdfSmrg            be initialized with zeroes.
6735c4bbdfSmrg
6835c4bbdfSmrg  @param[in/out] ht  The hash table
6935c4bbdfSmrg  @param[key] key    The key. The contents of the key will be copied.
7035c4bbdfSmrg
7135c4bbdfSmrg  @return On error NULL is returned, otherwise a pointer to the data
7235c4bbdfSmrg          associated with the newly inserted key.
7335c4bbdfSmrg
7435c4bbdfSmrg  @note  If dataSize is 0, a pointer to the end of the key may be returned
7535c4bbdfSmrg         to avoid returning NULL. Obviously the data pointed cannot be
7635c4bbdfSmrg         modified, as implied by dataSize being 0.
7735c4bbdfSmrg*/
7835c4bbdfSmrgextern _X_EXPORT void *ht_add(HashTable ht, const void *key);
7935c4bbdfSmrg
8035c4bbdfSmrg/** @brief  Removes a key from the hash table along with its
8135c4bbdfSmrg            associated data, which will be free'd.
8235c4bbdfSmrg*/
8335c4bbdfSmrgextern _X_EXPORT void ht_remove(HashTable ht, const void *key);
8435c4bbdfSmrg
8535c4bbdfSmrg/** @brief  Finds the associated data of a key from the hash table.
8635c4bbdfSmrg
8735c4bbdfSmrg   @return  If the key cannot be found, the function returns NULL.
8835c4bbdfSmrg            Otherwise it returns a pointer to the data associated
8935c4bbdfSmrg            with the key.
9035c4bbdfSmrg
9135c4bbdfSmrg   @note  If dataSize == 0, this function may return NULL
9235c4bbdfSmrg          even if the key has been inserted! If dataSize == NULL,
9335c4bbdfSmrg          use HtMember instead to determine if a key has been
9435c4bbdfSmrg          inserted.
9535c4bbdfSmrg*/
9635c4bbdfSmrgextern _X_EXPORT void *ht_find(HashTable ht, const void *key);
9735c4bbdfSmrg
9835c4bbdfSmrg/** @brief  A generic hash function */
9935c4bbdfSmrgextern _X_EXPORT unsigned ht_generic_hash(void *cdata,
10035c4bbdfSmrg                                          const void *ptr,
10135c4bbdfSmrg                                          int numBits);
10235c4bbdfSmrg
10335c4bbdfSmrg/** @brief  A generic comparison function. It compares data byte-wise. */
10435c4bbdfSmrgextern _X_EXPORT int ht_generic_compare(void *cdata,
10535c4bbdfSmrg                                        const void *l,
10635c4bbdfSmrg                                        const void *r);
10735c4bbdfSmrg
10835c4bbdfSmrg/** @brief  A debugging function that dumps the distribution of the
10935c4bbdfSmrg            hash table: for each bucket, list the number of elements
11035c4bbdfSmrg            contained within. */
11135c4bbdfSmrgextern _X_EXPORT void ht_dump_distribution(HashTable ht);
11235c4bbdfSmrg
11335c4bbdfSmrg/** @brief  A debugging function that dumps the contents of the hash
11435c4bbdfSmrg            table: for each bucket, list the elements contained
11535c4bbdfSmrg            within. */
11635c4bbdfSmrgextern _X_EXPORT void ht_dump_contents(HashTable ht,
11735c4bbdfSmrg                                       void (*print_key)(void *opaque, void *key),
11835c4bbdfSmrg                                       void (*print_value)(void *opaque, void *value),
11935c4bbdfSmrg                                       void* opaque);
12035c4bbdfSmrg
12135c4bbdfSmrg/** @brief  A hashing function to be used for hashing resource IDs when
12235c4bbdfSmrg            used with HashTables. It makes no use of cdata, so that can
12335c4bbdfSmrg            be NULL. It uses HashXID underneath, and should HashXID be
12435c4bbdfSmrg            unable to hash the value, it switches into using the generic
12535c4bbdfSmrg            hash function. */
12635c4bbdfSmrgextern _X_EXPORT unsigned ht_resourceid_hash(void *cdata,
12735c4bbdfSmrg                                             const void * data,
12835c4bbdfSmrg                                             int numBits);
12935c4bbdfSmrg
13035c4bbdfSmrg/** @brief  A comparison function to be used for comparing resource
13135c4bbdfSmrg            IDs when used with HashTables. It makes no use of cdata,
13235c4bbdfSmrg            so that can be NULL. */
13335c4bbdfSmrgextern _X_EXPORT int ht_resourceid_compare(void *cdata,
13435c4bbdfSmrg                                           const void *a,
13535c4bbdfSmrg                                           const void *b);
13635c4bbdfSmrg
13735c4bbdfSmrg#endif // HASHTABLE_H
138