Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

hash_table.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file hash_table.h
  25. * \brief Implementation of a generic, opaque hash table data type.
  26. *
  27. * \author Ian Romanick <ian.d.romanick@intel.com>
  28. */
  29. #ifndef HASH_TABLE_H
  30. #define HASH_TABLE_H
  31. #include <string.h>
  32. struct hash_table;
  33. typedef unsigned (*hash_func_t)(const void *key);
  34. typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
  35. /**
  36. * Hash table constructor
  37. *
  38. * Creates a hash table with the specified number of buckets. The supplied
  39. * \c hash and \c compare routines are used when adding elements to the table
  40. * and when searching for elements in the table.
  41. *
  42. * \param num_buckets Number of buckets (bins) in the hash table.
  43. * \param hash Function used to compute hash value of input keys.
  44. * \param compare Function used to compare keys.
  45. */
  46. extern struct hash_table *hash_table_ctor(unsigned num_buckets,
  47. hash_func_t hash, hash_compare_func_t compare);
  48. /**
  49. * Release all memory associated with a hash table
  50. *
  51. * \warning
  52. * This function cannot release memory occupied either by keys or data.
  53. */
  54. extern void hash_table_dtor(struct hash_table *ht);
  55. /**
  56. * Flush all entries from a hash table
  57. *
  58. * \param ht Table to be cleared of its entries.
  59. */
  60. extern void hash_table_clear(struct hash_table *ht);
  61. /**
  62. * Search a hash table for a specific element
  63. *
  64. * \param ht Table to be searched
  65. * \param key Key of the desired element
  66. *
  67. * \return
  68. * The \c data value supplied to \c hash_table_insert when the element with
  69. * the matching key was added. If no matching key exists in the table,
  70. * \c NULL is returned.
  71. */
  72. extern void *hash_table_find(struct hash_table *ht, const void *key);
  73. /**
  74. * Add an element to a hash table
  75. */
  76. extern void hash_table_insert(struct hash_table *ht, void *data,
  77. const void *key);
  78. /**
  79. * Compute hash value of a string
  80. *
  81. * Computes the hash value of a string using the DJB2 algorithm developed by
  82. * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
  83. * a time. I was unable to find the original posting in the archives.
  84. *
  85. * \param key Pointer to a NUL terminated string to be hashed.
  86. *
  87. * \sa hash_table_string_compare
  88. */
  89. extern unsigned hash_table_string_hash(const void *key);
  90. /**
  91. * Compare two strings used as keys
  92. *
  93. * This is just a macro wrapper around \c strcmp.
  94. *
  95. * \sa hash_table_string_hash
  96. */
  97. #define hash_table_string_compare ((hash_compare_func_t) strcmp)
  98. #endif /* HASH_TABLE_H */